Code that reponds to input from the mouse, keyboard, an dother devices depend on the program to run continuously.
The code within the draw()
block funs from top to bottom, then repeats until you quit the program.
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
print("I'm drawing");
}
To complement the looping of draw()
, we have setup()
that runs just once when the program starts:
function setup() {
createCanvas(400, 400);
print("I'm starting")
}
function draw() {
background(220);
print("I'm drawing");
}
Here is the order of which code runs in p5.js.
- Variables declared outside of
setup()
anddraw()
are created. - Code inside
setup()
is run once. - Code inside
draw()
is run continuously.
var x = 280;
var y = -100;
var diameter = 380;
function setup() {
createCanvas(400, 120);
fill(102);
}
function draw() {
background(220);
ellipse(x, y, diameter, diameter)
}
Since code is running continuously, we can track the mouse position and use those elements on screen.
The mouseX
variable stores the x coordinate, and the mouseY
sotres the y coordinate.
Each time the code in draw()
block is run, a new circle is drawn to the canvas.Since the fill is set to be partially transparent, denser black areas show where the mouse spend more time and where it moded slower.
The circles that are spaced farther apart show when the mouse was moving faster.
function setup() {
createCanvas(400, 120);
fill(0, 102);
noStroke()
}
function draw() {
ellipse(mouseX, mouseY, 9,9)
}
A new circle is added o the canvas each time the code in draw()
is run. To refresh the screen and only display the newest circle, place a background()
function at the beginning.
function setup() {
createCanvas(400, 120);
fill(0, 102);
noStroke()
}
function draw() {
background(220);
ellipse(mouseX, mouseY, 9,9)
}
Use the "Track the Mouse" Example from above as templates for the homework. Students are to:
- EASY: Recreate the inclass activity. Students should edit the "Track the Mouse" activity so that it has a rainbow effect while draging the mouse over the Canvas.
- MODERATE: Edit the shape of the ellipse to be something else; triangle, line, another size of ellipse.
- HARD: Use the code from the "Make Circles" Example from the introduction to have the screen clear when the mouse is pressed.