Home > Article > Web Front-end > How to draw the outline of text using HTML5 canvas
If you want to use HTML5 Canvas to draw the outline of text, we need to use the strokeText() method in the canvas context. Let’s look at the specific content below.
Let’s look at the specific example first
The code is as follows
Create the following HTML file
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <meta charset="utf-8" /> <style type="text/css"> <!-- /*背景颜色和背景图*/ .canvas { background-color: #FFFFFF; background-image: url("img/flower.jpg"); } --> </style> <script type="text/javascript"> function draw() { var canvas = document.getElementById('SimpleCanvas'); if ( ! canvas || ! canvas.getContext ) { return false; } var context = canvas.getContext('2d'); context.font = 'normal 28pt "楷体"'; context.strokeText('你好,PHP中文网!!!', 60, 200); } </script> </head> <body onload="draw()" style="background-color:#D0D0D0;"> <canvas id="SimpleCanvas" width="640" height="360" class="canvas"></canvas> <div>Canvas Demo</div> </body> </html>
Description:
The following will get the canvas object and get the context.
var canvas = document.getElementById('SimpleCanvas'); if ( ! canvas || ! canvas.getContext ) { return false; } var context = canvas.getContext('2d');
The following is the code for drawing characters. Specify the font information of the character to be drawn in the font property, use the strokeText() method to draw the string outline on the canvas, as the first parameter the string drawn, the X coordinate and Y coordinate of the drawing start are given to the second and The third parameter.
context.font = 'normal 28pt "楷体"'; context.strokeText('你好,PHP中文网!!!', 60, 200);
Running results
Use a web browser to display the above HTML file. You will get the display effect as shown below.
The above is the detailed content of How to draw the outline of text using HTML5 canvas. For more information, please follow other related articles on the PHP Chinese website!