Quick start
Copy src/pool.ts into your project, or hit Copy pool.ts in the playground. Give it an element whose CSS sets the size, font and color, with room for the ink to spread:
.glyph {
width: 600px; /* about 2× the font size */
height: 500px; /* and 1.7× */
font: 700 300px/1 "Poppins", sans-serif;
color: #111;
}
import { inkPool } from "./pool";
const pool = inkPool(document.querySelector<HTMLElement>(".glyph")!);
pool.show("A"); // the first glyph appears as is
pool.show("B"); // A pools into B
Recipes
Cycle words. show resolves once its glyph settles, or false if something replaced it first.
for (let i = 0; ; i++) {
await pool.show(words[i % words.length]);
await sleep(1500);
}
Your settings. Tune them in the playground and hit Copy settings to get the ones you changed. set eases to new ones over 500ms; { ease: 0 } snaps.
const pool = inkPool(el, { spread: 0.1, sink: 12 });
pool.set({ hardness: 8 });
Sentences wrap and shrink to fit. If something covers part of the element, room says where text may go, in px down it.
const pool = inkPool(el, {}, { room: () => ({ top: 80, bottom: 420 }) });
pool.show("Ink runs into ink");
Freeze it at start, melting, pooled, forming or settled, or anywhere from 0 to 1. The stops fall in different places for different settings, and pool.phases() says where.
pool.hold("pooled");
pool.hold(null); // lets it finish
Run it up the page. The glyph turns inside the element and grows to its height, so make the element tall. A sentence keeps its lines.
pool.set({ turn: 1 });
Your own shapes
Pass draw and draw anything, in any color. If it’s smaller than size, return the share (0.5, say) and the effect scales with it.
import { inkPool, type Draw } from "./pool";
const draw: Draw = (ctx, key, { width, height, size }) => {
ctx.beginPath();
ctx.arc(width / 2, height / 2, size * (key === "big" ? 0.45 : 0.2), 0, 7);
ctx.fill();
};
const pool = inkPool(el, {}, { draw });
pool.show("big");
React
Make it once, show when the text changes, teardown when it unmounts.
import { useEffect, useRef } from "react";
import { inkPool, type Pool } from "./pool";
export const InkGlyph = ({ text }: { text: string }) => {
const el = useRef<HTMLDivElement>(null);
const pool = useRef<Pool | null>(null);
useEffect(() => {
pool.current = inkPool(el.current!);
return () => pool.current?.teardown();
}, []);
useEffect(() => {
pool.current?.show(text);
}, [text]);
return <div ref={el} className="glyph" role="img" aria-label={text} />;
};
API
| Call | Does |
|---|---|
inkPool(el, settings, options) |
Starts a pool in el. Both after el are optional. |
pool.show(glyph, { replace }) |
Shows a glyph after any still morphing; replace starts it now. |
pool.hold(stop) |
Freezes it at a stop. null lets go. |
pool.set(settings, { ease }) |
Eases to the settings you pass. ease is in ms; 0 snaps. |
pool.phases() |
Where each stop falls in a morph, as shares of it. |
pool.teardown() |
Stops it and removes the canvas. |
Options: draw, room, queue (how many glyphs can wait, 3; 0 cuts a morph short instead), and onPhase / onProgress to follow a morph.
Settings
| Setting | Default | Range | Does |
|---|---|---|---|
duration |
950 |
150–3000ms | How long a morph takes. |
spread |
0.05 |
0–0.15em | How far the ink spreads. |
sink |
7 |
0–30% | How far the old glyph drops on the way out. |
rise |
4 |
0–30% | How far above its spot the new one starts. |
hardness |
24 |
1–60 | How sharp the edge is. Low gets smoky. |
threshold |
0.42 |
0.1–0.9 | Where the edge falls. Lower is bolder. |
rest |
0 |
0–0.04em | Blur when it’s sitting still. |
opacity |
1 |
0.02–1 | Overall opacity. |
fadeStart |
100 |
0–100% | Fades out downward from here. 100 is off. |
fadeEnd |
100 |
0–100% | Gone by here. |
breathe |
0 |
0–6s | Settles dimmer at rest over this long, and brightens as the next morph starts. 0 is off. |
turn |
0 |
0–1 | 1 runs it up the page. |
The playground’s presets make good starting points: Glass (the defaults), Backdrop (faint, for behind copy), Syrup, Drip, Crisp, Mercury and Smoke.
Good to know
- The ink is the element’s
color, and the pool picks up changes to it, so dark mode needs no code. - With reduced motion, glyphs swap without the morph. Without WebGL2, each one shows at rest.
- The canvas is hidden from screen readers, so label the element.
- A morph cut short picks up the pool where it is.
- Agents can read llms.txt, or llms-full.txt for this README and the source in one file.
Performance
It runs on the GPU, but only while a glyph is morphing and on screen: a pool at rest, scrolled away or in a background tab does no work.
- No limit on how many. Browsers allow only about 16 WebGL contexts at once (Chrome drops the least used past that), so every pool on a page draws through one shared WebGL2 context onto its own 2D canvas. If the browser takes that context away, each pool keeps its last frame and shows new glyphs at rest; if it doesn’t come back within a second, a new context takes over.
- Cost grows with area, not count. On a 2× screen a pool holds about 1MB of GPU memory at icon size, 10MB for a 600×200 heading and 55MB full screen (1440×900).
- Frames. A full-screen morph takes about 2–3ms of GPU time a frame on an M-series Mac, well inside 90fps (11ms) and 120fps (8ms); a GPU several times slower should still hold 90fps. The first frame of a new glyph costs a little more, since that glyph is drawn then.
- How it stays light. The blur runs on a smaller image, with two pixels per read. Glyphs are kept as alpha only, four at most. A glyph 96px or larger on a dense screen is drawn and blurred at half the pixels, and only the final cut, which makes the hard edge, runs at full resolution, so it shows as sharp.
- What to avoid. Several full-screen pools morphing at once, or one morphing nonstop on a phone, where it costs battery like any animation that size.
teardown()frees what a pool held on the GPU, so mounting and unmounting is safe.
[!NOTE] I’ve mostly tested it in Chrome. Safari and Firefox should be fine, but check.
The playground
npm install
npm run dev
An Astro page around src/pool.ts. Pick a set (Words, Lines, Type or Shapes), then press any key to write it, ← → to step through the set, or click the glyph for another. The turn button runs words and lines up the page.
Beside the glyph, the presets change the feel, and the phases show where each morph is as its dot runs down them. Click one to hold the morph there. Settings has every value, with Copy settings and Copy pool.ts.
npm run check typechecks and formats; npm test runs the morph timeline on a fake clock.
License
MIT. Poppins and DM Mono are under the SIL Open Font License.