setup()
setup() runs once when the sketch starts. It initializes the canvas size and populates the fireflies and stars arrays with new objects. The upper 60% constraint for stars keeps them in the 'sky' portion of the scene.
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)));
}
}
๐ง Subcomponents:
for (let i = 0; i < NUM_FIREFLIES; i++) { fireflies.push(new Firefly(random(width), random(height))); }
Creates 70 firefly objects at random positions across the full canvas and stores them in the fireflies array
for (let i = 0; i < NUM_STARS; i++) { stars.push(new Star(random(width), random(height * 0.6))); }
Creates 100 star objects in the upper 60% of the canvas and stores them in the stars array
Line by Line:
createCanvas(windowWidth, windowHeight)- Creates a canvas that fills the entire browser window, making the sketch responsive to different screen sizes
noStroke()- Disables outlines around shapes so circles are drawn as solid filled shapes without borders
fireflies.push(new Firefly(random(width), random(height)))- Creates a new Firefly object at a random x,y position and adds it to the fireflies array
stars.push(new Star(random(width), random(height * 0.6)))- Creates a new Star object at a random position in the upper 60% of the canvas (height * 0.6) to keep stars in the sky