We’ve been doing it all along, but it’s worth reviewing really specifically how time works in our programs, specifically “what gets executed when” is a big question. And as we begin to add events to our programs, this gets even more important.
time-and-javascript-and-p5index.html (maybe “Time and JavaScript and p5”)The way your program is run in JavaScript is pretty simple: it starts at the top and it goes down from there.
So if your whole program looks like this:
let x = 10;
let y = 11;
let z = x * y;
Then JavaScript will first assign 10 to x, then 11 to y and then the result of multiplying them (110) to z, and then it will be done.
Of course it gets more complicated as you define functions and call them, but this remains the most basic rule: top to bottom.
When using p5 we’ve been able to define a couple of special functions that will be called at specific times in the program. Those are notably setup() and draw().
JavaScript will still run our program from the top to the bottom first, but after that it will also call our special functions at the right times.
So if our program is:
const ball = {
x: 0,
y: 200,
size: 50
};
function setup() {
createCanvas(400, 400);
}
function draw() {
background(0);
ball.x += 1;
push();
noStroke();
ellipse(ball.x, ball.y, ball.size);
pop();
}
First JavaScript will start at the top, which means it will
ball objectsetup() and draw() but not call them!Remember that, just having a function definition does not mean that code automatically runs. It just means that you can call the function as needed.
However, thanks to p5 magic behind the scenes, JavaScript will
setup() immediately (which will create the canvas)draw() right after that (which will draw the ball on the canvas)draw() about 0.033 seconds later to draw the next frame (which will draw the ball on the canvas one pixel further to the right)draw() about 0.033 seconds later to draw the next frame (which will draw the ball on the canvas one pixel further to the right)draw() about 0.033 seconds later to draw the next frame (which will draw the ball on the canvas one pixel further to the right)draw() every 0.033 seconds until the program stopsSo time in p5 for now is mostly being driven by the draw() function being called about 30 times per second. That’s also known as the framerate.
By default JavaScript literally just runs our program from the top to the bottom. p5 adds in the idea that setup() is called once at the start, and that draw() is called once every frame.