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>Rolling Numbers — Animation Handbook</title>
<meta name="description" content="Each digit rolls to its new value, like a mileage counter. Best for prices.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/rolling-numbers/">
<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="Rolling Numbers — Animation Handbook">
<meta property="og:description" content="Each digit rolls to its new value, like a mileage counter. Best for prices.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/rolling-numbers/">
<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="Rolling Numbers — Animation Handbook">
<meta name="twitter:description" content="Each digit rolls to its new value, like a mileage counter. Best for prices.">
<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": "Rolling Numbers", "name": "Rolling Numbers", "description": "Each digit rolls to its new value, like a mileage counter. Best for prices.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/rolling-numbers/", "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, Rolling Numbers"}</script>
<style>
:root{--ui-accent:#ff9d5c;--ui-text:#f4f4f2;--ui-muted:#8a8a92;--card:#16161a}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
.stage{display:flex;align-items:center;justify-content:center;padding:16px;background:#0b0b0d}
.counter{display:flex;flex-direction:column;align-items:center;gap:14px;width:100%;max-width:340px;padding:clamp(18px,4vw,26px) 16px;border:1px solid rgba(255,255,255,.1);border-radius:14px;background:var(--card)}
.cap{font-size:14px;color:var(--ui-muted)}
.num{display:flex;align-items:center;font-size:clamp(46px,13vw,76px);font-weight:800;line-height:1.1;color:var(--ui-text)}
/* Each digit is a window one line tall over a column of the digits 0 to 9 and 0 again; the column slides, the window clips it,
and the soft top and bottom edges make a digit rolling through look like a turning wheel */
.wheel{display:block;width:.64em;height:1.1em;overflow:hidden;text-align:center;-webkit-mask-image:linear-gradient(transparent,#000 16%,#000 84%,transparent);mask-image:linear-gradient(transparent,#000 16%,#000 84%,transparent)}
.strip{display:block;will-change:transform}
.strip::before{content:"0\A 1\A 2\A 3\A 4\A 5\A 6\A 7\A 8\A 9\A 0";white-space:pre}
.sep{width:.3em;text-align:center}
/* Reduced motion: a changed digit is swapped at once and fades in */
.wheel.fade{animation:rn-fade .35s ease-out both}
@keyframes rn-fade{from{opacity:.1}to{opacity:1}}
.btns{display:flex;gap:8px}
.btn{min-width:64px;height:44px;padding:0 16px;border:1px solid rgba(255,255,255,.16);border-radius:8px;background:#1d1d23;color:var(--ui-text);font:600 16px/1 var(--hb-font);cursor:pointer}
@media(hover:hover){.btn:hover{border-color:var(--ui-accent)}}
.sr-only{position:absolute;width:1px;height:1px;margin:-1px;overflow:hidden;clip-path:inset(50%);white-space:nowrap}
/* See inside and Compare: their parts stay out of sight in Normal; their words sit on a dark backing */
.xi,.cm,.cp{display:none}
.lbl{padding:3px 6px;border-radius:5px;background:rgba(11,11,13,.9);color:#f4f4f2;font:600 13px/1.25 var(--hb-font);text-wrap:balance}
.gh{display:none;position:absolute;top:0;width:.64em;height:1.1em;border:1.5px dashed rgba(244,244,242,.55);border-radius:6px;pointer-events:none}
.ar{position:relative;flex:none;align-self:stretch;width:12px}
.ar::before{content:"";position:absolute;left:5px;top:5px;bottom:5px;width:2px;background:var(--ui-accent)}
.ar i{position:absolute;left:0;border:6px solid transparent}
.au{top:0;border-top-width:0!important;border-bottom:7px solid var(--ui-accent)!important}
.ad{bottom:0;border-bottom-width:0!important;border-top:7px solid var(--ui-accent)!important}
.xd[data-d="up"] .ad,.xd[data-d="down"] .au{display:none}
/* See inside: the card widens to give the words room and each window grows to show part of the digit above and below (--ext,
set by the script from the room the box has); the column outside the real one-line window is dimmed, not hidden, and that
window is outlined. The column gets a 9 before its first 0 and a 1 after its last, so every digit has both neighbors. */
.stage[data-hb-mode="inside"]{padding:clamp(4px,1.6vw,16px)}
.stage[data-hb-mode="inside"] .counter{display:grid;grid-template-columns:1fr auto 1fr;align-items:center;gap:10px clamp(4px,1.5vw,8px);max-width:640px;padding:10px clamp(4px,2vw,12px)}
.stage[data-hb-mode="inside"] .cap{display:none}
.stage[data-hb-mode="inside"] .xi{display:block}
.stage[data-hb-mode="inside"] .xd{display:flex;grid-area:2/3;align-self:stretch;align-items:center;gap:4px}
.stage[data-hb-mode="inside"] .num{grid-area:2/2;font-size:clamp(32px,10vw,76px)}
.stage[data-hb-mode="inside"] .xt{grid-area:1/1/2/4;justify-self:center;text-align:center}
.stage[data-hb-mode="inside"] .xw{grid-area:2/1;justify-self:end;text-align:right}
.stage[data-hb-mode="inside"] .xs{grid-area:3/1/4/4;justify-self:center;text-align:center}
.stage[data-hb-mode="inside"] .btns{grid-area:4/1/5/4;justify-self:center}
.stage[data-hb-mode="inside"] .wheel{position:relative;height:calc(1.1em + 2*var(--ext,0px));
--m:linear-gradient(transparent,rgba(0,0,0,.45) calc(var(--ext,0px)*.35),rgba(0,0,0,.45) var(--ext,0px),#000 0,#000 calc(var(--ext,0px) + 1.1em),rgba(0,0,0,.45) 0,rgba(0,0,0,.45) calc(1.1em + var(--ext,0px)*1.65),transparent);
-webkit-mask-image:var(--m);mask-image:var(--m)}
.stage[data-hb-mode="inside"] .wheel::after{content:"";position:absolute;left:0;right:0;top:var(--ext,0px);height:1.1em;border:1.5px dashed var(--ui-accent);border-radius:6px}
.stage[data-hb-mode="inside"] .strip{position:relative;height:12.1em;margin-top:var(--ext,0px)}
.stage[data-hb-mode="inside"] .strip::before{content:"9\A 0\A 1\A 2\A 3\A 4\A 5\A 6\A 7\A 8\A 9\A 0\A 1";position:absolute;inset:-1.1em 0 auto}
/* Compare: two numbers side by side, the real buttons in one row under both. On the left a copy whose windows are slid sideways
to where the font's own digit widths set each digit; on the right the real number, every window the same width. Between
presses both are held part way through a change, with a dashed box where each digit stood before it. */
.stage[data-hb-mode="compare"]{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));grid-template-rows:auto minmax(0,1fr) auto auto;gap:8px clamp(8px,2vw,16px);padding:12px clamp(8px,2vw,16px)}
.stage[data-hb-mode="compare"] .counter{display:contents}
.stage[data-hb-mode="compare"] .cap{display:none}
.stage[data-hb-mode="compare"] .cm{display:block}
.stage[data-hb-mode="compare"] .num{position:relative;z-index:1;justify-self:center;align-self:center;font-size:clamp(32px,12.5vw,88px)}
.stage[data-hb-mode="compare"] .num:not(.cp){grid-area:2/2}
.stage[data-hb-mode="compare"] .cp{display:flex;grid-area:2/1}
.stage[data-hb-mode="compare"] .pane{grid-row:2;align-self:stretch;z-index:0;border:1px solid rgba(255,255,255,.1);border-radius:14px;background:var(--card)}
.stage[data-hb-mode="compare"] .p1{grid-column:1}
.stage[data-hb-mode="compare"] .p2{grid-column:2}
.stage[data-hb-mode="compare"] .nm1{grid-area:1/1;justify-self:center}
.stage[data-hb-mode="compare"] .nm2{grid-area:1/2;justify-self:center}
.stage[data-hb-mode="compare"] .pc{grid-area:3/1/4/3;justify-self:center;text-align:center;visibility:hidden}
.stage[data-hb-mode="compare"] .btns{grid-area:4/1/5/3;justify-self:center}
.stage[data-hb-mode="compare"].posed .pc{visibility:visible}
.stage[data-hb-mode="compare"].posed .gh{display:block}
@media(max-width:600px){.lbl{font:600 12px/1.25 var(--hb-font);padding:2px 5px}}
@media(max-height:640px){.stage[data-hb-mode="inside"] .num{font-size:clamp(32px,10vw,56px)}.stage[data-hb-mode="compare"] .num{font-size:clamp(32px,11vw,64px)}}
</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="../drag-to-reorder/" rel="prev" aria-label="Previous: Drag to Reorder"><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>Drag to Reorder</span></a><a href="../typing-indicator/" rel="next" aria-label="Next: Typing Indicator"><span><span class="hb-dir">Next: </span>Typing Indicator</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.34 · Micro-Interactions</p>
<h1>Rolling Numbers</h1>
<p class="hb-lede">Each digit rolls to its new value, like a mileage counter. Best for prices.</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>Click or tap −1, +1 or Random to change the number (Reset brings it back), 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 hb-dots">
<div class="counter">
<p class="cap" id="cap">Visitors today</p>
<div class="num" aria-hidden="true"><span class="wheel"><span class="strip"></span></span><span class="sep">,</span><span class="wheel"><span class="strip"></span></span><span class="wheel"><span class="strip"></span></span><span class="wheel"><span class="strip"></span></span></div>
<p class="sr-only" id="value" aria-live="polite">2,048</p>
<p class="lbl xi xt" aria-hidden="true" data-hb-caption>A column of 0 to 9 slides behind each window</p>
<p class="lbl xi xw" aria-hidden="true" data-hb-caption>Window: one digit shows</p>
<div class="xi xd" data-d="short" aria-hidden="true"><span class="ar"><i class="au"></i><i class="ad"></i></span><p class="lbl xl" id="l-dir" data-hb-caption>Rolls the shorter way</p></div>
<p class="lbl xi xs" id="l-speed" aria-hidden="true" data-hb-caption>Right digit first, each roll about half a second</p>
<div class="btns">
<button class="btn" type="button" data-add="-1" aria-label="Subtract 1">−1</button>
<button class="btn" type="button" data-add="1" aria-label="Add 1">+1</button>
<button class="btn" type="button" data-add="0">Random</button>
</div>
</div>
<div class="pane cm p1" aria-hidden="true"></div>
<div class="pane cm p2" aria-hidden="true"></div>
<p class="lbl cm nm1" aria-hidden="true" data-hb-caption>Shifts sideways</p>
<p class="lbl cm nm2" aria-hidden="true" data-hb-caption>Equal widths</p>
<p class="lbl cm pc" aria-hidden="true" data-hb-caption>Dashed boxes: where each digit stood before the change</p>
</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>
<button class="hb-play" type="button" id="btn-reset" data-hb-reset><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/></svg>Reset</button>
<label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="slow-tog" data-hb-slowmo autocomplete="off"><span>Slow motion</span></label>
</div>
<div class="hb-mode-notes" aria-live="polite">
<p data-hb-for="inside" hidden>Inside, each digit is a window over a column of 0 to 9: a change slides the column, so the new digit rolls into view. Press −1, +1 or Random, or Show me.</p>
<p data-hb-for="compare" hidden>Shifts sideways: each digit takes only its own width, so the number jumps sideways; Equal widths: every digit has the same room, so the number stays in place. Press −1, +1, Random or 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="speed-lbl">Roll speed</p>
<div class="seg" id="speed-seg" role="group" aria-labelledby="speed-lbl">
<button type="button" data-ms="900" aria-pressed="false">Slow</button>
<button type="button" data-ms="550" class="on" aria-pressed="true">Normal</button>
<button type="button" data-ms="300" aria-pressed="false">Fast</button>
</div>
<p class="hb-hint">How long a digit takes to roll to its new value.</p>
</div>
<div class="hb-setting">
<p class="hb-setting-name" id="dir-lbl">Direction</p>
<div class="seg" id="dir-seg" role="group" aria-labelledby="dir-lbl">
<button type="button" data-dir="up" aria-pressed="false">Up</button>
<button type="button" data-dir="down" aria-pressed="false">Down</button>
<button type="button" data-dir="short" class="on" aria-pressed="true">Shortest</button>
</div>
<p class="hb-hint">Up turns every digit forward like a mileage counter; shortest takes the nearer way round.</p>
</div>
<div class="hb-setting">
<label class="hb-switch-row"><span>Digits one after another</span><input class="hb-switch" type="checkbox" role="switch" id="stagger-tog" checked autocomplete="off"></label>
<p class="hb-hint">Each digit starts a moment after the one to its right.</p>
</div>
</div>
</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 rolling numbers to [the number that changes, such as a counter, score or price]. Show each digit in a small window over a column of the digits 0 to 9, and when the value changes, slide each column so its new digit rolls into view, like the wheels of a mileage counter; digits that stay the same do not move, and separators stay still. Give every digit the same width so the number never shifts sideways, and only slide the columns, never resize anything, so it stays smooth. Let screen readers hear the plain number once. If the visitor has reduced motion turned on, swap the digits with a short fade instead of rolling. 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>Counters</li><li>Prices</li><li>Scores</li><li>Dashboards</li></ul>
<h3>Avoid on</h3>
<ul class="hb-tags hb-avoid"><li>Numbers that change every second</li><li>Long decimals</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 $=id=>document.getElementById(id);
const slowTog=$('slow-tog'),valueEl=$('value'),reduceMq=matchMedia('(prefers-reduced-motion:reduce)');
const START=2048,STAGGER=70,POSE_MS=400,EQ=[0,.94,1.58,2.22];
let value=START,dur=550,dir='short',oneByOne=true,raf=0,base=START,mode='normal',posed=false,dirty=false,runDone=false,poseT=0,adv=null;
const numEl=document.querySelector('.num'),counter=numEl.parentElement,stage=counter.parentElement;
// Compare's left half: a copy of the number whose windows keep their size and are only slid sideways (see lay). Each window of
// the number also gets a dashed box, for the place it stood before a change; the copy is made from it, then hidden from everyone.
for(let i=0;i<4;i++){const g=document.createElement('span');g.className='gh';g.style.left=EQ[i]+'em';numEl.append(g)}
const cp=numEl.cloneNode(true),cws=[...cp.querySelectorAll('.wheel')],csep=cp.querySelector('.sep'),cgh=[...cp.querySelectorAll('.gh')];
cp.classList.add('cp');cp.inert=true;cp.setAttribute('aria-hidden','true');stage.append(cp);
// Each wheel keeps a position that only grows or shrinks: 7 shows a 7, and so do 17 and -3. A roll moves it from where it is
// drawn now (mid-roll too) to its new target, so a roll that starts before the last one ended carries on without a jump.
const wheels=[...numEl.querySelectorAll('.wheel')].map((el,i)=>({el,strip:el.firstChild,cel:cws[i],cs:cws[i].firstChild,pos:0,from:0,to:0,t0:0,ms:1}));
const digitsOf=v=>String(v).padStart(4,'0').split('').map(Number);
function draw(w){w.cs.style.transform=w.strip.style.transform=`translateY(${(-(((w.pos%10)+10)%10)*100/11).toFixed(3)}%)`}
// How many steps a wheel turns from the digit it shows to the one it should show, the way Direction says
function turn(shown,target){let s=(target-shown+10)%10;if(dir==='down')s-=10;else if(dir==='short'&&s>5)s-=10;return s}
// The frame loop: every moving wheel is placed by the time passed since its start, eased out, so it runs at the same speed at any
// frame rate. It stops when every wheel has reached its target.
function frame(now){
let moving=false;
for(const w of wheels){
if(w.pos===w.to)continue;
const k=Math.min(1,Math.max(0,(now-w.t0)/w.ms));
w.pos=k<1?w.from+(w.to-w.from)*(1-Math.pow(1-k,3)):w.to;
draw(w);
if(k<1)moving=true;
}
raf=moving?requestAnimationFrame(frame):0;
if(!raf){settle(900,runDone);runDone=false}
}
// Shows v: every digit that changes rolls the way Direction says, the ones on the left a little after the ones on their right when
// Digits one after another is on; a wheel still rolling turns toward its new digit at once. Slow motion makes the rolls and the steps
// between them three times as long. Under reduced motion the digit is swapped at once and fades in.
function show(v,quiet){
clearTimeout(poseT);lift();dirty=true;if(!quiet)runDone=false;
v=((v%10000)+10000)%10000;
const target=digitsOf(v),slow=slowTog.checked?3:1,now=performance.now();
let n=0;
for(let i=wheels.length-1;i>=0;i--){
const w=wheels[i],shown=((Math.round(w.to)%10)+10)%10;
if(target[i]===shown)continue;
const steps=turn(shown,target[i]);
const delay=oneByOne&&w.pos===w.to?n*STAGGER*slow:0;n++;
w.from=w.pos;w.to+=steps;w.t0=now+delay;w.ms=dur*slow;
if(reduceMq.matches){
w.pos=w.to;draw(w);
for(const el of [w.el,w.cel]){el.classList.remove('fade');void el.offsetWidth;el.style.animationDelay=delay+'ms';el.classList.add('fade')}
}
}
value=v;lay();
if(!quiet)valueEl.textContent=v.toLocaleString('en-US');
if(!raf&&!reduceMq.matches)raf=requestAnimationFrame(frame);
if(reduceMq.matches){settle(900,runDone);runDone=false}
}
wheels.forEach((w,i)=>{w.pos=w.to=digitsOf(START)[i];draw(w)});
document.querySelectorAll('.btn').forEach(b=>b.addEventListener('click',()=>{
const add=+b.dataset.add;
let next=value+add;
if(!add)do{next=1000+Math.floor(Math.random()*9000)}while(next===value);
show(next);
}));
// 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.
let runTimers=[];
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}
// A run stopped partway leaves its number on the stage, so screen readers are given that number too (say), unless the press that
// stopped it is on −1, +1 or Random, whose own number follows. This listener is added before the shared script's, so it runs first.
function stopRun(say){runDone=false;if(!runTimers.length)return;runTimers.forEach(clearTimeout);runTimers=[];if(say)valueEl.textContent=value.toLocaleString('en-US')}
let onButton=false;
['pointerdown','keydown','click','wheel','touchstart'].forEach(t=>document.querySelector('.stage').addEventListener(t,e=>{
onButton=!!e.target.closest('.btn')&&(t==='pointerdown'||t==='click'||e.key==='Enter'||e.key===' ')},true));
// The number goes up by one, then jumps to a number whose every digit is different, then rolls back to where it started. Each
// step waits for the roll before it (one or two digits, then all four); the holds grow when the rolls are short, so the run lasts
// two to four seconds at any Roll speed. It says nothing aloud.
// A Show me pressed while one is still running starts from the number that run started from, so it too ends where they began
// (screen readers keep that number, so the stopped run's number is not given to them).
$('btn-demo').addEventListener('click',()=>{
const v=running()?base:value;base=v;
stopRun(false);lift();
// In Compare the middle stop trades 1s for wide digits (and back), the change where the left half's digits jump most.
const d=digitsOf(v),far=d.map((x,i)=>mode==='compare'?(x===1?8:1):(x+3+2*i)%10);
if(!far[0])far[0]=1;
const stagger=oneByOne?STAGGER:0,roll=dur+3*stagger,pad=Math.max(0,2100-(dur+stagger+2*roll+700));
if(reduceMq.matches){later(()=>show(v+1,true),0,0);later(()=>show(+far.join(''),true),0,900);later(()=>{runDone=true;show(v,true)},0,1800);return}
later(()=>show(v+1,true),0,150);
later(()=>show(+far.join(''),true),dur+stagger,350+pad/2);
later(()=>{runDone=true;show(v,true)},dur+stagger+roll,700+pad);
});
$('btn-reset').addEventListener('click',()=>{stopRun(false);show(START)});
// The visitor pressed, typed or touched inside the stage: the run stops and the number stays as it is.
document.addEventListener('hb:input',()=>{if(running()){stopRun(!onButton);if(!onButton)settle(900)}});
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);changed();
}));
}
choices('speed-seg',d=>{dur=+d.ms});
choices('dir-seg',d=>{dir=d.dir});
$('stagger-tog').addEventListener('change',e=>{oneByOne=e.target.checked;changed()});
slowTog.addEventListener('change',refresh);
// ---- See inside and Compare ----
// The advance widths (in em) of the digits 0 to 9 and the comma in the page's own font, measured once the font is there
function measure(){
const m=document.createElement('span'),fs=parseFloat(getComputedStyle(numEl).fontSize);
m.style.cssText='position:absolute;visibility:hidden;white-space:pre';numEl.append(m);
adv=[...'0123456789,'].map(c=>{m.textContent=c;return m.getBoundingClientRect().width/fs});
m.remove();
}
// Where a number set in those widths puts its four digits and its comma, as a shift in em from the equal windows (.64 each, the comma .3)
function shifts(v){
const d=digitsOf(v),a=[adv[d[0]],adv[10],adv[d[1]],adv[d[2]],adv[d[3]]],mid=[.32,.79,1.26,1.9,2.54];
let x=(2.86-a.reduce((s,n)=>s+n,0))/2;
return a.map((n,i)=>{x+=n;return x-n/2-mid[i]});
}
// The copy's windows take the shifts of the number it shows (of the held number when posed); its dashed boxes the shifts of the
// number shown now. Nothing glides: a shift is written once and the number jumps.
function lay(){
if(!adv)return;
const o=shifts(posed?poseNo():value),b=shifts(value);
[cws[0],csep,cws[1],cws[2],cws[3]].forEach((el,i)=>{el.style.transform=`translateX(${o[i].toFixed(3)}em)`});
cgh.forEach((g,i)=>{g.style.left=(EQ[i]+b[[0,2,3,4][i]]).toFixed(3)+'em'});
}
// Compare's resting moment: the change from the shown number to 1,111 (to 2,048 when that is shown) part way in, 0.4 seconds, each
// column where Roll speed, Direction and the one-after-another delay put it by then (by progress, so Slow motion keeps it), set
// on the drawn place only. A press, a Show me or leaving Compare puts the columns back on their digits first.
const poseNo=()=>value===1111?START:1111;
function pose(){
posed=true;
const tg=digitsOf(poseNo());let n=0;
for(let i=wheels.length-1;i>=0;i--){
const w=wheels[i],shown=((Math.round(w.to)%10)+10)%10;
w.pos=w.to;
if(tg[i]!==shown){
const t=POSE_MS-(oneByOne?n*STAGGER:0);n++;
if(t>0)w.pos+=turn(shown,tg[i])*(1-Math.pow(1-Math.min(1,t/dur),3));
}
draw(w);
}
stage.classList.add('posed');lay();
}
function lift(){
if(!posed)return;
posed=false;stage.classList.remove('posed');
wheels.forEach(w=>{w.pos=w.to;draw(w)});lay();
}
// The browser keeps the way the columns' layers were drawn while they moved (or while a screenshot of the whole page was taken), a
// hair off where a column's rest is not a whole pixel, so at rest in Normal the number is built again, within one frame
function renew(){
if(!dirty||mode!=='normal'||raf||running())return;
dirty=false;numEl.querySelectorAll('.fade').forEach(el=>el.classList.remove('fade'));numEl.style.display='none';void numEl.offsetWidth;numEl.style.display='';
}
// Nothing rolling and no run under way: after a moment that shows the number the last change reached, Compare rests posed (when
// andPose: after a mode press and the end of a Show me run, not after the visitor's own press, which it would undo on screen)
function settle(wait,andPose){
clearTimeout(poseT);
const go=()=>{if(andPose&&mode==='compare'&&!raf&&!running())pose();renew()};
if(wait)poseT=setTimeout(go,wait);else go();
}
const noteIn=document.querySelector('[data-hb-for="inside"]'),NOTE_IN=noteIn.textContent,lDir=$('l-dir'),lSpeed=$('l-speed'),xd=document.querySelector('.xd');
const NOTE_STILL='Inside, shown still: each digit is a window over a column of 0 to 9, and a change would slide the column; with reduced motion the new digit fades in instead.';
const say=ms=>ms<=400?'about a third of a second':ms<=700?'about half a second':ms<=1000?'almost a second':ms<=1800?'about a second and a half':ms<=2200?'about two seconds':'about three seconds';
const put=(el,t)=>{if(el.textContent!==t)el.textContent=t};
// See inside: the words and arrow for the settings, then the room: the windows grow until the card fills the box, to half a digit
function refresh(){
const still=reduceMq.matches;
put(noteIn,still?NOTE_STILL:NOTE_IN);
if(mode!=='inside')return;
xd.dataset.d=dir;
put(lDir,(still?'Would roll ':'Rolls ')+{up:'up',down:'down',short:'the shorter way'}[dir]);
put(lSpeed,(oneByOne?'Right digit first, ':'All digits together, ')+(still?'each fades in':'each roll '+say(dur*(slowTog.checked?3:1))));
const fs=parseFloat(getComputedStyle(numEl).fontSize),cs=getComputedStyle(stage);
counter.style.setProperty('--ext','0px');
const room=stage.clientHeight-parseFloat(cs.paddingTop)-parseFloat(cs.paddingBottom)-counter.offsetHeight;
counter.style.setProperty('--ext',Math.max(0,Math.min(.55*fs,room/2-1)).toFixed(1)+'px');
}
const idle=()=>mode==='compare'&&!raf&&!running();
function changed(){refresh();if(idle())pose()}
document.addEventListener('hb:mode',e=>{
const was=mode;mode=e.detail.mode;cws.forEach(el=>el.classList.remove('fade'));
if(was==='compare'&&mode!=='compare')lift();
refresh();settle(0,true);
});
new ResizeObserver(()=>{if(mode==='inside')refresh();if(idle())pose()}).observe(stage);
reduceMq.addEventListener('change',refresh);
const ready=()=>{measure();lay();if(posed)pose();refresh()};
ready();document.fonts.ready.then(ready);document.fonts.addEventListener('loadingdone',ready);
</script>
</body>
</html>