setup()
setup() runs exactly once when the sketch starts. It's where you initialize all the 'infrastructure' your animation needs: canvas size, global variables, and data structures. The order matters—for instance, we record cycleStartTime before any draw() frame runs, so that the first frame will have dayTime = 0.
function setup() {
createCanvas(windowWidth, windowHeight);
noStroke(); // Most shapes will not have a border
sunColor = color(255, 200, 0); // Yellow for the sun
moonColor = color(200, 200, 255); // Light blue/white for the moon
// Initialize the cycle timer
cycleStartTime = millis();
// Create the createGraphics buffer for the sky
skyBuffer = createGraphics(width, height);
skyBuffer.noStroke(); // Ensure no stroke is drawn in the buffer for the gradient
// Generate buildings
generateCity();
// Generate clouds
generateClouds();
// Generate birds (will only appear at dawn)
generateBirds();
// Generate shooting stars (will only appear at night)
generateShootingStars();
// Generate stars for the night sky
generateStars();
}
Line-by-line explanation (12 lines)
🔧 Subcomponents:
sunColor = color(255, 200, 0); // Yellow for the sun
moonColor = color(200, 200, 255); // Light blue/white for the moon
Pre-define the sun's yellow and moon's pale blue colors so they don't need to be created each frame
skyBuffer = createGraphics(width, height);
skyBuffer.noStroke();
Create an off-screen graphics buffer to cache expensive sky gradient rendering for performance
generateCity();
generateClouds();
generateBirds();
generateShootingStars();
generateStars();
Populate all the animated objects (buildings, clouds, birds, stars) before the first draw
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire window; windowWidth and windowHeight update if the window is resized
noStroke();- Turns off outlines for all shapes drawn after this point—cleaner, simpler visuals
sunColor = color(255, 200, 0);- Defines the sun's warm yellow color (red=255, green=200, blue=0) as a global variable so it's ready to use in draw()
moonColor = color(200, 200, 255);- Defines the moon's pale blue color as a global variable
cycleStartTime = millis();- Records the current time in milliseconds when the sketch starts; later, (millis() - cycleStartTime) will measure elapsed time
skyBuffer = createGraphics(width, height);- Creates an invisible canvas the same size as the main canvas; we'll draw the expensive gradient to this once and reuse it
skyBuffer.noStroke();- Ensures the buffer also has no strokes when drawing the gradient
generateCity();- Calls generateCity() to create the buildings array with random widths and heights, then sorts them by depth
generateClouds();- Populates the clouds array with 10 drifting clouds at random positions and sizes
generateBirds();- Creates a formation of 10 birds in a V-shape flying pattern off-screen
generateShootingStars();- Adds 3 initial shooting stars to the array; more will be spawned dynamically during the night
generateStars();- Scatters 200 static stars randomly across the upper 40% of the sky for the night background