setup()
setup() runs once when the sketch starts. It is the place to initialize your canvas, settings, and objects. Notice how the sound synthesizer is created here once and reused for all pops—this is more efficient than creating new oscillators on demand.
function setup() {
createCanvas(windowWidth, windowHeight); // 2D canvas
// Use HSB for nice rainbow colors
colorMode(HSB, 360, 100, 100, 255);
noStroke();
textFont('system-ui');
textSize(24);
// Setup pop sound: one oscillator + envelope reused for all pops
popOsc = new p5.Oscillator('sine');
popEnv = new p5.Envelope();
// Fast, percussive pop: short attack/decay
popEnv.setADSR(0.001, 0.1, 0.0, 0.1);
popEnv.setRange(0.5, 0.0); // max amp, sustain amp
popOsc.amp(popEnv);
popOsc.start();
popOsc.freq(400); // base; will be changed per bubble
// Create initial bubbles
for (let i = 0; i < NUM_BUBBLES; i++) {
bubbles.push(new Bubble());
}
}
Line-by-line explanation (12 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that fills the entire browser window, responsive to screen size
colorMode(HSB, 360, 100, 100, 255);
Switches to HSB color space so hue (rainbow), saturation, and brightness can be controlled independently
popOsc = new p5.Oscillator('sine');
Creates a reusable sine-wave oscillator that will be triggered for every bubble pop
popEnv.setADSR(0.001, 0.1, 0.0, 0.1);
Defines the amplitude shape (attack/decay/sustain/release) so each pop is a quick percussive blip
for (let i = 0; i < NUM_BUBBLES; i++) {
bubbles.push(new Bubble());
}
Populates the bubbles array with the initial fleet of floating bubbles
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the browser window; windowWidth and windowHeight are p5.js variables that update if the window resizes
colorMode(HSB, 360, 100, 100, 255);- Switches from RGB to HSB color mode: hue (0-360°), saturation (0-100%), brightness (0-100%), and alpha (0-255). This makes it easy to generate rainbow colors and brighten/darken them
noStroke();- Disables outlines on all shapes drawn after this, so circles are solid with no border
textFont('system-ui');- Sets the font for all text drawn in the sketch (used for the score display)
textSize(24);- Makes text 24 pixels tall
popOsc = new p5.Oscillator('sine');- Creates an oscillator that generates a sine wave; this will be the source of every pop sound
popEnv = new p5.Envelope();- Creates an envelope object that controls how the oscillator's amplitude changes over time, shaping it into a percussive pop
popEnv.setADSR(0.001, 0.1, 0.0, 0.1);- Sets the envelope shape: attack (0.001s = instant), decay (0.1s = fade quickly), sustain (0 = silent), release (0.1s = tail out). This creates a short 'pop' sound
popEnv.setRange(0.5, 0.0);- Sets the envelope's amplitude range: it will rise to 0.5 (half volume) and fall to 0 (silent)
popOsc.amp(popEnv);- Connects the envelope as the amplitude controller for the oscillator; when the envelope plays, it modulates the oscillator's volume
popOsc.start();- Starts the oscillator running continuously (but silently until the envelope is triggered)
popOsc.freq(400);- Sets a default frequency of 400 Hz; this will be changed dynamically based on each bubble's size