176 web animation techniques, each a live demo with plain settings and a ready-made prompt for your AI assistant. No dependencies, no build step.
HTML ai-promptsanimationcss-animationsfrontendgithub-pagesjavascriptmicrointeractionsmotion-designno-dependenciesreferencescroll-animationui-animationweb-animationweb-developmentwebgl
fix: make Cursor Follower's lag the same on every screen refresh rate
matin514e8e2bb7ef
diff --git a/animations/04-micro-interactions/cursor-follower/README.md b/animations/04-micro-interactions/cursor-follower/README.mdindex 8ea38677..3108e2ac 100644--- a/animations/04-micro-interactions/cursor-follower/README.md+++ b/animations/04-micro-interactions/cursor-follower/README.md@@ -10,25 +10,30 @@ A cursor follower is a small shape that trails the mouse pointer with a slight d- Any site where the default OS cursor is too subtle for the intended aesthetic## How it works-Track `pointermove` to record target coordinates (`mx`, `my`). Each animation frame, interpolate the follower's position toward the target using a lerp (linear interpolation):+Track `pointermove` to record target coordinates (`mx`, `my`). Each animation frame, interpolate the follower's position toward the target using a lerp (linear interpolation). The share of the gap to close is scaled by the time since the last frame, so the follower keeps the same pace on a 30, 60 or 120 Hz screen:```js-let mx = 0, my = 0; // pointer position-let fx = 0, fy = 0; // follower position-const ease = 0.15; // fraction to close each frame+let mx = 0, my = 0; // pointer position+let fx = 0, fy = 0; // follower position+let last = null; // time of the previous frame+const FRAME = 1000 / 60; // one frame of a 60 Hz screen, in ms+const ease = 0.15; // fraction of the gap to close in one 60 Hz framedocument.addEventListener('pointermove', e => { mx = e.clientX; my = e.clientY; });-function loop() {- fx += (mx - fx) * ease;- fy += (my - fy) * ease;+function loop(ts) {+ const dt = last === null ? FRAME : Math.min(ts - last, 50); // ms since the last frame, at most 50+ last = ts;+ const a = 1 - Math.pow(1 - ease, dt / FRAME); // equals ease at 60 Hz+ fx += (mx - fx) * a;+ fy += (my - fy) * a;follower.style.transform = `translate(${fx}px, ${fy}px)`; // transform only: nothing is laid out againrequestAnimationFrame(loop);}-loop();+requestAnimationFrame(loop);```-Each frame closes 15% of the remaining gap between follower and cursor. At 60fps, the follower reaches ~95% of the way in about 15 frames (~250ms), producing a smooth lag without ever quite reaching the cursor.+Each 60 Hz frame closes 15% of the remaining gap between follower and cursor. A frame twice as long closes 28% of it and one half as long about 8%, so the follower reaches ~95% of the way in about 18 frames' worth of time (~300ms) on any screen, producing a smooth lag without ever quite reaching the cursor. The first frame counts as one 60 Hz frame, and a gap longer than 50ms (a tab coming back from the background) counts as 50ms.The `mix-blend-mode: difference` CSS property makes the follower invert whatever background color is beneath it:@@ -62,7 +67,7 @@ stage.addEventListener('pointermove', e => {## Key parameters| Parameter | Default | Effect ||-----------|---------|--------|-| Lag | Medium | How closely the dot follows: each frame it closes 35% (short), 15% (medium) or 6% (long) of the gap to the pointer; a longer lag feels heavier |+| Lag | Medium | How closely the dot follows: on a 60 Hz screen each frame closes 35% (short), 15% (medium) or 6% (long) of the gap to the pointer, and other screens close the share that fits their frame time, so the dot gets 95% of the way in about 0.1, 0.3 or 0.8 seconds everywhere; a longer lag feels heavier || Size | Medium | The dot is 20px, 32px or 48px across || Flips the colors under it | on | The dot inverts the colors beneath it, so it stays visible on light and dark areas; off, it is a see-through orange dot || Shape | Circle | A filled circle, a ring or a small square |@@ -71,9 +76,9 @@ stage.addEventListener('pointermove', e => {## Production notes- **Touch devices**: cursor effects don't apply to touch. Detect `(pointer: coarse)` and hide the follower entirely — never simulate a cursor on touch. The demo sends the dot to each tap only so the effect can be seen on a phone.- **Performance**: `position: fixed` + `will-change: transform` promotes the follower to its own GPU layer. The demo moves it with `transform: translate(x, y)` and grows it with `scale()`, so nothing is laid out again while it moves.-- **Lag calibration**: at 60fps with `ease = 0.15`, the follower lags ~250ms. At 120fps (high refresh displays), the same `ease` value produces ~125ms lag — the follower will appear snappier. Account for frame rate differences in high-performance contexts.+- **Lag calibration**: `ease` is the share for a 60 Hz frame and the demo scales it by the frame time, so `ease = 0.15` gives the same ~300ms lag on a 30, 60 or 120 Hz screen. A plain per-frame lerp gives about 150ms on a 120 Hz display and 600ms on a 30 Hz phone, so the follower feels snappier or heavier from one device to the next.- **Cursor hide**: add `cursor: none` to the `body` to hide the OS cursor when the follower is active. Re-enable on touch or when the cursor leaves the window.-- **GSAP QuickTo**: `const xTo = gsap.quickTo(follower, 'x', { duration: 0.3 })` is GSAP's optimized cursor follower pattern — more precise than rAF lerp at variable frame rates.+- **GSAP QuickTo**: `const xTo = gsap.quickTo(follower, 'x', { duration: 0.3 })` is GSAP's optimized cursor follower pattern; it works from a duration in seconds, so it is time-based without the `dt` arithmetic.- **`@motionone/animate`**: lightweight alternative to GSAP for cursor followers; no tree-shaking overhead.## See alsodiff --git a/animations/04-micro-interactions/cursor-follower/index.html b/animations/04-micro-interactions/cursor-follower/index.htmlindex cd8f0145..f9f4edba 100644--- a/animations/04-micro-interactions/cursor-follower/index.html+++ b/animations/04-micro-interactions/cursor-follower/index.html@@ -136,7 +136,7 @@<div><h2 id="hb-prompt-title">Copy the prompt</h2><p>Paste it into an AI assistant such as ChatGPT, Claude or Cursor to build this in your own project.</p></div></div><div class="hb-card">- <p class="hb-prompt">Add a cursor follower to [the page or section where it should appear]. Draw a small shape that follows the pointer with a slight delay: on every frame, move it a set share of the way toward the pointer, so it glides after it and eases in. Hide it when the pointer leaves. When the settings include them, let it flip the colors beneath it so it stays visible on light and dark areas alike, and let it grow over buttons and links. Move it with transforms only. Touch screens have no pointer, so hide it there or let it glide to where the visitor taps. If the visitor has reduced motion turned on, keep the normal cursor instead. Match the settings listed below.</p>+ <p class="hb-prompt">Add a cursor follower to [the page or section where it should appear]. Draw a small shape that follows the pointer with a slight delay: each frame, move it a share of the way that grows with the time since the last frame, so it follows at the same pace on every screen. Hide it when the pointer leaves. When the settings include them, let it flip the colors beneath it so it stays visible on light and dark areas alike, and let it grow over buttons and links. Move it with transforms only. Touch screens have no pointer, so hide it there or let it glide to where the visitor taps. If the visitor has reduced motion turned on, keep the normal cursor instead. Match the settings listed below.</p><div class="hb-chips-box" hidden><p class="hb-chips-title">Your settings</p><ul class="hb-chips"></ul>@@ -174,8 +174,9 @@const buttons=[...stage.querySelectorAll('[data-hover]')];const pieces=[follower.querySelector('.shape'),follower.querySelector('circle')];const vars=document.documentElement.style;- // mx,my: where the dot is heading; fx,fy: where it is; px,py: the last spot a pointer or a tap put on the stage- let mx=0,my=0,fx=0,fy=0,px=null,py=null,ease=0.15,inside=false,expandOnHover=true,runTimers=[];+ const FRAME=1000/60; // one frame of a 60 Hz screen, in ms: ease is the share of the gap the dot closes in one of them+ // mx,my: where the dot is heading; fx,fy: where it is; px,py: the last spot a pointer or a tap put on the stage; last: the previous frame's time+ let mx=0,my=0,fx=0,fy=0,px=null,py=null,ease=0.15,inside=false,expandOnHover=true,runTimers=[],last=null;// Every spot is measured on the stage, from the top left of its inner box, which is where the dot's left:0, top:0 liesfunction spot(x,y){const r=stage.getBoundingClientRect();return [x-r.left-stage.clientLeft,y-r.top-stage.clientTop]}@@ -191,11 +192,14 @@grow(onButton);}- function loop(){- if(inside){fx+=(mx-fx)*ease;fy+=(my-fy)*ease;draw()}+ // A frame longer or shorter than a 60 Hz one closes the share of the gap that fits its time, so the lag is the same on every screen.+ // The first frame counts as one 60 Hz frame, and no gap counts as more than 50 ms.+ function loop(ts){+ const dt=last===null?FRAME:Math.min(ts-last,50);last=ts;+ if(inside){const a=1-Math.pow(1-ease,dt/FRAME);fx+=(mx-fx)*a;fy+=(my-fy)*a;draw()}requestAnimationFrame(loop);}- loop();+ requestAnimationFrame(loop);// Show me: one run at a time. Every timer of the run goes through later(); stopRun() cancels the whole run and shrinks the dot.// later(fn, move, hold) waits move + hold ms; this page has no Slow motion, so every step is a hold: later(step, 0, ms).