pretext

chenglou

A pure JavaScript and TypeScript library for measuring and laying out multiline text quickly and accurately, supporting scripts most libraries ignore, and rendering to the DOM, Canvas or SVG.

TypeScriptCreated

What is chenglou/pretext?

Pretext is a pure JavaScript and TypeScript library for measuring and laying out multiline text. Its key idea is to avoid DOM measurements such as getBoundingClientRect and offsetHeight, which force the browser to recalculate layout and are expensive. The README says it is fast and accurate, supports many writing systems, and can render to the DOM, to Canvas and to SVG.

Because it does its measurement in code, it can lay out text before it reaches the page, which helps with virtualised lists, animations and responsive typography. The project is published as a library with documentation in the repository.

It suits front-end engineers building text-heavy interfaces, graphics developers who render text on Canvas or SVG, and anyone who has struggled with layout thrashing caused by measuring text in the browser.

canvasjavascriptline-breakingocclusiontext-layouttext-measurementtypescripttypography