Laser Eyes with JavaScript and MediaPipe

4 Dec 2026

Laser Eyes demo, red laser beams from both eyes to two fingertips, drawing a heart shaped trail of fire

Shoot laser beams from your eyes to your fingertips using MediaPipe and Canvas 2D. No npm, no backend, one HTML file.

Try the live demo or play it in Pug's Hunt

What You'll Build#

A real-time laser eyes effect that runs entirely in the browser. Open your mouth and hold up your hand to fire red laser beams from your iris to your fingertip, complete with fire particles at the impact point. Raise your eyebrows and your pupils glow red like the Terminator. Two hands work too, with each eye targeting the nearest finger.

Features at a glance:

  • 👄 Mouth trigger: open your mouth to fire lasers from both eyes to your fingertip
  • ✌️ Two-hand support: each eye locks onto the nearest finger
  • 🔴 Eyebrow glow: raise your eyebrows for red Terminator-style pupils (independent of the lasers)
  • 🔥 Fire particles: HSL-colored fire explodes at the impact point with additive blending

We'll use Google's MediaPipe FaceLandmarker to find eyes, mouth, and eyebrows, the HandLandmarker to track fingertips, and plain Canvas 2D for all the visual effects.

This builds on my earlier MediaPipe Hand Tracking & Face Detection in JavaScript tutorial, where we drew rainbow trails with our fingers and used an open mouth as a trigger. If you haven't used MediaPipe before, that one is a gentler start. Here we combine the same two trackers into something a bit more dramatic.

If you want a full breakdown of all 478 face mesh landmark points, check out the MediaPipe Face Mesh: All 478 Landmark Points reference guide.

Before You Start#

You need to serve the file over HTTP. getUserMedia() (webcam access) is blocked on file:// URLs. Open a terminal in your project folder and run:

Then open http://localhost:8080 in Chrome or Edge.

Step 1: HTML + CSS Skeleton#

Create a file called index.html. We need a <video> element for the webcam and a <canvas> on top for effects. The canvas sits absolutely positioned over the video so we can draw without blocking the feed.

Both the video and canvas get transform: scaleX(-1). This mirrors the image so it feels natural, like looking in a mirror. Skip this and moving your left hand moves things on the right side of the screen.

What you should see: A black page with "Loading laser eyes..." centered on screen.

Step 2: Start the Webcam#

Inside the <script> tag, grab the DOM elements and write a startCamera() function. We request 1280x720 with facingMode: "user" for the front camera. Once the video fires loadeddata, we match the canvas dimensions to the native video resolution so our drawing coordinates line up pixel for pixel.

What you should see: Your webcam feed appears, mirrored like a selfie camera. If the browser asks for camera permission, click Allow.

Step 3: Load the FaceLandmarker#

We import directly from the MediaPipe CDN. No npm, no bundler needed. The FilesetResolver loads the WASM runtime, then we create a FaceLandmarker configured for video mode with GPU acceleration. We only need one face and don't need blendshapes, so those stay off.

What you should see: The webcam feed as before. The model loads in the background. We'll start using it in the next step.

Step 4: Detect the Face and Draw Iris Dots#

Now the fun part. We set up a requestAnimationFrame loop that feeds each video frame to the landmarker. It returns 478 landmark points on your face, but we only need a handful.

Let's create a FACE object to store the landmark indices we care about. This way we never have to remember that 468 is the left iris or that 13 is the upper lip. We just write FACE.iris.left or FACE.mouth.upperLip. If you've used the Face Mesh Landmarks reference, you'll recognize the region names.

Each landmark gives us x and y as values between 0 and 1, so we multiply by the canvas dimensions to get pixel coordinates.

What you should see: Two red dots tracking your irises in real time. Move your head around and they follow.

Try this step live

Step 5: Detect Mouth Open/Close#

We need a trigger for the lasers. Opening your mouth will fire them.

The detection is simple: measure the vertical distance between FACE.mouth.upperLip (landmark 13) and FACE.mouth.lowerLip (landmark 14), then normalize by face height. Normalizing by the distance from forehead to chin makes it work regardless of how close or far you are from the camera.

A threshold of about 0.035 works well. Below that your mouth is closed, above it you're open.

To test it, change the iris dot color based on mouth state. In the loop, replace the iris drawing with:

What you should see: Red iris dots that turn green when you open your mouth. Close your mouth and they go back to red.

Step 6: Add the HandLandmarker#

The lasers need a target. We'll use your index fingertip (landmark 8) for that. We create the HandLandmarker the same way as the face one, sharing the same WASM fileset. numHands: 2 lets us track both hands.

Update the import to include HandLandmarker:

Add the hand landmarker creation inside startMediaPipe():

In the loop, detect hands alongside the face and draw white dots on the fingertips:

What you should see: Red/green iris dots plus white dots tracking your index fingertips. Both trackers running together on every frame.

Step 7: Draw Laser Beams#

Here's where it comes together. When your mouth is open AND at least one hand is visible, we fire lasers from both irises to the fingertip.

