Home > Web Front-end > H5 Tutorial > body text

How to draw text using HTML5 canvas

不言
Release: 2020-10-21 09:20:25
Original
5996 people have browsed it

How to use HTML5canvas to draw text: first create the corresponding HTML sample file; then use the fillText method to draw the filled text on the canvas.

How to draw text using HTML5 canvas

If you want to use HTML5 Canvas to draw text, you need to use the fillText() method of the canvas context. Let’s look at the specific content below.

HTML5 canvas

Let’s look at the specific example first

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title></title>
  <meta charset="utf-8"/>
  <script type="text/javascript">
  function draw() {
    var canvas = document.getElementById(&#39;SimpleCanvas&#39;);
    if ( ! canvas || ! canvas.getContext ) {
      return false;
    }
    var context = canvas.getContext(&#39;2d&#39;);
    context.font = &#39;normal 18pt "楷体"&#39;;
    context.fillText(&#39;Hello HTML Canvas World!&#39;, 60, 200);
}
  </script>
</head>
<body onload="draw()" style="background-color:#D0D0D0;">
  <canvas id="SimpleCanvas" width="640" height="480" style="background-color:#FFFFFF;"></canvas>
  <div>Canvas Demo</div>
</body>
</html>
Copy after login

Explanation:

The following code obtains the canvas object and obtains the context .

var canvas = document.getElementById(&#39;SimpleCanvas&#39;);
  if ( ! canvas || ! canvas.getContext ) {
    return false;
  }
  var context = canvas.getContext(&#39;2d&#39;);
Copy after login

Below is the code for drawing characters. Specifies the font information for the characters to be drawn in the font property. Use the fillText() method to draw a string on the canvas. The string to be drawn as the first argument, the X and Y coordinates of the start of the drawing are given to the second and third arguments.

context.font = &#39;normal 18pt "楷体"&#39;;
context.fillText(&#39;Hello HTML Canvas World!&#39;, 60, 200);
Copy after login

Running results

Use a web browser to display the above HTML file. Obtain the display result shown below.

HTML5 canvas

Next let’s look at how to change the color of the text

The code is as follows

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title></title>
  <meta charset="utf-8" />
  <script type="text/javascript">
  function draw() {
    var canvas = document.getElementById(&#39;SimpleCanvas&#39;);
    if ( ! canvas || ! canvas.getContext ) {
      return false;
    }
    var context = canvas.getContext(&#39;2d&#39;);
    context.font = &#39;normal 18pt "楷体"&#39;;
    context.fillStyle = &#39;red&#39;;
    context.fillText(&#39;你好,PHP中文网!!!&#39;, 60, 200);
}
  </script>
</head>
<body onload="draw()" style="background-color:#D0D0D0;">
  <canvas id="SimpleCanvas" width="640" height="360" style="background-color:#FFFFFF;"></canvas>
  <div>Canvas Demo</div>
</body>
</html>
Copy after login

Instructions:

Change the text Color, you need to set the fillStyle attribute to the text color.

 context.font = &#39;normal 18pt "楷体"&#39;;
 context.fillStyle = &#39;red&#39;;
 context.fillText(&#39;你好,PHP中文网!!!&#39;, 60, 200);
Copy after login

Running results:

Use a web browser to display the above HTML file. You will get the effect as shown in the picture below, with red fonts drawn.

HTML5 canvas

The above is the detailed content of How to draw text using HTML5 canvas. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template