Skip to content

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.

fix: Text Particles (05.19): under a resting pointer the dots settle into the gap and every other dot home within about 2 s, starts that are not pointer moves keep normal motion, reduced motion switched on puts every dot home, small dots stay crisp at 1x

matina3db636d9eb8
diff --git a/animations/05-text-typography/text-particles/README.md b/animations/05-text-typography/text-particles/README.md
index 8841ebf7..0faf1dd1 100644
--- a/animations/05-text-typography/text-particles/README.md
+++ b/animations/05-text-typography/text-particles/README.md
@@ -32,14 +32,14 @@ vx = (vx + ax) * D; vy = (vy + ay) * D; // D: the share of its speed a dot
x += vx; y += vy;
```
-Because the push grows with the spring `K`, a pointer that holds still opens a gap of the same size, about 0.7 of the reach, at every return speed; `K` and `D` only set how quickly the dots come back. A spring changes with the length of its step, so the frame loop adds up the time that has passed (a third of it in slow motion), takes one fixed step per 60th of a second and draws each dot between its last two steps. The motion is then the same on a 60 Hz and a 120 Hz screen, and slow motion stays smooth. A dot that is out of reach, nearly still and within a third of a pixel of home is put there, and a dot at home and out of reach is skipped, so the work follows the dots that move. Once every dot is home the loop stops, so a word at rest costs nothing. Under a pointer that holds still for a second, friction rises over half a second, so dots still drifting around the rim of the gap come to rest and the loop stops there too.
+Because the push grows with the spring `K`, a pointer that holds still opens a gap of the same size, about 0.7 of the reach, at every return speed; `K` and `D` only set how quickly the dots come back. A spring changes with the length of its step, so the frame loop adds up the time that has passed (a third of it in slow motion), takes one fixed step per 60th of a second and draws each dot between its last two steps. The motion is then the same on a 60 Hz and a 120 Hz screen, and slow motion stays smooth. A dot that is out of reach, nearly still and within a third of a pixel of home is put there, and a dot at home and out of reach is skipped, so the work follows the dots that move. Once every dot is home the loop stops, so a word at rest costs nothing. Under a pointer that holds still, the spring alone settles slowly: a dot whose home sits near the pointer can land on the far side of the gap and creep around its rim for many seconds. So half a second after the pointer stops, each dot whose home lies inside the reach slides around the pointer, at most 3 px a step, to its resting place on the line from the pointer through its home, where push and spring balance (at (2.4R + h) / 3.4 from the pointer, for a home h from it); the other dots finish springing home. The gap then holds its shape, every other dot is exactly home and the loop stops, about 2 seconds after the pointer does. Show me, a setting and a new word start that half second over, so the dots still spring back first.
-The palette is spread across the word as 32 colors, and each dot takes the color at its home, so it keeps its color as it flies. Each dot is filled as a circle of its own (`beginPath`, `arc`, `fill`): the browser draws a lone circle with a fast special case on the graphics chip, while one path holding thousands of circles has to be cut into triangles again on every frame, which halved the frame rate on 2× screens. A dot that is away from home is drawn brighter and a little larger, in three steps the further it was pushed, so it glows in its own color. Phones draw each dot as a small square with `fillRect`, which costs even less to paint and looks much the same at that size. A new word or dot size samples the word again, and each new dot starts where an old one stands (both taken from left to right), so the dots flow into the new letters. Show me moves a pretend pointer through the word along a wavy path, drawn as a small light with a ring for its reach.
+The palette is spread across the word as 32 colors, and each dot takes the color at its home, so it keeps its color as it flies. Each dot is filled as a circle of its own (`beginPath`, `arc`, `fill`): the browser draws a lone circle with a fast special case on the graphics chip, while one path holding thousands of circles has to be cut into triangles again on every frame. In the demo, one path per color ran Show me at 43 to 60 frames a second on 2× screens, depending on the run; one circle at a time holds 60. The fast circle has a softer edge, which on a 1× screen fills the gaps between small dots with a haze, so each circle is drawn 0.12 device pixels smaller. A dot that is away from home is drawn brighter and a little larger, in three steps the further it was pushed, so it glows in its own color. Phones draw each dot as a small square with `fillRect`, which costs even less to paint and looks much the same at that size. A new word or dot size samples the word again, and each new dot starts where an old one stands (both taken from left to right), so the dots flow into the new letters. Show me moves a pretend pointer through the word along a wavy path, drawn as a small light with a ring for its reach.
## Key parameters
| Parameter | Default | Effect |
|-----------|---------|--------|
-| Dot size | Medium | How far apart the dots sit: small is 3 px, medium 4.2 and large 6, scaled with the word between 0.75 and 1.25 times; each dot is about three quarters of that step wide. The dots are counted at the small size: past 2,000 on a phone or 6,000 on a larger screen, every size is spread out by the same factor, so the three stay apart |
+| Dot size | Medium | How far apart the dots sit: small is 3 px, medium 4.2 and large 6, scaled with the word between 0.75 and 1.25 times; each dot is narrower than that step, so neighbors never touch. The dots are counted at the small size: past 2,000 on a phone or 6,000 on a larger screen, every size is spread out by the same factor, so the three stay apart |
| How far they scatter | Medium | The pointer's reach: short is 0.28 of the font size, medium 0.42 and far 0.65, counting the font as at least 120 px; a still pointer opens a gap about 0.7 of the reach wide |
| Colors | Sunset | The colors across the word, in 32 steps: Sunset runs from pink to gold, Cool from teal to purple, Rainbow through seven colors and White from white to pale gray; the glow behind the word matches |
| How fast they come back | Normal | The spring and the share of its speed a dot keeps each step: slow is 0.022 and 0.87, normal 0.045 and 0.82, fast 0.09 and 0.76. A dot pushed 100 px away is home in about 1.4, 1 or 0.8 seconds, after swinging about a sixth of the way past it |
@@ -52,7 +52,7 @@ The palette is spread across the word as 32 colors, and each dot takes the color
- **Sharp dots**: size the canvas backing store by `devicePixelRatio` (the demo caps it at 2) and keep the positions in CSS pixels with `setTransform`.
- **Touch**: `touch-action: none` on the canvas lets a finger drag through the word without scrolling the page. Keep the canvas from filling a phone screen, so the page can still be scrolled past it.
- **Accessibility**: a canvas is not text. Give it `role="img"` and an `aria-label` that holds the word, and keep a real heading in the page for screen readers and search engines.
-- **Reduced motion**: the demo leaves every dot at home and only brightens the dots near the pointer.
+- **Reduced motion**: the demo leaves every dot at home and only brightens the dots near the pointer. Switched on while the dots move, it puts every dot home at once. Check the setting in the frame loop: in Chrome, reading `matchMedia(...).matches` every frame can swallow its change event.
- **Libraries**: tsParticles has a canvas mask plugin that places its particles inside text; PixiJS (a ParticleContainer) and three.js (Points) draw many thousands of dots with WebGL. GSAP can tween the dots to new homes when the word changes, while the push and the spring are simplest in your own frame loop.
## See also
diff --git a/animations/05-text-typography/text-particles/index.html b/animations/05-text-typography/text-particles/index.html
index 618c2218..aac98747 100644
--- a/animations/05-text-typography/text-particles/index.html
+++ b/animations/05-text-typography/text-particles/index.html
@@ -161,9 +161,8 @@
const BACK={slow:[.022,.87],normal:[.045,.82],fast:[.09,.76]};
let text='HELLO',gapK=4.2,far=.42,pal='sunset',back='normal',W=0,H=0,fs=0,box={l:0,r:0,t:0,b:0},R=60,rad=1.6,cols=[],sq=false,N=0;
let hx,hy,x,y,px,py,vx,vy,bk,L=[],user=null,touchId=null,run=null,ptr=null,mark=null,markA=0,lightAt=null,glow=0,calm=0;
- let raf=0,lastT=null,acc=0,still=true,ready=false,wantShow=false;
- // The word is drawn on a hidden canvas (as large as fits 86% of its width and 42% of its height) and a dot is kept wherever a letter
- // covers a point of an offset grid, read column by column, so the dots run left to right.
+ let raf=0,lastT=null,acc=0,still=true,ready=false,wantShow=false,DPR=1,redWas=reduce.matches;
+ // The word is drawn on a hidden canvas (86% of its width, 42% of its height at most); a dot is kept wherever a letter covers a point of an offset grid
function sample(){
if(!W||!H)return [];
const off=document.createElement('canvas');off.width=W;off.height=H;
@@ -191,42 +190,47 @@
hx[j]=pts[2*j];hy[j]=pts[2*j+1];bk[j]=Math.min(31,Math.max(0,(hx[j]-box.l)/(box.r-box.l)*32|0));x[j]=px[j]=k<0?hx[j]:o.x[k];y[j]=py[j]=k<0?hy[j]:o.y[k];
if(k>=0){vx[j]=o.vx[k];vy[j]=o.vy[k];}
}
- R=far*Math.max(fs,120);paint();cvs.setAttribute('aria-label','The word '+text+', made of small dots');
+ R=far*Math.max(fs,120);calm=0;paint();cvs.setAttribute('aria-label','The word '+text+', made of small dots');
// Canvas text uses whatever font is ready: once the site font has loaded for these letters, the dots flow into it
if(document.fonts&&!document.fonts.check(F100,text))document.fonts.load(F100,text).then(()=>{if(document.fonts.check(F100,text))rebuild(!reduce.matches)},()=>{});
still=!(fly&&o.N)&&!user;
if(still)draw(1);else wake();
}
- // The palette as 32 colors from left to right (each dot takes the one at its home, so it keeps its color as it flies), and three
- // brighter versions of them for dots that are pushed away
+ // The palette as 32 colors from left to right (a dot keeps the one at its home as it flies), and three brighter sets for pushed dots
function paint(){const [stops,glowCol]=PAL[pal],rgb=stops.map(c=>[1,3,5].map(i=>parseInt(c.slice(i,i+2),16)));
const at=k=>{const t=k/31*(rgb.length-1),i=Math.min(rgb.length-2,t|0),f=t-i;return rgb[i].map((v,j)=>v+(rgb[i+1][j]-v)*f)};
cols=[0,.3,.55,.85].map(g=>Array.from({length:32},(_,k)=>'rgb('+at(k).map(v=>Math.min(255,Math.round(v*(1+g))))+')'));
stage.style.setProperty('--glow',glowCol);}
- // One step is 1/60 s. Inside the pointer's reach R a dot is pushed away, harder the closer it is; a spring pulls it home and it keeps
- // part of its speed. The push grows with the spring and the reach, so the gap it opens is the same share of R at every return speed.
- // A dot at home and out of reach is skipped; one out of reach, within a third of a pixel of home and nearly still is put there.
+ // One step is 1/60 s. Inside the pointer's reach R a dot is pushed away, harder the closer it is; a spring pulls it home and it keeps part
+ // of its speed. The push grows with the spring and the reach, so the gap is the same share of R at every return speed. A dot at home and
+ // out of reach is skipped; one out of reach, within a third of a pixel of home and nearly still is put there.
function step(){
- // Under the visitor's pointer, still for a second, friction rises over half a second, so dots still drifting around the rim come to rest
- const p=ptr,held=!!p&&p===user&&++calm>60,[K,D0]=BACK[back],D=held?D0*Math.max(.15,1-(calm-60)/30):D0,P=2.4*K*R,R2=R*R;let maxV=0,act=0;
+ const p=ptr,rest=!!p&&p===user&&++calm>30,[K,D]=BACK[back],P=2.4*K*R,R2=R*R;let act=0;
for(let i=0;i<N;i++){
const X=x[i],Y=y[i],dx=p?X-p.x:0,dy=p?Y-p.y:0,d2=p?dx*dx+dy*dy:R2;
if(X===hx[i]&&Y===hy[i]&&vx[i]===0&&vy[i]===0&&d2>=R2)continue;
px[i]=X;py[i]=Y;
+ // Half a second after the visitor's pointer stops, a dot whose home lies inside the reach slides around it to its resting place (where
+ // push and spring balance, on the line from the pointer through its home) instead of creeping there for seconds
+ const hdx=p?hx[i]-p.x:0,hdy=p?hy[i]-p.y:0,h2=hdx*hdx+hdy*hdy;
+ if(rest&&h2<R2&&d2<R2*1.69){const h=Math.sqrt(h2),r0=Math.sqrt(d2),rT=(2.4*R+h)/3.4,a0=Math.atan2(dy,dx),a1=h>.5?Math.atan2(hdy,hdx):a0,da=a1-a0-TAU*Math.round((a1-a0)/TAU);
+ const a=Math.abs(da)*r0<.3?a1:a0+Math.sign(da)*Math.min(Math.abs(da)*.12,3/r0),r=Math.abs(rT-r0)<.3?rT:r0+(rT-r0)*.12;
+ x[i]=p.x+Math.cos(a)*r;y[i]=p.y+Math.sin(a)*r;vx[i]=vy[i]=0;if(a!==a1||r!==rT)act++;continue;}
let ax=(hx[i]-X)*K,ay=(hy[i]-Y)*K;
if(d2<R2&&d2>.01){const d=Math.sqrt(d2),f=(1-d/R)*P/d;ax+=dx*f;ay+=dy*f;}
vx[i]=(vx[i]+ax)*D;vy[i]=(vy[i]+ay)*D;x[i]+=vx[i];y[i]+=vy[i];
- const v=Math.abs(vx[i])+Math.abs(vy[i]);
- if(d2>=R2&&v<.05&&Math.abs(x[i]-hx[i])+Math.abs(y[i]-hy[i])<.3){x[i]=px[i]=hx[i];y[i]=py[i]=hy[i];vx[i]=vy[i]=0;}
- else{act++;if(v>maxV)maxV=v;}
+ if(d2>=R2&&Math.abs(vx[i])+Math.abs(vy[i])<.05&&Math.abs(x[i]-hx[i])+Math.abs(y[i]-hy[i])<.3){x[i]=px[i]=hx[i];y[i]=py[i]=hy[i];vx[i]=vy[i]=0;}
+ else act++;
}
- still=!act||!!p&&maxV<(held?.1:.02); // every dot home, or the dots under a pointer still
+ still=!act; // every dot home, or at its resting place under a pointer at rest
}
const ease=(v,to,k,t)=>{const n=v+(to-v)*(1-Math.pow(1-k,t/FRAME));return Math.abs(to-n)<.01?to:n};
// The loop runs only while something moves: it adds up the time (a third in slow motion), takes one step per 60th of a second and
// draws the dots between their last two steps, so slow motion stays smooth; a first frame counts as one 60th.
function frame(ts){
const t=(lastT===null?FRAME:Math.min(ts-lastT,50))*(slowTog.checked?1/3:1),red=reduce.matches;lastT=ts;
+ // Reduced motion switched on: every dot goes home at once and a run ends (reading the setting here can swallow its change event)
+ if(red&&!redWas){x.set(hx);y.set(hy);px.set(hx);py.set(hy);vx.fill(0);vy.fill(0);run=null;markA=0;still=true;}redWas=red;
if(run){run.t+=t;mark=run.at(Math.min(run.t,run.dur));if(run.t>=run.dur)run=null;}
ptr=run?mark:user;if(ptr)lightAt=ptr;
markA=ease(markA,run?1:0,.25,t);glow=ease(glow,ptr?1:0,.2,t);
@@ -235,20 +239,18 @@
raf=run||markA||(red?glow%1:!still)?requestAnimationFrame(frame):0;
if(!raf){lastT=null;acc=0;}
}
- function wake(){if(!raf&&N){lastT=null;acc=0;raf=requestAnimationFrame(frame);}}
- document.addEventListener('visibilitychange',()=>{lastT=null;acc=0;});
- // Each dot is filled as a circle of its own, which browsers draw much faster than one path of thousands (phones draw squares). A dot
- // pushed away from home is drawn brighter and larger, more the further it is pushed; under reduced motion, the dots near the pointer.
+ function wake(){if(!raf&&N){lastT=null;acc=0;raf=requestAnimationFrame(frame);}}document.addEventListener('visibilitychange',()=>{lastT=null;acc=0;});
+ // Each dot is a circle of its own (browsers draw these much faster than one path of thousands; phones draw squares), 0.12 device pixels
+ // smaller for its softer edge. A dot pushed from home is drawn brighter and larger; under reduced motion, the dots near the pointer.
function draw(a){
ctx.clearRect(0,0,W,H);sq=PHONE.matches;
const red=reduce.matches,l=lightAt,R2=R*R,far2=R*.6,n=[0,0,0,0];
for(let i=0;i<N;i++){
- let v=0;
- if(!red)v=(Math.abs(px[i]+(x[i]-px[i])*a-hx[i])+Math.abs(py[i]+(y[i]-py[i])*a-hy[i]))/far2;
+ let v=0;if(!red)v=(Math.abs(px[i]+(x[i]-px[i])*a-hx[i])+Math.abs(py[i]+(y[i]-py[i])*a-hy[i]))/far2;
else if(glow&&l){const dx=hx[i]-l.x,dy=hy[i]-l.y,d2=dx*dx+dy*dy;if(d2<R2)v=(1-Math.sqrt(d2)/R)*glow;}
const g=v>.62?3:v>.32?2:v>.1?1:0;L[g][n[g]++]=i;
}
- for(let g=0;g<4;g++){const s=rad*(1+g*.09),list=L[g],cs=cols[g];let c=-1;
+ for(let g=0;g<4;g++){const s=rad*(1+g*.09)-(sq?0:.12/DPR),list=L[g],cs=cols[g];let c=-1;
for(let k=0;k<n[g];k++){const i=list[k],X=px[i]+(x[i]-px[i])*a,Y=py[i]+(y[i]-py[i])*a;
if(bk[i]!==c){c=bk[i];ctx.fillStyle=cs[c];}
if(sq)ctx.fillRect(X-s*.75,Y-s*.75,s*1.5,s*1.5);else{ctx.beginPath();ctx.arc(X,Y,s,0,TAU);ctx.fill();}}}
@@ -268,13 +270,13 @@
document.addEventListener('hb:input',()=>{if(run){run=null;wake();}});
// Show me: a pretend pointer sweeps through the word in 2.6 s, rising and falling; under reduced motion it rests on the middle for 1.4 s
const middle=()=>({x:(box.l+box.r)/2,y:(box.t+box.b)/2});
- function showMe(){if(!N){wantShow=true;return;}
+ function showMe(){if(!N){wantShow=true;return;}calm=0;
run=reduce.matches?{t:0,dur:1400,at:middle}:{t:0,dur:2600,at:t=>{
const s=t/2600,e=s<.5?2*s*s:1-2*(1-s)*(1-s),x0=box.l-R*.2,x1=box.r+R*.2;
return {x:x0+(x1-x0)*e,y:(box.t+box.b)/2-(box.b-box.t)*.3*Math.sin(TAU*1.5*e)};}};wake();}
document.getElementById('btn-demo').addEventListener('click',showMe);
// A new reach or return speed shows at once: the pretend pointer touches the middle of the word for a moment
- function poke(){if(!run&&N){run={t:0,dur:reduce.matches?900:400,at:middle};wake();}}
+ function poke(){if(!run&&N){run={t:0,dur:reduce.matches?900:400,at:middle};calm=0;wake();}}
function choices(id,pick){const buttons=document.querySelectorAll('#'+id+' button');
buttons.forEach(b=>b.addEventListener('click',()=>{if(b.classList.contains('on'))return; // pressing the choice already made does nothing
buttons.forEach(o=>{o.classList.toggle('on',o===b);o.setAttribute('aria-pressed',String(o===b))});pick(b.dataset);}));}
@@ -286,15 +288,13 @@
txtIn.addEventListener('input',()=>{const t=txtIn.value.replace(/\s+/g,' ').trim()||'HELLO';if(t!==text){text=t;if(ready)rebuild(!reduce.matches);}});
txtIn.addEventListener('change',()=>{if(!txtIn.value.trim()){txtIn.value='HELLO';txtIn.dispatchEvent(new Event('input',{bubbles:true}));}});
// The canvas works in CSS pixels, its backing store scaled by the pixel ratio (at most 2); a new stage size samples the word again
- function layout(){const w=stage.clientWidth,h=stage.clientHeight,dpr=Math.min(window.devicePixelRatio||1,2);
+ function layout(){const w=stage.clientWidth,h=stage.clientHeight;DPR=Math.min(window.devicePixelRatio||1,2);
if(!w||!h||(w===W&&h===H))return false;
- W=w;H=h;cvs.width=Math.round(W*dpr);cvs.height=Math.round(H*dpr);ctx.setTransform(dpr,0,0,dpr,0,0);return true;}
+ W=w;H=h;cvs.width=Math.round(W*DPR);cvs.height=Math.round(H*DPR);ctx.setTransform(DPR,0,0,DPR,0,0);return true;}
// The first word waits for the site font (at most 1.5 s), so it does not form in a fallback font first
function begin(){if(ready)return;ready=true;layout();rebuild(false);if(wantShow){wantShow=false;showMe();}}
if(document.fonts&&document.fonts.load){document.fonts.load(F100,text).then(begin,begin);setTimeout(begin,1500);}else begin();
- new ResizeObserver(()=>{if(ready&&layout())rebuild(false);}).observe(stage);
- // Reduced motion switched on while the dots move: they are put home at once
- reduce.addEventListener('change',()=>{if(reduce.matches&&N){x.set(hx);y.set(hy);px.set(hx);py.set(hy);vx.fill(0);vy.fill(0);still=true;}run=null;wake();});
+ new ResizeObserver(()=>{if(ready&&layout())rebuild(false);}).observe(stage);reduce.addEventListener('change',wake); // a frame sees reduced motion
</script>
</body>
</html>