setup()
setup() runs once at the start to prepare the canvas, set up the color system, and populate the initial array of boid objects that draw() will animate every frame.
🔬 This lerp() blends from hue 10 to hue 60 across the flock. What happens if you change the range to lerp(180, 300, ...) for cool blues and purples instead?
// Warm gradient from red/orange to yellow (approx 10–60° hue)
const hue = lerp(10, 60, 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));
}
}
Line-by-line explanation (7 lines)
🔧 Subcomponents:
for (let i = 0; i < NUM_BOIDS; i++) {
Creates NUM_BOIDS boid objects at random positions with a hue that gradually shifts from red-orange to yellow
createCanvas(windowWidth, windowHeight);- Makes the canvas fill the entire browser window.
colorMode(HSB, 360, 100, 100, 100);- Switches color values to Hue/Saturation/Brightness/Alpha, each ranging 0-360 or 0-100, which makes it easy to pick warm hues and control transparency.
background(0);- Fills the canvas with solid black once, before any trails start forming.
const x = random(width);- Picks a random horizontal starting position for this boid.
const y = random(height);- Picks a random vertical starting position for this boid.
const hue = lerp(10, 60, i / (NUM_BOIDS - 1));- Interpolates a hue value between 10 (red-orange) and 60 (yellow) based on the boid's index, so the flock has a smooth color gradient.
boids.push(new Boid(x, y, hue));- Creates a new Boid object with its position and hue and adds it to the global boids array.