Pretext is a pure JavaScript/TypeScript library for multiline text measurement and layout that avoids DOM reflow by implementing its own text measurement logic using the browser's font engine via Canvas. It exposes two main use cases: measuring paragraph height without touching the DOM (useful for virtualization, masonry layouts, and scroll anchoring), and manually laying out paragraph lines for rendering to Canvas, SVG, or WebGL. The API separates a one-time preparation pass (text segmentation, bidi handling, canvas measurement) from a cheap arithmetic-based layout pass. It supports all languages including mixed-bidi text and emojis, and provides iterator-style APIs for variable-width line routing (e.g., flowing text around floated images).

7m read timeFrom github.com
Post cover image
Table of contents
InstallationDemosAPICaveatsDevelopCredits
5 Comments

Sort: