setup()
setup() runs exactly once when the sketch starts. It is the ideal place to initialize your canvas, set up variables, and configure p5.js settings that should stay constant throughout the sketch's lifetime.
function setup() {
createCanvas(windowWidth, windowHeight);
textAlign(CENTER, CENTER);
textSize(16);
}
Line-by-line explanation (3 lines)
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, using built-in p5.js variables that give the window's current width and height in pixels
textAlign(CENTER, CENTER);- Tells p5.js that all text drawn after this line should be centered horizontally and vertically, making it easier to position text at exact coordinates
textSize(16);- Sets the font size to 16 pixels for all text drawn in the sketch