Reference setup()

setup()

A function that's called once when the sketch begins running.

Declaring the function setup() sets a code block to run once automatically when the sketch starts running. It's used to perform setup tasks such as creating the canvas and initializing variables:

function setup() {
  // Code to run once at the start of the sketch.
}

Code placed in setup() will run once before code placed in draw() begins looping. If the preload() is declared, then setup() will run immediately after preload() finishes loading assets.

Note: setup() doesn’t have to be declared, but it’s common practice to do so.

Examples

Notice any errors or typos? Please let us know. Please feel free to edit src/core/main.js and open a pull request!

Related References