setup()
setup() runs once when the sketch starts, and here it does the heavy lifting: requesting webcam access, sizing the video down for fast pixel sampling, and preparing both the canvas and an off-screen buffer. Getting these dimensions and modes right up front keeps the draw() loop simple and fast.
🔬 This sets up the classic green terminal look using HSB hue 120. What happens if you change 120 to 0 (red) or 240 (blue)? What about setting saturation to 0 for grayscale text?
colorMode(HSB, 360, 100, 100);
fill(120, 100, 100); // Bright green color in HSB (120 hue, 100 saturation, 100 brightness)
function setup() {
// Create a canvas that fills the entire browser window
createCanvas(windowWidth, windowHeight);
// Set pixel density to 1 for consistent pixel access across screens
pixelDensity(1);
// Set text properties for ASCII output
textFont('monospace'); // Use a monospace font for consistent character width
textSize(10); // Set text size
// Use HSB color mode for intuitive green text on black background
colorMode(HSB, 360, 100, 100);
fill(120, 100, 100); // Bright green color in HSB (120 hue, 100 saturation, 100 brightness)
// Initialize webcam capture
video = createCapture(VIDEO);
// Resize the video feed to match our ASCII grid dimensions for efficient pixel sampling
video.size(gridWidth, gridHeight);
video.hide(); // Hide the actual video element from the DOM
// Calculate the width and height of each character cell on the canvas
cellWidth = width / gridWidth;
cellHeight = height / gridHeight;
// Create an off-screen graphics buffer for the drawing mode
// This buffer will have the same dimensions as our ASCII grid
pg = createGraphics(gridWidth, gridHeight);
pg.pixelDensity(1); // Ensure consistent pixel density for the drawing buffer
pg.background(0); // Initialize drawing buffer with black
pg.stroke(255); // White stroke for drawing
pg.strokeWeight(1); // 1-pixel stroke weight for drawing
// Create a button to toggle between webcam and drawing modes
toggleButton = createButton('Toggle Mode');
toggleButton.position(10, 30); // Position the button below the FPS counter
toggleButton.mousePressed(toggleMode); // Assign the toggleMode function to the button's click event
}
Line-by-line explanation (14 lines)
createCanvas(windowWidth, windowHeight);- Makes the canvas fill the entire browser window so the ASCII art has room to spread out.
pixelDensity(1);- Forces the canvas to use exactly 1 pixel per unit regardless of the screen's retina/HiDPI density, so pixel array math stays predictable.
textFont('monospace');- Monospace fonts give every character the same width, which is essential for lining characters up into a neat grid.
textSize(10);- Sets how big each printed character is - this also indirectly affects how spread out the ASCII grid looks.
colorMode(HSB, 360, 100, 100);- Switches p5's color system to Hue-Saturation-Brightness, making it easy to describe 'bright green' as a single hue number (120).
fill(120, 100, 100); // Bright green color in HSB (120 hue, 100 saturation, 100 brightness)- Sets the default text color to a vivid green, giving the sketch its classic terminal look.
video = createCapture(VIDEO);- Requests access to the user's webcam and starts streaming video frames into the `video` object.
video.size(gridWidth, gridHeight);- Shrinks the incoming video to a tiny 100x60 resolution so sampling its pixels for ASCII conversion is fast and simple.
video.hide();- Hides the raw <video> HTML element since we only want to see the ASCII version, not the actual video.
cellWidth = width / gridWidth;- Figures out how many canvas pixels each ASCII character cell should occupy horizontally so the grid fills the whole canvas width.
cellHeight = height / gridHeight;- Same idea as cellWidth, but for the vertical spacing between rows of characters.
pg = createGraphics(gridWidth, gridHeight);- Creates a separate, small off-screen canvas used only for the 'drawing' mode, matching the ASCII grid's resolution.
toggleButton = createButton('Toggle Mode');- Adds an actual HTML button to the page that the user can click.
toggleButton.mousePressed(toggleMode);- Tells p5.js to run the toggleMode() function whenever this button is clicked.