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

May 16, 2016 pm 03:46 PM

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 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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run the h5 project How to run the h5 project Apr 06, 2025 pm 12:21 PM

Running the H5 project requires the following steps: installing necessary tools such as web server, Node.js, development tools, etc. Build a development environment, create project folders, initialize projects, and write code. Start the development server and run the command using the command line. Preview the project in your browser and enter the development server URL. Publish projects, optimize code, deploy projects, and set up web server configuration.

How do I use viewport meta tags to control page scaling on mobile devices? How do I use viewport meta tags to control page scaling on mobile devices? Mar 13, 2025 pm 08:00 PM

The article discusses using viewport meta tags to control page scaling on mobile devices, focusing on settings like width and initial-scale for optimal responsiveness and performance.Character count: 159

What exactly does H5 page production mean? What exactly does H5 page production mean? Apr 06, 2025 am 07:18 AM

H5 page production refers to the creation of cross-platform compatible web pages using technologies such as HTML5, CSS3 and JavaScript. Its core lies in the browser's parsing code, rendering structure, style and interactive functions. Common technologies include animation effects, responsive design, and data interaction. To avoid errors, developers should be debugged; performance optimization and best practices include image format optimization, request reduction and code specifications, etc. to improve loading speed and code quality.

How do I use the HTML5 Page Visibility API to detect when a page is visible? How do I use the HTML5 Page Visibility API to detect when a page is visible? Mar 13, 2025 pm 07:51 PM

The article discusses using the HTML5 Page Visibility API to detect page visibility, improve user experience, and optimize resource usage. Key aspects include pausing media, reducing CPU load, and managing analytics based on visibility changes.

How do I handle user location privacy and permissions with the Geolocation API? How do I handle user location privacy and permissions with the Geolocation API? Mar 18, 2025 pm 02:16 PM

The article discusses managing user location privacy and permissions using the Geolocation API, emphasizing best practices for requesting permissions, ensuring data security, and complying with privacy laws.

How do I use the HTML5 Drag and Drop API for interactive user interfaces? How do I use the HTML5 Drag and Drop API for interactive user interfaces? Mar 18, 2025 pm 02:17 PM

The article explains how to use the HTML5 Drag and Drop API to create interactive user interfaces, detailing steps to make elements draggable, handle key events, and enhance user experience with custom feedback. It also discusses common pitfalls to a

Does H5 page production require continuous maintenance? Does H5 page production require continuous maintenance? Apr 05, 2025 pm 11:27 PM

The H5 page needs to be maintained continuously, because of factors such as code vulnerabilities, browser compatibility, performance optimization, security updates and user experience improvements. Effective maintenance methods include establishing a complete testing system, using version control tools, regularly monitoring page performance, collecting user feedback and formulating maintenance plans.

What application scenarios are suitable for H5 page production What application scenarios are suitable for H5 page production Apr 05, 2025 pm 11:36 PM

H5 (HTML5) is suitable for lightweight applications, such as marketing campaign pages, product display pages and corporate promotion micro-websites. Its advantages lie in cross-platformity and rich interactivity, but its limitations lie in complex interactions and animations, local resource access and offline capabilities.

See all articles