TextMetrics
The TextMetrics interface represents the dimensions of a piece of text in the canvas; a TextMetrics instance can be retrieved using the CanvasRenderingContext2D.measureText() method.
Properties
-
TextMetrics.widthRead only -
Is a
doublegiving the calculated width of a segment of inline text in CSS pixels. It takes into account the current font of the context. -
TextMetrics.actualBoundingBoxLeftRead only -
Is a
doublegiving the distance from the alignment point given by theCanvasRenderingContext2D.textAlignproperty to the left side of the bounding rectangle of the given text, in CSS pixels. The distance is measured parallel to the baseline. -
TextMetrics.actualBoundingBoxRightRead only -
Is a
doublegiving the distance from the alignment point given by theCanvasRenderingContext2D.textAlignproperty to the right side of the bounding rectangle of the given text, in CSS pixels. The distance is measured parallel to the baseline. -
TextMetrics.fontBoundingBoxAscentRead only -
Is a
doublegiving the distance from the horizontal line indicated by theCanvasRenderingContext2D.textBaselineattribute to the top of the highest bounding rectangle of all the fonts used to render the text, in CSS pixels. -
TextMetrics.fontBoundingBoxDescentRead only -
Is a
doublegiving the distance from the horizontal line indicated by theCanvasRenderingContext2D.textBaselineattribute to the bottom of the bounding rectangle of all the fonts used to render the text, in CSS pixels. -
TextMetrics.actualBoundingBoxAscentRead only -
Is a
doublegiving the distance from the horizontal line indicated by theCanvasRenderingContext2D.textBaselineattribute to the top of the bounding rectangle used to render the text, in CSS pixels. -
TextMetrics.actualBoundingBoxDescentRead only -
Is a
doublegiving the distance from the horizontal line indicated by theCanvasRenderingContext2D.textBaselineattribute to the bottom of the bounding rectangle used to render the text, in CSS pixels. -
TextMetrics.emHeightAscentRead only -
Is a
doublegiving the distance from the horizontal line indicated by theCanvasRenderingContext2D.textBaselineproperty to the top of the em square in the line box, in CSS pixels. -
TextMetrics.emHeightDescentRead only -
Is a
doublegiving the distance from the horizontal line indicated by theCanvasRenderingContext2D.textBaselineproperty to the bottom of the em square in the line box, in CSS pixels. -
TextMetrics.hangingBaselineRead only -
Is a
doublegiving the distance from the horizontal line indicated by theCanvasRenderingContext2D.textBaselineproperty to the hanging baseline of the line box, in CSS pixels. -
TextMetrics.alphabeticBaselineRead only -
Is a
doublegiving the distance from the horizontal line indicated by theCanvasRenderingContext2D.textBaselineproperty to the alphabetic baseline of the line box, in CSS pixels. -
TextMetrics.ideographicBaselineRead only -
Is a
doublegiving the distance from the horizontal line indicated by theCanvasRenderingContext2D.textBaselineproperty to the ideographic baseline of the line box, in CSS pixels.
Examples
Baselines illustrated
This example demonstrates the baselines the TextMetrics object holds. The default baseline is the alphabeticBaseline. See also the CanvasRenderingContext2D.textBaseline property.
HTML
<canvas id="canvas" width="550" height="500"></canvas>
JavaScript
const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const baselinesAboveAlphabetic = ['fontBoundingBoxAscent', 'actualBoundingBoxAscent', 'emHeightAscent', 'hangingBaseline']; const baselinesBelowAlphabetic = ['ideographicBaseline', 'emHeightDescent', 'actualBoundingBoxDescent', 'fontBoundingBoxDescent']; const baselines = [...baselinesAboveAlphabetic, ...baselinesBelowAlphabetic]; ctx.font = '25px serif'; ctx.strokeStyle = 'red'; baselines.forEach(function (baseline, index) { let text = 'Abcdefghijklmnop (' + baseline + ')'; let textMetrics = ctx.measureText(text); let y = 50 + index * 50; ctx.beginPath(); ctx.fillText(text, 0, y); let lineY = y - Math.abs(textMetrics[baseline]); if (baselinesBelowAlphabetic.includes(baseline)) { lineY = y + Math.abs(textMetrics[baseline]); } ctx.moveTo(0, lineY); ctx.lineTo(550, lineY); ctx.stroke(); });
Result
Measuring text width
When measuring the x-direction of a piece of text, the sum of actualBoundingBoxLeft and actualBoundingBoxRight can be wider than the width of the inline box (width), due to slanted/italic fonts where characters overhang their advance width.
It can therefore be useful to use the sum of actualBoundingBoxLeft and actualBoundingBoxRight as a more accurate way to get the absolute text width:
const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const text = 'Abcdefghijklmnop'; ctx.font = 'italic 50px serif'; const textMetrics = ctx.measureText(text); console.log(textMetrics.width); // 459.8833312988281 console.log(Math.abs(textMetrics.actualBoundingBoxLeft) + Math.abs(textMetrics.actualBoundingBoxRight)); // 462.8833333333333
Specifications
| Specification |
|---|
| HTML Standard (HTML) # textmetrics |
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
TextMetrics |
1 |
12 |
1.5 |
9 |
9 |
4 |
≤37 |
18 |
31 |
10.1 |
3.2 |
1.0 |
actualBoundingBoxAscent |
77 |
79 |
74 |
No |
64 |
11.1 |
77 |
77 |
No |
55 |
11.3 |
12.0 |
actualBoundingBoxDescent |
77 |
79 |
74 |
No |
64 |
11.1 |
77 |
77 |
No |
55 |
11.3 |
12.0 |
actualBoundingBoxLeft |
77 |
79 |
74 |
No |
64 |
11.1 |
77 |
77 |
No |
55 |
11.3 |
12.0 |
actualBoundingBoxRight |
77 |
79 |
74 |
No |
64 |
11.1 |
77 |
77 |
No |
55 |
11.3 |
12.0 |
alphabeticBaseline |
Yes |
79 |
74 |
No |
No |
11.1 |
No |
Yes |
No |
No |
11.3 |
No |
emHeightAscent |
Yes |
79 |
74 |
No |
No |
11.1 |
No |
Yes |
No |
No |
11.3 |
No |
emHeightDescent |
Yes |
79 |
74 |
No |
No |
11.1 |
No |
Yes |
No |
No |
11.3 |
No |
fontBoundingBoxAscent |
87
Yes
|
79 |
74 |
No |
73 |
11.1 |
87 |
87
Yes
|
No |
No |
11.3 |
14.0 |
fontBoundingBoxDescent |
87
Yes
|
79 |
74 |
No |
73 |
11.1 |
87 |
87
Yes
|
No |
No |
11.3 |
14.0 |
hangingBaseline |
Yes |
79 |
74 |
No |
No |
11.1 |
No |
Yes |
No |
No |
11.3 |
No |
ideographicBaseline |
Yes |
79 |
74 |
No |
No |
11.1 |
No |
Yes |
No |
No |
11.3 |
No |
width |
4 |
12 |
1.5 |
9 |
9 |
4 |
Yes |
Yes |
31 |
Yes |
3.2 |
Yes |
See also
- Creator method in
CanvasRenderingContext2D - The
<canvas>element and its associated interface,HTMLCanvasElement
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/TextMetrics