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

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support 9Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support

See also

Document Tags and Contributors

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