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
feat: move Cursor Follower to the guided-steps page
matinac41c388c98d
diff --git a/animations/04-micro-interactions/cursor-follower/README.md b/animations/04-micro-interactions/cursor-follower/README.mdindex c5074df0..8ea38677 100644--- a/animations/04-micro-interactions/cursor-follower/README.md+++ b/animations/04-micro-interactions/cursor-follower/README.md@@ -1,7 +1,7 @@# Cursor Follower## What it is-A cursor follower is a custom DOM element that tracks the mouse position with a lag, creating a smooth trailing effect. Combined with `mix-blend-mode: difference`, it inverts the color of whatever is beneath it — making the follower automatically visible against any background: white on dark areas, black on light areas, without any color calculation.+A cursor follower is a small shape that trails the mouse pointer with a slight delay, so it glides after it and eases in as it catches up instead of sticking to it. When it flips the colors of whatever is under it, it stays visible on any background, light over dark areas and dark over light ones, without choosing a color for each.## When to use it- Portfolio and agency sites where the cursor itself becomes part of the brand experience@@ -10,20 +10,19 @@ A cursor follower is a custom DOM element that tracks the mouse position with a- Any site where the default OS cursor is too subtle for the intended aesthetic## How it works-Track `mousemove` 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):```js-let mx = 0, my = 0; // mouse position+let mx = 0, my = 0; // pointer positionlet fx = 0, fy = 0; // follower positionconst ease = 0.15; // fraction to close each frame-document.addEventListener('mousemove', e => { mx = e.clientX; my = e.clientY; });+document.addEventListener('pointermove', e => { mx = e.clientX; my = e.clientY; });function loop() {fx += (mx - fx) * ease;fy += (my - fy) * ease;- follower.style.left = fx + 'px';- follower.style.top = fy + 'px';+ follower.style.transform = `translate(${fx}px, ${fy}px)`; // transform only: nothing is laid out againrequestAnimationFrame(loop);}loop();@@ -36,43 +35,48 @@ The `mix-blend-mode: difference` CSS property makes the follower invert whatever```css#follower {position: fixed;- width: 32px; height: 32px;- border-radius: 50%;- background: #fff;+ left: 0; top: 0; /* moved only by transform */mix-blend-mode: difference;pointer-events: none;- transform: translate(-50%, -50%);will-change: transform;z-index: 9999;}+#follower .shape {+ width: 32px; height: 32px;+ border-radius: 50%;+ background: #fff;+ transform: translate(-50%, -50%);+ transition: transform .2s ease;+}+#follower.expanded .shape { transform: translate(-50%, -50%) scale(2.5); }```For hover reactivity, scale the follower up when over interactive targets:```js-document.querySelectorAll('a, button, [data-hover]').forEach(el => {- el.addEventListener('mouseenter', () => follower.classList.add('expanded'));- el.addEventListener('mouseleave', () => follower.classList.remove('expanded'));+stage.addEventListener('pointermove', e => {+ follower.classList.toggle('expanded', !!e.target.closest('a, button, [data-hover]'));});```## Key parameters| Parameter | Default | Effect ||-----------|---------|--------|-| Easing factor | 0.15 | 0.05 = heavy lag (slow follower); 0.5 = almost no lag; 0.1–0.2 is the readable range |-| Size | 32px | 16–24px for subtle; 40–60px for dramatic; scales with hover expansion |-| Mix-blend-mode | difference | `normal` falls back to a colored dot; `difference` works universally |-| Hover expansion | 2.5× | Enlarges on hover targets for "magnetic" cursor feel |+| 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 |+| 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 |+| Grows over buttons | on | The dot grows to two and a half times its size while the pointer is on a button |## 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.-- **Performance**: `position: fixed` + `will-change: transform` promotes the follower to its own GPU layer. Updating `left`/`top` (rather than `transform`) on every frame is less optimal — in production, update `transform: translate(x, y)` instead.+- **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.- **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.- **`@motionone/animate`**: lightweight alternative to GSAP for cursor followers; no tree-shaking overhead.## See also-- [Hover State Animation](../hover-state/) — cursor-triggered changes on interactive elements-- [Tooltip Reveal](../tooltip-reveal/) — cursor-proximity information reveal-- [Click / Tap Ripple](../click-ripple/) — click-point feedback complementing the cursor follower+- [Hover State Animation](../hover-state/) — items that react when the pointer is on them+- [Tooltip Reveal](../tooltip-reveal/) — a label that appears where the pointer rests+- [Click / Tap Ripple](../click-ripple/) — a ripple from the exact point you clickdiff --git a/animations/04-micro-interactions/cursor-follower/index.html b/animations/04-micro-interactions/cursor-follower/index.htmlindex 9db8c736..cd8f0145 100644--- a/animations/04-micro-interactions/cursor-follower/index.html+++ b/animations/04-micro-interactions/cursor-follower/index.html@@ -4,7 +4,7 @@<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Cursor Follower — Animation Handbook</title>- <meta name="description" content="Custom element follows mouse with lerp lag — mix-blend-mode: difference inverts any background.">+ <meta name="description" content="A dot trails your pointer and flips the colors under it. Best for portfolios."><link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/cursor-follower/"><link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg">@@ -12,27 +12,18 @@<meta property="og:type" content="article"><meta property="og:site_name" content="Animation Handbook"><meta property="og:title" content="Cursor Follower — Animation Handbook">- <meta property="og:description" content="Custom element follows mouse with lerp lag — mix-blend-mode: difference inverts any background.">+ <meta property="og:description" content="A dot trails your pointer and flips the colors under it. Best for portfolios."><meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/cursor-follower/"><meta property="og:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png"><meta name="twitter:card" content="summary_large_image"><meta name="twitter:title" content="Cursor Follower — Animation Handbook">- <meta name="twitter:description" content="Custom element follows mouse with lerp lag — mix-blend-mode: difference inverts any background.">+ <meta name="twitter:description" content="A dot trails your pointer and flips the colors under it. Best for portfolios."><meta name="twitter:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">- <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Cursor Follower", "name": "Cursor Follower", "description": "Custom element follows mouse with lerp lag — mix-blend-mode: difference inverts any background.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/cursor-follower/", "inLanguage": "en", "articleSection": "Micro-Interactions", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Micro-Interactions, Cursor Follower"}</script>+ <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Cursor Follower", "name": "Cursor Follower", "description": "A dot trails your pointer and flips the colors under it. Best for portfolios.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/cursor-follower/", "inLanguage": "en", "articleSection": "Micro-Interactions", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Micro-Interactions, Cursor Follower"}</script><style>- @font-face{font-family:'Bricolage';src:url('../../../assets/fonts/brico-700.woff2') format('woff2');font-weight:700;font-display:swap}- @font-face{font-family:'Bricolage';src:url('../../../assets/fonts/brico-800.woff2') format('woff2');font-weight:800;font-display:swap}- @font-face{font-family:'PlexMono';src:url('../../../assets/fonts/plex-400.woff2') format('woff2');font-weight:400;font-display:swap}- @font-face{font-family:'PlexMono';src:url('../../../assets/fonts/plex-500.woff2') format('woff2');font-weight:500;font-display:swap}- :root{--bg:#0b0b0d;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--ui-accent:#ff9d5c;--ui-text:#f4f4f2;--ui-muted:#77777e;--disp:'Bricolage','Arial Narrow',sans-serif;--mono:'PlexMono',ui-monospace,Menlo,Consolas,monospace;--stage-h:620px;--follower-size:32px;--follower-ease:0.15}+ :root{--ui-accent:#ff9d5c;--ui-border:rgba(255,255,255,.09);--follower-size:32px}*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}- body{background:var(--bg);color:var(--ui-text);font-family:var(--mono);-webkit-font-smoothing:antialiased;padding:clamp(12px,3vw,24px)}- header{margin-bottom:20px}- header h1{font-family:var(--disp);font-weight:700;font-size:clamp(11px,1.8vw,15px);letter-spacing:-.01em}- header p{font-size:clamp(10px,1.2vw,12px);color:var(--ui-muted);margin-top:4px}- .layout{display:flex;gap:clamp(12px,2vw,24px);align-items:flex-start}- .stage{flex:1;min-width:0;height:var(--stage-h);border:1px solid var(--ui-border);border-radius:8px;overflow:hidden;position:relative;cursor:none;display:grid;grid-template-columns:1fr 1fr;grid-template-rows:1fr 1fr}+ .stage{position:relative;overflow:hidden;cursor:none;display:grid;grid-template-columns:repeat(2,minmax(0,1fr));grid-template-rows:repeat(2,minmax(0,1fr));-webkit-user-select:none;user-select:none}/* Content zones for contrast effect */.zone{display:flex;align-items:center;justify-content:center;font-size:clamp(12px,1.5vw,16px);font-weight:bold;padding:20px;text-align:center}.zone-dark{background:#060a10;color:#c9d1d9}@@ -40,138 +31,242 @@.zone-light{background:#1a2535;color:#c9d1d9}.zone-accent{background:#0d1a2e;color:#58a6ff}/* Hover targets */- .hover-target{padding:10px 18px;border-radius:6px;border:1px solid var(--ui-border);cursor:none;font-size:11px;font-family:monospace;background:rgba(255,255,255,.05);transition:border-color .2s}- /* Cursor follower */- #follower{position:absolute;pointer-events:none;z-index:100;opacity:0;transform:translate(-50%,-50%);will-change:transform;transition:width .2s ease,height .2s ease,border-radius .2s ease}- #follower.circle{width:var(--follower-size);height:var(--follower-size);border-radius:50%;background:#fff;mix-blend-mode:difference}- #follower.ring{width:var(--follower-size);height:var(--follower-size);border-radius:50%;border:2px solid #fff;background:transparent;mix-blend-mode:difference}- #follower.square{width:var(--follower-size);height:var(--follower-size);border-radius:4px;background:#fff;mix-blend-mode:difference}- #follower.expanded{width:calc(var(--follower-size)*2.5)!important;height:calc(var(--follower-size)*2.5)!important}- #follower.no-blend{mix-blend-mode:normal;background:rgba(255,157,92,.5);border-color:var(--ui-accent)}- /* Panel */- aside{width:clamp(200px,22vw,240px);flex-shrink:0;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:8px;padding:16px;display:flex;flex-direction:column;gap:10px}- .note{font-size:10px;color:var(--ui-muted);line-height:1.6;border-left:2px solid var(--ui-accent);padding-left:8px}- .lbl{font-size:10px;text-transform:uppercase;letter-spacing:1px;color:var(--ui-accent)}- .div{height:1px;background:var(--ui-border)}- .sr{display:flex;align-items:center;gap:6px;font-size:10px}- .sr input[type=range]{flex:1;accent-color:var(--ui-accent);min-height:44px}- .sv{width:44px;text-align:right;color:var(--ui-text)}- select{background:var(--ui-bg);border:1px solid var(--ui-border);color:var(--ui-text);font-family:monospace;font-size:10px;padding:8px 6px;border-radius:4px;width:100%;min-height:44px}- .tog{display:flex;align-items:center;gap:8px;font-size:10px;cursor:pointer;min-height:44px}- .tog input{accent-color:var(--ui-accent);width:16px;height:16px;flex-shrink:0}- .kv{display:flex;justify-content:space-between;font-size:10px;color:var(--ui-muted)}- .kv .v{color:var(--ui-text)}- @media(max-width:600px){.layout{flex-direction:column}.stage{flex:none;width:100%}aside{width:100%}:root{--stage-h:400px}}- @media(prefers-reduced-motion:reduce){#follower{transition:none!important}}+ .hover-target{display:flex;align-items:center;min-height:44px;padding:0 18px;border-radius:6px;border:1px solid var(--ui-border);cursor:none;font-size:11px;background:rgba(255,255,255,.05)}+ /* Cursor follower: a wrapper that only moves (by transform), holding a .shape that carries the size, fill and corners and is the part that grows */+ #follower{position:absolute;left:0;top:0;pointer-events:none;z-index:100;opacity:0;will-change:transform;mix-blend-mode:difference}+ #follower.no-blend{mix-blend-mode:normal}+ #follower .shape{position:absolute;left:0;top:0;width:var(--follower-size);height:var(--follower-size);border-radius:50%;background:#fff;transform:translate(-50%,-50%);transition:transform .2s ease,border-radius .2s ease}+ #follower.square .shape{border-radius:4px}+ #follower.no-blend .shape{background:rgba(255,157,92,.5)}+ #follower.expanded .shape{transform:translate(-50%,-50%) scale(2.5)}+ #follower.square.expanded .shape{border-radius:1.6px}+ /* The ring is an SVG stroke, not a border: a border is snapped to whole device pixels and would not stay 2px when scaled up */+ #follower.ring .shape,#follower.ring.no-blend .shape{background:none}+ #follower .shape svg{display:none;position:absolute;inset:0;width:100%;height:100%;overflow:visible}+ #follower.ring .shape svg{display:block}+ #follower .shape circle{cx:50%;cy:50%;r:calc(50% - 1px);fill:none;stroke:#fff;stroke-width:2;transition:stroke-width .2s ease,r .2s ease}+ #follower.expanded .shape circle{stroke-width:.8;r:calc(50% - .4px)}+ #follower.no-blend.ring circle{fill:rgba(255,157,92,.5);stroke:var(--ui-accent)}+ @media(prefers-reduced-motion:reduce){#follower .shape,#follower circle{transition:none!important}}</style>-<style id="ah-inject">-:root{--ah-bg:rgba(17,17,20,.82);--ah-line:rgba(255,255,255,.1);--ah-ink:#f4f4f2;--ah-muted:#9a9aa2;--ah-accent:#6ea8ff}-body{padding-top:46px}-.ah-bar{position:fixed;top:0;left:0;right:0;height:46px;z-index:2147483000;display:flex;align-items:center;justify-content:space-between;gap:10px;padding:0 clamp(10px,3vw,18px);background:var(--ah-bg);backdrop-filter:blur(10px);-webkit-backdrop-filter:blur(10px);border-bottom:1px solid var(--ah-line);font-family:ui-monospace,Menlo,Consolas,monospace;font-size:12px;box-sizing:border-box}-.ah-bar a,.ah-bar button{color:var(--ah-muted);text-decoration:none;background:none;border:0;font:inherit;cursor:pointer;display:inline-flex;align-items:center;gap:6px;min-height:34px;padding:0 7px;border-radius:6px;transition:color .18s,background .18s;white-space:nowrap}-.ah-bar a:hover,.ah-bar button:hover{color:var(--ah-ink)}-.ah-home{color:var(--ah-ink);font-weight:600}-.ah-actions{display:flex;align-items:center;gap:2px}-.ah-sep{width:1px;height:16px;background:var(--ah-line);margin:0 4px}-.ah-copied{color:var(--ah-accent)!important}-@media(max-width:600px){.ah-bar .ah-hideSm{display:none}}-@media print{.ah-bar{display:none}body{padding-top:0}}-</style>+ <link rel="stylesheet" href="../../../assets/css/demo-page.css?v=4">+ <script src="../../../assets/js/demo-page.js?v=4" defer></script></head>-<body>-<div class="ah-bar" role="navigation" aria-label="Animation Handbook"><a class="ah-home" href="../../../">← Animation Handbook</a><span class="ah-actions"><a class="ah-hideSm" href="../accordion/" rel="prev" title="Previous">‹ Prev</a><a class="ah-hideSm" href="../error-shake/" rel="next" title="Next">Next ›</a><span class="ah-sep ah-hideSm"></span><button type="button" id="ah-copy" class="ah-hideSm">Copy source</button><a class="ah-hideSm" href="https://github.com/matinmonshizadeh/animation-handbook/blob/main/animations/04-micro-interactions/cursor-follower/index.html" target="_blank" rel="noopener noreferrer">Source ↗</a></span></div>-<header>- <h1>Cursor Follower</h1>- <p>Custom element follows mouse with lerp lag — mix-blend-mode: difference inverts any background.</p>-</header>-<div class="layout">- <div class="stage" id="stage">- <div class="zone zone-dark">Move cursor<br>over stage</div>- <div class="zone zone-accent"><div class="hover-target" data-hover>Hover target</div></div>- <div class="zone zone-light"><div class="hover-target" data-hover>Hover target</div></div>- <div class="zone zone-mid">Mix-blend-mode<br>inverts here</div>- <div id="follower" class="circle"></div>- </div>- <aside>- <p class="note">The easing factor controls lag: follower.x += (mouse.x - follower.x) * ease. At 0.15, about 15% of the remaining gap closes each frame — giving smooth, weighted following.</p>- <div class="lbl">Size</div>- <div class="sr"><input type="range" id="sz-sl" min="10" max="60" value="32" step="2"><span class="sv" id="sz-v">32px</span></div>- <div class="lbl">Easing (lag)</div>- <div class="sr"><input type="range" id="ease-sl" min="5" max="50" value="15" step="1"><span class="sv" id="ease-v">0.15</span></div>- <div class="lbl">Shape</div>- <select id="shape-sel">- <option value="circle" selected>Circle</option>- <option value="ring">Ring</option>- <option value="square">Square</option>- </select>- <label class="tog"><input type="checkbox" id="blend-tog" checked><span>mix-blend-mode: difference</span></label>- <label class="tog"><input type="checkbox" id="expand-tog" checked><span>Expand on hover targets</span></label>- <div class="div"></div>- <div class="kv"><span>Position</span><span class="v" id="pos-label">—</span></div>- <p class="note" style="opacity:.7">Cursor effects don't apply on touch — this demo gracefully shows nothing on touch devices.</p>- </aside>-</div>+<body class="hb" data-hb-kind="do" data-hb-autoplay>+<nav class="hb-bar" aria-label="Animation Handbook">+ <a class="hb-home" href="../../../"><svg class="hb-mark" viewBox="0 0 32 32" aria-hidden="true"><g class="g"><circle cx="11.16" cy="20.84" r="6.1"/><circle cx="15.12" cy="16.88" r="6.1"/><circle cx="18.51" cy="13.49" r="6.1"/></g><circle class="b" cx="21.2" cy="10.8" r="6.6"/></svg><span>Animation Handbook</span></a>+ <span class="hb-pager"><a href="../accordion/" rel="prev" aria-label="Previous: Accordion Open/Close"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m15 18-6-6 6-6"/></svg><span><span class="hb-dir">Previous: </span>Accordion Open/Close</span></a><a href="../error-shake/" rel="next" aria-label="Next: Error Shake"><span><span class="hb-dir">Next: </span>Error Shake</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m9 18 6-6-6-6"/></svg></a></span>+</nav>+<main class="hb-page">+ <header class="hb-head">+ <p class="hb-cat">04.20 · Micro-Interactions</p>+ <h1>Cursor Follower</h1>+ <p class="hb-lede">A dot trails your pointer and flips the colors under it. Best for portfolios.</p>+ </header>++ <section class="hb-step hb-watch" aria-labelledby="hb-watch-title">+ <div class="hb-step-head">+ <span class="hb-num" aria-hidden="true">1</span>+ <div><h2 id="hb-watch-title">Hover it</h2><p>Move your pointer over the box or tap in it, or press Show me.</p></div>+ </div>+ <div class="stage" id="stage">+ <div class="zone zone-dark">Move your pointer here</div>+ <div class="zone zone-accent"><div class="hover-target" data-hover>Button</div></div>+ <div class="zone zone-light"><div class="hover-target" data-hover>Button</div></div>+ <div class="zone zone-mid">Pass over these words</div>+ <div id="follower" class="circle" aria-hidden="true"><div class="shape"><svg aria-hidden="true"><circle cx="50%" cy="50%" r="50%"/></svg></div></div>+ </div>+ <div class="hb-player">+ <button class="hb-play" type="button" id="btn-demo" data-hb-demo><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z"/></svg>Show me</button>+ </div>+ </section>++ <section class="hb-step hb-try" aria-labelledby="hb-try-title">+ <div class="hb-step-head">+ <span class="hb-num" aria-hidden="true">2</span>+ <div><h2 id="hb-try-title">Try it</h2><p>Change a setting, then try it again or press Show me.</p></div>+ </div>+ <div class="hb-settings">+ <div class="hb-setting">+ <p class="hb-setting-name" id="lag-lbl">Lag</p>+ <div class="seg" id="lag-seg" role="group" aria-labelledby="lag-lbl">+ <button type="button" data-ease="0.35" aria-pressed="false">Short</button>+ <button type="button" data-ease="0.15" class="on" aria-pressed="true">Medium</button>+ <button type="button" data-ease="0.06" aria-pressed="false">Long</button>+ </div>+ <p class="hb-hint">A longer lag feels heavier and smoother.</p>+ </div>+ <div class="hb-setting">+ <p class="hb-setting-name" id="size-lbl">Size</p>+ <div class="seg" id="size-seg" role="group" aria-labelledby="size-lbl">+ <button type="button" data-px="20" aria-pressed="false">Small</button>+ <button type="button" data-px="32" class="on" aria-pressed="true">Medium</button>+ <button type="button" data-px="48" aria-pressed="false">Large</button>+ </div>+ <p class="hb-hint">How big the dot is before it grows.</p>+ </div>+ <div class="hb-setting">+ <label class="hb-switch-row"><span>Flips the colors under it</span><input class="hb-switch" type="checkbox" role="switch" id="blend-tog" checked autocomplete="off"></label>+ <p class="hb-hint">Keeps the dot visible on light and dark areas.</p>+ </div>+ </div>+ <details class="hb-options">+ <summary><span class="hb-more-label">More options</span><span class="hb-less">Fewer options</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg></summary>+ <div class="hb-settings">+ <div class="hb-setting">+ <p class="hb-setting-name" id="shape-lbl">Shape</p>+ <div class="seg" id="shape-seg" role="group" aria-labelledby="shape-lbl">+ <button type="button" data-shape="circle" class="on" aria-pressed="true">Circle</button>+ <button type="button" data-shape="ring" aria-pressed="false">Ring</button>+ <button type="button" data-shape="square" aria-pressed="false">Square</button>+ </div>+ <p class="hb-hint">The outline of the follower.</p>+ </div>+ <div class="hb-setting">+ <label class="hb-switch-row"><span>Grows over buttons</span><input class="hb-switch" type="checkbox" role="switch" id="expand-tog" checked autocomplete="off"></label>+ <p class="hb-hint">The dot swells while the pointer is on a button.</p>+ </div>+ </div>+ </details>+ </section>++ <section class="hb-step hb-prompt-step" aria-labelledby="hb-prompt-title">+ <div class="hb-step-head">+ <span class="hb-num" aria-hidden="true">3</span>+ <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>+ <div class="hb-chips-box" hidden>+ <p class="hb-chips-title">Your settings</p>+ <ul class="hb-chips"></ul>+ </div>+ <div class="hb-copy-row">+ <button class="hb-copy" type="button"><svg class="hb-ic hb-i-copy" viewBox="0 0 24 24" aria-hidden="true"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg><svg class="hb-ic hb-i-done" viewBox="0 0 24 24" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg><span class="hb-copy-label" aria-live="polite">Copy prompt</span></button>+ <p class="hb-hint">Replace the words in brackets with your own element. Your settings are added at the end.</p>+ </div>+ </div>+ </section>++ <section class="hb-about" aria-labelledby="hb-what-title">+ <div class="hb-what">+ <h2 id="hb-what-title">What it is</h2>+ <div class="hb-what-text"><p>The full explanation is in <a href="README.md">README.md</a>.</p></div>+ </div>+ <div class="hb-fit">+ <h2>Good for</h2>+ <ul class="hb-tags hb-good"><li>Portfolios</li><li>Agency sites</li><li>Interactive stories</li></ul>+ <h3>Avoid on</h3>+ <ul class="hb-tags hb-avoid"><li>Forms and tools</li><li>Reading pages</li></ul>+ </div>+ </section>++ <section class="hb-related" aria-labelledby="hb-related-title" hidden>+ <h2 id="hb-related-title">Similar animations</h2>+ <div class="hb-rel-list"></div>+ </section>+</main>+<footer class="hb-foot"><span>Animation Handbook</span><a href="#top">Back to top ↑</a></footer><script>const stage=document.getElementById('stage');const follower=document.getElementById('follower');- const posLabel=document.getElementById('pos-label');- let mx=0,my=0,fx=0,fy=0,ease=0.15,rafId=null,inside=false,shape='circle',blend=true,expandOnHover=true;-- // Only show on non-touch- const hasTouch=window.matchMedia('(pointer:coarse)').matches;- if(hasTouch){follower.style.display='none';}+ const zones=[...stage.querySelectorAll('.zone')];+ 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=[];- stage.addEventListener('mouseenter',e=>{- // Snap to the entry point so the follower doesn't swoop in from its last position- const r=stage.getBoundingClientRect();- mx=fx=e.clientX-r.left;my=fy=e.clientY-r.top;- follower.style.left=fx.toFixed(1)+'px';follower.style.top=fy.toFixed(1)+'px';- inside=true;follower.style.opacity='1';- });- stage.addEventListener('mouseleave',()=>{inside=false;follower.style.opacity='0'});- stage.addEventListener('mousemove',e=>{- const r=stage.getBoundingClientRect();- mx=e.clientX-r.left;my=e.clientY-r.top;- posLabel.textContent=`${Math.round(mx)}, ${Math.round(my)}`;- });-- // Hover targets expand follower- stage.querySelectorAll('[data-hover]').forEach(el=>{- el.addEventListener('mouseenter',()=>{if(expandOnHover)follower.classList.add('expanded')});- el.addEventListener('mouseleave',()=>follower.classList.remove('expanded'));- });+ // 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 lies+ function spot(x,y){const r=stage.getBoundingClientRect();return [x-r.left-stage.clientLeft,y-r.top-stage.clientTop]}+ function centre(el){const r=el.getBoundingClientRect();return spot(r.left+r.width/2,r.top+r.height/2)}+ function draw(){follower.style.transform=`translate(${fx.toFixed(1)}px, ${fy.toFixed(1)}px)`}+ function grow(on){follower.classList.toggle('expanded',on&&expandOnHover)}+ function hide(){inside=false;follower.style.opacity='0';follower.classList.remove('expanded')}+ // The pointer, a tap and Show me all send the dot to a spot. It appears there directly, as a pointer entering the stage+ // would, so it never swoops in from where it was last; once it is shown it glides after each new spot.+ function aim(x,y,onButton){+ if(inside){mx=x;my=y}+ else{mx=fx=x;my=fy=y;draw();inside=true;follower.style.opacity='1'}+ grow(onButton);+ }function loop(){- if(inside){- fx+=(mx-fx)*ease;fy+=(my-fy)*ease;- follower.style.left=fx.toFixed(1)+'px';- follower.style.top=fy.toFixed(1)+'px';- }- rafId=requestAnimationFrame(loop);+ if(inside){fx+=(mx-fx)*ease;fy+=(my-fy)*ease;draw()}+ requestAnimationFrame(loop);}loop();- document.getElementById('sz-sl').addEventListener('input',e=>{- document.getElementById('sz-v').textContent=e.target.value+'px';- document.documentElement.style.setProperty('--follower-size',e.target.value+'px');- });- document.getElementById('ease-sl').addEventListener('input',e=>{- ease=e.target.value/100;document.getElementById('ease-v').textContent=ease.toFixed(2);- });- document.getElementById('shape-sel').addEventListener('change',e=>{- shape=e.target.value;- follower.classList.remove('circle','ring','square');- follower.classList.add(shape);- });- document.getElementById('blend-tog').addEventListener('change',e=>{- blend=e.target.checked;- follower.classList.toggle('no-blend',!blend);+ // 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).+ function later(fn,move,hold){+ const id=setTimeout(()=>{ runTimers=runTimers.filter(t=>t!==id); fn(); }, move+(hold||0));+ runTimers.push(id);+ }+ function running(){ return runTimers.length>0; }+ function stopRun(){ runTimers.forEach(clearTimeout); runTimers=[]; grow(false); }++ // A mouse or a pen in the air steers the dot: it shows where the pointer enters the stage, follows it and hides when it leaves.+ // A finger or a pen touching the surface sends the dot to the spot it presses, and the dot stays. Pointer events only, so the+ // mouse events that follow a tap leave no pointer behind. A run under way is stopped first, and the dot follows the visitor after it.+ function aimAt(e){+ if(e.isTrusted&&running())stopRun();+ [px,py]=spot(e.clientX,e.clientY);+ aim(px,py,!!e.target.closest('[data-hover]'));+ }+ stage.addEventListener('pointerenter',e=>{if(e.pointerType!=='touch')aimAt(e)});+ stage.addEventListener('pointermove',e=>{if(e.pointerType!=='touch')aimAt(e)});+ stage.addEventListener('pointerdown',e=>{if(e.pointerType!=='mouse')aimAt(e)});+ // Leaving the stage ends the follow (and a run, or it would show the dot again). A finger's lift also leaves the stage, but+ // its dot stays where the tap put it.+ ['pointerleave','pointercancel'].forEach(type=>stage.addEventListener(type,e=>{+ if(e.pointerType==='touch')return;+ if(e.isTrusted&&running())stopRun();+ hide();+ }));++ // A run starts with the dot hidden and small, put there without animating: the transitions are turned off, the state is set,+ // a reflow makes it stick, and the transitions come back.+ function toRest(){+ pieces.forEach(p=>p.style.setProperty('transition','none','important'));+ hide();+ void follower.offsetWidth;+ pieces.forEach(p=>p.style.removeProperty('transition'));+ }+ // An invisible pointer visits four spots: the middle of the first area, the button in the second, the button in the third+ // (the dot stays grown between the two) and the middle of the fourth, where it shrinks back. The dot glides after it (loop()+ // draws it), and disappears at the end unless a real pointer rests on the stage, which then keeps its dot.+ document.getElementById('btn-demo').addEventListener('click',()=>{+ stopRun();+ toRest();+ aim(...centre(zones[0]),false);+ later(()=>aim(...centre(buttons[0]),true),0,700);+ later(()=>aim(...centre(buttons[1]),true),0,1400);+ later(()=>aim(...centre(zones[3]),false),0,2100);+ later(()=>{+ inside=stage.matches(':hover');+ follower.style.opacity=inside?'1':'0';+ if(inside&&px!==null){mx=px;my=py}+ },0,2800);});+ // The visitor pressed, typed, scrolled or touched inside the stage: the run stops and the dot follows the visitor.+ document.addEventListener('hb:input',()=>{ if(running()) stopRun(); });++ function choices(id,pick){+ const btns=document.querySelectorAll('#'+id+' button');+ btns.forEach(b=>b.addEventListener('click',()=>{+ btns.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});+ pick(b.dataset);+ }));+ }+ choices('lag-seg',d=>{ease=+d.ease});+ choices('size-seg',d=>{vars.setProperty('--follower-size',d.px+'px')});+ choices('shape-seg',d=>{follower.classList.remove('circle','ring','square');follower.classList.add(d.shape)});+ document.getElementById('blend-tog').addEventListener('change',e=>{follower.classList.toggle('no-blend',!e.target.checked)});document.getElementById('expand-tog').addEventListener('change',e=>{expandOnHover=e.target.checked;if(!expandOnHover)follower.classList.remove('expanded');});</script>-<script>(function(){var b=document.getElementById("ah-copy");if(!b)return;b.addEventListener("click",function(){fetch(location.href).then(function(r){return r.text();}).catch(function(){return document.documentElement.outerHTML;}).then(function(t){return navigator.clipboard.writeText(t);}).then(function(){b.textContent="Copied!";b.classList.add("ah-copied");setTimeout(function(){b.textContent="Copy source";b.classList.remove("ah-copied");},1500);}).catch(function(){b.textContent="Copy failed";setTimeout(function(){b.textContent="Copy source";},1500);});});})();</script></body></html>diff --git a/index.html b/index.htmlindex 6bd89774..a6e7d945 100644--- a/index.html+++ b/index.html@@ -970,7 +970,7 @@ const CATS=[['drawer-slide','Drawer / Panel Slide','A side panel slides in over a dimmed page. Best for mobile menus.'],['modal-expand','Modal Expand','A window grows out of the button you pressed. Best for detail views.'],['accordion','Accordion Open/Close','Each question opens smoothly to show its answer. Best for FAQ pages.'],- ['cursor-follower','Cursor Follower','Custom element follows mouse with lerp lag — mix-blend-mode: difference inverts any background.'],+ ['cursor-follower','Cursor Follower','A dot trails your pointer and flips the colors under it. Best for portfolios.'],['error-shake','Error Shake','An invalid field shakes horizontally with a decaying wobble and flashes red — the universal "no".'],['swipe-to-dismiss','Swipe to Dismiss','Drag a card past a threshold and it flies off as the row collapses — pointer-driven, touch-ready.'],['hamburger-menu-toggle','Hamburger Menu Toggle','The bars icon morphs to an X — top and bottom bars rotate to cross while the middle fades out.'],