setup()
setup() runs once when the sketch starts. It is the perfect place to initialize the canvas, load media like videos or images, and set default styling that will apply to the rest of the sketch.
function setup() {
createCanvas(windowWidth, windowHeight);
// Initialize the user's camera in the background
video = createCapture(VIDEO);
video.hide(); // Hide the default HTML element so we can draw it in the canvas
// Set up text styling
textAlign(CENTER, CENTER);
textStyle(BOLD);
}
Line-by-line explanation (5 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that fills the entire browser window
video = createCapture(VIDEO);
Requests and stores the user's camera feed as a p5.js object
textAlign(CENTER, CENTER);
textStyle(BOLD);
Centers all text and makes it bold for consistent styling
createCanvas(windowWidth, windowHeight);- Creates a canvas that is as wide and tall as the entire browser window, using p5.js global variables that always contain the window dimensions
video = createCapture(VIDEO);- Asks the browser to access the user's camera and returns a video object that contains the live camera feed data
video.hide();- Hides the default HTML video element that p5.js creates—we will draw the camera feed on the canvas ourselves instead
textAlign(CENTER, CENTER);- Makes all text drawn after this point center around the coordinates you give it, instead of starting from them
textStyle(BOLD);- Makes all text drawn after this point bold for emphasis