Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

HTML | canvas measureText() Method

  • Last Updated : 10 Oct, 2019

The measureText() method is used to return an object that represents the width of the specified text in terms of pixels. This method can be used to get the width of the text before writing it on canvas.




  • text: This parameter specifies the text to be measured.


<!DOCTYPE html>
        HTML canvas measureText() Method
<body style="text-align:left;">
    <h2>HTML canvas measureText() Method</h2>
    <canvas id="GFG" width="500" height="200" style="border:2px solid">
        var doc_id = document.getElementById("GFG");
        var context = doc_id.getContext("2d");
        context.font = "30px solid";
        var txt = "GeeksforGeeks"
        context.fillText(txt, 180, 100);
        context.fillText("Width of GeeksforGeeks:" +
            context.measureText(txt).width, 0, 50);


Supported Browsers: The browser supported by HTML canvas measureText() Method are listed below:

  • Google Chrome
  • Internet Explorer 9.0
  • Firefox
  • Safari
  • Opera
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!