setup()
setup() runs once when the sketch starts. It's the right place to size your canvas and populate arrays of objects before the animation loop begins.
function setup() {
createCanvas(windowWidth, windowHeight); // Create a full-window canvas
noStroke(); // No outlines for shapes
// Initialize fireflies
for (let i = 0; i < NUM_FIREFLIES; i++) {
fireflies.push(new Firefly(random(width), random(height)));
}
// Initialize stars in the upper 60% of the canvas
for (let i = 0; i < NUM_STARS; i++) {
stars.push(new Star(random(width), random(height * 0.6)));
}
}
Line-by-line explanation (4 lines)
🔧 Subcomponents:
for (let i = 0; i < NUM_FIREFLIES; i++) {
Creates NUM_FIREFLIES new Firefly objects at random positions and adds them to the fireflies array
for (let i = 0; i < NUM_STARS; i++) {
Creates NUM_STARS new Star objects positioned only in the top 60% of the canvas to look like a sky
createCanvas(windowWidth, windowHeight);- Makes the canvas fill the entire browser window instead of a fixed size
noStroke();- Turns off outlines so all circles drawn later are solid, borderless shapes
fireflies.push(new Firefly(random(width), random(height)));- Creates a new Firefly object at a random x,y position anywhere on the canvas and adds it to the fireflies array
stars.push(new Star(random(width), random(height * 0.6)));- Creates a new Star, but restricts its y-position to the top 60% of the screen so stars only appear in the 'sky' area