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 setup()
is declared async
(e.g. async function setup()
), execution pauses at each await
until its promise resolves. For example, font = await loadFont(...)
waits for the font asset to load because loadFont()
function returns a promise, and the await keyword means the program will wait for the promise to resolve. This ensures that all assets are fully loaded before the sketch continues.
loading assets.
Note: setup()
doesn’t have to be declared, but it’s common practice to do so.
Examples
Syntax
setup()