prepare() is the one-time measurement pass. It normalizes whitespace, segments the text via Intl.Segmenter, measures each segment with canvas measureText, and caches the widths. The result is an opaque handle you pass to layout().
Signature
Parameters
The text string to measure. Whitespace is normalized according to the
whiteSpace option (defaults to CSS white-space: normal behavior — collapsible spaces, no preserved newlines).CSS font shorthand in the same format as
canvasContext.font. Must exactly match your CSS font declaration for the element being measured — including size, weight, style, and family.Examples: "16px Inter", "bold 14px Arial", "italic 500 18px \"Helvetica Neue\"".Optional configuration object.
Returns
An opaque handle. Do not inspect its contents — pass it directly to
layout(). The handle is width-independent: the same PreparedText can be laid out at any maxWidth and lineHeight without re-preparing.Performance
On the current benchmark snapshot,prepare() takes approximately 19ms for a batch of 500 texts. Call it once when a text block first appears (e.g. when a comment loads), then call layout() on every resize.
Whitespace modes
For standard paragraph text, the default'normal' mode is correct. For textarea content where newlines and tabs must be preserved, pass { whiteSpace: 'pre-wrap' }:
Font accuracy note
Thefont string must be an exact match for the CSS font shorthand on the measured element. A mismatch in weight, size, or family will produce incorrect widths.