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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Spotlight Hover Glow — Animation Handbook</title>
<meta name="description" content="A soft glow follows the pointer and lights card edges. Best for feature grids.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/spotlight-hover/">
<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="Spotlight Hover Glow — Animation Handbook">
<meta property="og:description" content="A soft glow follows the pointer and lights card edges. Best for feature grids.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/spotlight-hover/">
<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="Spotlight Hover Glow — Animation Handbook">
<meta name="twitter:description" content="A soft glow follows the pointer and lights card edges. Best for feature grids.">
<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": "Spotlight Hover Glow", "name": "Spotlight Hover Glow", "description": "A soft glow follows the pointer and lights card edges. Best for feature grids.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/spotlight-hover/", "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, Spotlight Hover Glow"}</script>
<style>
:root{--ui-accent:#ff9d5c;--glow:#b98cff;--g:300px;--bw:2px}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
.stage{display:flex;overflow:hidden;background:#09090b;padding:clamp(12px,2.4vw,24px)}
/* touch-action none: a finger dragged over the cards moves the light instead of scrolling the page */
.grid{position:relative;flex:1;min-width:0;display:grid;grid-template-columns:repeat(3,minmax(0,1fr));grid-auto-rows:minmax(0,1fr);gap:clamp(8px,1.4vw,14px);touch-action:none;-webkit-user-select:none;user-select:none}
/* The grid takes keyboard focus (the arrow keys move the light): its ring goes around it, in the stage's padding, not over the cards */
.hb-page .stage .grid:focus-visible{outline-offset:4px;border-radius:18px}
/* A card is a faint frame (its padding is the edge) around an opaque face */
.sc{position:relative;overflow:hidden;border-radius:14px;padding:var(--bw);background:rgba(255,255,255,.09)}
/* Both lights are the same soft disc, moved to the pointer by transform only: the one behind the face shows only at
the edge, the one inside the face lights it faintly. The face light stays at 22% so the text over it keeps 4.5:1
in every color (White is the closest) */
.eg,.fg{position:absolute;left:0;top:0;width:var(--g);height:var(--g);margin:calc(var(--g)/-2) 0 0 calc(var(--g)/-2);border-radius:50%;
pointer-events:none;opacity:0;transition:opacity .35s ease;will-change:transform,opacity}
.eg{background:radial-gradient(closest-side,var(--glow) 30%,color-mix(in srgb,var(--glow) 60%,transparent) 65%,transparent)}
.fg{background:radial-gradient(closest-side,color-mix(in srgb,var(--glow) 22%,transparent) 30%,transparent)}
.grid.lit .eg,.grid.lit .fg{opacity:1}
.grid.no-edge .eg{visibility:hidden}
.face{position:relative;display:flex;flex-direction:column;gap:6px;height:100%;overflow:hidden;border-radius:calc(14px - var(--bw));
background:#121216;padding:clamp(12px,1.8vw,18px);color:#f4f4f2}
.face>:not(.fg){position:relative}
.ic{width:22px;height:22px;margin-bottom:auto;fill:none;stroke:#d6d6da;stroke-width:1.8;stroke-linecap:round;stroke-linejoin:round}
.t{font-size:clamp(14px,1.6vw,16px);font-weight:700;letter-spacing:-.01em}
.d{font-size:clamp(12px,1.3vw,13px);line-height:1.35;color:#b4b4bb}
@media(max-width:600px){
.grid{grid-template-columns:repeat(2,minmax(0,1fr))}
.sc:nth-child(n+5){display:none}
}
@media(prefers-reduced-motion:reduce){.eg,.fg{transition-duration:.2s}}
/* See inside and Compare, drawn in the grid's own coordinates with the same numbers as the lights: the dashed circle is as big as the
light and moves with it, the pretend pointer sits where the light is heading, and in Compare a ghost of it crosses the copy */
.views,.twin,.lbl,.ring{display:none}
.views{position:absolute;inset:0;z-index:2;pointer-events:none}
.ring{position:absolute;left:0;top:0;width:var(--g);height:var(--g);margin:calc(var(--g)/-2) 0 0 calc(var(--g)/-2);border:2px dashed rgba(244,244,242,.85);border-radius:50%;will-change:transform,opacity}
.pt{position:absolute;left:0;top:0;width:16px;height:23px;fill:#f4f4f2;stroke:#09090b;stroke-width:1.5;stroke-linejoin:round;overflow:visible;will-change:transform,opacity}
.ring,.pt{opacity:0;transition:opacity .35s ease}
.grid.lit .ring,.grid.lit .pt{opacity:1}
.grid.mouse .pt{display:none}
.grid.lit .pt.ghost{opacity:.65}
.lbl{padding:2px 6px;border-radius:5px;background:rgba(9,9,11,.92);color:#f4f4f2;font:600 13px/1.15 var(--hb-font);white-space:nowrap}
.in-l{position:absolute;left:0;top:0}
.stage[data-hb-mode="inside"] .views,.stage[data-hb-mode="compare"] .views,.stage[data-hb-mode="inside"] .ring,
.stage[data-hb-mode="inside"] .in-l,.stage[data-hb-mode="compare"] .cmp-l{display:block}
.stage[data-hb-mode="inside"] .grid.no-edge .l-edge{display:none}
/* See inside: a band along each face's edge lets the edge light show through; the words stay on the solid middle */
.stage[data-hb-mode="inside"] .face{background:linear-gradient(#121216,#121216) content-box,rgba(18,18,22,.45)}
/* Compare: the copy beside the real grid, each half with its name above it (a row of its own, so no name covers a card), four
cards a half (two on phones, in one row) */
.stage[data-hb-mode="compare"]{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));grid-template-rows:auto minmax(0,1fr);gap:8px clamp(14px,3vw,28px)}
.stage[data-hb-mode="compare"] .grid{grid-template-columns:repeat(2,minmax(0,1fr))}
.stage[data-hb-mode="compare"] .sc:nth-child(n+5){display:none}
.stage[data-hb-mode="compare"] .grid.twin{display:grid;touch-action:auto}
.twin{grid-area:2/1}
.stage[data-hb-mode="compare"] #grid{grid-area:2/2}
.cmp-l.first{grid-area:1/1}
.cmp-l.second{grid-area:1/2}
.cmp-l{justify-self:start}
/* Compare's cards are shorter than Normal's (a row of half names is added), so on a short stage the icons would shrink to specks:
they go, in both halves, there and on phones */
@media(max-width:600px),(max-height:680px){.stage[data-hb-mode="compare"] .ic{display:none}}
@media(max-width:600px){
.stage[data-hb-mode="inside"] .l-trail{display:none}
.stage[data-hb-mode="compare"]{grid-template-columns:minmax(0,1fr);grid-template-rows:auto minmax(0,1fr) auto minmax(0,1fr)}
.stage[data-hb-mode="compare"] .sc:nth-child(n+3){display:none}
.stage[data-hb-mode="compare"] #grid{grid-area:4/1}
.cmp-l.second{grid-area:3/1}
}
@media(prefers-reduced-motion:reduce){.ring,.pt{transition-duration:.2s}}
</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="../gradient-border/" rel="prev" aria-label="Previous: Animated Gradient Border"><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>Animated Gradient Border</span></a><a href="../add-to-cart-fly/" rel="next" aria-label="Next: Add-to-Cart Fly"><span><span class="hb-dir">Next: </span>Add-to-Cart Fly</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.31 · Micro-Interactions</p>
<h1>Spotlight Hover Glow</h1>
<p class="hb-lede">A soft glow follows the pointer and lights card edges. Best for feature grids.</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 cards, drag a finger across them (or Tab to them 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">
<div class="grid" id="grid" tabindex="0" role="group" aria-label="Feature cards: use the arrow keys to move the light">
<div class="sc"><span class="eg" aria-hidden="true"></span><div class="face"><span class="fg" aria-hidden="true"></span><svg class="ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M3 3v18h18"/><path d="m7 15 4-4 3 3 5-6"/></svg><p class="t">Analytics</p><p class="d">See what people click.</p></div></div>
<div class="sc"><span class="eg" aria-hidden="true"></span><div class="face"><span class="fg" aria-hidden="true"></span><svg class="ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M13 2 4 14h7l-1 8 9-12h-7z"/></svg><p class="t">Automations</p><p class="d">Runs tasks for you.</p></div></div>
<div class="sc"><span class="eg" aria-hidden="true"></span><div class="face"><span class="fg" aria-hidden="true"></span><svg class="ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3 20 6v6c0 5-3.5 8-8 9-4.5-1-8-4-8-9V6z"/></svg><p class="t">Security</p><p class="d">Logins kept safe.</p></div></div>
<div class="sc"><span class="eg" aria-hidden="true"></span><div class="face"><span class="fg" aria-hidden="true"></span><svg class="ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M20 11a8 8 0 0 0-14.6-4.4L3 9"/><path d="M3 4v5h5"/><path d="M4 13a8 8 0 0 0 14.6 4.4L21 15"/><path d="M21 20v-5h-5"/></svg><p class="t">Sync</p><p class="d">Work on every device.</p></div></div>
<div class="sc"><span class="eg" aria-hidden="true"></span><div class="face"><span class="fg" aria-hidden="true"></span><svg class="ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M6 3h9l4 4v14H6z"/><path d="M14 3v5h5"/><path d="M9 13h7M9 17h5"/></svg><p class="t">Reports</p><p class="d">Weekly summaries.</p></div></div>
<div class="sc"><span class="eg" aria-hidden="true"></span><div class="face"><span class="fg" aria-hidden="true"></span><svg class="ic" viewBox="0 0 24 24" aria-hidden="true"><circle cx="9" cy="8" r="3.5"/><path d="M3 20c0-3.3 2.7-6 6-6s6 2.7 6 6"/><path d="M16 4.5a3.5 3.5 0 0 1 0 7"/><path d="M21 20c0-2.6-1.6-4.8-4-5.6"/></svg><p class="t">Teams</p><p class="d">Invite and share.</p></div></div>
<!-- See inside and Compare, drawn over the cards: the circle the light covers, the pretend pointer and the labels -->
<div class="views" aria-hidden="true">
<span class="ring"></span>
<span class="lbl in-l l-ring" data-hb-caption>One light for all cards</span>
<span class="lbl in-l l-cut" data-hb-caption>Each card cuts its copy</span>
<span class="lbl in-l l-edge" data-hb-caption>Only the edge glows</span>
<span class="lbl in-l l-face" data-hb-caption>A faint copy on the face</span>
<span class="lbl in-l l-trail" data-hb-caption>Lags a little behind</span>
<!-- after the labels, so the arrow is never hidden under one -->
<svg class="pt" viewBox="0 0 16 23"><path d="M1 1V19L5.5 15L8.5 22L11.5 20.7L8.5 14H14.5Z"/></svg>
</div>
</div>
<span class="lbl cmp-l first" data-hb-caption aria-hidden="true">No glow</span>
<span class="lbl cmp-l second" data-hb-caption aria-hidden="true">Spotlight</span>
</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 dashed circle is one light, copied into every card and cut to its shape; each card's face hides the middle, so only the edge glows. Move over the cards, or press Show me.</p>
<p data-hb-for="compare" hidden>No glow: the cards ignore the pointer, shown as a ghost; Spotlight: the edges near the pointer light up and the light follows it. Move over the cards, 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="size-lbl">Glow size</p>
<div class="seg" id="size-seg" role="group" aria-labelledby="size-lbl">
<button type="button" data-k="0.7" aria-pressed="false">Small</button>
<button type="button" data-k="1.15" class="on" aria-pressed="true">Medium</button>
<button type="button" data-k="1.7" aria-pressed="false">Large</button>
</div>
<p class="hb-hint">How far the light reaches around the pointer.</p>
</div>
<div class="hb-setting">
<p class="hb-setting-name" id="color-lbl">Color</p>
<div class="swatches" id="color-sw" role="group" aria-labelledby="color-lbl">
<button type="button" aria-pressed="false" aria-label="White" data-c="#f4f4f2" style="background:#f4f4f2"></button>
<button type="button" aria-pressed="false" aria-label="Orange" data-c="#ff9d5c" style="background:#ff9d5c"></button>
<button type="button" aria-pressed="false" aria-label="Pink" data-c="#ff6f8b" style="background:#ff6f8b"></button>
<button type="button" class="on" aria-pressed="true" aria-label="Purple" data-c="#b98cff" style="background:#b98cff"></button>
<button type="button" aria-pressed="false" aria-label="Blue" data-c="#6ea8ff" style="background:#6ea8ff"></button>
<button type="button" aria-pressed="false" aria-label="Green" data-c="#5fd88a" style="background:#5fd88a"></button>
</div>
<p class="hb-hint">The color of the light and of the lit edges.</p>
</div>
<div class="hb-setting">
<label class="hb-switch-row"><span>Lights the edges</span><input class="hb-switch" type="checkbox" role="switch" id="edge-tog" checked autocomplete="off"></label>
<p class="hb-hint">The card edges near the pointer glow brightly.</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="lag-lbl">Lag</p>
<div class="seg" id="lag-seg" role="group" aria-labelledby="lag-lbl">
<button type="button" data-ease="1" aria-pressed="false">None</button>
<button type="button" data-ease="0.3" class="on" aria-pressed="true">Short</button>
<button type="button" data-ease="0.1" aria-pressed="false">Long</button>
</div>
<p class="hb-hint">How far the light trails behind a moving pointer.</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 spotlight glow to [the grid of dark cards you want to light up]. As the pointer moves over the grid, a soft round light follows it across the cards and, when the settings include it, lights up the card edges closest to the pointer while the far edges stay dim. Give each card its own light, clipped to the card, and move it to the pointer with transforms only, so nothing is repainted while it moves. Fade the light in when the pointer arrives and out when it leaves. On touch screens, let a finger dragged across the cards move the light. If the visitor has reduced motion turned on, keep the light on the pointer with no glide. 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>Feature grids</li><li>Pricing cards</li><li>Dashboards</li><li>Portfolios</li></ul>
<h3>Avoid on</h3>
<ul class="hb-tags hb-avoid"><li>Light themes</li><li>Text-heavy 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.querySelector('.stage'),grid=document.getElementById('grid');
const lights=[...grid.querySelectorAll('.eg,.fg')];
const cards=[...grid.querySelectorAll('.sc')].map(el=>({el,eg:el.querySelector('.eg'),fg:el.querySelector('.fg'),x:0,y:0,w:0,h:0}));
// See inside and Compare: the circle and the pretend pointer in the grid, and for Compare a copy of the grid that ignores the pointer,
// made once (no id, role or tab stop, inert, hidden from screen readers, no lights) with a ghost of the pointer in it
const ring=grid.querySelector('.ring'),pt=grid.querySelector('.pt'),twin=grid.cloneNode(true),ghost=pt.cloneNode(true);
['id','tabindex','role','aria-label'].forEach(a=>twin.removeAttribute(a));
twin.querySelectorAll('.eg,.fg,.views').forEach(n=>n.remove());
twin.classList.add('twin');twin.inert=true;twin.setAttribute('aria-hidden','true');
ghost.classList.add('ghost');twin.append(ghost);stage.insertBefore(twin,grid);
lights.push(ring,pt,ghost);
const reduce=window.matchMedia('(prefers-reduced-motion: reduce)');
const FRAME=1000/60; // one frame of a 60 Hz screen, in ms: ease is the share of the gap the light closes in one of them
// lx,ly: where the light is; tx,ty: where it is heading (both on the grid, from its top left); W,H: the grid's size
let lx=0,ly=0,tx=0,ty=0,W=0,H=0,bw=2,k=1.15,ease=0.3,lit=false,raf=0,last=null,touchId=null,runTimers=[],runFrame=0;
// mode: what the box shows; posed: the light is parked at its spot (a pretend pointer where four cards meet) because nothing else holds it;
// hover: a mouse or pen is over the grid; bits: the places of the icons and words, which the labels keep clear of
let mode='normal',posed=false,hover=false,wide=false,bits=[];
// Card places and the light's size are measured once and again whenever the grid changes size; a pointer move reads only the grid's rectangle
function measure(){
W=grid.clientWidth;H=grid.clientHeight;
bw=parseFloat(getComputedStyle(cards[0].el).paddingLeft)||0;
cards.forEach(c=>{c.x=c.el.offsetLeft;c.y=c.el.offsetTop;c.w=c.el.offsetWidth;c.h=c.el.offsetHeight});
grid.style.setProperty('--g',Math.round(k*Math.min(W,H))+'px');
// the icons and words of the cards on show (the words as tight as their lines), in the grid's frame, with a little air
const g0=grid.getBoundingClientRect(),rg=document.createRange();
// and each card's edge band (the card less its middle: a negative weight takes the middle off again)
const pad=bw+parseFloat(getComputedStyle(cards[0].el.querySelector('.face')).paddingLeft);
bits=cards.filter(c=>c.w).flatMap(c=>[{x:c.x,y:c.y,r:c.x+c.w,b:c.y+c.h,w:3},{x:c.x+pad,y:c.y+pad,r:c.x+c.w-pad,b:c.y+c.h-pad,w:-3},
...[...c.el.querySelectorAll('.ic,.t,.d')].map(el=>{
rg.selectNodeContents(el);const r=el.matches('.ic')?el.getBoundingClientRect():rg.getBoundingClientRect(),icon=el.matches('.ic');
return{x:r.left-g0.left-3,y:r.top-g0.top-3,r:r.right-g0.left+3,b:r.bottom-g0.top+3,w:icon?1:12};
})]);
draw();
if(posed)pose();
labels();
}
// Every card's two lights go to the same spot of the grid, each counted from its own corner
function draw(){
for(const c of cards){
if(!c.w)continue; // a card hidden on phones
c.eg.style.transform=`translate(${(lx-c.x).toFixed(1)}px,${(ly-c.y).toFixed(1)}px)`;
c.fg.style.transform=`translate(${(lx-c.x-bw).toFixed(1)}px,${(ly-c.y-bw).toFixed(1)}px)`;
}
ring.style.transform=`translate(${lx.toFixed(1)}px,${ly.toFixed(1)}px)`;
// the arrow (16 by 23, its tip at the spot) is drawn wholly inside its grid, so in Compare neither it nor its ghost leaves its half
pt.style.transform=ghost.style.transform=`translate(${Math.min(tx,W-16).toFixed(1)}px,${Math.min(ty,H-23).toFixed(1)}px)`;
}
// 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. Under reduced motion the
// light goes straight to its spot.
function tick(ts){
const dt=last===null?FRAME:Math.min(ts-last,50);last=ts;
const a=reduce.matches?1:1-Math.pow(1-ease,dt/FRAME);
lx+=(tx-lx)*a;ly+=(ty-ly)*a;
const done=Math.abs(tx-lx)<.3&&Math.abs(ty-ly)<.3;
if(done){lx=tx;ly=ty;last=null}
draw();
raf=done?0:requestAnimationFrame(tick);
}
// The pointer, a finger and Show me all send the light to a spot. It appears there directly, as the pointer enters (or takes over
// from the parked light), so it never swoops in from where it was last; once it is shown it glides after each new spot.
function aim(x,y){
tx=x;ty=y;
if(!lit||posed){lx=x;ly=y;posed=false;shine(true);draw();return}
if(!raf)raf=requestAnimationFrame(tick);
}
function shine(on){lit=on;grid.classList.toggle('lit',on);twin.classList.toggle('lit',on)}
function off(){posed=false;shine(false);cancelAnimationFrame(raf);raf=0;last=null}
// Nothing holds the light any more: in See inside and Compare it goes back to its parked spot, in Normal it fades out
function release(){if(mode!=='normal'&&!inUse())pose();else off()}
function inUse(){return hover||touchId!==null||keyed||running()}
// The parked spot, where four cards meet (the middle of a row of two): the pretend pointer is there and the light shows it
function spot(){const a=cards[0],b=cards[1],below=cards.find(c=>c.w&&c.y>a.y)||a;return{x:(a.x+a.w+b.x)/2,y:(a.y+a.h+below.y)/2}}
function pose(){const s=spot();aim(s.x,s.y);posed=true}
function aimAt(e){const r=grid.getBoundingClientRect();aim(e.clientX-r.left,e.clientY-r.top)}
// Show me: one run at a time. Every timer of the run goes through later(); its path is drawn frame by frame (runFrame).
// 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||runFrame!==0}
function stopRun(){runTimers.forEach(clearTimeout);runTimers=[];cancelAnimationFrame(runFrame);runFrame=0}
// A mouse or a pen in the air lights the cards while it moves over them; a finger where it touches and while it drags
// (touch-action none keeps the drag on the grid). Pointer events only, so the mouse events that follow a tap leave nothing
// behind. A run under way is stopped first, and the light follows the visitor after it.
grid.addEventListener('pointermove',e=>{
if(e.pointerType==='touch'&&e.pointerId!==touchId)return;
if(e.isTrusted&&running())stopRun();
hover=e.pointerType!=='touch';grid.classList.toggle('mouse',hover);
aimAt(e);
});
grid.addEventListener('pointerleave',e=>{
if(e.pointerType==='touch')return;
if(e.isTrusted&&running())stopRun();
hover=false;grid.classList.remove('mouse');
release();
});
grid.addEventListener('pointerdown',e=>{
if(e.pointerType!=='touch')return;
touchId=e.pointerId;
try{grid.setPointerCapture(touchId)}catch(_){}
aimAt(e);
});
['pointerup','pointercancel'].forEach(type=>grid.addEventListener(type,e=>{if(e.pointerId===touchId){touchId=null;release()}}));
// A run starts with the light off, put there without its fade: the transitions are turned off, the state is set, a reflow
// makes it stick, and the transitions come back.
function toRest(){
lights.forEach(l=>{l.style.transition='none'});
off();
void grid.offsetWidth;
lights.forEach(l=>{l.style.transition=''});
}
// A pretend pointer crosses the grid from left to right in 2.8 s, easing in and out, while it swings up and down across the
// gap between the rows; then the light fades away. Under reduced motion the light shows where four cards meet, holds 1.4 s
// and fades, with no glide. The light follows the pretend pointer with the Lag setting, as it follows a real one.
const RUN=2800;
document.getElementById('btn-demo').addEventListener('click',()=>{
stopRun();
toRest();
measure();
if(reduce.matches){
const a=cards[0],b=cards[1],below=cards.find(c=>c.w&&c.y>a.y)||a;
aim((a.x+a.w+b.x)/2,(a.y+a.h+below.y)/2);
later(release,0,1400);
return;
}
let t0=0;
(function frame(now){
if(!t0)t0=now;
const s=Math.min(1,Math.max(0,now-t0)/RUN),e=s<.5?2*s*s:1-2*(1-s)*(1-s);
aim(W*(.06+.88*e),H*(.5-.3*Math.sin(2*Math.PI*s)));
runFrame=s<1?requestAnimationFrame(frame):0;
if(s>=1)later(release,0,150);
})(performance.now());
});
// The visitor pressed, typed, scrolled or touched inside the stage: the run stops and the light goes; their own pointer
// brings it back where they are.
document.addEventListener('hb:input',()=>{if(running()){stopRun();release()}});
// The keyboard: Tab focuses the grid and the light appears where the keys last left it (kx,ky: shares of the grid's width and
// height, first its middle), stopping a run as a pointer does. Each arrow press moves that spot by STEP (from the pointer's
// spot when a pointer placed the light) through aim(), so it trails with the same lag; a held key repeats. Escape puts it out,
// as the pointer leaving does, and so does the focus leaving after the keys placed it. A click or a tap focuses the grid
// without :focus-visible, and the light stays with the pointer.
const STEP=0.06,ARROWS={ArrowLeft:[-1,0],ArrowRight:[1,0],ArrowUp:[0,-1],ArrowDown:[0,1]};
let kx=.5,ky=.5,keyed=false;
function aimKeys(){keyed=true;aim(kx*W,ky*H)}
grid.addEventListener('focus',()=>{if(grid.matches(':focus-visible')){if(running())stopRun();if(posed&&W&&H){kx=tx/W;ky=ty/H}aimKeys()}});
grid.addEventListener('blur',()=>{if(keyed){keyed=false;release()}});
grid.addEventListener('pointermove',()=>{keyed=false});
grid.addEventListener('keydown',e=>{
const d=ARROWS[e.key];
if(e.altKey||e.ctrlKey||e.metaKey)return;
if(e.key==='Escape'){keyed=false;release();return}
if(!d)return;
e.preventDefault();
if(!keyed&&lit&&W&&H){kx=tx/W;ky=ty/H}
kx=Math.min(1,Math.max(0,kx+d[0]*STEP));ky=Math.min(1,Math.max(0,ky+d[1]*STEP));
aimKeys();
});
// The labels go where they cover no icon, title or description and no other label, as near as they can to the part they name.
// They are placed for the parked spot, when the size, a setting or the mode changes, never while the light moves.
function labels(){
if(mode!=='inside'||!W)return;
const s=spot(),r=parseFloat(grid.style.getPropertyValue('--g'))/2,vis=cards.filter(c=>c.w),mid=i=>{const c=vis[Math.min(i,vis.length-1)];return[c.x+c.w/2,c.y+c.h/2]};
// a light bigger than the box (its farthest corner, with the stage's padding, is inside the circle) leaves no circle to see
const pad=parseFloat(getComputedStyle(stage).paddingLeft);
wide=Math.hypot(Math.max(s.x,W-s.x)+pad,Math.max(s.y,H-s.y)+pad)<r;
grid.querySelector('.l-ring').textContent=wide?'One light covers every card':'One light for all cards';
notes();
const taken=[...bits,{x:s.x-2,y:s.y-2,r:s.x+18,b:s.y+25,w:4}];
const overlap=(a,b)=>Math.max(0,Math.min(a.r,b.r)-Math.max(a.x,b.x))*Math.max(0,Math.min(a.b,b.b)-Math.max(a.y,b.y));
[['.l-ring',s.x-.7*r,s.y-.7*r],['.l-cut',s.x+.7*r,s.y-.7*r],['.l-edge',...mid(3)],['.l-face',...mid(4)],['.l-trail',s.x+30,s.y]].forEach(([q,ax,ay])=>{
const el=grid.querySelector(q),w=el.offsetWidth,h=el.offsetHeight;
if(!w)return;
let best=[Infinity,0,0];
for(let y=2;y<=H-h-2;y+=2)for(let x=2;x<=W-w-2;x+=8){
const b={x,y,r:x+w,b:y+h};let c=0;
for(const o of taken)c+=overlap(b,o)*o.w;
const sc=c*1000+Math.hypot(x+w/2-ax,y+h/2-ay);
if(sc<best[0])best=[sc,x,y];
}
el.style.left=best[1]+'px';el.style.top=best[2]+'px';
taken.push({x:best[1],y:best[2],r:best[1]+w,b:best[2]+h,w:30});
});
}
// The notes say what the box shows now (the markup's own words while the edges are on and the circle is in view): with Lights the edges
// off there is no edge light to point at, and where the light is bigger than the whole box its circle lies outside it
function notes(){
const on=!grid.classList.contains('no-edge'),say=(m,t)=>{const p=document.querySelector(`[data-hb-for="${m}"]`);if(p.textContent!==t)p.textContent=t};
say('inside',(wide?'Inside, the light is so big that its dashed circle lies outside the box; ':'Inside, the dashed circle is one light, copied into every card and cut to its shape; ')+
(on?"each card's face hides the middle, so only the edge glows. Move over the cards, or press Show me.":'with Lights the edges off, only the faint copy on each face shows. Move over the cards.'));
say('compare','No glow: the cards ignore the pointer, shown as a ghost; Spotlight: '+
(on?'the edges near the pointer light up and the light follows it.':'with Lights the edges off, only a faint light on the faces follows it.')+' Move over the cards, or press Show me.');
}
const TRAIL={1:'Stays on the pointer',0.3:'Lags a little behind',0.1:'Lags far behind'};
// The mode switch: the box shows the mode at once. Compare changes the size of the grid, so the cards are measured again before the
// parked spot is worked out; the shared script then presses Show me (not under reduced motion), which plays it.
document.addEventListener('hb:mode',e=>{
mode=e.detail.mode;
measure();
if(inUse())return;
if(mode==='normal'){if(posed)off()}else pose();
});
function choices(id,pick){
const buttons=document.querySelectorAll('#'+id+' button');
buttons.forEach(b=>b.addEventListener('click',()=>{
buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});
pick(b.dataset);
}));
}
choices('size-seg',d=>{k=+d.k;measure()});
choices('color-sw',d=>{grid.style.setProperty('--glow',d.c)});
choices('lag-seg',d=>{ease=+d.ease;grid.querySelector('.l-trail').textContent=TRAIL[ease];labels()});
document.getElementById('edge-tog').addEventListener('change',e=>{grid.classList.toggle('no-edge',!e.target.checked);notes();labels()});
new ResizeObserver(measure).observe(grid);
</script>
</body>
</html>