Revolutionizing Text Layout with Pretext

For 30 years, we've been measuring text the same expensive way. Every React app. Every dashboard. Every UI you've shipped. When the browser needs to know where text ends, it calls getBoundingClientRect() — which halts everything, recalculates the entire page layout, and returns a number. That's layout reflow. It's costly, and we do it on every resize and every render. Meanwhile, magazines have flowed text around images for centuries. On the web? Not possible. The layout engine just doesn't work that way. A library called Pretext just changed that. The trick was canvas.measureText() — a Canvas API that's always been there. It measures text using the browser's own font engine, with zero DOM involvement. Nobody had built a full text layout engine on top of it. Pretext did. How it works: prepare() runs once — segments your text, measures glyph widths via canvas, caches everything. ~19ms for 500 texts. layout() is pure math over that cache. Zero DOM. ~0.09ms for 500 texts, every time after. The real unlock is layoutNextLine() — feed each line a different width as you go. Narrower beside an image. Full width below it. That's how magazine text wrapping works. First time it's possible on the web. What you can now build: → Text that wraps around floated images, line by line → Proper list virtualization — exact heights, no guessing → Multiline shrink-wrap — tightest container that fits your text → Render to DOM, Canvas, SVG, or WebGL Full language support — RTL, CJK, emojis, mixed-bidi, all handled. npm install @chenglou/pretext — Pure TypeScript. MIT. → https://lnkd.in/e_v6WBfE #webdev #javascript #typescript #frontend #opensource

  • graphical user interface, text, application, email

To view or add a comment, sign in

Explore content categories