webdevpuneet.com

Eyes Follow Cursor: Mouse Tracking Mascot in JavaScript

Eyes Follow Cursor: Mouse Tracking Mascot in JavaScript · Animations · Plain HTML, CSS & JS · Live preview

CategoryAnimations

Copy & paste

Source Code

<main class="ey-stage" id="eyStage">
  <div class="ey-face" id="eyFace">
    <div class="ey-brows" aria-hidden="true"><i></i><i></i></div>
    <div class="ey-eyes">
      <div class="ey-eye"><span class="ey-pupil"></span><span class="ey-lid"></span></div>
      <div class="ey-eye"><span class="ey-pupil"></span><span class="ey-lid"></span></div>
    </div>
    <div class="ey-mouth" aria-hidden="true"></div>
  </div>
  <h1>Peeker is watching</h1>
  <p class="ey-sub">The eyes follow your cursor or finger, blink now and then, look down when you scroll, and get excited near the button.</p>
  <button type="button" class="ey-cta" id="eyCta">Say hello</button>
</main>

Extend with AI

Take This Snippet Further With AI

Paste this snippet into an AI coding assistant and ask it to explain how each eye computes its own direction to the pointer, how the pupil travel is capped, and how lerp smoothing in requestAnimationFrame produces the natural feel. Follow-ups worth trying: a version with SVG eyebrows that react to the cursor distance, an eyes-only logo for a footer, or a mascot that falls asleep after 30 seconds of idle. Ask it to keep reduced-motion handling.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a "cursor-tracking eyes mascot" in plain HTML, CSS and JavaScript with no libraries.

Requirements:
- A centred stage with a rounded purple face containing two white oval eyes, each with a dark pupil (with a small white highlight) and an eyelid layer, plus two brows and a mouth. Below the face: a heading, a short paragraph and a call-to-action button.
- A requestAnimationFrame loop. For each eye, read its centre with getBoundingClientRect, compute the vector to the pointer, and move the pupil with a CSS transform by min(maxTravel, distance * 0.3) along that direction. maxTravel is the eye radius minus the pupil size so it stays inside the white. Ease toward the target with a lerp factor of about 0.2.
- Listen to pointermove on the window. Initially look at the button.
- Blink by toggling a class that slides the eyelids down for about 140ms at random intervals of 2 to 5 seconds.
- On scroll or wheel, glance down for about 700ms.
- If the pointer has not moved for 3 seconds, pick random points to look at every 1.4 to 2.6 seconds.
- On button hover or focus (and leave or blur), toggle an "excited" class: face scales up slightly, brows rise, mouth opens, pupils grow.
- If prefers-reduced-motion is set, disable blinking and wandering and remove smoothing.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

What's included

Features

Per-eye tracking
Each pupil aims from its own eye centre, so the gaze converges naturally.
Pupils stay inside
Travel is capped at the eye's radius minus the pupil size.
Smooth easing
A lerp in requestAnimationFrame gives the eyes natural inertia.
Random blinking
An eyelid slides over each eye every two to five seconds.
Scroll glance
Scroll and wheel events make the eyes look down briefly.
Idle wandering
With no pointer for three seconds the eyes look around by themselves.
Button reaction
Hover and focus raise the brows, widen the pupils and open the mouth.
Touch and keyboard ready
Pointer events cover touch, and focus triggers the same excited state.

About this UI Snippet

Eyes That Follow the Cursor: A Mascot That Reacts to You

Screenshot of the Eyes Follow Cursor: Mouse Tracking Mascot in JavaScript snippet rendered live

Cursor effects usually leave a trail or add a glow. This one gives the page a character. A purple mascot has two eyes whose pupils slide toward your mouse or finger, blink at random, glance down when you scroll, wander off when you stop moving, and light up when you go near the call-to-action button. It uses plain JavaScript and no library.

Pupils aim from each eye's own centre

Every frame, each eye reads its position with getBoundingClientRect(), measures the vector to the pointer and moves the pupil along it. Because each eye computes its own direction, the pupils converge slightly when the cursor is close, which looks far more alive than moving both by the same offset.

The pupil stays inside the eye

The travel distance is Math.min(max, dist * 0.3), where max is the eye's radius minus the pupil's size. Close to an eye, the pupil moves proportionally. Far away, it stops at the edge instead of leaving the white.

Smoothing makes it feel organic

Position is eased toward the target with eye.x += (tx - eye.x) * 0.2 inside a requestAnimationFrame loop. The slight lag is what makes it read as an eye turning, not a cursor attached to a dot. The code changes only transform, so the animation is cheap.

Behaviours layered on top

A blink timer adds a blink class for 140ms at random intervals. A scroll and wheel listener sets a short "glance down" window. If the pointer has not moved for three seconds, a wander timer picks random points. Hovering or focusing the button toggles an excited class: larger pupils, raised brows and an open mouth.

Touch, keyboard and reduced motion

Pointer events cover mouse, pen and touch. Keyboard focus on the button also triggers the excited state. With prefers-reduced-motion, blinking and wandering are switched off and smoothing is removed, but the pupils still follow the pointer. Pair it with a custom cursor or a magnetic button for a more playful page.

Step by step

How to Use

  1. 1
    Paste the HTML, CSS and JSNo library needed. A mascot face with two eyes and a button renders.
  2. 2
    Move your mouse or fingerBoth pupils turn toward the pointer, each from its own centre.
  3. 3
    Hover or focus the buttonThe face scales up, brows rise, the mouth opens and pupils grow.
  4. 4
    Scroll or use the wheelThe eyes glance down for about 0.7 seconds.
  5. 5
    Stay still for three secondsThe eyes start to wander around the page.
  6. 6
    Restyle the characterChange the face colours, eye sizes and the 0.3 follow factor to suit your brand.

Real-world uses

Common Use Cases

404 and empty-state pages
Turn a dead end into a moment of charm with a mascot that watches the visitor look for the way back.
Hero and footer mascots
Put a character next to the main call-to-action so its gaze leads attention to the button, like a magnetic button with a face.
Kids, education and games sites
Make a learning app or game landing page feel responsive and friendly before anything has been clicked.
Newsletter and support widgets
Use a small tracking face on a sign-up or help form so it looks attentive when the visitor reaches the field.

Got questions?

Frequently Asked Questions

A pointermove listener on the window stores the latest clientX and clientY. Each animation frame, every eye measures its own centre with getBoundingClientRect, works out the direction to that point and moves its pupil a limited distance along it.

Yes. Pointer events fire for touch as well as mouse, so the eyes follow a dragging finger. Phones have no hover, so the excited state appears when the button is tapped or receives focus, and idle wandering keeps the face alive.

No. The loop reads two rectangles and writes one transform per eye per frame. Only transform is animated, so the browser does no layout for the movement. The blink and wander use timers, not extra frame loops.

Keep the .ey-eye and .ey-pupil structure and replace the face shape with your own artwork or SVG. Position the eyes where your character's eyes are, and adjust the max travel if your pupils are larger or smaller.