Home >Web Front-end >H5 Tutorial >HTML5 Canvas Drawing - Use Canvas to draw graphics and text tutorials Use html5 canvas to draw beautiful pictures_html5 tutorial skills

HTML5 Canvas Drawing - Use Canvas to draw graphics and text tutorials Use html5 canvas to draw beautiful pictures_html5 tutorial skills

WBOY
WBOYOriginal
2016-05-16 15:46:301723browse

HTML5 is very popular. Recently, I have an idea to use HTML-related functions, so I should learn it carefully.

After taking 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:



XML/HTML CodeCopy content to clipboard
  1. >  
  2. <html>  
  3.     <head>  
  4.         <meta charset="UTF-8">  
  5.     head>  
  6.     <style type="text/css">  
  7.         canvas{border:dashed 2px #CCC}   
  8.     style>  
  9.     <script type="text/javascript">  
  10.         function $$(id){   
  11.             return document.getElementById(id);   
  12.         }   
  13.         function pageLoad(){   
  14.             var can = $$('can');   
  15.             var cancans = can.getContext('2d');   
  16.             cans.moveTo(20,30);//第一个起点   
  17.             cans.lineTo(120,90);//第二个点   
  18.             cans.lineTo(220,60);//第三个点(以第二个点为起点)   
  19.             cans.lineWidth=3;   
  20.             cans.strokeStyle = 'red';   
  21.             cans.stroke();   
  22.         }
  23.  script> 
  24.  <body onload="pageLoad( );">
  25.  <canvas id="can" width="400px" height="300px">4canvas>
  26.  body> 
  27. html>

The two API methods used here, moveTo and lineTo are the starting point and end point coordinates of the line segment respectively, the variables are (X coordinate, Y coordinate), strokeStyle and stroke respectively path drawing style and drawing path.

2. Draw gradient lines

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

XML/HTML CodeCopy content to clipboard
  1. >  
  2. <html>  
  3.     <head>  
  4.         <meta charset="UTF-8">  
  5.     head>  
  6.     <style type="text/css">  
  7.         canvas{border:dashed 2px #CCC}   
  8.     style>  
  9.     <script type="text/javascript">  
  10.         function $$(id){   
  11.             return document.getElementById(id);   
  12.         }   
  13.         function pageLoad(){   
  14.             var can = $$('can');   
  15.             var cancans = can.getContext('2d');   
  16.             cans.moveTo(0,0);   
  17.             cans.lineTo(400,300);   
  18.             var gnt1 = cans.createLinearGradient(0,0,400,300);//线性渐变的起止坐标   
  19.             gnt1.addColorStop(0,'red');//创建渐变的开始颜色,0表示偏移量,个人理解为直线上的相对位置,最大为1,一个渐变中可以写任意个渐变颜色   
  20.             gnt1.addColorStop(1,'yellow');   
  21.             cans.lineWidth=3;   
  22.             cans.strokeStyle = gnt1;   
  23.             cans.stroke();   
  24.         }
  25.  script> 
  26.  <body onload="pageLoad( );">
  27.  <canvas id="can" width="400px" height="300px">4canvas>
  28.  body> 
  29. html>

3. Draw a rectangle or square:

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 advantages of HTML5 are quite high.

XML/HTML CodeCopy content to clipboard
  1. >  
  2. <html>  
  3.     <head>  
  4.         <meta charset="UTF-8">  
  5.     head>  
  6.     <style type="text/css">  
  7.         canvas{border:dashed 2px #CCC}   
  8.     style>  
  9.     <script type="text/javascript">  
  10.         function $$(id){   
  11.             return document.getElementById(id);   
  12.         }   
  13.         function pageLoad(){   
  14.             var can = $$('can');   
  15.             var cancans = can.getContext('2d');   
  16.             cans.fillStyle = 'yellow';   
  17.             cans.fillRect(30,30,340,240);   
  18.         }   
  19.     script>  
  20.     <body onload="pageLoad();">  
  21.         <canvas id="can" width="400px" height="300px">4canvas>  
  22.     body>  
  23. html>  

A method is used here - fillRect(). From the literal meaning, you 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

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.

XML/HTML CodeCopy content to clipboard
  1. >  
  2. <html>  
  3.     <head>  
  4.         <meta charset="UTF-8">  
  5.     head>  
  6.     <style type="text/css">  
  7.         canvas{border:dashed 2px #CCC}   
  8.     style>  
  9.     <script type="text/javascript">  
  10.         function $$(id){   
  11.             return document.getElementById(id);   
  12.         }   
  13.         function pageLoad(){   
  14.             var can = $$('can');   
  15.             var cancans = can.getContext('2d');   
  16.             cans.strokeStyle = 'red';   
  17.             cans.strokeRect(30,30,340,240);   
  18.         }   
  19.     script>  
  20.     <body onload="pageLoad();">  
  21.         <canvas id="can" width="400px" height="300px">4canvas>  
  22.     body>  
  23. html>  
  24.   

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

5. Draw a rectangle with linear gradient

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

XML/HTML CodeCopy content to clipboard
  1. >  
  2. <html>  
  3.     <head>  
  4.         <meta charset="UTF-8">  
  5.     head>  
  6.     <style type="text/css">  
  7.         canvas{border:dashed 2px #CCC}   
  8.     style>  
  9.     <script type="text/javascript">  
  10.         function $$(id){   
  11.             return document.getElementById(id);   
  12.         }   
  13.         function pageLoad(){   
  14.             var can = $$('can');   
  15.             var cancans = can.getContext('2d');   
  16.             var gnt1 = cans.createLinearGradient(10,0,390,0);   
  17.             gnt1.addColorStop(0,'red');   
  18.             gnt1.addColorStop(0.5,'green');   
  19.             gnt1.addColorStop(1,'blue');   
  20.             cans.fillStyle = gnt1;   
  21.             cans.fillRect(10,10,380,280);   
  22.         }
  23.  script> 
  24.  <body onload="pageLoad( );">
  25.  <canvas id="can" width="400px" height="300px">4canvas>
  26.  body> 
  27. html>

No need to explain, just remember fillRect(X,Y,Width,Height).

6. Fill a circle


Circular shapes are widely used, including ovals.

XML/HTML CodeCopy content to clipboard
  1. >  
  2. <html>  
  3.     <head>  
  4.         <meta charset="UTF-8">  
  5.     head>  
  6.     <style type="text/css">  
  7.         canvas{border:dashed 2px #CCC}   
  8.     style>  
  9.     <script type="text/javascript">  
  10.         function $$(id){   
  11.             return document.getElementById(id);   
  12.         }   
  13.         function pageLoad(){   
  14.             var can = $$('can');   
  15.             var cancans = can.getContext('2d');   
  16.             cans.beginPath();   
  17.             cans.arc(200,150,100,0,Math.PI*2,true);   
  18.             cans.closePath();   
  19.             cans.fillStyle = 'green';//本来这里最初使用的是red,截图一看,傻眼了,怕上街被爱国者打啊,其实你懂的~~   
  20.             cans.fill();   
  21.         }
  22.  script> 
  23.  <body onload="pageLoad( );">
  24.  <canvas id="can" width="400px" height="300px">4canvas>
  25.  body> 
  26. html>

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

Comparison of parameters in arc:

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

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

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

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

7. Circular block

XML/HTML CodeCopy content to clipboard
  1. >  
  2. <html>  
  3.     <head>  
  4.         <meta charset="UTF-8">  
  5.     head>  
  6.     <style type="text/css">  
  7.         canvas{border:dashed 2px #CCC}   
  8.     style>  
  9.     <script type="text/javascript">  
  10.         function $$(id){   
  11.             return document.getElementById(id);   
  12.         }   
  13.         function pageLoad(){   
  14.             var can = $$('can');   
  15.             var cancans = can.getContext('2d');   
  16.             cans.beginPath();   
  17.             cans.arc(200,150,100,0,Math.PI*2,false);   
  18.             cans.closePath();   
  19.             cans.lineWidth = 5;   
  20.             cans.strokeStyle = 'red';   
  21.             cans.stroke();   
  22.         }
  23.  script> 
  24.  <body onload="pageLoad( );">
  25.  <canvas id="can" width="400px" height="300px">4canvas>
  26.  body> 
  27. html>

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

8. Circular gradient

XML/HTML CodeCopy content to clipboard
  1. >  
  2. <html>  
  3.     <head>  
  4.         <meta charset="UTF-8">  
  5.     head>  
  6.     <style type="text/css">  
  7.         canvas{border:dashed 2px #CCC}   
  8.     style>  
  9.     <script type="text/javascript">  
  10.         function $$(id){   
  11.             return document.getElementById(id);   
  12.         }   
  13.         function pageLoad(){   
  14.             var can = $$('can');   
  15.             var cancans = can.getContext('2d');   
  16.             var gnt = cans.createRadialGradient(200,300,50,200,200,200);   
  17.             gnt.addColorStop(1,'red');   
  18.             gnt.addColorStop(0,'green');   
  19.             cans.fillStyle = gnt;   
  20.             cans.fillRect(0,0,800,600);   
  21.         }
  22.  script> 
  23.  <body onload="pageLoad( );">
  24.  <canvas id="can" width="800px" height="600px">4canvas>
  25.  body> 
  26. html>

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, it uses two circles, one is the original circle and the other It is a gradient circle. In fact, this method of coordinate and radius control can achieve many styles, such as

Three-dimensional circle

XML/HTML CodeCopy content to clipboard
  1. var gnt = cans.createRadialGradient(200,150,0,200,50,250);
  2. gnt.addColorStop(0,'red');
  3. gnt.addColorStop(1,'#333');
Statement:
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