setup()
setup() runs once when the sketch first loads. Use it to initialize your canvas, load assets (like audio), populate data structures, and build your UI. The function is called by p5.js automatically before draw() starts running.
🔬 This loop creates all the floating dots. What happens if you change 'random(2, 5)' to 'random(1, 20)' so dots have wildly different sizes? Or change 'random(0.2, 0.6)' to '0.3' so all dots move at exactly the same speed?
for (let i = 0; i < 200; i++) {
bgDots.push({
x: random(width),
y: random(height),
size: random(2, 5),
speed: random(0.2, 0.6)
});
}
function setup() {
createCanvas(windowWidth, windowHeight);
for (let i = 0; i < 200; i++) {
bgDots.push({
x: random(width),
y: random(height),
size: random(2, 5),
speed: random(0.2, 0.6)
});
}
backgroundMusic = createAudio('https://cdn.pixabay.com/download/audio/2022/03/15/audio_9d00c6f89c.mp3?filename=ambient-110057.mp3');
backgroundMusic.volume(0.35);
backgroundMusic.loop();
backgroundMusic.stop();
backgroundMusic.hideControls();
setupUI();
}
Line-by-line explanation (13 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a p5.js canvas that fills the entire browser window
for (let i = 0; i < 200; i++) {
Populates the bgDots array with 200 objects, each storing x/y position, size, and upward speed
backgroundMusic = createAudio('https://cdn.pixabay.com/download/audio/2022/03/15/audio_9d00c6f89c.mp3?filename=ambient-110057.mp3');
Loads the default ambient track from a CDN and prepares it for playback
createCanvas(windowWidth, windowHeight);- Creates a canvas that matches the full browser window dimensions, making the app responsive to window size
for (let i = 0; i < 200; i++) {- Loop that runs 200 times, creating 200 separate dot objects to store in the bgDots array
bgDots.push({- Adds a new object to the bgDots array with four properties: position (x, y), visual size, and animation speed
x: random(width),- Sets each dot's starting x position to a random value between 0 and canvas width
y: random(height),- Sets each dot's starting y position to a random value between 0 and canvas height
size: random(2, 5),- Assigns each dot a random size between 2 and 5 pixels, creating visual variety
speed: random(0.2, 0.6)- Assigns each dot a random upward speed between 0.2 and 0.6 pixels per frame
backgroundMusic = createAudio('https://cdn.pixabay.com/download/audio/2022/03/15/audio_9d00c6f89c.mp3?filename=ambient-110057.mp3');- Uses p5.sound to load an ambient background track from an external URL
backgroundMusic.volume(0.35);- Sets the track volume to 35% so it doesn't overwhelm the user
backgroundMusic.loop();- Configures the audio to loop infinitely when playing
backgroundMusic.stop();- Immediately stops the track so it doesn't play until the user unlocks the vault
backgroundMusic.hideControls();- Hides the browser's default audio player controls to keep the UI clean
setupUI();- Calls setupUI() to create all the login interface elements and input fields