Text
To draw text on the canvas, use the drawText()
method.
The resulting text on the canvas is determined by the value of the text
property, as well as any of the following font properties:
fontStyle
fontSize
fontFamily
Font Sizes
The value for the fontSize
property accepts two different types of values. If you specify the value as a plain number (e.g. 48
), the font size is interpreted in pixels.
However, you can also specify the number as a string with additional units attached (e.g. '36pt'
). Examples of both use cases can be found throughout this section.
Transforming text
Just like other shapes, the drawText()
method respects the value of the fromCenter
and transformation properties such as rotate
.
Scaling text
You can animate the font size of text using the scale
, scaleX
, or scaleY
properties.
Measuring text
The measureText()
method returns an object containing the text’s calculated width
and height
(as well as of its other properties).
The method accepts a layer identifier (either the layer’s index, or its name). The method also accepts an object of properties (the same properties you use when calling the drawText()
method) as the only argument.
In the example below, the circle is only as wide as the text inside of it.
Wrapping text
You can also define a maximum line width for the text using the maxWidth
property. In doing so, each line of text will never be longer than the maximum width (this is known as text ‘wrapping’).
Manually adding a line break can be done so by adding the standard newline character in your string (\n
).
Aligning text
If you wish to align your text to the left
or right
(rather than the center
by default), use the align
property.
Aligning text to a margin
With the above align
property, the text will, by default, appear to align the text to either the left or right, but will still be centered relative to its x
and y
coordinates.
The respectAlign
property will ensure that the text’s (x, y) coordinates are in line with either the left or right margin (depending of the value of the align
property). Therefore, enabling this property will require you to adjust your x
position accordingly.
Changing line height
The lineHeight
property will change the line height of your text. The value of this property is a multiple of the default line height (which is 1
).
For example, the demo below will double the line height of the text it draws.
A line height with a negative value will swap any lines of text (e.g. the top line becomes the bottom line).
Note that a negative line height will produce a negative number when retrieving the height of the text (using the measureText()
method).
Text along an arc
jCanvas can also draw arc text (that is, text along an arc). Doing so only requires specifying one extra property: the radius
property.
For arc text only, there exists an optional letterSpacing
which controls the spacing between characters along the arc.
Technically speaking, the value of the letterSpacing
property represents a multiple of pi, which in turn represents the angular distance between each letter. For example, a value of 0.05
implies that each character is 9 degrees apart (0.05π is equivalent to 9°)
Flipping arc text
Sometimes, you may wish to orient the text such that it curves upward (like a smile). Normally, attempting to do so will yield undesirable flipped text.
However, you can set the flipArcText
property to true
and the text will be flipped to be readable again.