setup()
setup() runs exactly once when the sketch starts. It's the right place to size your canvas and set up initial positions before any animation begins.
function setup() {
// Create a canvas that fills the entire window
createCanvas(windowWidth, windowHeight);
// Initialize cat at the center of the screen
catX = width / 2;
catY = height / 2;
// Initialize hat at a random position, ensuring it's within bounds
resetHat();
}
Line-by-line explanation (4 lines)
createCanvas(windowWidth, windowHeight);- Makes a canvas that exactly fills the browser window, so the sketch works on any screen size.
catX = width / 2;- Starts the cat's horizontal position in the middle of the canvas before the mouse takes over.
catY = height / 2;- Starts the cat's vertical position in the middle of the canvas.
resetHat();- Calls the helper function that picks a random spot for the hat and resets the 'eaten' flag to false.