CanvasRenderingContext2D.measureText()

The CanvasRenderingContext2D.measureText() method returns a TextMetrics object that contains information about the measured text (such as its width for example).

Syntax

TextMetrics ctx.measureText(text);

Parameters

text
The text to measure.

Return value

A TextMetrics object.

Examples

Given this <canvas> element:

<canvas id="canvas"></canvas>

You can get a TextMetrics object using the following code:

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

var text = ctx.measureText('foo'); // TextMetrics object
text.width; // 16;

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'CanvasRenderingContext2D.measureText' in that specification.
Living Standard  

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support Yes12 Yes9 Yes Yes
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes Yes Yes Yes Yes Yes Yes

See also

Document Tags and Contributors

Contributors to this page: fscholz, thatmichael85, nmve, erikadoyle
Last updated by: fscholz,