Below is a single-file demo that
• Opens your webcam
• Detects the rectangle that contains your head/face (Face Detection API)
• Scales that rectangle upward from its bottom edge, so you get a comic “big-head” that grows upward
Copy everything into a file called big-head.html, open it in Chrome (or any browser that supports the FaceDetector API), allow webcam access, and watch the effect live.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Big-Head Webcam Demo</title>
<style>
html,body{margin:0;height:100%;background:#111;color:#eee;font-family:sans-serif;display:flex;flex-direction:column;align-items:center;justify-content:center;overflow:hidden}
#wrapper{position:relative;width:640px;height:480px}
video,canvas{position:absolute;top:0;left:0;width:100%;height:100%;border-radius:8px;object-fit:cover}
#msg{margin-top:12px;font-size:14px;opacity:.8}
</style>
</head>
<body>
<h2>🧑🚀 Live Big-Head Filter</h2>
<div id="wrapper">
<video id="video" autoplay muted playsinline></video>
<canvas id="output"></canvas>
</div>
<div id="msg">Grant webcam permission to begin…</div>
<script>
(async () => {
// Elements
const video = document.getElementById('video');
const canvas = document.getElementById('output');
const ctx = canvas.getContext('2d');
const msgElt = document.getElementById('msg');
// Helpers
const wait = ms => new Promise(r => setTimeout(r, ms));
// 1️⃣ Start webcam
try {
const stream = await navigator.mediaDevices.getUserMedia({ video: { width: 640, height: 480 }});
video.srcObject = stream;
await new Promise(r => video.onloadedmetadata = r);
} catch(e){
msgElt.textContent = 'Could not access webcam 😢';
console.error(e);
return;
}
// 2️⃣ Prepare detector
if (!('FaceDetector' in window)) {
msgElt.innerHTML = 'FaceDetector API not supported in this browser.<br>Try Chrome 83+ or enable the flag: <code>#enable-experimental-web-platform-features</code>.';
return;
}
const detector = new FaceDetector({ fastMode:true, maxDetectedFaces:1 });
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
msgElt.textContent = 'Detecting face…';
// 3️⃣ Animation loop
async function render(){
// Draw the raw video frame first
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
try {
const faces = await detector.detect(video);
if (faces.length){
const face = faces[0].boundingBox; // {x,y,width,height}
// --- Calculate enlarged head rect (anchor bottom) ---
const scale = 1.6; // how big
const newW = face.width * scale;
const newH = face.height * scale;
const dx = face.x - (newW - face.width)/2; // keep it centered horizontally
const dy = face.y - (newH - face.height); // grow upward, so bottom stays put
// Grab original head pixels into an off-screen canvas
const headCrop = document.createElement('canvas');
headCrop.width = face.width;
headCrop.height = face.height;
headCrop.getContext('2d').drawImage(
video,
face.x, face.y, face.width, face.height, // src
0, 0, face.width, face.height // dst
);
// Optional: darken the original head area so the enlargement fully masks it
ctx.save();
ctx.fillStyle = 'rgba(17,17,17,0.6)';
ctx.fillRect(face.x, face.y, face.width, face.height);
ctx.restore();
// Draw the enlarged head on top
ctx.drawImage(headCrop, dx, dy, newW, newH);
msgElt.textContent = 'Big-head mode ON 🤪';
}else{
msgElt.textContent = 'Looking for a face…';
}
} catch(e){
console.error(e);
}
requestAnimationFrame(render);
}
// Give the camera a moment to warm up
await wait(300);
render();
})();
</script>
</body>
</html>
How it works (quick rundown):
- getUserMedia grabs a 640 × 480 webcam feed.
- FaceDetector detects one face per frame (bounding rectangle).
- For each frame:
a. Draw entire video frame onto a canvas.
b. Copy the face region into a tiny off-screen canvas.
c. Black-out that original region a bit (so we don’t see the old head).
d. Draw the face again, but scaled up (scale = 1.6) and anchored at the rectangle’s bottom edge, so it “grows” upward.
Because everything lives in one HTML file (only native APIs + inline JS), you can drop it anywhere and run. If your browser doesn’t support the experimental FaceDetector API, consider swapping in a CDN-loaded library such as face-api.js or MediaPipe FaceMesh; the scaling logic stays identical—you’d just supply the bounding box.