search
HomeWeb Front-endH5 TutorialHTML5 Canvas Drawing - Use Canvas to draw graphics and text tutorials Use html5 canvas to draw beautiful pictures_html5 tutorial skills

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. html>  
  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. html>  
  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. html>  
  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. html>  
  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. html>  
  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. html>  
  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. html>  
  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. html>  
  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
Is H5 a Shorthand for HTML5? Exploring the DetailsIs H5 a Shorthand for HTML5? Exploring the DetailsApr 14, 2025 am 12:05 AM

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5: Commonly Used Terms in Web DevelopmentH5 and HTML5: Commonly Used Terms in Web DevelopmentApr 13, 2025 am 12:01 AM

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

What Does H5 Refer To? Exploring the ContextWhat Does H5 Refer To? Exploring the ContextApr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

H5: Tools, Frameworks, and Best PracticesH5: Tools, Frameworks, and Best PracticesApr 11, 2025 am 12:11 AM

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

The Legacy of HTML5: Understanding H5 in the PresentThe Legacy of HTML5: Understanding H5 in the PresentApr 10, 2025 am 09:28 AM

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5 Code: Accessibility and Semantic HTMLH5 Code: Accessibility and Semantic HTMLApr 09, 2025 am 12:05 AM

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.

Is h5 same as HTML5?Is h5 same as HTML5?Apr 08, 2025 am 12:16 AM

"h5" and "HTML5" are the same in most cases, but they may have different meanings in certain specific scenarios. 1. "HTML5" is a W3C-defined standard that contains new tags and APIs. 2. "h5" is usually the abbreviation of HTML5, but in mobile development, it may refer to a framework based on HTML5. Understanding these differences helps to use these terms accurately in your project.

What is the function of H5?What is the function of H5?Apr 07, 2025 am 12:10 AM

H5, or HTML5, is the fifth version of HTML. It provides developers with a stronger tool set, making it easier to create complex web applications. The core functions of H5 include: 1) elements that allow drawing graphics and animations on web pages; 2) semantic tags such as, etc. to make the web page structure clear and conducive to SEO optimization; 3) new APIs such as GeolocationAPI support location-based services; 4) Cross-browser compatibility needs to be ensured through compatibility testing and Polyfill library.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment