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
fix: Text Particles (05.19): slow motion moves the dots from rest, the stage stays dark in forced colors, phones draw only tiny dots as squares, stage colors are variables, the settle skips dots that arrived, and the README wording is exact
matinc715514f2de9
diff --git a/animations/05-text-typography/text-particles/README.md b/animations/05-text-typography/text-particles/README.mdindex 0faf1dd1..76afd8f3 100644--- a/animations/05-text-typography/text-particles/README.md+++ b/animations/05-text-typography/text-particles/README.md@@ -32,22 +32,22 @@ vx = (vx + ax) * D; vy = (vy + ay) * D; // D: the share of its speed a dotx += 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, 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.+Because the push grows with the spring `K`, a pointer that holds still opens a gap of the same size at every return speed, its radius about 0.7 of the reach; `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, and that is itself within 1.3 times the reach of the pointer, moves 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). Each step it slides around the pointer by 12% of the angle left, at most 3 px sideways, and closes 12% of the distance left toward or away from the pointer; measured at the far reach, that came to at most about 5 px a step toward or away and about 6 px in all. A dot that has arrived is skipped until the pointer moves again, and the other dots finish springing home. The gap then holds its shape, every other dot is exactly home and the loop stops, 2 to 3 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. 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.+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`): Chrome 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. Measured in Chrome, 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. Other browsers take their own fast and slow paths, so measure both ways in the browsers you target. 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. On a phone, a dot whose radius is under 2.5 pixels of the canvas (about 1.25 CSS pixels, as a phone's canvas holds two pixels to one; usually the small size, with the phone held upright) is drawn as a square with `fillRect`, which costs even less to paint and still reads as a dot at that size: in Chrome, with the processor slowed four times, circles there ran Show me at about 52 frames a second and squares at 60. Larger dots stay round, as squares that big look like bricks. 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 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 |+| 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 whose radius is about 0.7 of the reach || 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 || Your text | HELLO | The word, up to 10 letters, drawn as large as fits 86% of the stage's width and 42% of its height; an emptied field goes back to HELLO |## Production notes- **Wait for the font**: canvas text is drawn in whatever font is ready, so sampling before a web font loads gives the fallback letters. Call `document.fonts.load()` for the weight and the text first; the demo waits up to 1.5 seconds, and if the font arrives later the dots flow into it.-- **The dot count is the cost**: every frame moves and paints the dots, so keep them to a few thousand, skip the ones at rest and stop the loop when all are home. Fill each circle on its own: a path that holds thousands of circles looks like one call but is the slow part, because it is cut into triangles again on every frame. On phones squares cost less still, which is why the demo draws them there. For tens of thousands of dots, move them on the graphics chip with WebGL points instead (see GPGPU Particle System).+- **The dot count is the cost**: every frame moves and paints the dots, so keep them to a few thousand, skip the ones at rest and stop the loop when all are home. In Chrome, fill each circle on its own: a path that holds thousands of circles looks like one call but is the slow part, because it is cut into triangles again on every frame. Other browsers draw paths and circles their own way, so measure both in the browsers you target. On phones, tiny squares cost less still, which is why the demo draws its smallest dots as squares there. For tens of thousands of dots, move them on the graphics chip with WebGL points instead (see GPGPU Particle System).- **Fixed steps**: scaling one spring step by the frame time makes the spring stiffer or looser on faster screens. Take fixed steps from an accumulator and interpolate the drawing, as the demo does.- **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.diff --git a/animations/05-text-typography/text-particles/index.html b/animations/05-text-typography/text-particles/index.htmlindex aac98747..8a2b25ca 100644--- a/animations/05-text-typography/text-particles/index.html+++ b/animations/05-text-typography/text-particles/index.html@@ -21,10 +21,10 @@<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": "Text Particles", "name": "Text Particles", "description": "A word of dots scatters from your pointer and springs back. Best for intros.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/05-text-typography/text-particles/", "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, Text Particles"}</script><style>- :root{--ui-accent:#ff6f8b}+ :root{--ui-accent:#ff6f8b;--bg:#0b0b0d;--glow:#ff6f8b}*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}- /* A dark stage with a soft glow of the word's colors behind it (still: only the dots move) */- .stage{position:relative;overflow:hidden;background:radial-gradient(ellipse 60% 46% at 50% 50%,color-mix(in srgb,var(--glow,#ff6f8b) 14%,transparent),transparent 72%),#0b0b0d}+ /* A dark stage with a soft glow of the word's colors (still); kept in forced colors, which do not adjust the canvas dots */+ .stage{position:relative;overflow:hidden;background:radial-gradient(ellipse 60% 46% at 50% 50%,color-mix(in srgb,var(--glow) 14%,transparent),transparent 72%),var(--bg);forced-color-adjust:none}/* touch-action none: a finger dragged over the word pushes the dots instead of scrolling the page */canvas{display:block;width:100%;height:100%;touch-action:none;-webkit-user-select:none;user-select:none;-webkit-tap-highlight-color:transparent}</style>@@ -160,7 +160,7 @@// How fast they come back: the spring (the share of the way home added to a dot's speed each step) and the speed a dot keepsconst 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 hx,hy,x,y,px,py,vx,vy,bk,dn,dnAny=false,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,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 gridfunction sample(){@@ -184,7 +184,7 @@// New homes for the dots. With fly, each new dot starts where an old one is (in the same left-to-right order), so they flow into the new letters.function rebuild(fly){const pts=sample(),n=pts.length/2,o={N,x,y,vx,vy};- N=n;[hx,hy,x,y,px,py,vx,vy]=Array.from({length:8},()=>new Float32Array(n));L=[0,0,0,0].map(()=>new Int32Array(n));bk=new Uint8Array(n);+ N=n;[hx,hy,x,y,px,py,vx,vy]=Array.from({length:8},()=>new Float32Array(n));L=[0,0,0,0].map(()=>new Int32Array(n));bk=new Uint8Array(n);dn=new Uint8Array(n);for(let j=0;j<n;j++){const k=fly&&o.N?Math.floor(j*o.N/n):-1;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];@@ -206,7 +206,8 @@// 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(){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++){+ if(!rest&&dnAny){dn.fill(0);dnAny=false;} // a dot that reached its resting place is skipped until the settle ends+ for(let i=0;i<N;i++){if(rest&&dn[i]){px[i]=x[i];py[i]=y[i];continue;}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;@@ -215,7 +216,7 @@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;}+ 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++;else{dn[i]=1;dnAny=true;}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];@@ -225,12 +226,11 @@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.+ // The loop runs while something moves: it adds up the time (a third in slow motion), steps once per 60th of a second, draws dots between stepsfunction 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(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;calm=0;}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);@@ -239,11 +239,11 @@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 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 wake(){if(!raf&&N){lastT=null;acc=0;still=false;raf=requestAnimationFrame(frame);}}document.addEventListener('visibilitychange',()=>{lastT=null;acc=0;});+ // Each dot is a circle of its own (Chrome draws these much faster than one path of thousands), 0.12 device pixels smaller for its softer+ // edge; a phone draws tiny dots as squares. A dot pushed from home is drawn brighter and larger; under reduced motion, those near the pointer.function draw(a){- ctx.clearRect(0,0,W,H);sq=PHONE.matches;+ ctx.clearRect(0,0,W,H);sq=PHONE.matches&&rad*DPR<2.5;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;