setup()
setup() runs once when the sketch starts. It prepares the canvas, initializes all variables and colors, creates the off-screen graphics buffer, and populates the boids array. The worldHeight = height * 3 trick is clever: a three-part buffer lets the code discard the top third that scrolled off-screen and regenerate the bottom third with new biomes without disrupting the visible animation.
function setup() {
createCanvas(windowWidth, windowHeight);
skyColor = color(135, 206, 235);
waterColor = color(0, 191, 255);
sandColor = color(244, 164, 96);
forestColor = color(34, 139, 34);
mountainColor = color(139, 69, 19);
worldHeight = height * 3;
worldGraphics = createGraphics(width, worldHeight);
generateWorldPattern(worldGraphics, 0, worldHeight, 0);
for (let i = 0; i < flockSize; i++) {
boids.push(new Boid());
}
userStartAudio();
}
Line-by-line explanation (12 lines)
🔧 Subcomponents:
skyColor = color(135, 206, 235);
Defines the color palette used throughout generateWorldPattern to paint each biome
worldGraphics = createGraphics(width, worldHeight);
Creates an off-screen drawing surface three times the canvas height, enabling smooth infinite scrolling
for (let i = 0; i < flockSize; i++) {
Creates 100 Boid objects and adds each to the boids array, populating the flock
createCanvas(windowWidth, windowHeight);- Creates a full-screen canvas that stretches to fill the entire browser window
skyColor = color(135, 206, 235);- Stores a sky-blue color as a p5.Color object so it can be reused in the biome drawing functions
waterColor = color(0, 191, 255);- Stores a deep blue color for water biomes
sandColor = color(244, 164, 96);- Stores a sandy brown color for beach biomes
forestColor = color(34, 139, 34);- Stores a forest green color for tree biomes
mountainColor = color(139, 69, 19);- Stores a mountain brown color for peak biomes
worldHeight = height * 3;- Sets the buffer height to 3 times the screen height; the top third scrolls off, middle becomes top, bottom gets new content
worldGraphics = createGraphics(width, worldHeight);- Creates an off-screen p5.Graphics buffer (invisible drawing surface) the size of the world buffer—this is where the landscape is drawn and scrolled
generateWorldPattern(worldGraphics, 0, worldHeight, 0);- Draws the initial biome landscape onto worldGraphics from top (y=0) to bottom (y=worldHeight)
for (let i = 0; i < flockSize; i++) {- Loop that repeats flockSize times (100 by default)
boids.push(new Boid());- Creates a new Boid object and adds it to the boids array; each iteration creates one bird
userStartAudio();- Initializes the p5.sound library; required so audio can play after user interaction (not used in this sketch but good practice)