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): the keyboard pushes the dots too: Tab focuses a layer over the word that puts an invisible pointer on its middle, the arrow keys move it, the dots scatter and settle around it as around a mouse, and Escape or a Tab out lets them go home

matin260817ecffc9
diff --git a/animations/05-text-typography/text-particles/README.md b/animations/05-text-typography/text-particles/README.md
index f91074e9..67376173 100644
--- a/animations/05-text-typography/text-particles/README.md
+++ b/animations/05-text-typography/text-particles/README.md
@@ -36,6 +36,8 @@ Because the push grows with the spring `K`, a pointer that holds still opens a g
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 1.25 CSS pixels (2.5 pixels of a phone's canvas, which holds two 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.
+The keyboard pushes the dots too: Tab focuses a see-through layer over the word, which puts an invisible pointer on the middle of the word (or where the keys last left it), and the arrow keys move that pointer a step at a time (6% of the stage's shorter side, three steps with Shift), so the dots scatter, spring back and settle around it as they do around a mouse. Escape, or a Tab out of the stage, lets them go home as the pointer leaving does.
+
## Key parameters
| Parameter | Default | Effect |
|-----------|---------|--------|
diff --git a/animations/05-text-typography/text-particles/index.html b/animations/05-text-typography/text-particles/index.html
index f48f1cbd..b3495473 100644
--- a/animations/05-text-typography/text-particles/index.html
+++ b/animations/05-text-typography/text-particles/index.html
@@ -25,6 +25,8 @@
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
/* 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}
+ /* The keyboard's way in: a layer over the word that Tab focuses (the pointer passes through it to the canvas) */
+ .keys{position:absolute;inset:0;border-radius:11px;pointer-events: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>
@@ -49,6 +51,7 @@
<div><h2 id="hb-watch-title">Hover it</h2><p>Move your pointer through the word, drag a finger across it, or press Show me.</p></div>
</div>
<div class="stage" id="stage">
+ <div class="keys" id="keys" tabindex="0" role="group" aria-label="Word of dots: use the arrow keys to push the dots"></div>
<canvas id="cvs" role="img" aria-label="The word HELLO, made of small dots"></canvas>
</div>
<div class="hb-player">
@@ -275,6 +278,16 @@
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);
+ // The keyboard: Tab focuses the layer over the word, which ends a run and puts an invisible pointer on the middle of the word, or
+ // where the keys last left it; the arrow keys move it a step (6% of the stage's shorter side, three with Shift) and it pushes the
+ // dots as the pointer does. Escape, or focus leaving, lets them go home as the pointer leaving does, unless a pointer has taken over.
+ const keys=document.getElementById('keys'),ARROWS={ArrowLeft:[-1,0],ArrowRight:[1,0],ArrowUp:[0,-1],ArrowDown:[0,1]};let kp=null;
+ function keyAim(dx,dy,big){const s=Math.min(W,H)*(big?.18:.06),p=kp||middle();run=null;
+ user=kp={x:Math.min(W,Math.max(0,p.x+dx*s)),y:Math.min(H,Math.max(0,p.y+dy*s))};calm=0;wake();}
+ function keyRest(){if(user&&user===kp){user=null;wake();}}
+ keys.addEventListener('focus',()=>keyAim(0,0));keys.addEventListener('blur',keyRest);
+ keys.addEventListener('keydown',e=>{if(e.altKey||e.ctrlKey||e.metaKey)return;if(e.key==='Escape'){keyRest();return;}
+ const d=ARROWS[e.key];if(d){e.preventDefault();keyAim(d[0],d[1],e.shiftKey);}});
// 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};calm=0;wake();}}
function choices(id,pick){const buttons=document.querySelectorAll('#'+id+' button');