setup()
setup() runs exactly once when the sketch starts. It prepares the canvas, compiles shaders, and sets up the UI button that unlocks the audio system. The WEBGL parameter tells p5.js to use the GPU for rendering instead of the CPU, which is essential for running shaders.
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
// Create UI overlay to trigger audio context
startBtn = createButton('ENTER EXPERIENCE');
startBtn.id('start-btn');
startBtn.mousePressed(initAudio);
liquidShader = createShader(vertShader, fragShader);
noStroke();
}
Line-by-line explanation (6 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight, WEBGL);
Creates a fullscreen WebGL-enabled canvas that can run GPU shaders
startBtn = createButton('ENTER EXPERIENCE');
Creates a clickable button that will initialize the audio system when pressed
liquidShader = createShader(vertShader, fragShader);
Compiles the vertex and fragment shader strings into a GPU program ready to use
createCanvas(windowWidth, windowHeight, WEBGL);- Creates a fullscreen canvas using WebGL rendering, which allows us to run custom shader code on the GPU
startBtn = createButton('ENTER EXPERIENCE');- Creates a clickable button that will appear on the page; we save it in a variable so we can remove it later
startBtn.id('start-btn');- Assigns a CSS id to the button so the stylesheet can style it with fonts, colors, and positioning
startBtn.mousePressed(initAudio);- Attaches a callback so that when the button is clicked, the initAudio() function runs and starts the music
liquidShader = createShader(vertShader, fragShader);- Compiles the vertex and fragment shader code into a program that the GPU can execute
noStroke();- Disables drawing outlines on shapes—we only want the filled rectangle from our shader