Reference textBounds()

textBounds()

Returns the bounding box for a string of text written using the font.

The bounding box is the smallest rectangle that can contain a string of text. font.textBounds() returns an object with the bounding box's location and size. For example, calling font.textBounds('p5*js', 5, 20) returns an object in the format { x: 5.7, y: 12.1 , w: 9.9, h: 28.6 }. The x and y properties are always the coordinates of the bounding box's top-left corner.

The first parameter, str, is a string of text. The second and third parameters, x and y, are the text's position. By default, they set the coordinates of the bounding box's bottom-left corner. See textAlign() for more ways to align text.

The fourth parameter, fontSize, is optional. It sets the font size used to determine the bounding box. By default, font.textBounds() will use the current textSize().

Examples

Syntax

textBounds(str, x, y, [fontSize])

Parameters

str
String:

string of text.

x
Number:

x-coordinate of the text.

y
Number:

y-coordinate of the text.

fontSize
Number:

font size. Defaults to the current textSize().

Returns

Object: object describing the bounding box with properties x, y, w, and h.
Notice any errors or typos? Please let us know. Please feel free to edit src/typography/p5.Font.js and open a pull request!

Related References