Reference draw()


A function that's called repeatedly while the sketch runs.

Declaring the function draw() sets a code block to run repeatedly once the sketch starts. It’s used to create animations and respond to user inputs:

function draw() {
  // Code to run repeatedly.

This is often called the "draw loop" because p5.js calls the code in draw() in a loop behind the scenes. By default, draw() tries to run 60 times per second. The actual rate depends on many factors. The drawing rate, called the "frame rate", can be controlled by calling frameRate(). The number of times draw() has run is stored in the system variable frameCount().

Code placed within draw() begins looping after setup() runs. draw() will run until the user closes the sketch. draw() can be stopped by calling the noLoop() function. draw() can be resumed by calling the loop() function.


Related References