Skip to content

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.

37.5 KB
Raw Blame
<!DOCTYPE html>
<html lang="en">
<head>
  <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="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">
  <link rel="apple-touch-icon" href="../../../apple-touch-icon.png">
  <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="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="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": "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>
    :root{--ui-accent:#ff9d5c;--ui-border:rgba(255,255,255,.09);--follower-size:32px}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    .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}
    .zone-mid{background:#0d1117;color:#8b949e}
    .zone-light{background:#1a2535;color:#c9d1d9}
    .zone-accent{background:#0d1a2e;color:#58a6ff}
    /* Hover targets */
    .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)}
    /* The keyboard's way in: a layer over the stage that Tab focuses (the pointer passes through it); its ring follows the stage's corners */
    .keys{position:absolute;inset:0;border-radius:11px;pointer-events:none}
    /* 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.
       Compare's copy of the dot (.dot-twin) shares every rule. */
    :is(#follower,.dot-twin){position:absolute;left:0;top:0;pointer-events:none;z-index:100;opacity:0;will-change:transform;mix-blend-mode:difference}
    :is(#follower,.dot-twin).no-blend{mix-blend-mode:normal}
    :is(#follower,.dot-twin) .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}
    :is(#follower,.dot-twin).square .shape{border-radius:4px}
    :is(#follower,.dot-twin).no-blend .shape{background:rgba(255,157,92,.5)}
    :is(#follower,.dot-twin).expanded .shape{transform:translate(-50%,-50%) scale(2.5)}
    :is(#follower,.dot-twin).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 */
    :is(#follower,.dot-twin).ring .shape,:is(#follower,.dot-twin).ring.no-blend .shape{background:none}
    :is(#follower,.dot-twin) .shape svg{display:none;position:absolute;inset:0;width:100%;height:100%;overflow:visible}
    :is(#follower,.dot-twin).ring .shape svg{display:block}
    :is(#follower,.dot-twin) .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}
    :is(#follower,.dot-twin).expanded .shape circle{stroke-width:.8;r:calc(50% - .4px)}
    :is(#follower,.dot-twin).no-blend.ring circle{fill:rgba(255,157,92,.5);stroke:var(--ui-accent)}
    @media(prefers-reduced-motion:reduce){:is(#follower,.dot-twin) .shape,:is(#follower,.dot-twin) circle{transition:none!important}}
    /* See inside and Compare, drawn from the page's own numbers above the dots (so the color flip never turns them), moved by transform only.
       See inside: a cross where the hidden pointer is, a line for the gap still to close, faint marks of where the dot was at equal moments,
       the buttons outlined. Compare: an inert copy of the areas whose dot sits right on the pointer; an arrow in each half it is not over. */
    .twin,.views,.cf-lbl,.cf-half{display:none}
    .views{position:absolute;inset:0;z-index:102;pointer-events:none}
    .cf-mark,.cf-gap,.cf-cross,.cf-lbl,.cf-arrow{position:absolute;left:0;top:0}
    .cf-mark{width:8px;height:8px;margin:-4px 0 0 -4px;border-radius:50%;background:var(--ui-accent);box-shadow:0 0 0 1.5px #0b0b0d}
    .cf-gap{width:100px;height:2px;margin-top:-1px;background:var(--ui-accent);transform-origin:0 50%}
    .cf-cross{width:14px;height:14px;margin:-7px 0 0 -7px;overflow:visible;fill:none;stroke-linecap:round}
    .cf-half{position:absolute;left:0;top:0;width:50%;height:100%;overflow:hidden}.cf-half.two{left:50%}
    .cf-arrow{width:15px;height:22px;overflow:visible}.cf-arrow path{fill:#f4f4f2;stroke:#0b0b0d;stroke-width:1.5;stroke-linejoin:round}
    .cf-lbl{padding:3px 7px;border-radius:5px;background:rgba(11,11,13,.88);color:#f4f4f2;font:600 13px/16px var(--hb-font);white-space:nowrap}
    .cmp-l{left:8px;top:8px}.cmp-l.two{left:calc(50% + 8px)}
    .stage[data-hb-mode="inside"] .views,.stage[data-hb-mode="compare"] .views,.stage[data-hb-mode="inside"] .in-l,
    .stage[data-hb-mode="compare"] .cmp-l,.stage[data-hb-mode="compare"] .cf-half{display:block}
    .stage[data-hb-mode="compare"] .cf-mark,.stage[data-hb-mode="compare"] .cf-gap,.stage[data-hb-mode="compare"] .cf-cross{display:none}
    .stage[data-hb-mode="inside"] .hover-target{outline:2px dashed var(--ui-accent);outline-offset:3px}
    /* Compare: the copy fills the first half (left, or top on phones) above the real dot, which keeps to the second; the cursor shows */
    .stage[data-hb-mode="compare"]{grid-template-columns:repeat(4,minmax(0,1fr))}
    .stage[data-hb-mode="compare"],.stage[data-hb-mode="compare"] .hover-target{cursor:default}
    .stage[data-hb-mode="compare"]>.zone:nth-child(1){grid-area:1/3}.stage[data-hb-mode="compare"]>.zone:nth-child(2){grid-area:1/4}
    .stage[data-hb-mode="compare"]>.zone:nth-child(3){grid-area:2/3}.stage[data-hb-mode="compare"]>.zone:nth-child(4){grid-area:2/4}
    .stage[data-hb-mode="compare"] .keys{left:50%}
    .stage[data-hb-mode="compare"] .twin{display:grid;position:absolute;left:0;top:0;width:50%;height:100%;grid-template-columns:repeat(2,minmax(0,1fr));grid-template-rows:repeat(2,minmax(0,1fr));overflow:hidden;z-index:101}
    @media(min-width:601px){.cf-br{display:none}}
    @media(max-width:600px){
      .cf-lbl{padding:2px 6px;font-size:12px;line-height:15px}.cmp-l{left:3px;top:2px;padding:1px 6px}.cmp-l.two{left:3px;top:calc(50% + 2px)}
      .cf-half{width:100%;height:50%}.cf-half.two{left:0;top:50%}
      .stage[data-hb-mode="compare"]{grid-template-columns:repeat(2,minmax(0,1fr));grid-template-rows:repeat(4,minmax(0,1fr))}
      .stage[data-hb-mode="compare"]>.zone:nth-child(1){grid-area:3/1}.stage[data-hb-mode="compare"]>.zone:nth-child(2){grid-area:3/2}
      .stage[data-hb-mode="compare"]>.zone:nth-child(3){grid-area:4/1}.stage[data-hb-mode="compare"]>.zone:nth-child(4){grid-area:4/2}
      .stage[data-hb-mode="compare"] .keys{left:0;top:50%}.stage[data-hb-mode="compare"] .twin{width:100%;height:50%}
      /* Stacked halves: each area's content sits away from its half's outer edge (the grown dot is 80px across), the same in both halves */
      .stage[data-hb-mode="compare"]>.zone:nth-child(-n+2),.stage[data-hb-mode="compare"] .twin>.zone:nth-child(-n+2){align-items:flex-end;padding-top:0;padding-bottom:8px}
      .stage[data-hb-mode="compare"]>.zone:nth-child(n+3),.stage[data-hb-mode="compare"] .twin>.zone:nth-child(n+3){align-items:flex-start;padding-top:8px;padding-bottom:0}
    }
  </style>
  <link rel="stylesheet" href="../../../assets/css/demo-page.css?v=11">
  <script src="../../../assets/js/demo-page.js?v=11" defer></script>
</head>
<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>
  <a class="hb-star" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener" aria-label="Star on GitHub"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg><span>Star on GitHub</span></a>
</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 Tab to it and use the arrow keys), or press Show me.</p></div>
    </div>
    <div class="seg hb-modes" role="group" aria-label="What the box shows" data-hb-modes>
      <button type="button" data-hb-mode="normal" class="on" aria-pressed="true">Normal</button>
      <button type="button" data-hb-mode="inside" aria-pressed="false">See inside</button>
      <button type="button" data-hb-mode="compare" aria-pressed="false">Compare</button>
    </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 class="keys" id="keys" tabindex="0" role="group" aria-label="Dot follower: use the arrow keys to move it"></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 class="views" aria-hidden="true">
        <i class="cf-gap"></i>
        <svg class="cf-cross" viewBox="-7 -7 14 14"><path d="M-5 0H5M0-5V5" stroke="#0b0b0d" stroke-width="4"/><path d="M-5 0H5M0-5V5" stroke="#f4f4f2" stroke-width="1.6"/></svg>
        <div class="cf-half"><svg class="cf-arrow"><path d="M0 0V19L5 14.5L8.5 22L11.5 20.7L8 13.3H14.5Z"/></svg></div><div class="cf-half two"><svg class="cf-arrow"><path d="M0 0V19L5 14.5L8.5 22L11.5 20.7L8 13.3H14.5Z"/></svg></div>
        <span class="cf-lbl in-l l-gap" data-hb-caption>Catches up in about a third of a second,<br class="cf-br"> in smaller and smaller steps</span>
        <span class="cf-lbl in-l l-ptr" data-hb-caption>Where the pointer is</span>
        <span class="cf-lbl in-l l-flip" data-hb-caption>Flips the colors<br class="cf-br"> under it</span>
        <span class="cf-lbl in-l l-grow" data-hb-caption>Grows two and a half times over a button</span>
        <span class="cf-lbl cmp-l" data-hb-caption>No lag</span><span class="cf-lbl cmp-l two" data-hb-caption>Trails behind</span>
      </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>
    <div class="hb-mode-notes" aria-live="polite">
      <p data-hb-for="inside" hidden>Inside, the cross is where the pointer is: each frame the dot closes part of the gap, in smaller and smaller steps. Move over the box, or press Show me.</p>
      <p data-hb-for="compare" hidden>No lag: the dot sits right on the pointer; Trails behind: it glides after it and eases in as it catches up. Move over either half, or press Show me.</p>
    </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: each frame, close a share of the gap that is exponential in 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>
      </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>
    <div class="hb-star-row"><p>Free and open source. If this helped, a star on GitHub helps other people find it.</p><a class="hb-star-btn" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg>Star on GitHub</a></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="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener">Source on GitHub</a><a href="#top">Back to top ↑</a></footer>
<script>
  const stage=document.getElementById('stage');
  const follower=document.getElementById('follower');
  const zones=[...stage.querySelectorAll('.zone')];
  const buttons=[...stage.querySelectorAll('[data-hover]')];
  const pieces=[follower.querySelector('.shape'),follower.querySelector('circle')];
  const vars=document.documentElement.style;
  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;
  // See inside and Compare: posed (resting on the pose P), See inside's marks (trail; tick: the time since the last), what steers the dot
  // (ptr: 'mouse', over half 1 or 2 in Compare, or 'pretend'), the real dot's box B, the boxes of the words and buttons WB, the labels' sizes SZ
  let mode='normal',posed=false,P=null,trail=[],tick=0,ptr='pretend',over=2,B=null,WB=null,SZ=new Map(),dotPx=32,sig='';
  // Compare's first half: an inert copy of the four areas (no ids, its buttons only words) with a dot of its own
  const twin=Object.assign(document.createElement('div'),{className:'twin',inert:true}),dotTwin=follower.cloneNode(true);twin.setAttribute('aria-hidden','true');
  zones.forEach(z=>{const c=z.cloneNode(true);c.querySelector('[data-hover]')?.removeAttribute('data-hover');twin.append(c)});dotTwin.removeAttribute('id');twin.append(dotTwin);stage.append(twin);
  const twinPieces=[dotTwin.querySelector('.shape'),dotTwin.querySelector('circle')],pane=stage.querySelector('.views'),$=s=>pane.querySelector(s);
  const gap=$('.cf-gap'),cross=$('.cf-cross'),arrows=[...pane.querySelectorAll('.cf-arrow')],L={gap:$('.l-gap'),ptr:$('.l-ptr'),flip:$('.l-flip'),grow:$('.l-grow')};
  const marks=[0,1,2,3,4,5].map(()=>pane.insertBefore(Object.assign(document.createElement('i'),{className:'cf-mark'}),gap));

  // 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 real dot's box: the whole stage, or in Compare the half the real areas fill (the second: right, or lower on phones)
  function box(){if(mode!=='compare')return{x:0,y:0,w:stage.clientWidth,h:stage.clientHeight};
    const a=zones[0].getBoundingClientRect(),d=zones[3].getBoundingClientRect(),[x,y]=spot(a.left,a.top),[r,b]=spot(d.right,d.bottom);return{x,y,w:r-x,h:b-y}}
  function overButton(x,y){return buttons.some(b=>{const r=b.getBoundingClientRect(),[l,t]=spot(r.left,r.top);return x>=l&&x<=l+r.width&&y>=t&&y<=t+r.height})}
  // 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){
    unpose();
    if(inside){mx=x;my=y}
    else{mx=fx=x;my=fy=y;draw();inside=true;follower.style.opacity='1'}
    grow(onButton);
  }

  // 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;
    // See inside marks where the dot sets off after the pointer (views() then adds a mark every twentieth of a second)
    if(inside&&mode==='inside'&&!trail.length&&Math.hypot(mx-fx,my-fy)>=1){trail=[[fx,fy]];tick=0}
    if(inside){const a=1-Math.pow(1-ease,dt/FRAME);fx+=(mx-fx)*a;fy+=(my-fy)*a;draw()}
    if(mode!=='normal')views(dt);
    requestAnimationFrame(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).
  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.
  // In Compare the pointer over either half, as a spot in that half's own frame, steers both: the real dot gets the spot in its half.
  function aimAt(e){
    if(e.isTrusted&&running())stopRun();
    [px,py]=spot(e.clientX,e.clientY);
    let on=!!e.target.closest('[data-hover]');
    ptr=e.pointerType==='touch'?'pretend':'mouse';
    if(mode==='compare'){const b=box();over=px<b.x||py<b.y?1:2;if(over===1){px+=b.x;py+=b.y}on=overButton(px,py)}
    aim(px,py,on);
  }
  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();
    idle();
  }));

  // A run starts with the dot hidden and small, put there without animating: still() turns the transitions off, makes the change,
  // makes it stick with a reflow and brings the transitions back.
  function still(ps,fn){ps.forEach(p=>p.style.setProperty('transition','none','important'));fn();void stage.offsetWidth;ps.forEach(p=>p.style.removeProperty('transition'))}
  function toRest(){unpose();still(pieces,hide);if(mode==='compare')mirror(true)}
  // 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 (See inside and
  // Compare go back to their pose).
  document.getElementById('btn-demo').addEventListener('click',()=>{
    stopRun();
    toRest();
    ptr='pretend';
    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;ptr='mouse'}
      else if(mode!=='normal')pose();
    },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(); });

  // The keyboard: Tab focuses the layer over the stage (over the real half in Compare), which stops a run and puts an invisible pointer
  // in the middle of the dot's box, or where the keys last left it; the arrow keys move it a step (6% of the box's shorter side, three
  // with Shift). It goes through aim() as the pointer does, so the dot glides after it and grows over a button. Escape, or focus
  // leaving, hides the dot as the pointer leaving does, unless a pointer on the stage or a tap has taken it since.
  const keys=document.getElementById('keys'),ARROWS={ArrowLeft:[-1,0],ArrowRight:[1,0],ArrowUp:[0,-1],ArrowDown:[0,1]};let kx=null,ky=null;
  function keyAim(dx,dy,big){
    const b=box(),W=b.w,H=b.h,s=Math.min(W,H)*(big?.18:.06);
    if(kx===null){kx=W/2;ky=H/2}
    kx=Math.min(W,Math.max(0,kx+dx*s));ky=Math.min(H,Math.max(0,ky+dy*s));
    ptr='pretend';aim(b.x+kx,b.y+ky,overButton(b.x+kx,b.y+ky));
  }
  function keyRest(){const b=box();if(inside&&(kx!==null&&mx===b.x+kx&&my===b.y+ky||!stage.matches(':hover')))idle()}
  keys.addEventListener('focus',()=>{if(running())stopRun();keyAim(0,0)});
  keys.addEventListener('blur',keyRest);
  keys.addEventListener('keydown',e=>{
    if(e.altKey||e.ctrlKey||e.metaKey)return;
    if(e.key==='Escape')keyRest();
    else if(ARROWS[e.key]){e.preventDefault();keyAim(...ARROWS[e.key],e.shiftKey)}
  });

  // See inside and Compare rest on a pose: six 60 Hz frames (a tenth of a second) after Show me's first jump, from the middle of the first
  // area to the button in the second, the dot as far along as Lag puts it then, as far grown as the 0.2 s grow is half way through its ease
  // (all the way under reduced motion, where it is instant) and a mark where it was at each frame; drawn without setting inside.
  function pose(){
    if(mode==='normal')return;
    const S=centre(zones[0]),T=centre(buttons[0]),at=k=>{const f=1-Math.pow(1-ease,k);return[S[0]+(T[0]-S[0])*f,S[1]+(T[1]-S[1])*f]};
    P={T,D:at(6),marks:[0,1,2,3,4,5].map(at),scale:expandOnHover?matchMedia('(prefers-reduced-motion: reduce)').matches?2.5:1+1.5*.802:1};
    posed=true;ptr='pretend';sig='';Object.values(L).forEach(l=>l._i=null);
    still(pieces,()=>{follower.classList.toggle('expanded',P.scale>1);pieces[0].style.transform=`translate(-50%,-50%) scale(${P.scale})`;
      follower.style.transform=`translate(${P.D[0].toFixed(1)}px, ${P.D[1].toFixed(1)}px)`;follower.style.opacity='1'});
    if(mode==='compare')mirror(true);
    views(0);
  }
  function unpose(){if(posed){posed=false;still(pieces,()=>{pieces[0].style.removeProperty('transform');hide()});mirror(true)}}
  // The dot hides (the pointer left, Escape): See inside and Compare go back to the pose
  function idle(){hide();if(mode!=='normal'&&!running())pose()}
  // The copy's dot takes the real one's look (shape, color flip, growing, the pose's size, whether it shows), at once where asked
  function mirror(now){const c='dot-twin '+follower.className,s=pieces[0].style.transform,o=follower.style.opacity,put=()=>{dotTwin.className=c;twinPieces[0].style.transform=s;dotTwin.style.opacity=o};
    if(dotTwin.className!==c||twinPieces[0].style.transform!==s||dotTwin.style.opacity!==o)now?still(twinPieces,put):put()}
  // A dot on show keeps its place on the areas when the box changes (Compare moves the real areas into a half, and back)
  function move(a,b){const m=(x,y)=>[b.x+(x-a.x)*b.w/a.w,b.y+(y-a.y)*b.h/a.h];
    [mx,my]=m(mx,my);[fx,fy]=m(fx,fy);if(px!==null)[px,py]=m(px,py);if(kx!==null){kx*=b.w/a.w;ky*=b.h/a.h}draw()}
  // What See inside's labels keep clear of (the areas' words, the buttons with their outline), and the labels' sizes
  function layout(){const r=(a,s)=>{const[x,y]=spot(a.left,a.top);return[x-s,y-s,x+a.width+s,y+a.height+s]},g=document.createRange();
    B=box();WB={words:zones.map(z=>{g.selectNodeContents(z);return r(g.getBoundingClientRect(),4)}),btns:buttons.map(b=>r(b.getBoundingClientRect(),6))};measure()}
  function measure(){sig='';if(mode==='inside')Object.values(L).forEach(l=>{const d=l.style.display;l.style.display='';SZ.set(l,[l.offsetWidth,l.offsetHeight]);l.style.display=d})}
  // The labels' words follow the settings: how long the catch-up takes (to 95% of the gap), the growing and the color flip
  function say(){const s=Math.log(.05)/Math.log(1-ease)/60,flip=!follower.classList.contains('no-blend'),f=L.flip.childNodes;
    L.gap.firstChild.textContent=`Catches up in ${s<.2?'about a tenth of a second':s<.5?'about a third of a second':'almost a second'},`;
    L.grow.textContent=expandOnHover?'Grows two and a half times over a button':'Stays its size over buttons';
    f[0].textContent=flip?'Flips the colors':'See-through orange,';f[2].textContent=flip?' under it':' no flip';measure()}
  function refresh(){if(mode!=='normal'){say();if(posed)pose()}}
  // Writes a part's transform (and opacity) only when it changes, and shows or hides it
  function set(el,t,o){if(el._t!==t)el.style.transform=el._t=t;if(o!==undefined&&el._o!==o)el.style.opacity=el._o=o}
  function show(el,on){const d=on?'':'none';if(el._d!==d)el.style.display=el._d=d}
  // Draws the mode in each frame, from the dot's own numbers (the pose's while posed). See inside: the cross, the line to the dot,
  // the marks (one where the dot set off, then one every twentieth of a second, the oldest faintest; gone once it is within a pixel
  // of the pointer) and the labels. Compare: the copy's dot right on the spot, and an arrow in each half the pointer is not over.
  function views(dt){
    const on=inside||posed,[cx,cy]=posed?P.T:[mx,my],f=n=>n.toFixed(1);
    if(mode==='compare'){const t=`translate(${f(cx-B.x)}px, ${f(cy-B.y)}px)`;
      mirror(false);set(dotTwin,t);arrows.forEach((a,i)=>{show(a,on&&(ptr==='pretend'||over!==i+1));set(a,t)});return}
    if(!inside||Math.hypot(mx-fx,my-fy)<1)trail=[];
    else if(trail.length&&(tick+=dt)>=49.5){tick-=50;trail.push([fx,fy]);if(trail.length>6)trail.shift()}
    const pts=!on?[]:posed?P.marks:trail,[dx,dy]=posed?P.D:[fx,fy],len=on?Math.hypot(cx-dx,cy-dy):0;
    marks.forEach((m,i)=>{const p=pts[i];show(m,!!p);if(p)set(m,`translate(${f(p[0])}px,${f(p[1])}px)`,(.3+.6*(i+1)/pts.length).toFixed(2))});
    show(gap,len>=1);if(len>=1)set(gap,`translate(${f(dx)}px,${f(dy)}px) rotate(${Math.atan2(cy-dy,cx-dx).toFixed(3)}rad) scaleX(${(len/100).toFixed(3)})`);
    show(cross,on);set(cross,`translate(${f(cx)}px,${f(cy)}px)`);
    labels(on,cx,cy,dx,dy,dotPx/2*(posed?P.scale:follower.classList.contains('expanded')?2.5:1),pts,len);
  }
  // See inside's labels, each beside the part it names, inside the box and clear of the labels placed before it, the dot, the cross,
  // the marks, the line, the areas' words and the buttons; a label keeps its place on a tie, so it does not flicker as the dot moves
  function labels(on,cx,cy,dx,dy,r,pts,len){
    const s=[on,cx,cy,dx,dy,r,pts.length].map(n=>+n|0).join();if(s===sig)return;sig=s;
    const g=6,taken=[],sq=(x,y,e)=>[x-e,y-e,x+e,y+e],join=bs=>[0,1,2,3].map(i=>(i<2?Math.min:Math.max)(...bs.map(b=>b[i])));
    const ov=(a,b)=>Math.max(0,Math.min(a[2],b[2])-Math.max(a[0],b[0]))*Math.max(0,Math.min(a[3],b[3])-Math.max(a[1],b[1]));
    const dot=sq(dx,dy,r),cr=sq(cx,cy,9),btn=WB.btns.find(b=>cx>b[0]&&cx<b[2]&&cy>b[1]&&cy<b[3]),ms=pts.map(p=>sq(p[0],p[1],5)),keep=[[cr,3],...WB.words.map(w=>[w,2]),...ms.map(m=>[m,1])];
    for(let d=12;d<len;d+=12)keep.push([sq(dx+(cx-dx)*d/len,dy+(cy-dy)*d/len,3),1]);
    const near=q=>Math.hypot(Math.max(q[0],Math.min(dx,q[2]))-dx,Math.max(q[1],Math.min(dy,q[3]))-dy)<r+3;
    for(const [el,a] of [[L.gap,on&&(pts.length||len>=1)?join([dot,cr,...ms]):null],[L.ptr,on?btn?join([cr,btn]):cr:null],[L.flip,on?dot:null],[L.grow,WB.btns[1]]]){
      show(el,!!a);if(!a)continue;
      const [w,h]=SZ.get(el)||[0,0],ax=(a[0]+a[2])/2,ay=(a[1]+a[3])/2,tries=[];
      for(const e of [0,14])for(const x of [ax-w/2,a[0],a[2]-w,ax-w,ax,a[0]-w,a[2]])tries.push([x,a[1]-g-e-h],[x,a[3]+g+e]);
      tries.push([a[2]+g,ay-h/2],[a[0]-g-w,ay-h/2]);
      let best=null,bc=Infinity;
      tries.forEach(([x,y],i)=>{x=Math.round(Math.max(4,Math.min(B.w-w-4,x)));y=Math.round(Math.max(4,Math.min(B.h-h-4,y)));
        const q=[x,y,x+w,y+h];let c=i/100-(i===el._i?.5:0)+(near(q)?100+ov(q,dot)*3:0);
        taken.forEach(t=>c+=ov(q,t)*100);keep.forEach(([k,wt])=>c+=ov(q,k)*wt);if(c<bc){bc=c;best=[q,i]}});
      el._i=best[1];taken.push(best[0]);set(el,`translate(${best[0][0]}px,${best[0][1]}px)`);
    }
  }
  // The mode switch: draws the mode at once. A dot on show keeps its place on the areas in the new box; otherwise See inside and
  // Compare rest on the pose, and Normal is the page as it was. The shared script then presses Show me (not under reduced motion).
  document.addEventListener('hb:mode',e=>{const was=B;mode=e.detail.mode;trail=[];unpose();layout();if(inside)move(was,B);
    if(mode!=='normal'){say();if(!inside&&!running())pose();else views(0)}});
  // A new size of the window changes the boxes; the pose is drawn again for it, as for the font arriving or reduced motion changing
  addEventListener('resize',()=>{const was=B;layout();if(mode!=='normal'){if(inside)move(was,B);if(posed)pose()}});
  document.fonts.ready.then(()=>{layout();if(posed)pose()});
  matchMedia('(prefers-reduced-motion: reduce)').addEventListener('change',()=>{if(posed)pose()});

  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);refresh();
    }));
  }
  choices('lag-seg',d=>{ease=+d.ease});
  choices('size-seg',d=>{vars.setProperty('--follower-size',d.px+'px');dotPx=+d.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);refresh()});
  document.getElementById('expand-tog').addEventListener('change',e=>{
    expandOnHover=e.target.checked;
    if(!expandOnHover)follower.classList.remove('expanded');refresh();
  });
  layout();
</script>
</body>
</html>