Reference textWidth()

textWidth()

Calculates the width of the given text string in pixels.

The textWidth() function processes the provided text string to determine its tight bounding box based on the current text properties such as font, textSize, and textStyle. Internally, it splits the text into individual lines (if line breaks are present) and computes the bounding box for each line using the renderer’s measurement functions. The final width is determined as the maximum width among all these lines.

For example, if the text contains multiple lines due to wrapping or explicit line breaks, textWidth() will return the width of the longest line.

Examples

Syntax

textWidth(text)

Parameters

text
String: The text to measure

Returns

Number: The width of the text
Notice any errors or typos? Please let us know. Please feel free to edit src/type/textCore.js and open a pull request!

Related References