1. Canvas tag:
1. The HTML5
2. The
3. There are many ways to draw paths, boxes, circles, characters and add images through Canvas.
2. Draw graphics with Canvas
1. Draw a rectangle
<br>
2. Draw a circle
## 3. moveTo and lineTo
4. Use bezierCurveTo to draw the Bezier curve
## 6. Draw a radial gradient
## 7. Draw deformed graphics
8. Draw graphics and synthesize globablCompositeOperation properties
Use different The globalCompositeOperation value draws the rectangle. The orange rectangle is the target image. The pink rectangle is the source image.
Definition and Usage
The globalCompositeOperation property sets or returns how to draw a source (new) image onto the target (existing) image.Source image = the drawing you intend to place on the canvas.
Target Image = The drawing you have placed on the canvas.
Attribute value:
值 | 属性 |
source-atop | <br>在先绘制的图形顶部显示后绘制的图形。后绘制的图形位于先绘制的图形之外的部分是不可见的。 |
source-in | 只绘制相交部分,由后绘制图形的填充覆盖,其余部分透明。 |
source-out | 只绘制后绘制图形不相交的部分,由后绘制图形的填充覆盖,其余部分透明。 |
source-over | 在先绘制的图形上显示后绘制的图形。相交部分由后绘制的图形填充(颜色,渐变,纹理)覆盖 |
destination-atop | 在后绘制的图形顶部显示先绘制的图形。源图像之外的目标图像部分不会被显示。 |
destination-in | 在后绘制的图形中显示先绘制的图形。只绘制相交部分,由先绘制图形的填充覆盖,其余部分透明 |
destination-out | 只有后绘制的图形外的目标图像部分会被显示,源图像是透明的。 |
destination-over | 相交部分由先绘制图形的填充(颜色,渐变,纹理)覆盖. |
lighter | 相交部分由根据先后图形填充来增加亮度。 |
copy | <br>显示后绘制的图形。只绘制后绘制图形。 |
xor | 相交部分透明 |
<br>
以上效果图的代码如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="../js/jquery-1.12.4.min.js"></script> <script> $(function(){ var options = new Array( "source-atop", "source-in", "source-out", "source-over", "destination-atop", "destination-in", "destination-out", "destination-over", "lighter", "copy", "xor" ); var str=""; for(var i=0;i<options.length;i++){ str = "<div id='p_"+i+"' style='float:left'>"+options[i]+"<br/> <canvas id='canvas"+i+"' width='120px' height='100px' style='border:1px solid #ccc;margin:10px 2px 20px;'> </canvas></div>"; $("body").append(str); var cas = document.getElementById('canvas'+i); var ctx = cas.getContext('2d'); ctx.fillStyle = "orange"; ctx.fillRect(10,10,50,50); ctx.globalCompositeOperation = options[i]; ctx.beginPath(); ctx.fillStyle = "pink"; ctx.arc(50,50,30,0,2*Math.PI); ctx.fill(); } }) </script> </head> <body></body> </html>
图形合成
9、给图形绘制阴影
代码如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas基础api</title> <style> canvas{ border:1px solid #ccc; margin:50px; } </style> <script src="../js/jquery-1.12.4.min.js"></script> <script> $(function(){ //获取标签 var cas = document.getElementById('canvas'); //获取绘制环境 var ctx = cas.getContext('2d'); ctx.fillStyle ="#eef"; ctx.fillRect(0,0,300,300); ctx.shadowOffsetX = 10; ctx.shadowOffsetY = 10; ctx.shadowColor = "rgba(100,100,100,0.5)"; ctx.shadowBlur = 7; for(var j=0;j<3;j++){ ctx.translate(80,80); create5star(ctx); ctx.fill(); } function create5star(ctx){ var dx =0; var dy=0; var s=50; ctx.beginPath(); ctx.fillStyle ='rgba(255,0,0,0.5)'; var x =Math.sin(0); var y =Math.cos(0); var dig = Math.PI/5*4; for(var i=0;i<5;i++){ x=Math.sin(i*dig); y=Math.cos(i*dig); ctx.lineTo(dx+x*s,dy+y*s) } ctx.closePath(); ctx.fill(); } }) </script> </head> <body> <canvas id="canvas" width="300" height="300">您的浏览器不支持canvas</canvas> </body> </html>
五角星阴影
10、canvas使用图像
语法:ctx.drawImage(imgobj,left,top,width,height)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas基础api</title> <style> canvas{ border:1px solid #ccc; } </style> <script src="../js/jquery-1.12.4.min.js"></script> <script> $(function(){ //获取标签 var cas = document.getElementById('canvas'); //获取绘制环境 var ctx = cas.getContext('2d'); //导入图片 var img = new Image(); img.src="../images/002.png"; //图片加载完之后,再开始绘制图片 img.onload = function(){ //绘制图片ctx.drawImage(imgobj,left,top,width,height) ctx.drawImage(img,100,50,300,200) } }) </script> </head> <body> <canvas id="canvas" width="500" height="300">您的浏览器不支持canvas</canvas> </body> </html>
The above is the detailed content of HTML5 Canvas to draw graphics. For more information, please follow other related articles on the PHP Chinese website!

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

H5 provides a variety of new features and functions, greatly enhancing the capabilities of front-end development. 1. Multimedia support: embed media through and elements, no plug-ins are required. 2. Canvas: Use elements to dynamically render 2D graphics and animations. 3. Local storage: implement persistent data storage through localStorage and sessionStorage to improve user experience.

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

Key elements of HTML5 include,,,,,, etc., which are used to build modern web pages. 1. Define the head content, 2. Used to navigate the link, 3. Represent the content of independent articles, 4. Organize the page content, 5. Display the sidebar content, 6. Define the footer, these elements enhance the structure and functionality of the web page.

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.

How to write clean and efficient HTML5 code? The answer is to avoid common mistakes by semanticizing tags, structured code, performance optimization and avoiding common mistakes. 1. Use semantic tags such as, etc. to improve code readability and SEO effect. 2. Keep the code structured and readable, using appropriate indentation and comments. 3. Optimize performance by reducing unnecessary tags, using CDN and compressing code. 4. Avoid common mistakes, such as the tag not closed, and ensure the validity of the code.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
