An array is an ordered list: []. You can put anything in it — numbers, strings, or whole objects with multiple properties. Once things are in an array, you can loop through all of them with a single for…of loop.
Here each firefly is an object with x, y, speed, and a personal glow pulse. Stored in an array. Every frame: one loop updates all 35 of them.
Tap or click to add more fireflies. Watch the counter.
fireflies.push(obj) — add one item to the end of the listfireflies.length — how many items are in the listfireflies[0] — the first item (arrays count from 0, not 1!)fireflies[fireflies.length - 1] — the last itemfor (let f of fireflies) — visit every item, call it ffireflies as the detective's suspect list — the for…of loop is the detective going through each suspect one by one, checking their alibi (updating their position).
function mousePressed() { fireflies.push({ x: mouseX, y: mouseY, phase: 0 }); let last = fireflies[fireflies.length]; // bug 1 console.log(last.x, last.y);}for (let f of firefly) { // bug 2 f.x += f.vx;}length - 1, not length.35 to 200 in setup(). Does it get slow? Try 500. At what number does it start to struggle?size property to each firefly: size: random(3, 9). Use f.size as the diameter of the bright core circle instead of the fixed 5.f.x += f.vx, add a tiny pull: f.vx += (mouseX - f.x) * 0.0003. Do the same for vy and mouseY. Watch them swarm.hue: random(360). Switch to colorMode(HSB, 360, 100, 100) in setup(), then use fill(f.hue, 80, 100, alpha). Each firefly gets its own colour!born: frameCount when pushing. In draw(), after the loop, write: fireflies = fireflies.filter(f => frameCount - f.born < 600);. The canvas slowly empties unless you keep clicking.