Canvas preview Ready
Line data 0 lines
# Text Width (px) Fill %
Generated code
 

How Pretext works

This page runs @chenglou/pretext in your browser (bundled with the site). When you edit text or sliders:

  1. prepare(text, font) — measures via Canvas and caches character widths (no layout tree).
  2. layout(prepared, width, lineHeight) — returns height and lineCount.
  3. layoutWithLines — returns each line’s text and width for tables and Canvas drawing.

Advanced mode uses prepareWithSegments + layoutWithLines throughout. See Canvas snippets and shape flows.

DOM measurement vs Pretext

Reading layout properties after DOM mutations can force synchronous reflow. Pretext stays on the Canvas measurement path for the hot loop.

Method 500 blocks (typical) Reflows
offsetHeight loop 15–30ms+ 500
@chenglou/pretext orders of magnitude less work on layout tree 0

Playground FAQ