setup()
setup() runs exactly once when the sketch starts. It's the right place to configure the canvas, color mode, and any starting data - here that means building the initial array of seed objects that everything else in the sketch depends on.
🔬 This loop decides how many crystal shards you start with. What happens visually if INITIAL_SEEDS is dropped to 3? What about pushed up to 100?
for (let i = 0; i < INITIAL_SEEDS; i++) {
seeds.push(createSeed(random(width), random(height)));
}
function setup() {
createCanvas(windowWidth, windowHeight); // https://p5js.org/reference/#/p5/createCanvas
colorMode(HSB, 360, 100, 100, 1); // HSB for nice color control
noStroke();
// Create initial drifting seeds
for (let i = 0; i < INITIAL_SEEDS; i++) {
seeds.push(createSeed(random(width), random(height)));
}
}
Line-by-line explanation (5 lines)
🔧 Subcomponents:
for (let i = 0; i < INITIAL_SEEDS; i++) {
Creates INITIAL_SEEDS seed objects at random positions and stores them in the seeds array
createCanvas(windowWidth, windowHeight);- Makes the drawing area fill the entire browser window
colorMode(HSB, 360, 100, 100, 1);- Switches from default RGB to HSB (Hue, Saturation, Brightness, Alpha) with ranges 0-360, 0-100, 0-100, 0-1, which makes it easy to pick evenly spaced hues
noStroke();- Turns off outlines for shapes drawn from now on, since edges are drawn separately later
for (let i = 0; i < INITIAL_SEEDS; i++) {- Repeats the seed-creation code INITIAL_SEEDS (20) times
seeds.push(createSeed(random(width), random(height)));- Builds one new seed object at a random x,y position and adds it to the global seeds array