Front-end developers know the crime scene well: an animation stutters, the profiler points at getBoundingClientRect, and the culprit is layout thrashing โ JavaScript forcing the browser to recalculate layout dozens of times per frame. The Pretext Project ("Typeflow Arena") is an interactive experiment asking a pointed question: what if text measurement and layout never touched the DOM at all?
The Idea
Two small functions carry the whole thesis:
prepare(text, font)โ measures text using an offscreen canvas andmeasureText, with results memoized in a cache so repeated measurements cost nothing.layout(handle, containerWidth, lineHeight)โ computes word-wrapped line breaks and block height in pure arithmetic. No elements are created, queried, or measured. No reflow can occur, because the DOM is never asked anything.
The Playground
To prove the approach holds up under stress, the demo throws it into an animation arena: text particles flying across a full-screen canvas in six physics modes โ Burst, Rain, Wave, Orbit, Spiral, and Columns. Each particle renders its own measured bounding box, line-break tick marks, and a width ร height metric tag, all computed by the pretext functions live.
You control everything: four typefaces, font size from 16 to 96 pixels, container width, particle count, colours (including a per-particle rainbow mode). A metrics panel tracks text width, line count, block height, cumulative layout operations, FPS โ and a "DOM reflows" counter that stays at zero no matter how hard you push.
Why It Matters in Real Work
Any UI that animates lots of text โ captions, tickers, data visualisations, canvas-based editors โ eventually hits the measure-layout wall. The pattern demonstrated here is directly liftable: measure once offscreen, cache aggressively, and drive the render loop with math. The browser's layout engine is a wonderful tool; the point is knowing when not to invoke it.
Play with it: Pretext Project โ Typeflow Arena.