HTML5 Canvas [29]
For our demonstration, instead of trying to use the font size to vertically center the text on the canvas, we will create the yPosition variable for the text by simply placing it at one-half the height of the canvas. The default baseline for a font is middle, so this works great for centering on the screen. We will talk more about baseline in the next section:
var yPosition = (theCanvas.height/2);
NOTE
In the chat example in Chapter 11, we will show you an example of breaking up text onto multiple lines.
fillText and strokeText
The context.fillText() function (as shown in Figure 3-1) will render solid colored text to the canvas. The color used is set in the context.fillColor property. The font used is set in the context.font property. The function call looks like this:
fillText([text],[x],[y],[maxWidth]);
where:
text
The text to render on the canvas.
x
The x position of the text on the canvas.
y
The y position of the text on the canvas.
maxWidth
The maximum width of the text as rendered on the canvas. At the time of this writing, support for this property was just being added to browsers.
Figure 3-1. fillText in action
The context.strokeText() function (as shown in Figure 3-2) is similar, but it specifies the outline of text strokes to the canvas. The color used to render the stroke is set in the context.strokeColor property; the font used is set in the context.font property. The function call looks like:
strokeText([text],[x],[y],[maxWidth])
where:
text
The text to render on the canvas.
x
The x position of the text on the canvas.
y
The y position of the text on the canvas.
maxWidth
The maximum width of the text as rendered on the canvas. At the time of this writing, this property does not appear to be implemented in any browsers.
Figure 3-2. strokeText setting outline properties
The next iteration of Text Arranger adds the ability for the user to select fillText, strokeText, or both. Selecting both will give the fillText text a black border (the strokeText). In the HTML