Message effects on single words, in a live text field
I wanted to know if the per-word effects from iOS 18 Messages could run on words inside a live text field, without splitting it into spans, so the caret, selection and IME keep working. Put the caret in a word, pick an effect and send.
After iOS 18 Messages and SwiftUI's TextRenderer (WWDC24 session 10151).
Show the code the markup, the poses, the ranges, the slices, the stamp, the paint, the composer
- The markup. A
<form>with an<ol>of messages, acontenteditablecomposer, a Send button and a row of effect buttons, as the one drawable child of a 2D canvas. The tagged words are offsets indata-fx. - Styles. Every bubble and the field have one flat colour, so a glyph box holds only background and glyph.
- The poses. Each effect is a function of time, glyph index and a seed, giving an offset, scale, rotation, alpha and glow.
- The ranges. Text offsets become
Rangeobjects over the element's text nodes, one per grapheme. - The slices. When text, tags or layout change, the glyph boxes are copied once with
drawImage()into a plain canvas, read, and keyed to alpha into a small atlas. - The stamp. One
drawImage()from the atlas per glyph, under its own transform. - The paint. After the card is drawn: rebuild what changed, paint over the tagged words with their background colour, stamp every glyph.
- The composer. Tags move with each edit, and a word is drawn as the browser drew it while the caret or a selection is in it.
- The card size. The form gets the canvas size in pixels from script, so its hit boxes match where it is drawn.
How it works
The card is an ordinary <form>: a list of sent messages, a contenteditable="plaintext-only" composer, a Send button and seven effect buttons. It's the one drawable child of a 2D canvas and is drawn at the canvas size with drawElementImage(form, 0, 0, width, height) once per paint. Nothing else is drawn with drawElementImage(), so every hit box is where the browser put it, and the composer takes focus, typing, selection and IME like any other editable element.
A tag is two numbers. Picking an effect stores the selection (or the word at the caret, from Intl.Segmenter) as a start and end offset in the composer's text. No <span> is added. Each edit moves the offsets along by comparing the text before and after it. A sent message is an <li> holding one text node, with its tags written into data-fx, for example data-fx="bloom 8-12".
Glyph boxes. For each tagged word, a Range is made over each grapheme and getClientRects() gives the glyph's box in the card. Those boxes are cut from the drawn canvas: when an element's text, tags or box change, the area around its tagged glyphs is copied once with drawImage() into a plain canvas made with willReadFrequently, and that copy is read once. Each texel's distance from the element's background colour towards its text colour becomes alpha, and the keyed glyphs go into a small atlas canvas. The drawable canvas is never read and never made with willReadFrequently.
Every frame. After the card is drawn, each tagged word's line boxes are painted over with the bubble's own colour, and each glyph is drawn back from the atlas with one drawImage() under its own translate, rotate and scale. Bloom swells the glyphs in a wave with a glow, Ripple runs a wave through them, Jitter shakes each glyph on its own, Explode throws them out and brings them back, Nod squashes them down onto the baseline and back, and Shake moves the word side to side. Everything loops on a 2.8 second clock. The poses are my own guesses from Apple's videos, not Apple's curves.
The composer stays native. A tagged word in the composer is drawn as the browser drew it while the caret is in it, while a selection covers it or while an IME is composing, so the caret, the highlight and the composition underline are never cut up. The effect buttons cancel pointerdown, so a tap keeps the focus and the selection in the field. With reduced motion on, each effect is drawn once at a fixed point of its loop.
Live checks
These run on their own copies of the card, on small canvases in the corner of the window. The hit test also probes the demo's own buttons without pressing them.
Findings
- Measured in Chrome Canary
161.0.8200.0on 2026-12-04 at 412 by 915 CSS pixels, device scale 2.625, software rendering (SwiftShader): all 11 live checks pass, with no script errors, no lost canvases, no wrong-size draws and no horizontal overflow. The demo canvas is 998 by 1,313 (1.31 MP, 5.2 MB on load). At 1280 by 900 and scale 1 all 11 pass as well. - Glyph boxes from
Range.getClientRects()line up with the drawn text. The six seeded words gave 4, 4, 7, 9, 7 and 3 slices, one per visible grapheme, 34 stamped a frame, and the composer still had 0 element children. - The stamps move the drawn pixels, not a copy beside them. With the clock frozen, Explode took the bright texels in the box of "flat" from 862 to 0 at 1.2 s and raised them around it from 2,380 to 3,830. The painted-over box matched the drawn bubble, rgb(44,44,46), to within 0. Bloom took the box around "huge" from 1,777 to 4,364 bright texels at 0.9 s.
- Tagging adds nothing to the DOM. Selecting "blue" and pressing Bloom stored the offsets 9 to 13 with 0 mutations in the field. A sent message is an
<li>with one text node anddata-fx="shake 9-13", and 1,782 texels of its bubble changed between 0 s and 0.035 s. - A selection over a tagged word hands that word back to the browser's own drawing: 4 glyphs stamped with no selection, 0 while "lu" inside it was selected.
setBaseAndExtent()on the fixture field did not give it focus, so the caret-in-word pause was not exercised by a check. - The controls are hit where they are drawn. The card matched the canvas height to 0.3 px on the fixture and 0.1 px on the demo, and
elementsFromPoint()found Send, the field and the bottom row of effect buttons at their centres and 1 px above the bottom edge, with 0.00 px vertical offset on both. - The drawable canvas is never read. Over a tag and a send the guard counted 0 reads of drawable canvases, 0 drawable canvases with
willReadFrequentlyand 0 unasked GPU readbacks, and the page read its plain copy twice, once per change. - With reduced motion two fresh copies gave the same frame, hash
2c1e8ba3both times, with 34 glyphs stamped. The effect pass (paint over and stamp) took 0.11 ms a frame on average and 0.20 ms at worst in the test browser. Not verified on a phone.