setup()
setup() runs exactly once when the sketch starts. It's where you initialize your canvas, set drawing modes, and prepare any objects or variables needed for the animation. The taco initialization loop is a classic pattern: loop N times, create an object with random properties, and push it into an array for later use.
function setup() {
// Create a canvas that fills the entire browser window
createCanvas(windowWidth, windowHeight);
// Set the background color once in setup to dark blue
background(20, 20, 60);
// Prevent drawing a border around shapes
noStroke();
// Set color mode to RGB for consistent color handling (R, G, B, Alpha)
colorMode(RGB, 255, 255, 255, 255);
// Initialize the tacos array
for (let i = 0; i < numTacos; i++) {
// Start tacos at random positions, some above the screen
let x = random(width);
let y = random(-height, 0);
// Give each taco a random falling speed
let speed = random(2, 6);
tacos.push(new Taco(x, y, speed));
}
}
Line-by-line explanation (9 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that fills the entire browser window, making the animation responsive to different screen sizes
for (let i = 0; i < numTacos; i++) {
let x = random(width);
let y = random(-height, 0);
let speed = random(2, 6);
tacos.push(new Taco(x, y, speed));
}
Creates 50 Taco objects with randomized starting positions, speeds, and pushes them into the tacos array
createCanvas(windowWidth, windowHeight);- Creates a canvas that matches the full width and height of the browser window, so the animation fills the screen
background(20, 20, 60);- Fills the entire canvas with a dark blue color (red=20, green=20, blue=60) to set the initial background
noStroke();- Removes outlines from all shapes drawn afterward, so tacos and circles appear solid without borders
colorMode(RGB, 255, 255, 255, 255);- Sets color values to range from 0-255 for red, green, blue, and alpha (transparency), the standard RGB color model
for (let i = 0; i < numTacos; i++) {- Loops 50 times (the value of numTacos) to create 50 individual taco objects
let x = random(width);- Assigns each taco a random horizontal position anywhere across the canvas width
let y = random(-height, 0);- Assigns each taco a random vertical position between the top of the screen (0) and above it (negative values), so tacos start above the visible canvas
let speed = random(2, 6);- Gives each taco a random falling speed between 2 and 6 pixels per frame
tacos.push(new Taco(x, y, speed));- Creates a new Taco object with the random x, y, and speed values and adds it to the tacos array