YouTip LogoYouTip

Met Canvas Measuretext

# HTML canvas measureText() Method [![Image 8: Canvas Object Reference](#) Canvas Object](#) ## Example Check the width of the font before outputting text on the canvas: YourbrowserdoesnotsupporttheHTML5canvastag. 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 Β»](#) * * * ## Browser Support ![Image 9: Internet Explorer](#)![Image 10: Firefox](#)![Image 11: Opera](#)![Image 12: Google Chrome](#)![Image 13: Safari](#) Internet Explorer 9, Firefox, Opera, Chrome, and Safari support the measureText() method. **Note:** Internet Explorer 8 and earlier versions do not support the element. * * * ## Definition and Usage The measureText() method returns an object that contains the width of the specified font, in pixels. **Tip:** Use this method if you need to know the width of a text before outputting it to the canvas. | JavaScript Syntax: | _context_.measureText(_text_).width; | | --- | ## Parameter Values | Parameter | Description | | --- | --- | | _text_ | The text to be measured. | * * Canvas Object](#)
← Met Canvas DrawimageMet Canvas Stroketext β†’