setup()
setup() runs once at the very start of your sketch. It is the perfect place to initialize the canvas, create buttons, and set up any variables that need starting values. After setup() finishes, draw() begins running repeatedly.
function setup() {
createCanvas(windowWidth, windowHeight);
textAlign(CENTER, CENTER);
textSize(24);
// Create UI buttons
recordBtn = createButton("Start Recording");
recordBtn.position(20, 20);
recordBtn.mousePressed(startRecording);
playBtn = createButton("Play Back");
playBtn.position(20, 60);
playBtn.attribute("disabled", ""); // disabled until we have a recording
playBtn.mousePressed(playRecording);
}
Line-by-line explanation (10 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Makes the canvas fill the entire browser window, so the dog draws at full screen
textAlign(CENTER, CENTER);
textSize(24);
Centers all text both horizontally and vertically, and sets a base font size
recordBtn = createButton("Start Recording");
recordBtn.position(20, 20);
recordBtn.mousePressed(startRecording);
Creates the button to start recording and connects it to the startRecording function
playBtn = createButton("Play Back");
playBtn.position(20, 60);
playBtn.attribute("disabled", "");
playBtn.mousePressed(playRecording);
Creates the play button, starts it disabled (grayed out) until a recording exists, and connects it to playRecording
createCanvas(windowWidth, windowHeight);- Creates a canvas as wide and tall as the browser window, so the dog scales to fill the screen
textAlign(CENTER, CENTER);- Tells p5.js to center text both horizontally and vertically at the point where you draw it
textSize(24);- Sets the default font size to 24 pixels for all text drawn afterward
recordBtn = createButton("Start Recording");- Creates a clickable button labeled 'Start Recording' and stores it in the recordBtn variable
recordBtn.position(20, 20);- Positions the record button 20 pixels from the top-left corner of the browser
recordBtn.mousePressed(startRecording);- Tells p5.js to call the startRecording function whenever someone clicks this button
playBtn = createButton("Play Back");- Creates the play button and stores it in the playBtn variable
playBtn.position(20, 60);- Positions the play button below the record button (60 pixels from the top)
playBtn.attribute("disabled", "");- Disables the play button (grays it out) so users cannot click it until a recording exists
playBtn.mousePressed(playRecording);- Tells p5.js to call the playRecording function when the play button is clicked