To draw a text on the canvas in HTML5, use the strokeText() method. Here’s the syntax of the strokeText() method. It strokes the given text at the given position, which is indicated by the given x and y coordinates. The default color is black.
strokeText(text, x, y [, maxWidth ] )
Here are the parameter values of the strokeText() method −
S. No | Parameter | Description |
1 | Text | The text to be written |
2 | X | x coordinate where the painting starts |
3 | Y | x coordinate where the painting starts |
4 | maxWidth | Maximum allowed width in pixels |
Example
You can try to run the following code to draw a text with strokeText() in HTML5 −
<!DOCTYPE html> <html> <head> <title>HTML5 Canvas strokeText()</title> </head> <body> <canvas id="newCanvas" width="400" height="150" style="border:1px solid #000000;"> </canvas> <script> var c = document.getElementById("newCanvas"); var ctxt = c.getContext("2d"); ctxt.fillStyle = '#00F'; ctxt.font = "20px Georgia"; ctxt.strokeText("Tutorialspoint", 10, 50); ctxt.fillStyle = '#Cff765'; ctxt.font = "30px Verdana"; ctxt.strokeText("Simply Easy Learning!", 10, 90); </script> </body> </html>