Pretext.JS Overview
Pretext.JS is a fast, pure JavaScript/TypeScript text layout engine designed to measure and position multiline text entirely through arithmetic, without ever touching the Document Object Model (DOM). It eliminates the performance bottleneck of "forced synchronous reflows" caused by traditional DOM measurements like getBoundingClientRect(), offering significantly faster and more efficient text layout. It's built on a "measure once, lay out forever" principle, making subsequent layout calculations instant.
Main Purpose
To provide highly performant, accurate, and reflow-free text layout measurements (such as height and line count) for dynamic and complex web interfaces. It solves the critical performance issue of browser thrashing caused by repeated DOM measurements.
Target User Group
Web developers and teams building performance-critical, text-heavy user interfaces. This includes applications with virtual scrolling lists, AI chat interfaces, code editors, collaborative whiteboards, data visualizations, and any scenario where dynamic text layout needs to be smooth, fast, and responsive without compromising user experience.
Function Details and Operations
Pretext.JS operates in two core steps:
-
prepare(text, font)
This function runs once for a given text string and font style. It normalizes whitespace, applies Unicode line-break rules, segments the text, and accurately measures each segment's glyph advance width using the browser's Canvas font engine. The results are cached in a reusable "handle." -
layout(handle, containerWidth, lineHeight)
This function is pure arithmetic and runs instantly. Given the prepared handle, a container width, and a line height, it computes line breaks by summing segment widths, calculates the total line count, and determines the overall text height. Crucially, this step performs zero DOM reads, ensuring no reflows.
Key Capabilities
-
Zero DOM Reads
After the initialprepare()call, alllayout()operations are pure arithmetic, completely avoidinggetBoundingClientRect(),offsetHeight, and forced synchronous reflows. -
Real Font Metrics
Utilizes the browser's native Canvas font engine to measure glyph widths, ensuring layout accuracy that matches actual browser rendering. -
Multilingual by Design
Provides full support for complex writing systems including CJK (Chinese, Japanese, Korean), Arabic, Hebrew, Thai, and Hindi, with correct Unicode segmentation and bidirectional text handling. -
TypeScript-Native
Developed from the ground up in TypeScript, offering precise types for all functions, parameters, and return values without requiring external@typespackages. -
Reusable Prepared Handles
A singleprepare()call generates a handle that can be reused to compute layout for various container widths (e.g., mobile, tablet, desktop) with minimal overhead. -
Zero Runtime Dependencies
Built entirely on standard browser APIs, ensuring a lean bundle size and no unexpected external package issues.
User Benefits
-
Exceptional Performance
Achieves up to 500x faster text layout compared to traditional DOM methods, enabling smooth 60fps scrolling and highly responsive UIs even with thousands of dynamic text elements. -
Enhanced User Experience
Eliminates UI "thrashing," shifts, and jumpiness, providing a fluid and stable visual experience, particularly in dynamic content feeds or chat interfaces. -
Accurate Layout
Guarantees precise text measurements that align with the browser's actual rendering, preventing visual inconsistencies. -
Simplified Development
Offers a straightforward, TypeScript-native API that abstracts away complex text measurement challenges, boosting developer productivity. -
Global Application Readiness
Robust support for diverse writing systems facilitates the creation of truly internationalized applications. -
Lightweight and Reliable
Zero external dependencies result in a smaller bundle size and greater stability.
Compatibility and Integration
-
Framework-Agnostic
As a pure JavaScript/TypeScript library, Pretext.JS can be seamlessly integrated into any modern web framework (e.g., React, Vue, Angular, Svelte) or vanilla JavaScript project. -
Modern Browser Support
Relies on standard browser APIs available in all modern web environments. -
Installation
Easily installable via popular package managers: npm, pnpm, and bun. -
TypeScript Integration
Native TypeScript support ensures smooth integration into TypeScript projects without additional configuration.
Real-World Applications and Demos
-
Virtual Scroll
Demonstrates rendering 10,000 variable-height rows with smooth 60fps scrolling, calculated entirely without DOM reads. -
AI Chat
Used to pre-compute chat bubble heights for streaming AI responses, eliminating layout shifts and jumpiness. -
Internationalization
Showcases accurate layout for multilingual content feeds mixing Chinese, Arabic, Korean, and Latin text in a single virtualized list. -
Community Showcases
Features creative applications like sand writing effects, geometric text visualizations, and real-time 3D object text wrapping, highlighting its versatility.
Access and Activation Method
-
Installation
- Using npm:
$ npm install @chenglou/pretext - Using pnpm:
$ pnpm add @chenglou/pretext - Using bun:
$ bun add @chenglou/pretext
- Using npm:
-
Usage
- Import the
prepareandlayoutfunctions:
import { prepare, layout } from '@chenglou/pretext' - Call
prepare(text, font)once to get a reusable handle. - Call
layout(handle, containerWidth, lineHeight)to instantly get the height and line count.
- Import the
-
Exploration
A live playground and various demos are available on the official website (pretextjs.dev) for immediate testing and understanding.