setup()
setup() runs once when the sketch starts. It is the right place to initialize your canvas, set variables, and create UI elements like buttons that should only exist once.
function setup() {
canvas = createCanvas(windowWidth, windowHeight);
canvas.position(0, 0);
canvas.style('display', 'block');
// Optional: create zoom buttons on top of canvas
createZoomButtons();
}
Line-by-line explanation (4 lines)
🔧 Subcomponents:
canvas = createCanvas(windowWidth, windowHeight);
Creates a canvas that fills the entire browser window
createZoomButtons();
Calls the helper function to build the zoom control buttons
canvas = createCanvas(windowWidth, windowHeight);- Creates a canvas as wide and tall as the browser window, storing the canvas object in a variable so we can resize it later
canvas.position(0, 0);- Positions the canvas at the top-left corner of the page (0, 0), covering the entire viewport
canvas.style('display', 'block');- Ensures the canvas displays as a block element, preventing extra spacing that inline elements can cause
createZoomButtons();- Calls the function that builds the zoom in/out buttons above the canvas