« Page management Finding help »
Drawing and colors
How to add new shapes to the page and create and use color swatches. By Ludwig Zeller
General information
- basil.js does not have a screen or canvas
- You cannot draw pixels
- Instead you add InDesign objects to your pages
- These can be used normally within InDesign afterwards
- And you can undo them!
1. Filling the paper with a rect() by reading its size
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
b.println( "width " + b.width );
b.println( "height " + b.height );
b.rect(0,0,b.width,b.height);
};
b.go();
- As there is no background() command in basil.js you should use rect() in order to change the background colour.
How to account for margins or bleed settings
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
b.noFill();
b.stroke(0,255,0);
b.canvasMode(b.MARGIN);
b.rect(0,0,b.width,b.height);
b.canvasMode(b.PAGE);
b.rect(0,0,b.width,b.height);
b.canvasMode(b.BLEED);
b.rect(0,0,b.width,b.height);
// the following examples only work for documents with facing pages
b.canvasMode(b.FACING_MARGINS);
b.rect(0,0,b.width,b.height);
b.canvasMode(b.FACING_PAGES);
b.rect(0,0,b.width,b.height);
b.canvasMode(b.FACING_BLEEDS);
b.rect(0,0,b.width,b.height);
}
b.go();
- Using different canvas modes will position the origin of the coordinate system (think 0,0) to the appropriate spots in the canvas.
- Additionally b.width and b.height will be updated to reflect the new size of the canvas.
- You can still use other spots of the canvas with extreme coordinates as the canvas modes are not limiting you down to them.
- Changing canvasMode() will reset any previous MatrixTransformations such as translate(), rotate() or scale()
2. Generate several swatches with color() and paint with rect()
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
b.colorMode(b.RGB); // this is the default anyways
// create new RGB color
var red = b.color(255,2,3); // variables can store data to be reused later
var green = b.color(0,255,0,"green");
b.fill( red );
b.rect(0,0,b.width,50);
b.fill( green );
b.rect(0,50,b.width,50);
// create new CMYK color
var magenta = b.color(1,100,3,4);
var yellow = b.color(0,0,100,0,"yellow");
var grey = b.color(100);
var lightGrey = b.color(5,"light grey");
b.fill( magenta );
b.rect(0,200,b.width,50);
b.fill( yellow );
b.rect(0,250,b.width,50);
b.fill( grey );
b.rect(0,300,b.width,50);
b.fill( lightGrey );
b.rect(0,350,b.width,50);
// get a color from document
var black = b.color("Black");
b.fill( black );
b.rect(0,500,b.width,50);
}
b.go();
- color() can accept between one and five parameters.
- basil.js uses the number of given colour channels to decide whether to create a RGB or CMYK colour.
- If the arguments do not clearly define the desired type of colour then the current basil.js colour mode is used.
- The default basil.js colour mode is RGB
- Please note that CMYK takes channel numbers between 0-100 where higher is darker (subtractive color), while RGB takes numbers between 0-255 where lower is darker (additive colour).
3. Using random() to pick a color and fill the background
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
// create new random RGB color
var newRandomColor = b.color( b.random(255), b.random(255), b.random(255) );
// fill rect with it
b.fill( newRandomColor );
b.rect(0,0,b.width,b.height);
}
b.go();
4. Using line() to create a cross
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
b.stroke( 100 );
b.strokeWeight( 5 );
// draw a cross
b.line(0,0,b.width,b.height);
b.line(0,b.height,b.width,0);
}
b.go();
5. Using random() and ellipse() to fill the page with circles
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
var count = 23;
b.doc();
b.println( b.width+" x "+b.height);
for (var i = 0; i < 23; i++) {
var x = b.random(0, b.width);
var y = b.random(0, b.height);
var size = b.random(10, 123);
b.ellipse(x, y, size, size);
}
}
b.go();
- This creates confetti
6. Using a for-loop and map() to create a gradient
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
var counter = 50;
b.noStroke();
var rectHeight = b.height/counter;
for (var i = 0; i < counter; i++) {
var y = b.map(i, 0,counter-1, 0,b.height-rectHeight);
var fillTint = b.map(i, 0,counter-1, 100,0);
b.fillTint( fillTint );
b.rect(0,y, b.width,rectHeight);
}
}
b.go();
8. Using random() and nested for loops to create a matrix of lines
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
var tileCount = 10;
var randomX = b.random(0,b.width);
var randomY = b.random(0,b.height);
b.strokeWeight(1);
for (var gridY = 0; gridY <= tileCount; gridY++) {
for (var gridX = 0; gridX <= tileCount; gridX++) {
var posX = b.width/tileCount*gridX;
var posY = b.height/tileCount*gridY;
b.line(posX,posY,randomX,randomY);
}
}
};
b.go();
