


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>
- 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>
- 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>
- 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>
- 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>
- 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>
- 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>
- 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>
- 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');

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

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

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.

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

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.

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

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.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

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
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 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
Powerful PHP integrated development environment