setup()
setup() runs once when the sketch starts. It initializes all the variables, creates the canvas, sets up the webcam, configures colors, and creates interactive elements. Think of it as the 'preparation' phase where everything is configured before the animation begins.
function setup() {
createCanvas(windowWidth, windowHeight);
pixelDensity(1);
textFont('monospace');
textSize(10);
colorMode(HSB, 360, 100, 100);
fill(120, 100, 100);
video = createCapture(VIDEO);
video.size(gridWidth, gridHeight);
video.hide();
cellWidth = width / gridWidth;
cellHeight = height / gridHeight;
pg = createGraphics(gridWidth, gridHeight);
pg.pixelDensity(1);
pg.background(0);
pg.stroke(255);
pg.strokeWeight(1);
toggleButton = createButton('Toggle Mode');
toggleButton.position(10, 30);
toggleButton.mousePressed(toggleMode);
}
π§ Subcomponents:
createCanvas(windowWidth, windowHeight); pixelDensity(1); textFont('monospace'); textSize(10);
Creates a full-window canvas with monospace font and sets pixel density for consistent rendering
colorMode(HSB, 360, 100, 100); fill(120, 100, 100);
Sets HSB color mode and fills with bright green (hue 120) for retro terminal aesthetic
video = createCapture(VIDEO); video.size(gridWidth, gridHeight); video.hide();
Captures webcam feed, resizes it to ASCII grid dimensions for efficient sampling, and hides the video element
cellWidth = width / gridWidth; cellHeight = height / gridHeight;
Calculates the pixel size of each ASCII character cell based on canvas and grid dimensions
pg = createGraphics(gridWidth, gridHeight); pg.pixelDensity(1); pg.background(0); pg.stroke(255); pg.strokeWeight(1);
Creates an off-screen drawing buffer for drawing mode with black background and white stroke
toggleButton = createButton('Toggle Mode'); toggleButton.position(10, 30); toggleButton.mousePressed(toggleMode);
Creates a button to switch between webcam and drawing modes
Line by Line:
createCanvas(windowWidth, windowHeight)- Creates a canvas that fills the entire browser window, making the ASCII art display full-screen
pixelDensity(1)- Sets pixel density to 1 for consistent pixel access across different screen densities (retina vs standard)
textFont('monospace')- Sets the font to monospace so each character has the same width, essential for ASCII art alignment
textSize(10)- Sets the text size to 10 pixels, controlling how large each ASCII character appears
colorMode(HSB, 360, 100, 100)- Switches to HSB (Hue, Saturation, Brightness) color mode, making it easier to work with green text
fill(120, 100, 100)- Sets the fill color to bright green (hue 120, full saturation, full brightness) for the retro terminal look
video = createCapture(VIDEO)- Requests access to the user's webcam and creates a video capture object
video.size(gridWidth, gridHeight)- Resizes the video feed to 100x60 pixels (matching the ASCII grid) for efficient pixel sampling
video.hide()- Hides the actual video element from the DOM so only the ASCII art is visible
cellWidth = width / gridWidth- Calculates how many canvas pixels wide each ASCII character cell should be
cellHeight = height / gridHeight- Calculates how many canvas pixels tall each ASCII character cell should be
pg = createGraphics(gridWidth, gridHeight)- Creates a separate off-screen graphics buffer with the same dimensions as the ASCII grid for drawing mode
pg.pixelDensity(1)- Ensures the graphics buffer also uses pixel density of 1 for consistent pixel access
pg.background(0)- Initializes the drawing buffer with a black background
pg.stroke(255)- Sets the stroke color to white (255 in HSB) for drawing on the buffer
pg.strokeWeight(1)- Sets the stroke width to 1 pixel for precise drawing
toggleButton = createButton('Toggle Mode')- Creates a button with the label 'Toggle Mode' that users can click to switch between modes
toggleButton.position(10, 30)- Positions the button 10 pixels from the left and 30 pixels from the top of the window
toggleButton.mousePressed(toggleMode)- Connects the button's click event to the toggleMode function so clicking it switches modes