pretext

Builds single-file browser demos using DOM-free text measurement and canvas layout.

Updated Jul 3, 2026
One-click install
npx skills add https://github.com/CHENHUI-X/toolbox --skill pretext-chenhui-x
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pretext
Source: https://github.com/CHENHUI-X/toolbox/tree/main/custom-skills/creative/pretext
Command: npx skills add https://github.com/CHENHUI-X/toolbox --skill pretext-chenhui-x

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires @chenglou/pretext, gsap, lil-gui, and includes references (resource) components.

What problem does it solve? Creating text layouts that flow around moving shapes, shatter into particles, or form game geometry requires knowing line breaks before rendering, which the DOM cannot provide without expensive reflows. This Skill uses the @chenglou/pretext library to measure multiline text via canvas and render it with full creative control. ## Core Features & Use Cases - Variable-width text reflow: Stream paragraphs around draggable sprites, ASCII logos, or morphing shapes at 60fps using layoutNextLineRange with per-row width functions. - Text-as-geometry demos: Build Breakout with word-bricks, shatter typography into per-grapheme particles, or drive kinetic type with per-line transforms. - Multiline shrink-wrap and measurement: Compute tightest container widths and exact heights without DOM reads for virtualized lists and masonry layouts. - Use Case: A user asks for a hero section where an editorial paragraph parts around a draggable glowing orb; the Skill produces a single self-contained HTML file using the hello-orb-flow template with a tuned palette and real prose corpus. ## Quick Start Ask the agent to build a pretext demo where a paragraph of real prose flows around a draggable orb, then open the generated HTML file with python -m http.server.

Frequently Asked Questions about pretext

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I make text flow around a moving shape in the browser?

Use pretext's layoutNextLineRange with a per-row width function that subtracts the obstacle's horizontal span at each y position. Call prepareWithSegments once, then stream lines each frame with a cursor, skipping rows where the corridor is too narrow.

What is the @chenglou/pretext library used for?

Pretext is a 15KB zero-dependency TypeScript library that measures multiline text via canvas without DOM reflow. Given text, font, and width, it returns line breaks, per-line widths, per-grapheme positions, and total height for custom rendering.

How do I import pretext in a browser without a build step?

Import from https://esm.sh/@chenglou/[email protected] inside a script type="module" tag, since esm.sh compiles the TypeScript exports to browser-ready ESM. Avoid unpkg, which may serve raw TypeScript or 404.

Why do pretext measurements not match my rendered text?

Measurement drift happens when the canvas font string differs from the rendered CSS font, often because a web font failed to load and CSS fell back to another family. Preload the font and keep the font string passed to prepare exactly in sync with the CSS.

When should I not use pretext for text layout?

Avoid pretext for static pages where CSS already solves layout, rich text editors, or general inline formatting engines, since the library is intentionally narrow. It fits cases needing line breaks known before rendering, like reflow around shapes or shrink-wrap sizing.

How do I split text into graphemes for particle effects?

Use Intl.Segmenter with granularity "grapheme" to split text, since naive string splitting breaks emoji, combining marks, and CJK characters. Combine with walkLineRanges or materializeLineRange to get per-glyph x and y positions for physics.