In this tutorial I am extending the previous animation example by better positioning the text using
measureText with the HTML5 canvas.
The problem with positioning text
When positioning text you will sometimes want to know the width and height so that you can centre it on the canvas. However this isn’t always that can easily be calculated due to differences in fonts and characters.
When specifying the size of font in pixels, the height of the text will be approximate to that size. This is because the tallest character will be set at that height. Depending on the difference between the smaller and larger characters this should allow you to approximately layout text vertically. This makes laying out a piece of text vertically relatively easy if a little inaccurate.
However the width of a piece of text depends on a variety of factors, not just the number of characters in the text. The simplest case is where the font is monospaced (all the same width). Then you can count the number of characters and the width of one character to work out the full width.
For non-monospaced fonts the differences in each character means that we need a helper function to help measure the width of a specific piece of text.
Measuring text with measureText
2dContext provides the function
measureText that can allow us to get information about the width of a piece of text.
When used this will draw the text using the current font at the current size and measure it. Then it will return an object giving the details of the width of this piece of text.
Using these dimensions I am able to modify my previous example to lay the text out in the middle of the canvas, regardless of the width of the text.
Modifying the previous example to use measureText
I am going to modify the previous example Creating a loading progress bar animation with HTML 5 canvas to centre the text.
Here I first decide what text I am going to draw and ask the context to measure it.
var percentageText = percentage + "%";
var textMeasurements = context.measureText(percentageText);[/code]
I am storing the text so that I don’t have to write it twice when I write out the text to the canvas.
Now I have the width I can easily place the text. Here I use 60 (half of the canvas) minus half the width of the text to place the text. This ensures that whatever the width of the text it will always be centred horizontally.
I use 70 as the y position as thats approximately half of the canvas plus half of the text height (60 + (20 / 2)).
Conclusions on measuring text with measureText
We can easily determine the height of text as we know it will be roughly the size of the text.
measureText we can ask the context to tell us the width of any piece of text. Using this and some simple calculations we can easily position a piece of text centrally on a canvas.
The example including the source code is available on my website.
If you have any questions or wish to learn more leave a comment below and I will endeavour to answer any questions.