TextMetrics
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
Note: This feature is available inWeb Workers.
TheTextMetrics interface represents the dimensions of a piece of text in the canvas; aTextMetrics instance can be retrieved using theCanvasRenderingContext2D.measureText() method.
In this article
Instance properties
TextMetrics.widthRead onlyReturns the width of a segment of inline text in CSS pixels. It takes into account the current font of the context.
TextMetrics.actualBoundingBoxLeftRead onlyDistance parallel to the baseline from the alignment point given by the
CanvasRenderingContext2D.textAlignproperty to the left side of the bounding rectangle of the given text, in CSS pixels; positive numbers indicating a distance going left from the given alignment point.TextMetrics.actualBoundingBoxRightRead onlyReturns the distance from the alignment point given by the
CanvasRenderingContext2D.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 onlyReturns the distance from the horizontal line indicated by the
CanvasRenderingContext2D.textBaselineattribute to the top of the highest bounding rectangle of all the fonts used to render the text, in CSS pixels.TextMetrics.fontBoundingBoxDescentRead onlyReturns the distance from the horizontal line indicated by the
CanvasRenderingContext2D.textBaselineattribute to the bottom of the bounding rectangle of all the fonts used to render the text, in CSS pixels.TextMetrics.actualBoundingBoxAscentRead onlyReturns the distance from the horizontal line indicated by the
CanvasRenderingContext2D.textBaselineattribute to the top of the bounding rectangle used to render the text, in CSS pixels.TextMetrics.actualBoundingBoxDescentRead onlyReturns the distance from the horizontal line indicated by the
CanvasRenderingContext2D.textBaselineattribute to the bottom of the bounding rectangle used to render the text, in CSS pixels.TextMetrics.emHeightAscentRead onlyReturns the distance from the horizontal line indicated by the
CanvasRenderingContext2D.textBaselineproperty to the top of theem square in the line box, in CSS pixels.TextMetrics.emHeightDescentRead onlyReturns the distance from the horizontal line indicated by the
CanvasRenderingContext2D.textBaselineproperty to the bottom of theem square in the line box, in CSS pixels.TextMetrics.hangingBaselineRead onlyReturns the distance from the horizontal line indicated by the
CanvasRenderingContext2D.textBaselineproperty to the hanging baseline of the line box, in CSS pixels.TextMetrics.alphabeticBaselineRead onlyReturns the distance from the horizontal line indicated by the
CanvasRenderingContext2D.textBaselineproperty to thealphabetic baseline of the line box, in CSS pixels.TextMetrics.ideographicBaselineRead onlyReturns the distance from the horizontal line indicated by the
CanvasRenderingContext2D.textBaselineproperty to the ideographic baseline of the line box, in CSS pixels.
Examples
>Baselines illustrated
This example demonstrates the baselines theTextMetrics object holds. The default baseline is thealphabeticBaseline. See also theCanvasRenderingContext2D.textBaseline property.
HTML
<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((baseline, index) => { const text = `Abcdefghijklmnop (${baseline})`; const textMetrics = ctx.measureText(text); const y = 50 + index * 50; ctx.beginPath(); ctx.fillText(text, 0, y); const baselineMetricValue = textMetrics[baseline]; if (baselineMetricValue === undefined) { return; } const lineY = baselinesBelowAlphabetic.includes(baseline) ? y + Math.abs(baselineMetricValue) : y - Math.abs(baselineMetricValue); 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 ofactualBoundingBoxLeft andactualBoundingBoxRight 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 ofactualBoundingBoxLeft andactualBoundingBoxRight 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.8833312988281console.log( textMetrics.actualBoundingBoxRight + textMetrics.actualBoundingBoxLeft,);// 462.8833333333333Specifications
| Specification |
|---|
| HTML> # textmetrics> |
Browser compatibility
See also
- Creator method in
CanvasRenderingContext2D - The
<canvas>element and its associated interface,HTMLCanvasElement