There are two special functions definitions that we need to look at before we can keep going. They are function setup(){}
and function draw(){}
. In general every sketch that you write in p5.js will have these two functions definitions.
The p5.js reference states
The setup() function is defined once when the program starts. It’s used to define initial environment properties such as screen size and background color and to load media such as images and fonts as the program starts. There can only be one setup() function for each program and it shouldn’t be called again after its initial execution.
You can think of this as establishing the initial conditions for your sketch. in order to define setup you need to type
function setup(){
}
Once you define the function setup()
in your sketch you do not need to explicitly call the function. It is a special type of function definition that is automatically run by the p5.js library. All of the code that you want to run when you start the sketch must go inside the two curly braces {}
. Typically you’ll want to include createCanvas()
and maybe background()
inside setup.
function setup(){
createCanvas(600,400);
background(0);
}
Defined directly after setup(), the draw() function continuously executes the lines of code contained inside its block until the program is stopped or noLoop() is called.
For now, while we are making static sketches, the draw()
function is not strictly necessary. Later on we’ll create sketches that change over time, such as a bouncing ball, in which as we’ll need to have a draw loop. In practice the definition of this function looks very similar to the setup()
function.
function draw(){
//code goes here;
}
As with setup, you only need to define the function once and it is automatically run by p5.js. i.e. you do not have to explicitly call the draw function, though there may be some instances where you might.
From now on, all of your sketches should have a setup()
function and should probably have draw()
function. A typical sketch will look something like this:
function setup(){
createCanvas(width, height);
background(color);
}
function draw(){
//your code here.
}
In general no functions calls will exist in your code that are not either in setup()
or draw()
. Again, note that setup()
or draw()
are only defined in you sketch and not explicity called.
submit a link to your work on Google Classroom