Home > Web Front-end > JS Tutorial > Basic tutorial on using JavaScript to draw graphics using Canvas

Basic tutorial on using JavaScript to draw graphics using Canvas

高洛峰
Release: 2016-12-08 15:02:55
Original
1609 people have browsed it

Since HTML5 has become very popular in the past two years, I did some research. Recently, I had an idea to use HTML-related functions, so I also need to learn it carefully.

I took a good look at the functions of Canvas. I feel that HTML5 is becoming more and more functional in client-side interaction. Today I took a look at Canvas drawing. Here are a few examples. Note them down for future use.

1. Use Canvas to draw a straight line:

Basic tutorial on using JavaScript to draw graphics using Canvas

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
  </head>
  <style type="text/css">
    canvas{border:dashed 2px #CCC}
  </style>
  <script type="text/javascript">
    function $$(id){
      return document.getElementById(id);
    }
    function pageLoad(){
      var can = $$(&#39;can&#39;);
      var cans = can.getContext(&#39;2d&#39;);
      cans.moveTo(20,30);//第一个起点
      cans.lineTo(120,90);//第二个点
      cans.lineTo(220,60);//第三个点(以第二个点为起点)
      cans.lineWidth=3;
      cans.strokeStyle = &#39;red&#39;;
      cans.stroke();
    }
  </script>
  <body onload="pageLoad();">
    <canvas id="can" width="400px" height="300px">4</canvas>
  </body>
</html>
Copy after login

The two API methods used here, moveTo and lineTo are the starting and ending coordinates of the line segment respectively, and the variables are (X coordinate, Y coordinate), strokeStyle and stroke respectively. Path drawing styles and drawing paths.

2. Draw gradient lines

Basic tutorial on using JavaScript to draw graphics using Canvas

Gradient lines have a gradient effect in color. Of course, the gradient style can follow the direction of the path or not:

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
  </head>
  <style type="text/css">
    canvas{border:dashed 2px #CCC}
  </style>
  <script type="text/javascript">
    function $$(id){
      return document.getElementById(id);
    }
    function pageLoad(){
      var can = $$(&#39;can&#39;);
      var cans = can.getContext(&#39;2d&#39;);
      cans.moveTo(0,0);
      cans.lineTo(400,300);
      var gnt1 = cans.createLinearGradient(0,0,400,300);//线性渐变的起止坐标
      gnt1.addColorStop(0,&#39;red&#39;);//创建渐变的开始颜色,0表示偏移量,个人理解为直线上的相对位置,最大为1,一个渐变中可以写任意个渐变颜色
      gnt1.addColorStop(1,&#39;yellow&#39;);
      cans.lineWidth=3;
      cans.strokeStyle = gnt1;
      cans.stroke();
    }
  </script>
  <body onload="pageLoad();">
    <canvas id="can" width="400px" height="300px">4</canvas>
  </body>
</html>
Copy after login

3. Draw a rectangle or square:

Basic tutorial on using JavaScript to draw graphics using Canvas

If you use HTML4, this kind of rectangular frame can only be generated using the background code. Now the Canvas function provided by HTML5 can be easily drawn, so the superiority of HTML5 is quite high.

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
  </head>
  <style type="text/css">
    canvas{border:dashed 2px #CCC}
  </style>
  <script type="text/javascript">
    function $$(id){
      return document.getElementById(id);
    }
    function pageLoad(){
      var can = $$(&#39;can&#39;);
      var cans = can.getContext(&#39;2d&#39;);
      cans.fillStyle = &#39;yellow&#39;;
      cans.fillRect(30,30,340,240);
    }
  </script>
  <body onload="pageLoad();">
    <canvas id="can" width="400px" height="300px">4</canvas>
  </body>
</html>
Copy after login

A method is used here - fillRect(). From the literal meaning, we can know that this is to fill a rectangle. The parameters here are worth explaining. fillRect(X, Y, Width, Height), this is not the same as the coordinates in mathematics. Same, please see

Basic tutorial on using JavaScript to draw graphics using Canvas

here for details. The X and Y here start from the starting point relative to the upper left corner of the Canvas. Remember! !

4. Draw a simple rectangular box

The above example talks about drawing a rectangular block and filling it with color. This example simply draws a rectangle without realizing the filling effect.

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
  </head>
  <style type="text/css">
    canvas{border:dashed 2px #CCC}
  </style>
  <script type="text/javascript">
    function $$(id){
      return document.getElementById(id);
    }
    function pageLoad(){
      var can = $$(&#39;can&#39;);
      var cans = can.getContext(&#39;2d&#39;);
      cans.strokeStyle = &#39;red&#39;;
      cans.strokeRect(30,30,340,240);
    }
  </script>
  <body onload="pageLoad();">
    <canvas id="can" width="400px" height="300px">4</canvas>
  </body>
</html>
Copy after login

This is very simple, just like the above example, just replace fill with stroke. See the above example for details.

5. Draw a linear gradient rectangle

Gradient is a pretty good effect of filling. Combining Example 2 and Example 3, we can create a gradient rectangle

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
  </head>
  <style type="text/css">
    canvas{border:dashed 2px #CCC}
  </style>
  <script type="text/javascript">
    function $$(id){
      return document.getElementById(id);
    }
    function pageLoad(){
      var can = $$(&#39;can&#39;);
      var cans = can.getContext(&#39;2d&#39;);
      var gnt1 = cans.createLinearGradient(10,0,390,0);
      gnt1.addColorStop(0,&#39;red&#39;);
      gnt1.addColorStop(0.5,&#39;green&#39;);
      gnt1.addColorStop(1,&#39;blue&#39;);
      cans.fillStyle = gnt1;
      cans.fillRect(10,10,380,280);
    }
  </script>
  <body onload="pageLoad();">
    <canvas id="can" width="400px" height="300px">4</canvas>
  </body>
</html>
Copy after login

I won’t explain it, remember fillRect(X, Y, Width, Height) will do.

6. Fill a circle

Circles are widely used, and of course they also include ovals.

Basic tutorial on using JavaScript to draw graphics using Canvas

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
  </head>
  <style type="text/css">
    canvas{border:dashed 2px #CCC}
  </style>
  <script type="text/javascript">
    function $$(id){
      return document.getElementById(id);
    }
    function pageLoad(){
      var can = $$(&#39;can&#39;);
      var cans = can.getContext(&#39;2d&#39;);
      cans.beginPath();
      cans.arc(200,150,100,0,Math.PI*2,true);
      cans.closePath();
      cans.fillStyle = &#39;green&#39;;//本来这里最初使用的是red,截图一看,傻眼了,怕上街被爱国者打啊,其实你懂的~~
      cans.fill();
    }
  </script>
  <body onload="pageLoad();">
    <canvas id="can" width="400px" height="300px">4</canvas>
  </body>
</html>
Copy after login

The usage of arc method here is arc(X,Y,Radius,startAngle,endAngle,anticlockwise), which means (center X coordinate, center Y coordinate, radius, start angle (radians), end angle radian , whether to draw clockwise); Comparison of parameters in

arc:

a, cans.arc(200,150,100,0,Math.PI,true);

Basic tutorial on using JavaScript to draw graphics using Canvas

c, cans.arc(200,150,100,0, Math.PI/2,true);[/code]

Basic tutorial on using JavaScript to draw graphics using Canvas

c、cans.arc(200,150,100,0,Math.PI/2,true);

Basic tutorial on using JavaScript to draw graphics using Canvas

d、cans.arc(200,150,100, 0,Math.PI/2,false);

Basic tutorial on using JavaScript to draw graphics using Canvas

7. Circular block

Basic tutorial on using JavaScript to draw graphics using Canvas

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
  </head>
  <style type="text/css">
    canvas{border:dashed 2px #CCC}
  </style>
  <script type="text/javascript">
    function $$(id){
      return document.getElementById(id);
    }
    function pageLoad(){
      var can = $$(&#39;can&#39;);
      var cans = can.getContext(&#39;2d&#39;);
      cans.beginPath();
      cans.arc(200,150,100,0,Math.PI*2,false);
      cans.closePath();
      cans.lineWidth = 5;
      cans.strokeStyle = &#39;red&#39;;
      cans.stroke();
    }
  </script>
  <body onload="pageLoad();">
    <canvas id="can" width="400px" height="300px">4</canvas>
  </body>
</html>
Copy after login

I won’t explain it here. Same as the example above, lineWidth controls the width of the line.

8. Circular Gradient

Basic tutorial on using JavaScript to draw graphics using Canvas

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
  </head>
  <style type="text/css">
    canvas{border:dashed 2px #CCC}
  </style>
  <script type="text/javascript">
    function $$(id){
      return document.getElementById(id);
    }
    function pageLoad(){
      var can = $$(&#39;can&#39;);
      var cans = can.getContext(&#39;2d&#39;);
      var gnt = cans.createRadialGradient(200,300,50,200,200,200);
      gnt.addColorStop(1,&#39;red&#39;);
      gnt.addColorStop(0,&#39;green&#39;);
      cans.fillStyle = gnt;
      cans.fillRect(0,0,800,600);
    }
  </script>
  <body onload="pageLoad();">
    <canvas id="can" width="800px" height="600px">4</canvas>
  </body>
</html>
Copy after login

Basic tutorial on using JavaScript to draw graphics using Canvas

What needs to be explained here is the createRadialGradient method, the parameters are (Xstart, Ystart, radiusStart, XEnd, YEnd, radiusEnd), that is to say, when it implements the gradient, Two circles are used, one is the original circle and the other is the gradient circle. In fact, this method of coordinate and radius control can achieve many styles, such as

three-dimensional circle

Basic tutorial on using JavaScript to draw graphics using Canvas

var gnt = cans.createRadialGradient(200,150,0,200,50,250);
gnt.addColorStop(0,&#39;red&#39;);
gnt.addColorStop(1,&#39;#333&#39;);
Copy after login

The above is the JavaScript implementation The content of the basic tutorial on drawing graphics with Canvas. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


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