setup()
setup() runs exactly once when the sketch starts. It initializes all DOM references, attaches event listeners, and loads the MediaPipe face detection library from CDN. Understanding this function teaches you how p5.js integrates with external JavaScript libraries and the HTML DOM.
function setup() {
canvasElement = createCanvas(windowWidth, windowHeight);
canvasElement.parent(document.body);
canvasElement.hide();
// Login elements
loginSection = select('#login');
appSection = select('#app');
usernameInput = select('#username');
passwordInput = select('#password');
loginButton = select('#loginBtn');
loginMessage = select('#loginMessage');
loginButton.mousePressed(handleLogin);
// Tab controls
tabButtons = selectAll('.tab-btn');
tabPanels = selectAll('.tab-panel');
tabButtons.forEach((btn) => btn.mousePressed(() => switchTab(btn)));
// App elements
imageInput = select('#imageUpload');
videoInput = select('#videoUpload');
statusDiv = select('#status');
processingButton = select('#startBtn');
textInput = select('#textInput');
textSizeSlider = select('#textSize');
textColorInput = select('#textColor');
outfitInput = select('#outfitUpload');
outfitScaleSlider = select('#outfitScale');
outfitOffsetSlider = select('#outfitOffset');
outfitOpacitySlider = select('#outfitOpacity');
clearOutfitButton = select('#clearOutfitBtn');
imageInput.elt.addEventListener('change', handleImageFile);
videoInput.elt.addEventListener('change', handleVideoFile);
outfitInput.elt.addEventListener('change', handleOutfitFile);
clearOutfitButton.mousePressed(() => {
outfitImage = null;
statusDiv.html('Outfit removed. Upload a new one to continue dressing up.');
});
processingButton.mousePressed(startProcessing);
// Recording controls
recordControls = select('#recordControls');
recordButton = select('#recordBtn');
downloadLink = select('#downloadLink');
recordButton.mousePressed(toggleRecording);
recordControls.style('display', 'none');
downloadLink.style('display', 'none');
if (window.FaceDetection) {
faceDetection = new FaceDetection({
locateFile: (file) =>
`https://cdn.jsdelivr.net/npm/@mediapipe/face_detection@0.4.1646425229/${file}`,
});
faceDetection.setOptions({
model: 'short',
minDetectionConfidence: 0.5,
});
faceDetection.onResults(onFaceResults);
faceDetectionReady = true;
} else {
console.error('MediaPipe Face Detection library not loaded.');
statusDiv.html('Error: Face detection library not loaded.');
}
}
Line-by-line explanation (9 lines)
🔧 Subcomponents:
canvasElement = createCanvas(windowWidth, windowHeight);
Creates a full-window canvas and hides it until video processing begins
loginSection = select('#login');
Caches all HTML elements into variables so they can be controlled from JavaScript
imageInput.elt.addEventListener('change', handleImageFile);
Connects file input changes to handler functions that process uploaded files
if (window.FaceDetection) {
Checks if MediaPipe library loaded from CDN, then configures and activates it
canvasElement = createCanvas(windowWidth, windowHeight);- Creates a p5.js canvas sized to the entire window; stores the reference for later access
canvasElement.parent(document.body);- Ensures the canvas is placed directly in the HTML body element
canvasElement.hide();- Hides the canvas initially so the login form and controls are visible first
tabButtons = selectAll('.tab-btn');- Selects all tab buttons from the HTML and stores them in an array for later manipulation
tabButtons.forEach((btn) => btn.mousePressed(() => switchTab(btn)));- Attaches a click handler to each tab button that switches between Face Swap and Dress Up panels
imageInput.elt.addEventListener('change', handleImageFile);- When the user picks an image file, triggers handleImageFile() to process it
faceDetection = new FaceDetection({- Creates a new MediaPipe FaceDetection instance using a CDN URL for its model files
minDetectionConfidence: 0.5,- Sets the detector to require 50% confidence before reporting a face; adjusting this tunes sensitivity
faceDetection.onResults(onFaceResults);- Registers a callback function that fires whenever the detector finishes processing a frame