Particle text that follows the cursor
How Inkwell's opening poem turns a word into thousands of points, pulls them home with a spring, and lets the cursor push them away.
Inkwell is a poem made of moving ink. Each scroll stage shows one word, and every letter is built from small dots that drift away from your cursor and settle back. It is plain 2D canvas, with no library.
From a word to points
I draw the word in a big, heavy font onto an offscreen canvas, then read the pixels back with getImageData. Wherever the alpha is high enough, I keep that position as a target point. I only look at every second or third pixel, which keeps the point count reasonable, and I shuffle the list so the dots that get used are spread evenly across the letters.
const d=o.getImageData(0,0,W,H).data,pts=[],st=W<700?2:3;
for(let y=0;y<H;y+=st)for(let x=0;x<W;x+=st)
if(d[(y*W+x)*4+3]>140)pts.push(x,y);The number of particles is fixed for the screen size, about 5800 on desktop and 3000 on a narrow screen. If a word gives fewer targets than there are particles, the leftover ones become faint dust that wanders around the page.
Spring, damping and a push
Each particle has a position, a velocity and a target. Every frame it gets an acceleration toward its target, like a weak spring. If the pointer is within a radius, a repulsion is added, strongest at the centre and falling off with the square of the distance, plus a little sideways swirl so the dots curl around the cursor instead of fleeing in straight lines. Then the velocity is multiplied by a damping factor below one, which stops the spring from ringing forever.
let ax=(p.tx-p.x)*k, ay=(p.ty-p.y)*k;
const dx=p.x-mx, dy=p.y-my, d2=dx*dx+dy*dy;
if(d2<R2){
const d=Math.sqrt(d2)||1, f=Math.pow(1-d/R,2)*(reduce?2:9);
ax+=dx/d*f-dy/d*f*.45; ay+=dy/d*f+dx/d*f*.45;
}
p.vx=(p.vx+ax)*damp; p.vy=(p.vy+ay)*damp;
p.x+=p.vx; p.y+=p.vy;The excerpt is trimmed: the real code gives the leftover dust a much weaker spring. In the file the spring constant k is 0.04 and the damping 0.89, which I found by trying values until it felt like ink rather than rubber. For reduced motion they change to a stiffer spring and heavier damping, so the dots snap into place instead of floating.
One word per stage
The page has five stages, and scroll progress picks the current one. When the stage changes, I sample the new word and write new targets into the same particles. Nothing is created or destroyed, so the old word dissolves into the new one. Each particle also gets a small random kick so the change reads as a burst, which is skipped for reduced motion.
Sampling a word means scanning a full-screen offscreen canvas, so it happens on a stage change and on resize, never every frame. I have not profiled it on slower phones, so that is the first place I would look if it ever stutters.