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).

Messagesword effects
  1. We got the flat
  2. That is huge news
  3. I am still shaking
  4. Party on Friday, no excuses
meet at the harbour at six, ok?

Enter sends. The field is one text node; effects are offsets.

 

Show the code the markup, the poses, the ranges, the slices, the stamp, the paint, the composer
  1. The markup. A <form> with an <ol> of messages, a contenteditable composer, a Send button and a row of effect buttons, as the one drawable child of a 2D canvas. The tagged words are offsets in data-fx.
  2. Styles. Every bubble and the field have one flat colour, so a glyph box holds only background and glyph.
  3. The poses. Each effect is a function of time, glyph index and a seed, giving an offset, scale, rotation, alpha and glow.
  4. The ranges. Text offsets become Range objects over the element's text nodes, one per grapheme.
  5. 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.
  6. The stamp. One drawImage() from the atlas per glyph, under its own transform.
  7. The paint. After the card is drawn: rebuild what changed, paint over the tagged words with their background colour, stamp every glyph.
  8. 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.
  9. 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