setup()
setup() runs once when the sketch starts. It is the place to initialize your canvas, capture device access, and set values that do not change every frame. By pre-setting textFont and textSize here instead of inside draw(), we avoid recalculating them sixty times per second.
function setup() {
createCanvas(windowWidth, windowHeight);
video = createCapture(VIDEO);
video.size(640, 480); // Keep video size consistent for pixel access
video.hide();
textFont('monospace');
textSize(10); // Set textSize once for ASCII mode
}
Line-by-line explanation (6 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that spans the entire browser window
video = createCapture(VIDEO);
Requests access to the user's webcam and stores the live video stream
video.size(640, 480);
Sets a consistent video resolution so pixel indexing calculations remain predictable
createCanvas(windowWidth, windowHeight);- Creates a p5.js canvas that matches your browser window size, so the video effect fills the screen
video = createCapture(VIDEO);- Asks the browser for permission to access your webcam and creates a video object that updates every frame
video.size(640, 480);- Forces the video stream to be 640×480 pixels internally, so mapping and pixel calculations are consistent no matter your screen size
video.hide();- Hides the default video preview element so only your effect-processed version appears on canvas
textFont('monospace');- Sets the font to monospace so ASCII characters are evenly spaced and look correct
textSize(10);- Pre-sets the text size for ASCII mode to 10 pixels, reducing computation during the draw loop