setup()
setup() runs once when the sketch starts. It initializes the canvas, sets up the color system, and creates all 8 beam objects with their audio oscillators ready to play.
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100, 100);
noCursor(); // Let the glowing dot act as our cursor
// Create beams, each with its own oscillator frequency
for (let i = 0; i < NUM_BEAMS; i++) {
const freq = SCALE_FREQS[i % SCALE_FREQS.length];
beams.push(new Beam(i, NUM_BEAMS, freq));
}
}
๐ง Subcomponents:
for (let i = 0; i < NUM_BEAMS; i++) { const freq = SCALE_FREQS[i % SCALE_FREQS.length]; beams.push(new Beam(i, NUM_BEAMS, freq)); }
Creates 8 Beam objects, each with a unique frequency from the C major scale, and stores them in the beams array
Line by Line:
createCanvas(windowWidth, windowHeight)- Creates a canvas that fills the entire browser window, making the sketch responsive
colorMode(HSB, 360, 100, 100, 100)- Switches from RGB to HSB (Hue, Saturation, Brightness) color mode with ranges 0-360 for hue and 0-100 for saturation/brightness/alpha. This makes rainbow colors easier to create
noCursor()- Hides the default mouse cursor so the custom glowing dot can be the only visual cursor
for (let i = 0; i < NUM_BEAMS; i++)- Loops 8 times to create one beam for each note in the scale
const freq = SCALE_FREQS[i % SCALE_FREQS.length]- Gets the frequency for this beam from the SCALE_FREQS array. The modulo operator (%) ensures we cycle through the array
beams.push(new Beam(i, NUM_BEAMS, freq))- Creates a new Beam object with its index, total number of beams, and frequency, then adds it to the beams array