The laser is three lines drawn on top of each other: a thick outer red glow, a medium orange layer, and a thin white core. The shadowBlur and shadowColor properties on the canvas context give us that bloom effect for free.

With two hands, we sort them by x position and send each eye's laser to the nearest finger.

Now update the loop to fire the lasers. Remove the debug iris dots and fingertip dots from earlier steps, and replace them with the real logic:

What you should see: Open your mouth while holding up a hand and red laser beams shoot from your eyes to your fingertip. Two hands? Each eye picks the nearest finger.

Try this step live

Step 8: Eyebrow Glow Pupils#

Time for the Terminator look. When you raise your eyebrows, your pupils glow red. This works independently from the lasers, so you can do the menacing red-eye look without firing.

We measure the distance from each eyebrow peak (FACE.eyebrows.left at landmark 52, FACE.eyebrows.right at 282) to the corresponding iris center. The y axis increases downward, so a raised eyebrow has a larger gap. Normalize by face height and check against a threshold of 0.11.

The pupil glow is three concentric circles: a dark red outer disc with a soft shadow, a bright red inner disc, and a tiny white hot core. Simple, but it reads well on camera.

In the loop, check for raised eyebrows before the firing logic:

What you should see: Raise your eyebrows and your pupils glow red. Lower them and the glow disappears. Combine with the lasers for the full effect.

Step 9: Fire Particles at the Impact Point#

Now the best part. Lasers need impact. Let's add fire where the beams hit your fingers.

Each frame we spawn particles at the fingertip position. They drift upward with a bit of random velocity, shrink, and fade. For the color we use HSL instead of RGB. The hue starts around 60 (orange/yellow) when a particle is born and drops toward 15 (deep red) as it dies, giving us that natural fire color shift without any manual RGB mixing.

The key trick: globalCompositeOperation: "lighter" makes overlapping particles add their brightness together instead of covering each other. Where particles cluster, the fire glows hotter. That's what makes it look real.

In the firing block, spawn fire at each fingertip after drawing the lasers:

What you should see: The full effect. Raise eyebrows for glowing red pupils. Open your mouth with a hand up and lasers fire with fiery explosions on your fingertips.

Step 10: Keep the Beam Steady#

Play with it for a minute and you'll notice the laser sometimes cuts out for a frame or two, even though your finger is clearly in view. There are two reasons.

The first is the mouth check. A single threshold means that when your mouth hovers right around 0.035, tiny jitter in the landmarks flips it between open and closed every few frames. The fix is hysteresis: use one threshold to open and a lower one to close. Once the mouth counts as open, it stays open until you really close it.

The second is the hand tracker. When you move your finger fast, the webcam frame gets motion blur and the HandLandmarker misses the hand for a frame. Without a hand there's no target, so the laser switches off. We fix this by remembering the last good result and reusing it for a short grace period, 250 milliseconds is plenty. The same trick works for the face.

With two hands there's a catch. If you remember the hands as one list, a frame where only one of the two hands is found replaces that list with a single hand, and both beams jump to the same finger for a frame before snapping back. So each hand gets its own "last seen" time. A new detection takes the place of the closest hand from the previous frame, and a hand that's missing is kept around until its own grace period runs out.

In the loop, replace the code that builds the hands list:

While we're here, the firing code can get simpler too. Instead of relying on which landmark is the "left" iris, sort both eyes and both fingers by their x position and pair them up. The eye on the left of the frame always aims at the finger on the left, so the beams never cross, and one loop covers the one-hand and two-hand case:

A quarter of a second is short enough that the beam still stops almost right away when you really drop your hand, but long enough to hide a blurry frame.

What you should see: A beam that stays on while you sweep your finger around, also with two hands up, and only switches off when you close your mouth or take your hand away.

Try this step live or try the finished demo

Putting It All Together#

The final initialize function ties everything together: load the models, start the camera, then kick off the loop.

The live demo adds one extra: a loading bar. The model files are about 11 MB together, and on a slow connection the first visit can take a while, so a plain "Loading models..." text looks like the page is stuck. The demo downloads both models in parallel with fetch(), reads the response stream to show real progress in megabytes, and passes the bytes to MediaPipe with modelAssetBuffer instead of modelAssetPath. It's all in loader.js if you want the same in your own project.

You can grab the complete source code or walk through each step interactively in the step-by-step tutorial.

From Demo to Game: Laser Eyes in Pug's Hunt#

This demo started as a side experiment, but it didn't stay one. Pug's Hunt is my webcam version of a classic duck hunting game, where your hand replaces the light gun and opening your mouth pulls the trigger. Since Season 1, Laser Eyes is a weapon you can pick in the menu.

The controls will feel familiar after this tutorial. You aim with your hand, open your mouth to fire a beam from the pug's eyes, and hold it on a duck until it roasts. Raise your eyebrows and the pug in the panel raises its eyebrows with you. The game adds a few things a demo doesn't need: a heat meter that shows how close a duck is to roasting, a cooling phase when the beam lets go, and separate leaderboards for Laser Eyes so the scores don't mix with the classic weapon.

