Canvas measureText() Method

Definition and Usage

measureText() The method returns an object containing the specified font width in pixels.

Tip:If you need to know the width of the text before it is output to the canvas, please use this method.

Example

Before outputting text on the canvas, check the font width:

Your browser does not support the HTML5 canvas tag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.font="30px Arial";
var txt="Hello World"
ctx.fillText("width:" + ctx.measureText(txt).width,10,50)
ctx.fillText(txt,10,100);

try it yourself

grammar

context.measureText(text).width;

Parametre değeri

Parametre Açıklama
text Ölçülecek metin.

Tarayıcı desteği

Tablo sayısız, bu özelliği tam olarak destekleyen ilk tarayıcı sürümünü belirtmektedir.

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
4.0 9.0 3.6 4.0 10.1

Açıklama:Internet Explorer 8 ve daha eski sürümler <canvas> elementini desteklememektedir.