setup()
setup() runs exactly once when the sketch starts. It's the right place to configure the canvas and create your initial set of objects - here, a full array of Star instances that draw() will animate forever after.
function setup() {
// Create a standard 2D canvas that fills the window
createCanvas(windowWidth, windowHeight);
// Disable stroke for all shapes (stars and glow)
noStroke();
// Initialize the array of stars
for (let i = 0; i < numStars; i++) {
stars.push(new Star());
}
}
Line-by-line explanation (4 lines)
🔧 Subcomponents:
for (let i = 0; i < numStars; i++) { stars.push(new Star()); }
Creates numStars new Star objects and adds them to the stars array so draw() has something to animate
createCanvas(windowWidth, windowHeight);- Creates a canvas that exactly fills the browser window, using p5's built-in windowWidth and windowHeight variables.
noStroke();- Turns off outlines for every shape drawn afterward, so stars and their glow appear as soft filled dots rather than dots with borders.
for (let i = 0; i < numStars; i++) {- Loops numStars times (400 by default) to build the initial batch of stars.
stars.push(new Star());- Creates a brand new Star object (which immediately calls reset() in its constructor) and adds it to the end of the stars array.