explaingit

chenglou/pretext

📈 Trending47,134TypeScriptAudience · developerComplexity · 2/5ActiveLicenseSetup · easy

TLDR

Measure text dimensions in JavaScript without triggering expensive browser layout recalculations. Uses Canvas to sample fonts, then calculates dimensions in pure math.

Mindmap

mindmap
  root((repo))
    What it does
      Measure text size
      Avoid layout reflow
      Handle word wrapping
    How it works
      Canvas font sampling
      Pure arithmetic math
      Zero DOM calls
    Use cases
      Virtual lists
      Masonry layouts
      Rich text editors
      Canvas rendering
    Tech stack
      TypeScript
      Canvas API
      JavaScript
    Audience
      Web developers
      Performance engineers

Things people build with this

USE CASE 1

Build virtual lists that know item heights before rendering, preventing layout shifts.

USE CASE 2

Create masonry layouts and prevent scroll-jumping when new content loads.

USE CASE 3

Render text to Canvas or SVG without DOM access, or in server-side environments.

USE CASE 4

Implement custom rich-text editors and complex typography layouts with precise text dimensions.

Tech stack

TypeScriptJavaScriptCanvas API

Getting it running

Difficulty · easy Time to first run · 5min
Use freely for any purpose including commercial, as long as you keep the copyright notice.

In plain English

Pretext is a JavaScript and TypeScript library that solves a fundamental problem in web development: measuring how much space a block of text will occupy before actually rendering it on screen. In a browser, the standard way to know a text element's height is to insert it into the page and measure it with methods like getBoundingClientRect. This approach forces the browser to recalculate the entire page layout, an expensive operation called \"layout reflow.\" Pretext bypasses this entirely by implementing its own text measurement logic in pure JavaScript, using the HTML Canvas API under the hood to sample character widths from the real font, then doing all subsequent calculations in pure arithmetic with no DOM involvement. The library handles the genuinely hard parts of text layout: word wrapping across languages (including Arabic, Chinese, Japanese, and emoji), correct line-breaking rules, whitespace normalization, letter spacing, and hard line breaks. You call prepare() once on a piece of text to do the expensive font measurement, then call layout() repeatedly with different container widths, for example, as a user resizes a window, at negligible cost. This matters practically for building virtual lists (where you need to know an item's height before rendering it to avoid guesswork), masonry layouts, preventing scroll-jumping when new content loads, flow text around floating images, and rendering text to Canvas, SVG, or server-side environments where the DOM is unavailable. You would use Pretext when building anything where you need precise text dimensions without a layout reflow penalty, custom rich-text editors, data-heavy dashboards, complex typography layouts, or any performance-sensitive UI with many text elements. The library is written in TypeScript, installable via npm, and has zero DOM dependencies for its core measurement path.

Copy-paste prompts

Prompt 1
Show me how to use Pretext to measure text height for a virtual scrolling list without triggering layout reflow.
Prompt 2
How do I integrate Pretext into a React component to handle text wrapping across different container widths?
Prompt 3
Use Pretext to render text to a Canvas element with proper word wrapping and line breaks.
Prompt 4
Explain how Pretext handles text measurement for languages like Arabic, Chinese, and Japanese without DOM access.
Prompt 5
How can I use Pretext to prevent scroll-jumping when loading new text content dynamically?
Open on GitHub → Explain another repo

Generated 2026-05-18 · Model: sonnet-4-6 · Verify against the repo before relying on details.