Reference textBounds()

textBounds()

Computes the tight bounding box for a block of text.

The textBounds() function calculates the precise pixel boundaries that enclose the rendered text based on the current text properties (such as font, textSize, textStyle, and alignment). If the text spans multiple lines (due to line breaks or wrapping), the function measures each line individually and then aggregates these measurements into a single bounding box. The resulting object contains the x and y coordinates along with the width (w) and height (h) of the text block.

Examples

Syntax

textBounds(str, x, y, [width], [height])

Parameters

str
String: The text string to measure.
x
Number: The x-coordinate where the text is drawn.
y
Number: The y-coordinate where the text is drawn.
width
Number: (Optional) The maximum width available for the text block. When specified, the text may be wrapped to fit within this width.
height
Number: (Optional) The maximum height available for the text block. Any lines exceeding this height will be truncated.

Returns

Object: An object with properties x, y, w, and h that represent the tight bounding box of the rendered 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