


HTML5 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:
- >
- <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 = $$('can');
- var cancans = can.getContext('2d');
- cans.moveTo(20,30);//第一个起点
- cans.lineTo(120,90);//第二个点
- cans.lineTo(220,60);//第三个点(以第二个点为起点)
- cans.lineWidth=3;
- cans.strokeStyle = 'red';
- cans.stroke();
- }
- script>
- <body onload="pageLoad( );">
- <canvas id="can" width="400px" height="300px">4canvas>
- body>
- 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:
- >
- <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 = $$('can');
- var cancans = can.getContext('2d');
- cans.moveTo(0,0);
- cans.lineTo(400,300);
- var gnt1 = cans.createLinearGradient(0,0,400,300);//线性渐变的起止坐标
- gnt1.addColorStop(0,'red');//创建渐变的开始颜色,0表示偏移量,个人理解为直线上的相对位置,最大为1,一个渐变中可以写任意个渐变颜色
- gnt1.addColorStop(1,'yellow');
- cans.lineWidth=3;
- cans.strokeStyle = gnt1;
- cans.stroke();
- }
- script>
- <body onload="pageLoad( );">
- <canvas id="can" width="400px" height="300px">4canvas>
- body>
- 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.
- >
- <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 = $$('can');
- var cancans = can.getContext('2d');
- cans.fillStyle = 'yellow';
- cans.fillRect(30,30,340,240);
- }
- script>
- <body onload="pageLoad();">
- <canvas id="can" width="400px" height="300px">4canvas>
- body>
- 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 detailsThe 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.
- >
- <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 = $$('can');
- var cancans = can.getContext('2d');
- cans.strokeStyle = 'red';
- cans.strokeRect(30,30,340,240);
- }
- script>
- <body onload="pageLoad();">
- <canvas id="can" width="400px" height="300px">4canvas>
- body>
- html>
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
- >
- <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 = $$('can');
- var cancans = can.getContext('2d');
- var gnt1 = cans.createLinearGradient(10,0,390,0);
- gnt1.addColorStop(0,'red');
- gnt1.addColorStop(0.5,'green');
- gnt1.addColorStop(1,'blue');
- cans.fillStyle = gnt1;
- cans.fillRect(10,10,380,280);
- }
- script>
- <body onload="pageLoad( );">
- <canvas id="can" width="400px" height="300px">4canvas>
- body>
- html>
No need to explain, just remember fillRect(X,Y,Width,Height).
6. Fill a circle
Circular shapes are widely used, including ovals.
- >
- <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 = $$('can');
- var cancans = can.getContext('2d');
- cans.beginPath();
- cans.arc(200,150,100,0,Math.PI*2,true);
- cans.closePath();
- cans.fillStyle = 'green';//本来这里最初使用的是red,截图一看,傻眼了,怕上街被爱国者打啊,其实你懂的~~
- cans.fill();
- }
- script>
- <body onload="pageLoad( );">
- <canvas id="can" width="400px" height="300px">4canvas>
- body>
- 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
- >
- <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 = $$('can');
- var cancans = can.getContext('2d');
- cans.beginPath();
- cans.arc(200,150,100,0,Math.PI*2,false);
- cans.closePath();
- cans.lineWidth = 5;
- cans.strokeStyle = 'red';
- cans.stroke();
- }
- script>
- <body onload="pageLoad( );">
- <canvas id="can" width="400px" height="300px">4canvas>
- body>
- html>
I won’t explain it here. Same as the above example, lineWidth controls the width of the line.
8. Circular gradient
- >
- <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 = $$('can');
- var cancans = can.getContext('2d');
- var gnt = cans.createRadialGradient(200,300,50,200,200,200);
- gnt.addColorStop(1,'red');
- gnt.addColorStop(0,'green');
- cans.fillStyle = gnt;
- cans.fillRect(0,0,800,600);
- }
- script>
- <body onload="pageLoad( );">
- <canvas id="can" width="800px" height="600px">4canvas>
- body>
- 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
- var gnt = cans.createRadialGradient(200,150,0,200,50,250);
- gnt.addColorStop(0,'red');
- gnt.addColorStop(1,'#333');

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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.

The steps to create an H5 click icon include: preparing a square source image in the image editing software. Add interactivity in the H5 editor and set the click event. Create a hotspot that covers the entire icon. Set the action of click events, such as jumping to the page or triggering animation. Export H5 documents as HTML, CSS, and JavaScript files. Deploy the exported files to a website or other platform.

H5 is not a standalone programming language, but a collection of HTML5, CSS3 and JavaScript for building modern web applications. 1. HTML5 defines the web page structure and content, and provides new tags and APIs. 2. CSS3 controls style and layout, and introduces new features such as animation. 3. JavaScript implements dynamic interaction and enhances functions through DOM operations and asynchronous requests.

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

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.

Yes, H5 page production is an important implementation method for front-end development, involving core technologies such as HTML, CSS and JavaScript. Developers build dynamic and powerful H5 pages by cleverly combining these technologies, such as using the <canvas> tag to draw graphics or using JavaScript to control interaction behavior.

H5 pop-up window creation steps: 1. Determine the triggering method (click, time, exit, scroll); 2. Design content (title, text, action button); 3. Set style (size, color, font, background); 4. Implement code (HTML, CSS, JavaScript); 5. Test and deployment.
