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: Highlighter Sweep shows a dashed box and a number on each key word's band with the sheet left at part way (See inside) and the sheet marked at once beside the sheet swept in order (Compare)
matindb8e4e85da0f
diff --git a/animations/05-text-typography/highlighter-sweep/index.html b/animations/05-text-typography/highlighter-sweep/index.htmlindex 025f600a..d182e255 100644--- a/animations/05-text-typography/highlighter-sweep/index.html+++ b/animations/05-text-typography/highlighter-sweep/index.html@@ -23,7 +23,7 @@<style>:root{--ui-accent:#ff6f8b;--marker:#ffe066;--top:.12em}*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}- .stage{display:flex;align-items:center;justify-content:center;padding:clamp(16px,3vw,24px);background:#0b0b0d}+ .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}@@ -44,6 +44,59 @@.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=10"><script src="../../../assets/js/demo-page.js?v=10" defer></script>@@ -66,16 +119,30 @@<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">@@ -160,6 +227,9 @@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 stretchlet 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 upconst phrases=[...para.querySelectorAll('mark')].map(m=>{@@ -174,22 +244,38 @@});});+ // 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 widths=words.map(w=>w.offsetWidth),sum=widths.reduce((a,b)=>a+b,0)||1;- let p0=0;+ const sh=shares(words);words.forEach((w,i)=>{- const p1=p0+widths[i]/sum,t0=inv(p0),t1=i===words.length-1?1:inv(p1),k=(t1-t0)/Math.max(p1-p0,1e-6);+ 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)+')');- p0=p1;});at+=(reduced?400:D)+GAP;});@@ -198,27 +284,27 @@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 againfunction sweep(){- para.classList.add('snap');- para.classList.remove('on','out');+ paras.forEach(p=>{p.classList.add('snap');p.classList.remove('on','out')});void para.offsetWidth;- para.classList.remove('snap');- para.classList.add('on');+ 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(){- plan();sweep();+ pose(false);plan();busy=true;sweep();+ later(()=>{busy=false;rest()},total+60);if(!loopTog.checked||!motionOk())return;later(()=>{if(!loopTog.checked)return;- para.classList.add('out');+ 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()});+ 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',()=>{@@ -227,8 +313,81 @@}));}choices('color-sw',d=>{vars.setProperty('--marker',d.c)});- choices('speed-seg',d=>{ms=+d.ms});+ 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(!(loopTog.checked&&motionOk()));refresh()}+ });+ new ResizeObserver(()=>refresh()).observe(para);+ if(document.fonts)document.fonts.ready.then(()=>refresh());+ if(!motionOk())for(const k in noteEl)noteEl[k].textContent=NOTES[k].still;</script></body></html>