HTML | canvas strokeText() Method

We can draw text (with no fill) on the canvas, by using strokeText() Method.Basically from this method we can render the specified text at the specified position by using the current font, lineWidth, and strokeStyle property.The default color of the text is black.
Syntax:

context.strokeText(text, x, y, maxWidth);

Parameter value:: This method accepts four parameter which are mentioned above and described below:

  • text: This parameter specifies the text that will be written on the canvas.
  • x: This parameter specifies the horizontal coordinate to start painting the text, relative to the canvas.
  • y: This parameter specifies the vertical coordinate to start painting the text, relative to the canvas.
  • maxWidth: This parameter specifies the maximum possible text width.

Example 1: Below example illustrate the strokeTex() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML canvas strokeText() Method
    </title>
</head>
  
<body>
    <center>
        <h1 style="color:green"
        GeeksforGeeks 
    </h1>
        <h4
        HTML canvas strokeText() Method 
    </h4>
        <canvas id="GFG" width="500" height="200">
        </canvas>
        <script>
            var c = document.getElementById("GFG");
            var ctx = c.getContext("2d");
            ctx.font = "60px Arial";
            ctx.strokeStyle = "green";
            ctx.strokeText("GeeksforGeeks", 50, 50);
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML canvas strokeText() Method
    </title>
</head>
  
<body>
    <center>
        <h1 style="color:green"
          GeeksforGeeks 
        </h1>
        <h4
          HTML canvas strokeText() Method 
        </h4>
        <canvas id="GFG" width="500" height="200">
        </canvas>
        <script>
            var c = document.getElementById("GFG");
            var ctx = c.getContext("2d");
            ctx.font = "60px Georgia";
            ctx.strokeStyle = "blue";
            ctx.strokeText("HTML Canvas", 60, 50);
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:

Supported Browsers: The browsers supported by HTML canvas strokeText() Method are listed below:

  • Google Chrome
  • Internet Explorer 9.0
  • Firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Budding Web DeveloperKeen learnerAverage CoderDancer&Social Activist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.