setup()
setup() runs once when the sketch starts. It initializes the canvas, sets up the color system, and creates all 150 boids with random starting positions and a beautiful warm color gradient. The lerp() function is key here—it smoothly transitions between two values based on a percentage (i / (NUM_BOIDS - 1)).
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100, 100); // HSB with alpha
background(0); // solid black to start
for (let i = 0; i < NUM_BOIDS; i++) {
const x = random(width);
const y = random(height);
// Warm gradient from red/orange to yellow (approx 10–60° hue)
const hue = lerp(10, 60, i / (NUM_BOIDS - 1));
boids.push(new Boid(x, y, hue));
}
}
đź”§ Subcomponents:
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100, 100);
Creates a full-window canvas and switches to HSB color mode for easier color gradients
for (let i = 0; i < NUM_BOIDS; i++) { ... }
Creates 150 boids at random positions with a warm color gradient from red to yellow
Line by Line:
createCanvas(windowWidth, windowHeight)- Creates a canvas that fills the entire browser window, making the sketch responsive to window size
colorMode(HSB, 360, 100, 100, 100)- Switches to HSB (Hue, Saturation, Brightness) color mode with values 0-360 for hue and 0-100 for saturation/brightness/alpha. This makes it easy to create color gradients
background(0)- Fills the canvas with black (0 brightness in HSB) as the initial background
const x = random(width)- Generates a random x position anywhere across the canvas width for a new boid
const y = random(height)- Generates a random y position anywhere across the canvas height for a new boid
const hue = lerp(10, 60, i / (NUM_BOIDS - 1))- Uses lerp() to smoothly interpolate hue values from 10 (red-orange) to 60 (yellow) based on the boid's index, creating a warm gradient across all boids
boids.push(new Boid(x, y, hue))- Creates a new Boid object with the random position and gradient hue, then adds it to the boids array