Typography as particles
Every letter below is a particle, and the paragraph is still one ordinary block of HTML that the canvas draws in pieces. I wanted to see if the text would survive that. Blow it apart, then select it, search for it, follow the link or edit the paragraph.
Letters that fly, words that stay words.
This paragraph is editable. Type into it and the new letters join the swarm. Nothing here was split into spans; each glyph is a small rectangle cut from one picture of the whole block.
Select any phrase and the chosen letters lift off the line. The HTML-in-canvas explainer is a real link, and find-in-page still finds everything. Emoji and accents travel as one piece: 👩🚀 café, naïve, fine.
Move the pointer over the text, or tap it, to push the letters around.
Show the code text as particles
- Wrap ordinary text in
<glyph-field>. No spans. Links, editing and selection keep working. - Call the motions.
- Inside the element: find every glyph without changing the markup. A
Rangearound each grapheme gives its rectangle withgetClientRects(). - Inside the element: draw the text once, then one piece of that picture per glyph.
drawElementImage()draws the block,createImageBitmap(canvas)keeps a copy, and each particle is adrawImage()crop of it. - Inside the element: say where the real text is. So selection, links and the caret stay on the line while the glyphs fly.
- The whole element, with the WebGPU and WebGL renderers.
Use it
<script type="module" src="/lib/glyph-field.js"></script>
<glyph-field pointer="repel">
<h1>Any markup at all</h1>
<p>Links, <em>emphasis</em>, even contenteditable.</p>
</glyph-field>
<script>
const field = document.querySelector('glyph-field');
await field.explode({ x: 100, y: 40 }); // resolves when the text is whole again
field.highlight('markup'); // matching glyphs lift off
</script>
Live checks
These run in your browser when this section is on screen, on the small fixtures in the box below. They don't touch the text above.
AVATAR link word
Findings
- The text is never split up.
Range.getClientRects()gives a rectangle per grapheme without touching the markup, and each particle is that rectangle cut from one picture of the whole block. Kerning, ligatures, emoji sequences, links and the accessibility tree are untouched. - First try, 2D: it worked and it was too slow.
drawElementImage(el, sx, sy, sw, sh, dx, dy, dw, dh)cuts a glyph out directly and is quick while glyphs stay upright. Give each glyph its own angle and every draw re-rasterises text at that angle; a thousand draws took over a second here. - Second try, WebGL: one texture, one draw call.
texElementSubImage2Duploads the block when the browser repaints it (thepaintevent says when), and the glyphs are instanced quads. Fast, but on an Android phone the canvas came out as a solid white block once the letters started moving. - Fourth, where it works: WebGPU. The same instanced quads in WGSL, with
queue.drawElementImageToTexture()for the block. It's picked when the browser has a real WebGPU adapter that passes a self-test and accepts the first frame; otherwise the page uses the 2D renderer below and the line under the renderer menu says why. - Third try, the fallback: 2D with a bitmap atlas. Draw the block into the 2D canvas, copy it with
createImageBitmap, clear, and draw each glyph withdrawImagefrom the copy, all inside onepainthandler so the intermediate picture never reaches the screen. Rotation is cheap again because the pieces are pixels. At rest the block is onedrawElementImage, so the text is sharp. - When the canvas fails, the element shows plain HTML. If the context is lost the element lifts its children out of the canvas and shows them as plain HTML until the context returns. A live test reads the canvas back and fails if it's blank or mostly solid.
- The selection is part of the picture. Selected glyphs carry their highlight when they fly.
selectionchangeplusRange.isPointInRangetells the element which particles are selected, so a selection or awindow.find()match can drive the motion. - Interaction stays where the text rests. The element's hit box is pinned to the home position with
updateElementGeometry, so selecting, clicking the link and placing the caret work there even mid-explosion. - A letter in the air can be pointed at, one pointer at a time. One element has one hit box, so the box follows the pointer: the letter under it is found from the particle list, and the whole block is placed with
updateElementGeometryso that letter's home sits exactly under the pointer, turn included. Hover, the caret and clicks then reach the link or paragraph the letter belongs to. A tap has no pointer move before it, so its click is aimed again and re-sent. Letters that have left the canvas margin aren't drawn and can't be hit. - The browser remembers what was at a point. After
updateElementGeometrymoves a box,elementFromPoint(and a click) at a point it has already been asked about still gives the old answer until something in the DOM changes. Toggling one attribute when the box moves clears it. - One drawable span per letter does get a hit box per letter, and inline spans nearly keep the shaping, but it puts an element around every character and each one is a separate draw.
- I don't know yet whether the browser's own find-in-page highlight is painted into the element's picture.
window.find()works and selects the match; the yellow find bar highlight needs a person at a real browser.