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 Star Rating to the guided-steps page
matin1371f17e4989
diff --git a/animations/04-micro-interactions/star-rating/README.md b/animations/04-micro-interactions/star-rating/README.mdindex 0077b1c0..caaed5ef 100644--- a/animations/04-micro-interactions/star-rating/README.md+++ b/animations/04-micro-interactions/star-rating/README.md@@ -1,7 +1,7 @@# Star Rating## What it is-An interactive five-star control for capturing a rating. Moving the pointer across the row fills the stars up to the pointer with an accent color; the fill cascades smoothly as you move. Clicking (or tapping) commits that value and plays a short scale pop on the selected star. Arrow keys drive the same control from the keyboard, and an option lets each star resolve to a half.+A star rating lets someone give a score by choosing a star. As the pointer moves along the row, the stars fill up to the one under it, so the choice shows before it is made; clicking or tapping a star chooses it and makes it pop. The arrow keys change the rating too, and a setting lets each star count in halves.## When to use it- Product, review, and feedback forms where a coarse 1–5 score is enough@@ -28,10 +28,10 @@ The pointer position within a star decides the value — left half rounds to `.5## Key parameters| Parameter | Default | Effect ||-----------|---------|--------|-| Star count | 5 | How many buckets; the control rebuilds and `aria-valuemax` follows |-| Pop intensity | 1.35× | Peak scale of the commit pop. Above ~1.6 reads as rubbery |-| Fill duration | 180ms | Color-transition time per star — the perceived smoothness of the cascade |-| Allow half-stars | off | Switches the step and pointer rounding between 1 and 0.5 |+| Pop size | Medium | How big the chosen star grows as it pops: 1.15, 1.35 or 1.6 times its size; above about 1.6 it looks rubbery |+| Speed | Normal | How long each star takes to fill with color: slow is 300ms, normal 180ms and fast 110ms; it sets how smoothly the fill follows the pointer |+| Allows half stars | off | Pointing at the left half of a star gives half a point, and the arrow keys move in halves |+| Number of stars | Five | Three, five or ten stars; five is the usual scale |## Production notes- **Accessibility**: this demo exposes the row as a single `role="slider"` with `aria-valuemin/max/now` and an `aria-valuetext` ("3 out of 5"), which handles half-steps cleanly from the keyboard. A `role="radiogroup"` of `radio` stars is the other idiomatic choice and maps better to discrete whole-star ratings; pick the model that matches whether half values exist.@@ -42,6 +42,6 @@ The pointer position within a star decides the value — left half rounds to `.5- **Library equivalents**: Framer Motion can drive the pop with a `whileTap`/`animate` scale spring; many form kits (e.g. rating inputs in headless UI libraries) ship the radiogroup semantics so you only style the stars.## See also-- [Heart Burst](../heart-burst/) — a sibling reward interaction where the commit is a particle burst instead of a fill-- [Button Press Scale](../button-press-scale/) — the scale-overshoot mechanic behind the star pop-- [Toggle / Switch Slide](../toggle-switch/) — another small, committed form control with a state animation+- [Heart / Like Burst](../heart-burst/) — a like button that bursts into small hearts+- [Button Press Scale](../button-press-scale/) — a button that shrinks as you press it+- [Toggle / Switch Slide](../toggle-switch/) — another small control that animates its statediff --git a/animations/04-micro-interactions/star-rating/index.html b/animations/04-micro-interactions/star-rating/index.htmlindex 721a70d2..37b59a10 100644--- a/animations/04-micro-interactions/star-rating/index.html+++ b/animations/04-micro-interactions/star-rating/index.html@@ -4,7 +4,7 @@<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Star Rating — Animation Handbook</title>- <meta name="description" content="Hover or tap to fill the stars up to the pointer; click commits the rating with a small pop on the selected star.">+ <meta name="description" content="Stars fill up to your pointer and pop when you choose. Best for reviews."><link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/star-rating/"><link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg">@@ -12,31 +12,22 @@<meta property="og:type" content="article"><meta property="og:site_name" content="Animation Handbook"><meta property="og:title" content="Star Rating — Animation Handbook">- <meta property="og:description" content="Hover or tap to fill the stars up to the pointer; click commits the rating with a small pop on the selected star.">+ <meta property="og:description" content="Stars fill up to your pointer and pop when you choose. Best for reviews."><meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/star-rating/"><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="Star Rating — Animation Handbook">- <meta name="twitter:description" content="Hover or tap to fill the stars up to the pointer; click commits the rating with a small pop on the selected star.">+ <meta name="twitter:description" content="Stars fill up to your pointer and pop when you choose. Best for reviews."><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": "Star Rating", "name": "Star Rating", "description": "Hover or tap to fill the stars up to the pointer; click commits the rating with a small pop on the selected star.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/star-rating/", "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, Star Rating"}</script>+ <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Star Rating", "name": "Star Rating", "description": "Stars fill up to your pointer and pop when you choose. Best for reviews.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/star-rating/", "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, Star Rating"}</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;--star:clamp(44px,11vw,60px);--pop:1.35;--fill-dur:180ms}+ :root{--ui-accent:#ff9d5c;--ui-muted:#8a8a92;--bg:#0b0b0d;--star:clamp(44px,11vw,60px);--pop:1.35;--fill-dur:180ms}*,*::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(16px,2.2vw,22px);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-wrap{flex:1;min-width:0}- .stage{border:1px solid var(--ui-border);border-radius:8px;background:var(--bg);min-height:clamp(280px,42vh,420px);display:flex;flex-direction:column;align-items:center;justify-content:center;gap:clamp(18px,4vh,32px);padding:clamp(16px,4vw,40px)}+ .stage{display:flex;flex-direction:column;align-items:center;justify-content:center;gap:clamp(18px,4vh,32px);padding:clamp(16px,4vw,40px);background:var(--bg)}/* Rating control */- .rate{display:inline-flex;flex-wrap:wrap;justify-content:center;max-width:100%;gap:clamp(4px,1.5vw,12px);outline:none;touch-action:none}- .rate:focus-visible{box-shadow:0 0 0 2px var(--bg),0 0 0 4px var(--ui-accent);border-radius:8px}+ .rate{--gap:clamp(4px,1.5vw,12px);display:inline-flex;flex-wrap:wrap;justify-content:center;max-width:100%;gap:var(--gap);outline:none;touch-action:none}+ /* the row draws its own focus look, so it also switches off the site ring (.stage keeps this above body.hb :focus-visible) */+ .stage .rate:focus-visible{outline:none;box-shadow:0 0 0 2px var(--bg),0 0 0 4px var(--ui-accent);border-radius:8px}.star{position:relative;width:var(--star);height:var(--star);color:var(--ui-muted);cursor:pointer;transition:color var(--fill-dur) ease;-webkit-tap-highlight-color:transparent;transform-origin:center;will-change:transform}.star svg{position:absolute;inset:0;width:100%;height:100%;display:block}.star .half{position:absolute;left:0;top:0;height:100%;width:50%;overflow:hidden;color:var(--ui-accent);opacity:0}@@ -45,155 +36,248 @@.star.half-on .half{opacity:1}@keyframes pop{0%{transform:scale(1)}35%{transform:scale(var(--pop))}70%{transform:scale(.94)}100%{transform:scale(1)}}.star.pop{animation:pop .42s cubic-bezier(.22,1,.36,1)}- .value{font-family:var(--disp);font-weight:700;font-size:clamp(20px,5vw,32px)}- .value small{font-family:var(--mono);font-weight:400;font-size:clamp(11px,1.6vw,14px);color:var(--ui-muted)}- .hint{font-size:10px;color:var(--ui-muted);text-align:center;max-width:32ch;line-height:1.6}- /* Controls */- aside{width:clamp(200px,24vw,250px);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}- .lbl2{font-size:10px;text-transform:uppercase;letter-spacing:1px;color:var(--ui-accent);margin-top:4px}- .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}- .sval{width:56px;text-align:right;color:var(--ui-text)}- .row{display:flex;align-items:center;justify-content:space-between;font-size:10px;min-height:44px}- .row span{color:var(--ui-muted)}- .tog{position:relative;width:44px;height:26px;border-radius:13px;background:#2a2a30;border:1px solid var(--ui-border);cursor:pointer;flex-shrink:0}- .tog::before{content:"";position:absolute;inset:-11px 0}- .tog::after{content:"";position:absolute;top:2px;left:2px;width:20px;height:20px;border-radius:50%;background:var(--ui-text);transition:transform .18s ease}- .tog[aria-checked=true]{background:var(--ui-accent)}- .tog[aria-checked=true]::after{transform:translateX(18px)}- .kv{display:flex;justify-content:space-between;font-size:10px;color:var(--ui-muted)}- .kv .v{color:var(--ui-text)}- @media(hover:hover){.star:hover{color:var(--ui-accent)}}- @media(max-width:600px){.layout{flex-direction:column;align-items:stretch}aside{width:100%}}- @media(prefers-reduced-motion:reduce){.star{transition:none}.star.pop{animation:none}.tog::after{transition:none}}+ .value{font-weight:700;font-size:clamp(20px,5vw,32px)}+ .value small{font-weight:400;font-size:clamp(11px,1.6vw,14px);color:var(--ui-muted)}+ /* A star half filled by the preview must not be painted whole by :hover */+ @media(hover:hover){.star:hover:not(.half-on){color:var(--ui-accent)}}+ /* More than five stars that cannot share one row wrap into two equal rows, not nine and one */+ @media(max-width:845px){.rate.many{max-width:calc(5*var(--star) + 4*var(--gap) + 1px)}}+ @media(prefers-reduced-motion:reduce){.star{transition:none}.star.pop{animation:none}}</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="../copy-to-clipboard/" rel="prev" title="Previous">‹ Prev</a><a class="ah-hideSm" href="../toast-notification/" 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/star-rating/index.html" target="_blank" rel="noopener noreferrer">Source ↗</a></span></div>-<header>- <h1>Star Rating</h1>- <p>Hover or tap to fill the stars up to the pointer; click commits the rating with a small pop on the selected star.</p>-</header>-<div class="layout">- <div class="stage-wrap">- <div class="stage">+<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="../copy-to-clipboard/" rel="prev" aria-label="Previous: Copy to Clipboard"><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>Copy to Clipboard</span></a><a href="../toast-notification/" rel="next" aria-label="Next: Toast Notification"><span><span class="hb-dir">Next: </span>Toast Notification</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.26 · Micro-Interactions</p>+ <h1>Star Rating</h1>+ <p class="hb-lede">Stars fill up to your pointer and pop when you choose. Best for reviews.</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">Click it</h2><p>Point along the stars and click or tap one to choose, or press Show me.</p></div>+ </div>+ <div class="stage hb-dots"><div class="rate" id="rate" role="slider" tabindex="0" aria-label="Rating"aria-valuemin="0" aria-valuemax="5" aria-valuenow="3" aria-valuetext="3 out of 5"></div><div class="value"><span id="val">3.0</span><small> / <span id="max">5</span></small></div>- <p class="hint">Click a star, or focus the control and use the arrow keys. Home clears, End maxes out.</p></div>- </div>- <aside>- <p class="note">Each star is two stacked shapes: a muted outline and an accent fill. Filling is a color transition that cascades across the row as the pointer moves; committing plays a transform:scale pop on the chosen star.</p>- <div class="lbl2">Star count</div>- <div class="sr"><input type="range" id="cnt-sl" min="3" max="10" value="5" step="1"><span class="sval" id="cnt-v">5</span></div>- <div class="lbl2">Pop intensity</div>- <div class="sr"><input type="range" id="pop-sl" min="1" max="1.8" value="1.35" step="0.05"><span class="sval" id="pop-v">1.35×</span></div>- <div class="lbl2">Fill duration</div>- <div class="sr"><input type="range" id="dur-sl" min="0" max="500" value="180" step="20"><span class="sval" id="dur-v">180ms</span></div>- <div class="div"></div>- <div class="row"><span>Allow half-stars</span><button class="tog" id="half" role="switch" aria-checked="false" aria-label="Allow half-stars"></button></div>- <div class="div"></div>- <div class="kv"><span>Committed</span><span class="v" id="state-v">3.0</span></div>- </aside>-</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>+ <label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="slow-tog" data-hb-slowmo="css" autocomplete="off"><span>Slow motion</span></label>+ </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="pop-lbl">Pop size</p>+ <div class="seg" id="pop-seg" role="group" aria-labelledby="pop-lbl">+ <button type="button" data-pop="1.15" aria-pressed="false">Small</button>+ <button type="button" data-pop="1.35" class="on" aria-pressed="true">Medium</button>+ <button type="button" data-pop="1.6" aria-pressed="false">Large</button>+ </div>+ <p class="hb-hint">How much the chosen star grows as it pops.</p>+ </div>+ <div class="hb-setting">+ <p class="hb-setting-name" id="speed-lbl">Speed</p>+ <div class="seg" id="speed-seg" role="group" aria-labelledby="speed-lbl">+ <button type="button" data-ms="300" aria-pressed="false">Slow</button>+ <button type="button" data-ms="180" class="on" aria-pressed="true">Normal</button>+ <button type="button" data-ms="110" aria-pressed="false">Fast</button>+ </div>+ <p class="hb-hint">How quickly each star fills with color.</p>+ </div>+ <div class="hb-setting">+ <label class="hb-switch-row"><span>Allows half stars</span><input class="hb-switch" type="checkbox" role="switch" id="half-tog" autocomplete="off"></label>+ <p class="hb-hint">The left half of a star gives half a point.</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="count-lbl">Number of stars</p>+ <div class="seg" id="count-seg" role="group" aria-labelledby="count-lbl">+ <button type="button" data-n="3" aria-pressed="false">Three</button>+ <button type="button" data-n="5" class="on" aria-pressed="true">Five</button>+ <button type="button" data-n="10" aria-pressed="false">Ten</button>+ </div>+ <p class="hb-hint">Five is the usual scale; ten gives finer steps.</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 star rating to [the review or feedback form]. As the pointer moves along the row, fill the stars up to the one under it so the choice shows before it is made, and bring back the chosen rating when the pointer leaves. Clicking or tapping a star chooses that rating and makes the star pop: it grows briefly, dips a little below its size and settles. On touch screens, a finger dragged along the row previews too. Make the row one control that the arrow keys can change, and show the rating as a number as well, so it never depends on color alone. If the visitor has reduced motion turned on, fill the stars without the pop. 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>Reviews</li><li>Surveys</li><li>Feedback forms</li><li>Media ratings</li></ul>+ <h3>Avoid on</h3>+ <ul class="hb-tags hb-avoid"><li>Precise scores</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 STAR='M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14l-5-4.87 6.91-1.01L12 2z';-const rate=document.getElementById('rate'),valEl=document.getElementById('val'),maxEl=document.getElementById('max'),stateEl=document.getElementById('state-v');-const reduce=window.matchMedia('(prefers-reduced-motion: reduce)').matches;-let count=5,rating=3,step=1,pop=1.35;-const svg=c=>`<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="${STAR}"/></svg>`;+ const STAR='M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14l-5-4.87 6.91-1.01L12 2z';+ const stage=document.querySelector('.stage');+ const rate=document.getElementById('rate'),valEl=document.getElementById('val'),maxEl=document.getElementById('max');+ const slowTog=document.getElementById('slow-tog');+ const vars=document.documentElement.style;+ const starSvg=`<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="${STAR}"/></svg>`;+ let count=5,rating=3,step=1,runTimers=[];-function build(){- rate.innerHTML='';- for(let i=0;i<count;i++){- const s=document.createElement('span');- s.className='star';s.dataset.i=i;- s.innerHTML=svg()+'<span class="half">'+svg()+'</span>';- rate.appendChild(s);+ function build(){+ rate.innerHTML='';+ for(let i=0;i<count;i++){+ const s=document.createElement('span');+ s.className='star';s.dataset.i=i;+ s.innerHTML=starSvg+'<span class="half">'+starSvg+'</span>';+ rate.appendChild(s);+ }+ rate.classList.toggle('many',count>5);+ rate.setAttribute('aria-valuemax',count);+ maxEl.textContent=count;+ if(rating>count)rating=count;+ commit(false);}- rate.setAttribute('aria-valuemax',count);- maxEl.textContent=count;- if(rating>count)rating=count;- render(rating);commit(false);-}-// Paint stars up to value v (0..count), supporting halves.-function render(v){- rate.querySelectorAll('.star').forEach((s,i)=>{- s.classList.toggle('full',v>=i+1);- s.classList.toggle('half-on',v>=i+0.5&&v<i+1);+ // Paint stars up to value v (0..count), supporting halves.+ function render(v){+ rate.querySelectorAll('.star').forEach((s,i)=>{+ s.classList.toggle('full',v>=i+1);+ s.classList.toggle('half-on',v>=i+0.5&&v<i+1);+ });+ }+ // The chosen rating: stars, number and what a screen reader hears. The visitor's click, tap and keys and Show me all end here.+ function commit(doPop){+ render(rating);+ valEl.textContent=rating.toFixed(1);+ rate.setAttribute('aria-valuenow',rating);+ rate.setAttribute('aria-valuetext',rating+' out of '+count);+ if(doPop&&rating>0){+ const star=rate.querySelector('.star[data-i="'+(Math.ceil(rating)-1)+'"]');+ if(star){star.classList.remove('pop');void star.offsetWidth;star.classList.add('pop')}+ }+ }+ // Map a pointer position within a star to a rating value.+ function valueAt(star,clientX){+ const i=+star.dataset.i,r=star.getBoundingClientRect();+ if(step===0.5&&clientX-r.left<r.width/2)return i+0.5;+ return i+1;+ }+ // The star under the pointer. A finger keeps hitting the star it first touched (touch pointers stay captured by it),+ // so a drag along the row has to ask what is at its position.+ function starAt(e){+ const el=document.elementFromPoint(e.clientX,e.clientY);+ return el&&el.closest('.star');+ }+ rate.addEventListener('pointermove',e=>{+ const star=starAt(e);if(star)render(valueAt(star,e.clientX));+ });+ rate.addEventListener('pointerleave',()=>render(rating));+ rate.addEventListener('pointerdown',e=>{+ const star=starAt(e);if(!star)return;+ rating=valueAt(star,e.clientX);rate.focus();commit(true);+ });+ rate.addEventListener('keydown',e=>{+ if(e.altKey||e.ctrlKey||e.metaKey)return;+ let handled=true;+ if(e.key==='ArrowRight'||e.key==='ArrowUp')rating=Math.min(count,rating+step);+ else if(e.key==='ArrowLeft'||e.key==='ArrowDown')rating=Math.max(0,rating-step);+ else if(e.key==='Home')rating=0;+ else if(e.key==='End')rating=count;+ else handled=false;+ if(handled){e.preventDefault();commit(true)}});-}-function commit(doPop){- render(rating);- valEl.textContent=rating.toFixed(1);- stateEl.textContent=rating.toFixed(1);- rate.setAttribute('aria-valuenow',rating);- rate.setAttribute('aria-valuetext',rating+' out of '+count);- if(doPop&&!reduce&&rating>0){- const star=rate.querySelector('.star[data-i="'+(Math.ceil(rating)-1)+'"]');- if(star){star.classList.remove('pop');void star.offsetWidth;star.classList.add('pop');}++ // Show me: one run at a time. Every timer of the run goes through later(); stopRun() cancels the whole run.+ // later(fn, move, hold) waits move*(slow?3:1) + hold ms: Slow motion triples the movement, never the hold.+ function later(fn,move,hold){+ const id=setTimeout(()=>{runTimers=runTimers.filter(t=>t!==id);fn()},move*(slowTog.checked?3:1)+(hold||0));+ runTimers.push(id);+ }+ function running(){return runTimers.length>0}+ function stopRun(){runTimers.forEach(clearTimeout);runTimers=[];render(rating)}+ // A run starts at the resting rating of 3, put there without animating: the fill transitions are turned off, the stars are+ // filled, a reflow makes it stick, and the transitions come back.+ function toRest(){+ const stars=[...rate.querySelectorAll('.star')];+ stars.forEach(s=>{s.style.setProperty('transition','none','important');s.classList.remove('pop')});+ rating=3;commit(false);+ void rate.offsetWidth;+ stars.forEach(s=>s.style.removeProperty('transition'));+ }+ // An invisible pointer sweeps along the row, one star every 120ms, and comes back one star; that star is chosen and pops+ // (420ms), and 1200ms later the rating goes back to 3. The run never moves the focus. Each step reads the current settings.+ document.getElementById('btn-demo').addEventListener('click',()=>{+ stopRun();+ toRest();+ const n=count;+ for(let i=0;i<n;i++)later(()=>render(i+1),i*120);+ later(()=>render(count-1),n*120);+ later(()=>{rating=step===0.5?count-0.5:count-1;commit(true)},(n+1)*120);+ later(()=>{rating=3;commit(false)},(n+1)*120+420,1200);+ });+ // The visitor pressed, typed, scrolled or touched inside the stage, or moved a pointer over it (the row's own hover preview+ // would fight the sweep): the run stops and the stars show the chosen rating.+ document.addEventListener('hb:input',()=>{if(running())stopRun()});+ stage.addEventListener('pointermove',e=>{if(e.isTrusted&&running())stopRun()},true);++ 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);+ }));}-}-// Map a pointer position within a star to a rating value.-function valueAt(star,clientX){- const i=+star.dataset.i,r=star.getBoundingClientRect();- if(step===0.5&&clientX-r.left<r.width/2)return i+0.5;- return i+1;-}-rate.addEventListener('pointermove',e=>{- const star=e.target.closest('.star');if(star)render(valueAt(star,e.clientX));-});-rate.addEventListener('pointerleave',()=>render(rating));-rate.addEventListener('pointerdown',e=>{- const star=e.target.closest('.star');if(!star)return;- rating=valueAt(star,e.clientX);rate.focus();commit(true);-});-rate.addEventListener('keydown',e=>{- let handled=true;- if(e.key==='ArrowRight'||e.key==='ArrowUp')rating=Math.min(count,rating+step);- else if(e.key==='ArrowLeft'||e.key==='ArrowDown')rating=Math.max(0,rating-step);- else if(e.key==='Home')rating=0;- else if(e.key==='End')rating=count;- else handled=false;- if(handled){e.preventDefault();commit(true);}-});-// Controls-document.getElementById('cnt-sl').addEventListener('input',e=>{- count=+e.target.value;document.getElementById('cnt-v').textContent=count;build();-});-document.getElementById('pop-sl').addEventListener('input',e=>{- pop=+e.target.value;document.documentElement.style.setProperty('--pop',pop);- document.getElementById('pop-v').textContent=pop.toFixed(2)+'×';-});-document.getElementById('dur-sl').addEventListener('input',e=>{- document.documentElement.style.setProperty('--fill-dur',e.target.value+'ms');- document.getElementById('dur-v').textContent=e.target.value+'ms';-});-document.getElementById('half').addEventListener('click',e=>{- const on=e.currentTarget.getAttribute('aria-checked')!=='true';- e.currentTarget.setAttribute('aria-checked',on);- step=on?0.5:1;- if(!on)rating=Math.round(rating);- commit(false);-});-build();+ choices('pop-seg',d=>vars.setProperty('--pop',d.pop));+ choices('speed-seg',d=>vars.setProperty('--fill-dur',d.ms+'ms'));+ choices('count-seg',d=>{count=+d.n;build()});+ document.getElementById('half-tog').addEventListener('change',e=>{+ step=e.target.checked?0.5:1;+ if(!e.target.checked)rating=Math.round(rating);+ commit(false);+ });+ build();</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 7b51f177..d8da2e02 100644--- a/index.html+++ b/index.html@@ -976,7 +976,7 @@ const CATS=[['hamburger-menu-toggle','Hamburger Menu Toggle','Three lines turn into an X as the menu opens. Best for mobile menus.'],['theme-toggle-morph','Theme Toggle Morph','A sun turns into a moon as the colors switch to dark. Best for theme buttons.'],['copy-to-clipboard','Copy to Clipboard','Copy turns into a tick and Copied, then changes back. Best for codes and links.'],- ['star-rating','Star Rating','Stars fill toward the pointer and pop on commit — an accessible five-star control.'],+ ['star-rating','Star Rating','Stars fill up to your pointer and pop when you choose. Best for reviews.'],['toast-notification','Toast Notification','Notification cards slide in from a corner, stack, and auto-dismiss with a progress bar — swipe or click to dismiss.'],['segmented-control','Segmented Control','A highlighted pill slides beneath the selected segment as the active label crossfades — the iOS control.'],['pull-to-refresh','Pull to Refresh','Dragging past the top reveals a spinner that fills with distance; release past the threshold to refresh.'],