setup()
setup() runs once when the sketch first loads. It's the ideal place to configure the canvas, set drawing modes like colorMode(), and populate arrays with starting data before the animation loop takes over.
🔬 This loop decides how many particles exist at the start. What happens visually if you drop it to 10? What if you push it up to 400 - does the sketch still run smoothly?
for (let i = 0; i < 70; i++) particles.push(spawn());
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100, 100);
noStroke();
for (let i = 0; i < 70; i++) particles.push(spawn());
}
Line-by-line explanation (4 lines)
🔧 Subcomponents:
for (let i = 0; i < 70; i++) particles.push(spawn());
Fills the particles array with 70 freshly spawned particle objects before the sketch starts animating
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, using the window's current width and height.
colorMode(HSB, 360, 100, 100, 100);- Switches p5's color system from the default RGB to HSB (Hue, Saturation, Brightness, Alpha), with hue ranging 0-360 and the other values 0-100. This makes it easy to smoothly cycle colors by just changing a single hue number.
noStroke();- Turns off shape outlines by default so circles are drawn as solid filled dots unless stroke() is turned back on later (as it is for the constellation lines).
for (let i = 0; i < 70; i++) particles.push(spawn());- Runs 70 times, each time calling spawn() to create a new particle object and pushing it into the particles array.