|
| 1 | +--- |
| 2 | +author: admin |
| 3 | +layout: drawing_color |
| 4 | +title: "Drawing and colors" |
| 5 | +--- |
| 6 | + |
| 7 | +## General information |
| 8 | + |
| 9 | +- basil.js does not have a screen or canvas |
| 10 | +- You cannot draw pixels |
| 11 | +- Instead you add InDesign objects to your pages |
| 12 | +- These can be used normally within InDesign afterwards |
| 13 | +- And you can undo them! |
| 14 | + |
| 15 | + |
| 16 | +### 1. Filling the paper with a rect() by reading its size |
| 17 | +``` |
| 18 | +// @includepath "~/Documents/;%USERPROFILE%Documents"; |
| 19 | +// @include "basiljs/basil.js"; |
| 20 | +
|
| 21 | +function draw() { |
| 22 | + println( "width " + width ); |
| 23 | + println( "height " + height ); |
| 24 | + rect(0, 0, width, height); |
| 25 | +}; |
| 26 | +``` |
| 27 | + |
| 28 | +- As there is no `background()` command in basil.js you should use `rect()` in order to change the background color. |
| 29 | + |
| 30 | +### How to account for margins or bleed settings |
| 31 | + |
| 32 | +``` |
| 33 | +// @includepath "~/Documents/;%USERPROFILE%Documents"; |
| 34 | +// @include "basiljs/basil.js"; |
| 35 | +
|
| 36 | +function draw() { |
| 37 | +
|
| 38 | + noFill(); |
| 39 | + stroke(0, 255, 0); |
| 40 | +
|
| 41 | + canvasMode(MARGIN); |
| 42 | + rect(0, 0, width, height); |
| 43 | +
|
| 44 | + canvasMode(PAGE); |
| 45 | + rect(0, 0, width, height); |
| 46 | + |
| 47 | + canvasMode(BLEED); |
| 48 | + rect(0, 0, width, height); |
| 49 | +
|
| 50 | + // the following examples only work for documents with facing pages |
| 51 | +
|
| 52 | + canvasMode(FACING_MARGINS); |
| 53 | + rect(0, 0, width, height); |
| 54 | +
|
| 55 | + canvasMode(FACING_PAGES); |
| 56 | + rect(0, 0, width, height); |
| 57 | +
|
| 58 | + canvasMode(FACING_BLEEDS); |
| 59 | + rect(0, 0, width, height); |
| 60 | +
|
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +- Using different canvas modes will position the origin of the coordinate system (think 0,0) to the appropriate spots in the canvas. |
| 65 | +- Additionally `width` and `height` will be updated to reflect the new size of the canvas. |
| 66 | +- You can still use other spots of the canvas with extreme coordinates as the canvas modes are not limiting you down to them. |
| 67 | + |
| 68 | +### 2. Generate several swatches with color() and paint with rect() |
| 69 | +``` |
| 70 | +// @includepath "~/Documents/;%USERPROFILE%Documents"; |
| 71 | +// @include "basiljs/basil.js"; |
| 72 | +
|
| 73 | +function draw() { |
| 74 | +
|
| 75 | + colorMode(RGB); // this is the default anyways |
| 76 | +
|
| 77 | + // create new RGB color |
| 78 | + var red = color(255, 2, 3); // variables can store data to be reused later |
| 79 | + var green = color(0, 255, 0, "green"); |
| 80 | +
|
| 81 | + fill(red); |
| 82 | + rect(0, 0, width, 50); |
| 83 | + fill(green); |
| 84 | + rect(0, 50, width, 50); |
| 85 | +
|
| 86 | + // create new CMYK color |
| 87 | + var magenta = color(1, 100, 3, 4); |
| 88 | + var yellow = color(0, 0, 100, 0, "yellow"); |
| 89 | + var grey = color(100); |
| 90 | + var lightGrey = color(5, "light grey"); |
| 91 | +
|
| 92 | + fill( magenta ); |
| 93 | + rect(0, 200, width, 50); |
| 94 | + fill( yellow ); |
| 95 | + rect(0, 250, width, 50); |
| 96 | + fill( grey ); |
| 97 | + rect(0, 300, width, 50); |
| 98 | + fill( lightGrey ); |
| 99 | + rect(0, 350, width, 50); |
| 100 | +
|
| 101 | + // get a color from document |
| 102 | + var black = color("Black"); |
| 103 | + fill(black); |
| 104 | + rect(0, 500, width, 50); |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +- `color()` can accept between one and five parameters. |
| 109 | +- basil.js uses the number of given color channels to decide whether to create a RGB or CMYK color. |
| 110 | +- If the arguments do not clearly define the desired type of color then the current basil.js color mode is used. |
| 111 | +- The default basil.js color mode is RGB |
| 112 | +- 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 color). |
| 113 | +<!-- Exercise: Create a horizontal rainbow --> |
| 114 | + |
| 115 | +### 3. Using random() to pick a color and fill the background |
| 116 | +``` |
| 117 | +// @includepath "~/Documents/;%USERPROFILE%Documents"; |
| 118 | +// @include "basiljs/basil.js"; |
| 119 | +
|
| 120 | +function draw() { |
| 121 | + // create new random RGB color |
| 122 | + var newRandomColor = color(random(255), random(255), random(255)); |
| 123 | +
|
| 124 | + // fill rect with it |
| 125 | + fill(newRandomColor); |
| 126 | + rect(0, 0, width, height); |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | + |
| 131 | +### 4. Using line() to create a cross |
| 132 | +``` |
| 133 | +// @includepath "~/Documents/;%USERPROFILE%Documents"; |
| 134 | +// @include "basiljs/basil.js"; |
| 135 | +
|
| 136 | +function draw() { |
| 137 | + stroke(100); |
| 138 | + strokeWeight(5); |
| 139 | + // draw a cross |
| 140 | + line(0, 0, width, height); |
| 141 | + line(0, height, width, 0); |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +<!-- Exercise: Create a Haus of Nikolaus --> |
| 146 | + |
| 147 | +### 5. Using random() and ellipse() to fill the page with circles |
| 148 | +``` |
| 149 | +// @includepath "~/Documents/;%USERPROFILE%Documents"; |
| 150 | +// @include "basiljs/basil.js"; |
| 151 | +
|
| 152 | +function draw() { |
| 153 | + var count = 23; |
| 154 | +
|
| 155 | + doc(); |
| 156 | + println(width + " x " + height); |
| 157 | +
|
| 158 | + for (var i = 0; i < 23; i++) { |
| 159 | + var x = random(0, width); |
| 160 | + var y = random(0, height); |
| 161 | + var size = random(10, 123); |
| 162 | + ellipse(x, y, size, size); |
| 163 | + } |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +- This creates confetti |
| 168 | + |
| 169 | + |
| 170 | +### 6. Using a for-loop and map() to create a gradient |
| 171 | +``` |
| 172 | +// @includepath "~/Documents/;%USERPROFILE%Documents"; |
| 173 | +// @include "basiljs/basil.js"; |
| 174 | +
|
| 175 | +function draw() { |
| 176 | + var counter = 50; |
| 177 | + noStroke(); |
| 178 | + var rectHeight = height/counter; |
| 179 | +
|
| 180 | + for (var i = 0; i < counter; i++) { |
| 181 | + var y = map(i, 0, counter-1, 0, height - rectHeight); |
| 182 | + var fillTint = map(i, 0, counter-1, 100, 0); |
| 183 | +
|
| 184 | + fill(fillTint); |
| 185 | + rect(0, y, width, rectHeight); |
| 186 | + } |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | + |
| 191 | +### 8. Using random() and nested for loops to create a matrix of lines |
| 192 | +``` |
| 193 | +// @includepath "~/Documents/;%USERPROFILE%Documents"; |
| 194 | +// @include "basiljs/basil.js"; |
| 195 | +
|
| 196 | +function draw() { |
| 197 | + var tileCount = 10; |
| 198 | + var randomX = random(0, width); |
| 199 | + var randomY = random(0, height); |
| 200 | +
|
| 201 | + strokeWeight(1); |
| 202 | +
|
| 203 | + for (var gridY = 0; gridY <= tileCount; gridY++) { |
| 204 | + for (var gridX = 0; gridX <= tileCount; gridX++) { |
| 205 | + var posX = width / tileCount * gridX; |
| 206 | + var posY = height / tileCount * gridY; |
| 207 | + line(posX, posY, randomX, randomY); |
| 208 | + } |
| 209 | + } |
| 210 | +
|
| 211 | +}; |
| 212 | +``` |
0 commit comments