Everything in setup() happens once at the beginning. Everything in draw() happens over and over again, 60 times every second. For example:
function setup() {
createCanvas(300, 200); // creates one canvas at the beginning.
}
function draw() {
createCanvas(300, 200); // creates 60 canvases every second, for as long as the code runs.
}
createCanvas(300, 200); in setup() (just like in the example above).setup() and on a new line after createCanvas(), type background('tomato');.setup() and on a new line after background(), type circle(100, 50, 25);.createCanvas(), circle() and the colour name for background()).setup() vs draw()createCanvas(300, 200); in setup().draw(), type:background('seaGreen');
circle(mouseX, mouseY, 20);circle(mouseX, mouseY, 20); to before background();. background('seaGreen'); to after createCanvas(); in setup().translate()translate(150, 100); in draw(), after background() and before point().translate(x, y).
setup(), type let squareSize = 20; and replace every 20 with squareSize.setup() and after let squareSize = 20;, type let squareSpacing = 30; and replace every 30 with squareSpacing (and every -30 with -squareSpacing).squareSize and squareSpacing.squareSize and squareSpacing).if ()if (cursor on the right side) with if (mouseX > 150).translate() does not apply to the mouse.)draw square on left side; with square(-75, 0, squareSize); and press 'play'.| Reference | Example | Example Description |
|---|---|---|
| createCanvas(x,y) | createCanvas(400, 250) |
Creates a canvas thatβs 400 pixels wide and 250 pixels tall. |
| background(color) | background('green') |
Colours the entire canvas green. (n.b. Colors can be defined a number of different ways.) |
| translate(x, y) | translate(100, 200) |
Moves the origin to 100 pixels from the left of the canvas and 200 pixels from the top of the canvas. |
| circle(x, y, diameter) | circle(10,20,30) |
Draws a circle with a 30 pixel diameter 10 pixels to the right of the origin and 20 pixels below the origin. more shapes |
| let | let animalType = "fox"; |
Creates a variable called animalType (and assigns a value of "fox" to it). |
| if statement | |
If the variable t is greater than 10, the canvas will be painted green. |