setup()
setup() runs once when the sketch starts. It's the right place to size the canvas and initialize data structures - here, the snowPileHeights array is prepared before any snow can fall.
function setup() {
createCanvas(windowWidth, windowHeight);
// Initialize snow pile heights. Each element corresponds to a segment of the canvas width.
// All segments start at 0 height.
snowPileHeights = Array(floor(width / pileResolution)).fill(0);
frameRate(30); // Set a stable frame rate for consistent animation
}
Line-by-line explanation (3 lines)
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, using p5's built-in windowWidth and windowHeight variables.
snowPileHeights = Array(floor(width / pileResolution)).fill(0);- Builds an array with one slot per horizontal segment of the canvas (width divided by pileResolution), and fills every slot with 0 - meaning no snow has piled up yet anywhere.
frameRate(30); // Set a stable frame rate for consistent animation- Locks draw() to run 30 times per second so the animation speed stays consistent across different computers.