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
string of text.
x-coordinate of the text.
y-coordinate of the text.
font size. Defaults to the current textSize().