setup()
setup() runs once when the sketch starts. It is where you prepare your canvas and initialize starting values for animation variables. Using windowWidth and windowHeight makes your sketch responsive—it adapts when the window is resized.
function setup() {
createCanvas(windowWidth, windowHeight);
xPos = width / 2; // Start in the center horizontally
yPos = height / 2; // Start in the center vertically
// Tip: Use windowWidth/windowHeight for responsive canvas
}
Line-by-line explanation (3 lines)
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window by using windowWidth and windowHeight instead of fixed pixel values
xPos = width / 2;- Sets the text's starting horizontal position to the center of the canvas (halfway across)
yPos = height / 2;- Sets the text's starting vertical position to the center of the canvas (halfway down)