<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Web-cam Breakout – your face is the joystick!</title>
<!---- Simple page styling ‑->
<style>
*{box-sizing:border-box}
body{
margin:0;
font:16px/1.4 Arial,Helvetica,sans-serif;
background:#222;
color:#fff;
text-align:center;
display:flex;
flex-direction:column;
align-items:center;
gap:.8rem;
}
h1{margin:.6rem 0 0}
#wrapper{
position:relative;
width:640px;
height:480px;
touch-action:none;
}
/* mirror the camera so movement feels natural */
video{
position:absolute;
width:640px;height:480px;
top:0;left:0;
transform:scaleX(-1);
opacity:.25; /* 25 % visible so you can still “see yourself”;
set to 0 if you want it hidden. */
pointer-events:none;
}
canvas{
position:absolute;
top:0;left:0;
z-index:2;
}
p{max-width:640px;margin:.3rem auto 1rem}
a{color:#0af}
</style>
</head>
<body>
<h1>Web-cam Breakout</h1>
<p>Allow camera access, then move your <b>head left/right</b> to steer the paddle and keep the ball in play.<br>
(All processing runs locally — nothing is uploaded!)</p>
<div id="wrapper">
<video id="video" playsinline muted></video>
<canvas id="game" width="640" height="480"></canvas>
</div>
<!-- TensorFlow JS + PoseNet from public CDNs -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@3.21.0/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/posenet@2.2.1/dist/posenet.min.js"></script>
<script>
// Elements
const video = document.getElementById('video');
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
/* --------------------------------------------------------------------- */
/* 1. Camera init */
/* --------------------------------------------------------------------- */
async function initCamera(){
const stream = await navigator.mediaDevices
.getUserMedia({video:{width:640,height:480},audio:false});
video.srcObject = stream;
return new Promise(res => video.onloadedmetadata = () => {video.play(); res();});
}
/* --------------------------------------------------------------------- */
/* 2. PoseNet init */
/* --------------------------------------------------------------------- */
async function loadPoseNet(){
return await posenet.load({
architecture:'MobileNetV1',
outputStride:16,
inputResolution:{width:640,height:480},
multiplier:0.75
});
}
/* --------------------------------------------------------------------- */
/* 3. Game state */
/* --------------------------------------------------------------------- */
const paddle = { w:120, h:15, x:(640-120)/2, y:480-35 };
const ball = { x:320, y:240, r:10, vx:5, vy:-5 };
let score = 0;
let noseX = null; // updated from PoseNet
/* --------------------------------------------------------------------- */
/* 4. Helpers */
/* --------------------------------------------------------------------- */
function updateGame(){
/* Paddle controlled by nose X (if available) */
if(noseX !== null){
paddle.x = (noseX / video.videoWidth) * (canvas.width - paddle.w);
}
/* Ball physics ------------------------------------------------------ */
ball.x += ball.vx;
ball.y += ball.vy;
// left/right walls
if(ball.x - ball.r < 0 || ball.x + ball.r > canvas.width) ball.vx *= -1;
// top wall
if(ball.y - ball.r < 0) ball.vy *= -1;
// paddle collision
if(ball.y + ball.r >= paddle.y &&
ball.x >= paddle.x && ball.x <= paddle.x + paddle.w){
ball.vy *= -1;
ball.y = paddle.y - ball.r; // stick to paddle top
score++;
}
// missed -> reset
if(ball.y - ball.r > canvas.height){
Object.assign(ball,{x:320,y:240,vx:5,vy:-5});
score = 0;
}
}
function draw(){
ctx.clearRect(0,0,canvas.width,canvas.height);
// paddle
ctx.fillStyle = '#0af';
ctx.fillRect(paddle.x, paddle.y, paddle.w, paddle.h);
// ball
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.r, 0, Math.PI*2);
ctx.fillStyle = '#ff9500';
ctx.fill();
// score
ctx.fillStyle = '#fff';
ctx.font = '20px Arial';
ctx.fillText('Score: '+score, 10, 30);
}
/* --------------------------------------------------------------------- */
/* 5. Main loop */
/* --------------------------------------------------------------------- */
async function main(){
await initCamera();
const net = await loadPoseNet();
let lastPose = 0;
async function loop(){
requestAnimationFrame(loop);
/* Pose ‑ run ~10 fps to save CPU */
const now = performance.now();
if(now - lastPose > 100){
const pose = await net.estimateSinglePose(video, {flipHorizontal:true});
const nose = pose.keypoints.find(k => k.part === 'nose');
if(nose && nose.score > 0.5){
noseX = nose.position.x; // already mirrored
}
lastPose = now;
}
updateGame();
draw();
}
loop();
}
main().catch(err=>{
alert('Unable to start camera: '+err.message);
console.error(err);
});
</script>
</body>
</html>
What this does
• Requests webcam access (video never leaves your device).
• Runs TensorFlow JS PoseNet in the browser to track your nose position.
• Maps the nose’s X-coordinate to a paddle in a tiny Breakout-style game.
• Bounce the ball, rack up points, no mouse/keyboard needed — just move your head! 🎮📷