setup()
setup() runs once when the sketch starts. It's where you initialize the canvas, variables, and audio. The responsive canvas (windowWidth, windowHeight) is tied to the windowResized() function, which updates canvas size and ball position if the window changes.
function setup() {
createCanvas(windowWidth, windowHeight);
// Tip: Use windowWidth/windowHeight for responsive canvas
// Initialize ball properties
ballRadius = 20; // Set ball radius to 20 pixels
ballX = width / 2; // Start ball in the middle of the canvas horizontally
ballY = height / 4; // Start ball near the top of the canvas vertically
// Give the ball an initial random velocity
ballVX = random(-5, 5); // Random x-velocity between -5 and 5
ballVY = random(-2, 2); // Random y-velocity between -2 and 2
// Calculate pit position. Its bottom edge should be at the canvas bottom.
pitX = width / 2;
pitY = height - pitRadius; // Center Y is pitRadius above the bottom
// Start the audio context (important for any sound)
userStartAudio();
// Setup the sound oscillator
ballSound = new p5.Oscillator();
ballSound.setType('sine'); // A simple sine wave
ballSound.freq(220); // A low frequency (A3)
ballSound.amp(0); // Start with 0 volume
ballSound.start(); // Start the oscillator, but it won't make sound yet
}
Line-by-line explanation (14 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that fills the entire browser window and responds to resizing
ballX = width / 2; ballY = height / 4; ballVX = random(-5, 5); ballVY = random(-2, 2);
Positions the ball at the top-center and gives it a random starting velocity so each run is different
pitX = width / 2; pitY = height - pitRadius;
Centers the pit horizontally and positions its center so it touches the bottom of the canvas
ballSound = new p5.Oscillator(); ballSound.setType('sine'); ballSound.freq(220); ballSound.amp(0); ballSound.start();
Creates a silent sine wave oscillator that will play bounce sounds without making noise until triggered
createCanvas(windowWidth, windowHeight);- Creates a canvas the size of the entire browser window. windowWidth and windowHeight update when the window resizes.
ballRadius = 20;- Sets the balloon's radius to 20 pixels. This is used for drawing and collision detection.
ballX = width / 2;- Places the ball horizontally at the center of the canvas (width / 2).
ballY = height / 4;- Places the ball vertically at 1/4 down from the top (height / 4), so it has room to fall.
ballVX = random(-5, 5);- Gives the ball a random initial horizontal velocity between -5 and 5 pixels per frame, making each run unique.
ballVY = random(-2, 2);- Gives the ball a random initial vertical velocity between -2 and 2 pixels per frame.
pitX = width / 2;- Centers the pit horizontally on the canvas.
pitY = height - pitRadius;- Positions the pit's center so its curved edge touches the bottom of the canvas (the center is one radius above the bottom).
userStartAudio();- Wakes up the browser's audio context. This is required before using p5.sound in many browsers due to autoplay policies.
ballSound = new p5.Oscillator();- Creates a new oscillator object that will generate sound waves.
ballSound.setType('sine');- Sets the oscillator to produce a smooth sine wave (as opposed to square, triangle, or sawtooth waves).
ballSound.freq(220);- Sets the frequency to 220 Hz, which is the musical note A3 (a low-pitched A).
ballSound.amp(0);- Sets the volume to 0, so the oscillator won't make sound yet even though it's running.
ballSound.start();- Starts the oscillator. It will remain silent until we change its amplitude in playBounceSound().