setup()
setup() runs once when the sketch starts. It's the perfect place to do expensive one-time work (like pre-rendering a background) and to fill arrays with your initial objects.
🔬 This loop randomizes each jellyfish's hue between 190 and 320. What happens visually if you narrow that range to just random(280, 320) so every jellyfish is some shade of pink/magenta?
for (let i = 0; i < NUM_JELLYFISH; i++) {
const x = random(width * 0.15, width * 0.85);
const y = random(height);
const baseSize = random(18, 40);
// Bioluminescent pinks, purples, and blues (HSB hues ~190–320)
const hue = random(190, 320);
jellyfishList.push(new Jellyfish(x, y, baseSize, hue));
}
function setup() {
createCanvas(windowWidth, windowHeight);
// Pre-render the static ocean background to an offscreen buffer
bgLayer = createGraphics(windowWidth, windowHeight);
drawBackground(bgLayer);
// Create jellyfish
for (let i = 0; i < NUM_JELLYFISH; i++) {
const x = random(width * 0.15, width * 0.85);
const y = random(height);
const baseSize = random(18, 40);
// Bioluminescent pinks, purples, and blues (HSB hues ~190–320)
const hue = random(190, 320);
jellyfishList.push(new Jellyfish(x, y, baseSize, hue));
}
// Create soft drifting particles
for (let i = 0; i < NUM_PARTICLES; i++) {
particles.push(new Particle(true));
}
}
Line-by-line explanation (7 lines)
🔧 Subcomponents:
for (let i = 0; i < NUM_JELLYFISH; i++) {
Creates NUM_JELLYFISH new Jellyfish objects at random positions with random size and hue
for (let i = 0; i < NUM_PARTICLES; i++) {
Creates NUM_PARTICLES drifting Particle objects scattered anywhere on screen
createCanvas(windowWidth, windowHeight);- Makes the canvas fill the entire browser window.
bgLayer = createGraphics(windowWidth, windowHeight);- Creates a separate offscreen drawing surface the same size as the canvas - drawing here doesn't show up until you image() it onto the main canvas.
drawBackground(bgLayer);- Paints the expensive gradient, light rays, and vignette onto bgLayer ONCE, instead of every single frame.
const x = random(width * 0.15, width * 0.85);- Picks a random horizontal starting spot, kept away from the very edges so jellyfish don't spawn half off-screen.
const hue = random(190, 320);- Picks a random hue between roughly cyan/blue and magenta/pink for that jellyfish's bioluminescent glow color.
jellyfishList.push(new Jellyfish(x, y, baseSize, hue));- Creates a new Jellyfish object with these randomized properties and adds it to the array that draw() will loop over.
particles.push(new Particle(true));- Creates a Particle and passes true so it starts at a completely random y position, instead of below the screen.