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>Highlighter Sweep — Animation Handbook</title>
<meta name="description" content="A marker sweeps in behind the key words, one after another. Best for key points.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/05-text-typography/highlighter-sweep/">
<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="Highlighter Sweep — Animation Handbook">
<meta property="og:description" content="A marker sweeps in behind the key words, one after another. Best for key points.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/05-text-typography/highlighter-sweep/">
<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="Highlighter Sweep — Animation Handbook">
<meta name="twitter:description" content="A marker sweeps in behind the key words, one after another. Best for key points.">
<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": "Highlighter Sweep", "name": "Highlighter Sweep", "description": "A marker sweeps in behind the key words, one after another. Best for key points.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/05-text-typography/highlighter-sweep/", "inLanguage": "en", "articleSection": "Text & Typography", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Text & Typography, Highlighter Sweep"}</script>
<style>
:root{--ui-accent:#ff6f8b;--marker:#ffe066;--top:.12em}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
.stage{position:relative;display:flex;align-items:center;justify-content:center;padding:clamp(16px,3vw,24px);background:#0b0b0d}
/* A sheet of paper: isolated, so each band can sit behind the words (z-index -1) without dropping behind the sheet */
.paper{position:relative;isolation:isolate;width:min(100%,640px);padding:clamp(16px,3vw,32px);border-radius:12px;background:#f7f4ec}
.para{font-size:clamp(18px,2.6vw,28px);font-weight:500;line-height:1.5;letter-spacing:-.005em;color:#1d1d22}
mark{background:none;color:inherit}
/* Each word of a key phrase, with the space after it, carries its own band: so a phrase that wraps is marked line by line */
.w{position:relative}
.w::before{content:'';position:absolute;z-index:-1;left:-.08em;right:-.08em;top:var(--top);bottom:-.06em;
background:var(--marker);transform:scaleX(0);transform-origin:left center;transition:transform var(--t,600ms) var(--e,linear) var(--d,0ms)}
/* Only the ends of a phrase are rounded, so its words read as one stroke of the pen */
.w:first-child::before{border-radius:.25em 0 0 .3em}
.w:last-child::before{border-radius:0 .35em .2em 0}
.w:only-child::before{border-radius:.25em .35em .2em .3em}
.para.on .w::before{transform:scaleX(1)}
.para.out .w::before{opacity:0;transition:opacity .35s linear}
.para.snap .w::before{transition:none!important}
/* Reduced motion: the bands are full width from the start and fade in, phrase by phrase */
@media(prefers-reduced-motion:reduce){
.w::before{transform:none;opacity:0;transition:opacity .4s linear var(--d,0ms)}
.para.on .w::before{opacity:1}
}
/* See inside and Compare. Their words sit on dark backings; the numbers are marks in dark dots */
.twin,.cap,.num,.head2{display:none}
.cap{position:absolute;z-index:3;max-width:calc(100% - 24px);padding:3px 7px;border-radius:5px;background:rgba(11,11,13,.9);color:#f4f4f2;font:600 13px/1.2 var(--hb-font)}
.num{position:absolute;z-index:2;width:16px;height:16px;border-radius:50%;background:#1d1d22;color:#fff;font:700 12px/16px var(--hb-font);text-align:center;box-shadow:0 0 0 2px #f7f4ec;pointer-events:none}
/* The part way point: phrase 1 marked, phrase 2 as far as the pen has come (--pz, set for each word), phrase 3 not yet */
.stage[data-hb-mode="inside"].posed .w::before,.stage[data-hb-mode="compare"].posed .paper:not(.twin) .w::before{transform:scaleX(var(--pz,0));opacity:1;transition:none}
/* See inside: the real sheet, with a dashed box where each word's band is, a number on each phrase's first word, and room for words above and below */
.stage[data-hb-mode="inside"]{padding:40px clamp(12px,3vw,24px)}
/* Lines far enough apart that a number fits between one line's boxes and the next */
.stage[data-hb-mode="inside"] .para{line-height:calc(1.2em + 20px)}
.stage[data-hb-mode="inside"] .num,.stage[data-hb-mode="inside"] .cap.i1,.stage[data-hb-mode="inside"] .cap.i2,.stage[data-hb-mode="inside"] .cap.i3{display:block}
.stage[data-hb-mode="inside"] .w::after{content:'';position:absolute;left:-.08em;right:-.08em;top:var(--top);bottom:-.06em;border:1.5px dashed rgba(29,29,34,.75);border-radius:4px;pointer-events:none}
/* A word whose trailing space falls on the next line is in two pieces: its band and box are drawn around the first piece only */
.stage[data-hb-mode="inside"] .w.split::before,.stage[data-hb-mode="inside"] .w.split::after,.stage[data-hb-mode="compare"] .w.split::before{
right:auto;bottom:auto;width:calc(var(--ow) + .16em);height:calc(var(--oh) - var(--top) + .06em)}
.cap.i1{top:8px;left:12px}
.cap.i2{bottom:8px;left:12px}
.cap.i3,.cap.c3{visibility:hidden}
.cap.i3{top:8px;right:12px}
.stage[data-hb-mode="inside"].posed .cap.i3,.stage[data-hb-mode="compare"].posed .cap.c3{visibility:visible}
@media(min-width:601px) and (max-height:660px){
.stage[data-hb-mode="inside"] .para{font-size:clamp(15px,1.5vw,19px)}
.stage[data-hb-mode="inside"] .paper{padding:14px 20px}
}
@media(max-width:600px),(max-height:500px){
.stage[data-hb-mode="inside"] .cap.i2,.stage[data-hb-mode="inside"] .cap.i3{display:none}
}
@media(max-width:600px){
.stage[data-hb-mode="inside"]{padding:52px 16px 16px}
.stage[data-hb-mode="inside"] .para{font-size:16px}
.stage[data-hb-mode="inside"] .paper{padding:12px 14px}
}
/* Compare: the sheet twice, each under its name. On the left a copy whose bands all go down at once (no sweep); on the right the real sheet */
.stage[data-hb-mode="compare"]{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));grid-template-rows:auto minmax(0,1fr);
align-items:center;gap:0 clamp(12px,3vw,32px);padding:10px clamp(8px,2vw,24px) 14px}
.stage[data-hb-mode="compare"] .cap{position:static;max-width:none;white-space:nowrap}
.stage[data-hb-mode="compare"] .cap.c1{display:block;grid-area:1/1;justify-self:start}
.stage[data-hb-mode="compare"] .head2{display:flex;flex-wrap:wrap;justify-content:space-between;align-items:center;gap:4px 8px;grid-area:1/2}
.stage[data-hb-mode="compare"] .head2 .cap{display:block}
.stage[data-hb-mode="compare"] .twin{display:block;grid-area:2/1}
.stage[data-hb-mode="compare"] .paper:not(.twin){grid-area:2/2}
.stage[data-hb-mode="compare"] .paper{width:100%;padding:clamp(10px,1.6vw,16px)}
.stage[data-hb-mode="compare"] .para{font-size:clamp(14px,2.3vw,24px)}
.stage[data-hb-mode="compare"] .twin .w::before{transition:none}
.stage[data-hb-mode="compare"] .twin .out .w::before{transition:opacity .35s linear}
.stage[data-hb-mode="compare"].posed .twin .w::before{transform:none;opacity:1;transition:none}
@media(max-width:600px){
.stage[data-hb-mode="compare"]{grid-template-columns:minmax(0,1fr);grid-template-rows:auto minmax(0,1fr) auto minmax(0,1fr);gap:4px 0;padding:8px}
.stage[data-hb-mode="compare"] .head2{grid-area:3/1}
.stage[data-hb-mode="compare"] .paper:not(.twin){grid-area:4/1}
.stage[data-hb-mode="compare"] .paper{padding:10px}
.stage[data-hb-mode="compare"] .para{font-size:clamp(12.5px,4vw,14px)}
}
</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="once" 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="../wavy-text/" rel="prev" aria-label="Previous: Wavy Text"><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>Wavy Text</span></a><a href="../split-flap-board/" rel="next" aria-label="Next: Split-Flap Board"><span><span class="hb-dir">Next: </span>Split-Flap Board</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">05.15 · Text & Typography</p>
<h1>Highlighter Sweep</h1>
<p class="hb-lede">A marker sweeps in behind the key words, one after another. Best for key points.</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">Watch it</h2><p>It plays by itself. Turn on slow motion to see each part of the movement.</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="paper">
<p class="para" id="para">Readers skim first. A marker sweeps behind <mark>the words that matter</mark>, one phrase at a time, so <mark>the main idea</mark> stands out <mark>at a glance</mark>.</p>
</div>
<span class="cap i1" aria-hidden="true" data-hb-caption>Each word has its own band, drawn behind it</span>
<span class="cap i2" aria-hidden="true" data-hb-caption>One phrase in about two thirds of a second, with a short pause between</span>
<span class="cap i3" aria-hidden="true" data-hb-caption>Shown part way through</span>
<span class="cap c1" aria-hidden="true" data-hb-caption>Marked at once</span>
<div class="head2"><span class="cap c2" aria-hidden="true" data-hb-caption>Swept in order</span><span class="cap c3" aria-hidden="true" data-hb-caption>Part way through</span></div>
</div>
<div class="hb-player">
<button class="hb-play" type="button" id="btn-play" data-hb-replay><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>Replay</button>
<label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="loop-tog" data-hb-loop autocomplete="off"><span>Loop</span></label>
<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, every key word has its own band behind it, so a phrase that wraps is marked line by line. The phrases are swept in order, 1, 2, 3, with a short pause between.</p>
<p data-hb-for="compare" hidden>Marked at once puts every band down in one go. Swept in order draws each phrase like a pen, one after another, so the eye follows the key words.</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 and the animation plays again.</p></div>
</div>
<div class="hb-settings">
<div class="hb-setting">
<p class="hb-setting-name" id="color-lbl">Marker color</p>
<div class="swatches" id="color-sw" role="group" aria-labelledby="color-lbl">
<button type="button" class="on" aria-pressed="true" aria-label="Yellow" data-c="#ffe066" style="background:#ffe066"></button>
<button type="button" aria-pressed="false" aria-label="Pink" data-c="#ffa8cc" style="background:#ffa8cc"></button>
<button type="button" aria-pressed="false" aria-label="Green" data-c="#a6efab" style="background:#a6efab"></button>
<button type="button" aria-pressed="false" aria-label="Blue" data-c="#a9d5ff" style="background:#a9d5ff"></button>
<button type="button" aria-pressed="false" aria-label="Orange" data-c="#ffc27d" style="background:#ffc27d"></button>
</div>
<p class="hb-hint">Light colors keep the dark words easy to read.</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="1300" aria-pressed="false">Slow</button>
<button type="button" data-ms="700" class="on" aria-pressed="true">Normal</button>
<button type="button" data-ms="400" aria-pressed="false">Fast</button>
</div>
<p class="hb-hint">How long the marker takes to cross each phrase.</p>
</div>
<div class="hb-setting">
<p class="hb-setting-name" id="cover-lbl">How much it covers</p>
<div class="seg" id="cover-seg" role="group" aria-labelledby="cover-lbl">
<button type="button" data-top=".12em" class="on" aria-pressed="true">Whole words</button>
<button type="button" data-top=".62em" aria-pressed="false">Lower half</button>
</div>
<p class="hb-hint">The lower half reads like a thick underline.</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 a highlighter sweep to [the key words in your paragraph or quote]. Behind each key phrase, a band of marker color grows from left to right, as if drawn with a highlighter pen, and the phrases are marked one after another in reading order. Draw the band behind the text, never over it, and grow it by scaling it from its left edge, so the words never move or reflow. When a phrase wraps onto a second line, sweep each line in turn, as one continuous stroke. Keep the words dark enough to read on the marker color. If the visitor has reduced motion turned on, fade the highlights in instead of sweeping them. 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>Key points</li><li>Quotes</li><li>Headlines</li><li>Study notes</li></ul>
<h3>Avoid on</h3>
<ul class="hb-tags hb-avoid"><li>Long paragraphs</li><li>Too many words</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 para=document.getElementById('para');
const vars=document.documentElement.style;
const loopTog=document.getElementById('loop-tog'),slowTog=document.getElementById('slow-tog');
const motionOk=()=>!window.matchMedia('(prefers-reduced-motion:reduce)').matches;
const START=200,GAP=250; // the wait before the first phrase and between phrases: holds, which Slow motion does not stretch
let ms=700,timers=[],total=0;
const stage=document.querySelector('.stage'),paper=para.parentElement,POSE_AT=.5,BADGE=16;
const phoneQ=window.matchMedia('(max-width:600px),(max-height:500px)');
let mode='normal',busy=false; // busy: a play is under way (or about to start again)
// Each key phrase's words become spans that keep the space after them, so the bands of one line join up
const phrases=[...para.querySelectorAll('mark')].map(m=>{
const parts=m.textContent.split(' ');
m.textContent='';
return parts.map((word,i)=>{
const s=document.createElement('span');
s.className='w';
s.textContent=i<parts.length-1?word+' ':word;
m.appendChild(s);
return s;
});
});
// Compare's first sheet is a copy of the paper made once, here: no ids, out of reach of the keyboard and screen readers. Its bands
// follow the real ones' classes but have no transition, so they all go down at once. The numbers are made after it, for See inside only.
const twin=paper.cloneNode(true),twinPara=twin.firstElementChild,paras=[para,twinPara];
twinPara.removeAttribute('id');twin.classList.add('twin');twin.inert=true;twin.setAttribute('aria-hidden','true');
paper.before(twin);
const nums=phrases.map((words,i)=>{
const n=document.createElement('span');
n.className='num';n.setAttribute('aria-hidden','true');n.textContent=i+1;
paper.appendChild(n);
return n;
});
// One smooth curve S per phrase: the pen speeds up and slows down once, not once per word. A word covering p0..p1 of the
// phrase's width runs between the times where S reaches p0 and p1 (inv), and its own curve is that piece of S, as a cubic Bezier.
const S=t=>t*t*(3-2*t),dS=t=>6*t*(1-t);
function inv(p){let a=0,b=1;for(let i=0;i<24;i++){const m=(a+b)/2;if(S(m)<p)a=m;else b=m}return (a+b)/2}
// Where each word of a phrase starts and ends along the phrase's width, 0 to 1
function shares(words){
const widths=words.map(w=>w.offsetWidth),sum=widths.reduce((a,b)=>a+b,0)||1;
let p0=0;
return widths.map(w=>{const s=[p0,p0+w/sum];p0=s[1];return s});
}
function plan(){
const D=ms*(slowTog.checked?3:1),reduced=!motionOk();
let at=START;
phrases.forEach(words=>{
const sh=shares(words);
words.forEach((w,i)=>{
const [p0,p1]=sh[i],t0=inv(p0),t1=i===words.length-1?1:inv(p1),k=(t1-t0)/Math.max(p1-p0,1e-6);
w.style.setProperty('--d',Math.round(at+(reduced?0:t0*D))+'ms');
w.style.setProperty('--t',Math.max(1,Math.round((t1-t0)*D))+'ms');
w.style.setProperty('--e','cubic-bezier(.333,'+(dS(t0)*k/3).toFixed(3)+',.667,'+(1-dS(t1)*k/3).toFixed(3)+')');
});
at+=(reduced?400:D)+GAP;
});
total=at-GAP;
}
function later(fn,wait){timers.push(setTimeout(fn,wait))}
// Every play starts from bare paper: the bands drop to nothing without a transition, then sweep in again
function sweep(){
paras.forEach(p=>{p.classList.add('snap');p.classList.remove('on','out')});
void para.offsetWidth;
paras.forEach(p=>{p.classList.remove('snap');p.classList.add('on')});
}
// One play marks the phrases in turn. While Loop is on, the marks hold 1600ms, fade away and the play starts again;
// switching Loop off leaves them marked.
function cycle(){
pose(false);plan();busy=true;sweep();
later(()=>{busy=false;rest()},total+60);
if(!loopTog.checked||!motionOk())return;
later(()=>{
if(!loopTog.checked)return;
busy=true;
paras.forEach(p=>p.classList.add('out'));
later(cycle,450);
},total+1600);
}
function play(){timers.forEach(clearTimeout);timers=[];cycle()}
document.getElementById('btn-play').addEventListener('click',play);
loopTog.addEventListener('change',()=>{if(loopTog.checked)play();else rest()});
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('color-sw',d=>{vars.setProperty('--marker',d.c)});
choices('speed-seg',d=>{ms=+d.ms;paint()});
choices('cover-seg',d=>{vars.setProperty('--top',d.top)});
slowTog.addEventListener('change',()=>paint());
// See inside and Compare. At rest (Loop off, nothing playing) both show the part way point: phrase 1 marked, phrase 2 as far as the
// pen has come when the phrase's curve is half done (each word's own share of that, --pz), phrase 3 not yet. A play lifts it.
const noteEl={inside:document.querySelector('[data-hb-for="inside"]'),compare:document.querySelector('[data-hb-for="compare"]')};
const NOTES={
inside:{still:'Shown still, part way: the first phrase is marked, the second half way, the third not yet. Each dashed box is one word\'s own band, behind the words.',
run:'Each dashed box is one word\'s own band, behind the words. The phrases fade in in order, 1, 2, 3, with a short pause between.'},
compare:{still:'Shown still, part way through: Marked at once has every band down, while Swept in order has reached the middle of its second phrase.',
run:'Marked at once has every band down. Swept in order fades its phrases in one after another, so the eye follows the key words.'}};
const markup={inside:noteEl.inside.textContent,compare:noteEl.compare.textContent};
const cap=n=>stage.querySelector('.cap.'+n);
function setText(el,text){if(el.textContent!==text)el.textContent=text}
function words(d){return d<=450?'under half a second':d<=900?'about two thirds of a second':d<=1250?'about a second':d<=1500?'a bit over a second':d<=2500?'about two seconds':'about four seconds'}
// The notes follow the page's own words, and under reduced motion tell a still from a fade; the words on the picture follow the
// speed and the phone layout. Each is written only when its words change.
function paint(){
if(mode==='normal')return;
const posed=stage.classList.contains('posed'),reduced=!motionOk();
for(const k in noteEl)setText(noteEl[k],reduced?NOTES[k][posed?'still':'run']:markup[k]);
setText(cap('i1'),phoneQ.matches&&posed?'Shown part way: each word has its own band':'Each word has its own band, drawn behind it');
setText(cap('i2'),reduced?'One phrase after another, with a short pause between':'One phrase in '+words(ms*(slowTog.checked?3:1))+', with a short pause between');
}
function pose(on){
if(stage.classList.contains('posed')===on)return;
if(on)stage.classList.add('posed');
else{
// leaving the pose shows the bands as they really are at once
para.classList.add('snap');stage.classList.remove('posed');void para.offsetWidth;para.classList.remove('snap');
}
paint();
}
function rest(){if(mode!=='normal'&&!busy&&!(loopTog.checked&&motionOk()))pose(true)}
// Each word's share of the way the pen has come at the part way point, from the same widths plan() uses
function dealPose(){
phrases.forEach((words,i)=>{
const sh=shares(words);
words.forEach((w,j)=>{
const [p0,p1]=sh[j];
w.style.setProperty('--pz',i===0?1:i===1?Math.min(1,Math.max(0,(POSE_AT-p0)/(p1-p0))).toFixed(3):0);
});
});
}
// The numbers sit on top of each phrase's first word, measured from its box
function placeNums(){
const pr=paper.getBoundingClientRect(),cs=getComputedStyle(para),fs=parseFloat(cs.fontSize),line=parseFloat(cs.lineHeight);
phrases.forEach((words,i)=>{
const r=words[0].getClientRects()[0];
// the room between the line above's boxes and this word's box, and the number in the middle of it
const room=line-r.height+.06*fs;
nums[i].style.left=(r.left-pr.left-.08*fs)+'px';
nums[i].style.top=(r.top-pr.top+.12*fs-room/2-BADGE/2)+'px';
});
}
function fitBoxes(){
paras.forEach(p=>p.querySelectorAll('.w').forEach(w=>{
const r=w.getClientRects();
w.classList.toggle('split',r.length>1);
if(r.length>1){w.style.setProperty('--ow',r[0].width+'px');w.style.setProperty('--oh',r[0].height+'px')}
}));
}
function refresh(){
if(mode==='normal')return;
dealPose();fitBoxes();placeNums();paint();
}
document.addEventListener('hb:mode',e=>{
mode=e.detail.mode;
if(mode==='normal')pose(false);
else{pose(!busy&&!(loopTog.checked&&motionOk()));refresh()}
});
new ResizeObserver(()=>refresh()).observe(para);
phoneQ.addEventListener('change',()=>paint());
if(document.fonts)document.fonts.ready.then(()=>refresh());
if(!motionOk())for(const k in noteEl)noteEl[k].textContent=NOTES[k].still;
</script>
</body>
</html>