The core is still the same handful of landmarks from this tutorial: the irises, the lips, the eyebrows, and your index fingertip. Turning a demo into a game was mostly about everything around them, like tuning the thresholds for faces and webcams I had never tested, and making sure a score can be checked on the server.

One thing did change under the hood. The eyebrow check in Step 8 divides by face height, and in recorded webcam footage a nodding head or an open jaw changes that height enough to read as raised eyebrows. In the game the eyebrow lift is measured from the line between the outer eye corners instead, which stays put when you nod or talk.

Want to skip the menu? This link opens the game with Laser Eyes already selected:

Play Pug's Hunt with Laser Eyes

Performance Tips#

If things run slow, these help the most:

Reduce fire particles. Lower FIRE_PER_FRAME from 12 to 6 on older machines. The visual difference is subtle but the frame rate impact is big.

One hand only. Change numHands: 2 to numHands: 1 if you don't need both hands. Detection time roughly halves.

Skip shadowBlur on particles. Canvas shadowBlur forces a full GPU blur pass per draw call. We use it on the laser lines (only 2-6 calls per frame), but never on individual particles.

Tweak thresholds. The MOUTH_OPEN, MOUTH_CLOSE and EYEBROW_RAISE thresholds work for most faces, but everyone's different. Log the values to the console and adjust for your face.

Landmark Reference#

Here are all the landmark indices used in this tutorial:

ConstantIndexWhat it is
FACE.iris.left468Left iris center
FACE.iris.right473Right iris center
FACE.mouth.upperLip13Upper lip center
FACE.mouth.lowerLip14Lower lip center
FACE.eyebrows.left52Left eyebrow peak
FACE.eyebrows.right282Right eyebrow peak
FACE.forehead.top10Top of forehead
FACE.jaw.chin152Bottom of chin
HAND.indexFingerTip8Index fingertip

For the full 478-point face mesh map, see MediaPipe Face Mesh: All 478 Landmark Points.

Frequently Asked Questions

How do I load MediaPipe FaceLandmarker and HandLandmarker together in one HTML file?

Import both from the @mediapipe/tasks-vision package via the jsdelivr CDN. Call FilesetResolver.forVisionTasks() once to load the WASM runtime, then create both landmarkers from the same fileset. Set both to runningMode: "VIDEO" and delegate: "GPU". In your animation loop, call detectForVideo() on both landmarkers with the same video element and timestamp. Both models run on every frame and share the GPU context.

What face landmarks do I need for laser eye tracking?

The iris centers are landmarks 468 (left eye) and 473 (right eye). These are part of the optional iris refinement landmarks that MediaPipe's FaceLandmarker provides by default. For mouth detection, use landmarks 13 (upper lip center) and 14 (lower lip center). Normalize the lip distance by the full face height (landmark 10 at the forehead to landmark 152 at the chin) so the threshold works at any distance from the camera.

How do I draw glowing laser beams on a Canvas 2D context?

Draw three lines stacked on top of each other along the same path. Start with a thick line (28px) in semi-transparent red with a large shadowBlur (35) for the outer glow. Then a medium line (12px) in orange with moderate blur. Finally a thin white line (3px) with no shadow for the hot core. The canvas shadowBlur and shadowColor properties create the bloom effect. This technique is fast because it only needs 3 draw calls per beam.

How do I create fire particles with Canvas 2D?

Use an array of particle objects with position, velocity, life, and size. Each frame, update positions, apply gravity (subtract from vy), and reduce life. For fire colors, use HSL: start the hue at 60 (orange) and fade toward 15 (deep red) as the particle dies. Set globalCompositeOperation to "lighter" before drawing so overlapping particles add brightness together (additive blending), making clustered areas glow hotter. Reset to "source-over" afterward.

How do I detect if a mouth is open using MediaPipe face landmarks?

Measure the vertical distance between landmark 13 (upper lip center) and landmark 14 (lower lip center). Divide this by the face height (the distance from landmark 10 to landmark 152) to normalize the value. A ratio above approximately 0.035 indicates an open mouth. Normalizing by face height makes the detection work regardless of the user's distance from the camera. The mouthOpen() function used in this tutorial returns both a boolean and the raw value so you can fine-tune the threshold.

How do I detect raised eyebrows with MediaPipe?

Measure the vertical gap between each eyebrow peak and its corresponding iris center. Landmark 52 is the left eyebrow peak and 282 is the right. Compare these to the iris centers at 468 and 473. Divide by face height to normalize. An average gap above 0.11 indicates raised eyebrows. This value works for most faces but you can log it to the console and adjust the threshold to match your own expression range.

Can I play Laser Eyes in a game?

Yes. Laser Eyes is a weapon in Pug's Hunt, a webcam duck hunting game that runs in the browser. Open pugshunt.com/play?weapon=laser to start with Laser Eyes selected. Aim with your hand, open your mouth to fire, and hold the beam on a duck until it roasts. It needs a webcam, so mouse and touch play use the classic weapon.

Resources

Related Articles