setup()
setup() runs once at the start of the sketch. It's where you initialize the canvas, load assets (like webcam video), and set up game state. Every variable you want to exist when the game starts should be prepared here.
function setup() {
createCanvas(windowWidth, windowHeight);
video = createCapture(VIDEO);
video.size(320, 240);
video.hide();
textFont('sans-serif');
textAlign(LEFT, TOP);
initUpgrades();
lastUpdateTime = millis();
}
Line-by-line explanation (8 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a fullscreen canvas that matches the current window size
video = createCapture(VIDEO);
Requests access to the user's webcam and stores the video stream
initUpgrades();
Populates the upgrades array with 40 unique upgrade definitions
createCanvas(windowWidth, windowHeight);- Creates a fullscreen p5.js canvas that spans the entire browser window
video = createCapture(VIDEO);- Asks the browser for permission to access your webcam and stores the video stream in the `video` variable
video.size(320, 240);- Resizes the internal webcam resolution to 320×240 pixels (smaller = faster processing)
video.hide();- Hides the raw HTML video element so it doesn't appear twice on the page—we'll draw it manually with p5.js
textFont('sans-serif');- Sets the font family for all text drawn by p5.js
textAlign(LEFT, TOP);- Tells p5.js that text coordinates refer to the top-left corner of each text string
initUpgrades();- Calls a helper function to create and populate the upgrades array with 40 unique items
lastUpdateTime = millis();- Records the current time in milliseconds; used later to calculate time elapsed between frames for auto-click income