加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

详解HTML5 canvas画图根基行使要领

发布时间:2020-05-13 08:39:49 所属栏目:编程 来源:站长网
导读:副问题#e# canvas/canvas是HTML5中新增的标签,用于绘制图形,现实上,这个标签和其他的标签一样,其非凡之处在于该标签可以获取一个CanvasRenderingContext2D工具,我们可以通过JavaScript脚原来节制该工具举办画图。 canvas/canvas只是一个绘制图形的容器

      值 描写
source-over   在方针图像上表现源图像(默认)  
source-atop   在方针图像顶部表现源图像。源图像位于方针图像之外的部门是不行见的  
source-in   在方针图像中表现源图像。只有方针图像之内的源图像部门会表现,方针图像是透明的  
source-out   在方针图像之外表现源图像。只有方针图像之外的源图像部门会表现,方针图像是透明的  
destination-over   在源图像上表现方针图像  
destination-atop   在源图像顶部表现方针图像。方针图像位于源图像之外的部门是不行见的  
destination-in   在源图像中表现方针图像。只有源图像之内的方针图像部门会被表现,源图像是透明的  
destination-out   在源图像之外表现方针图像。只有源图像之外的方针图像部门会被表现,源图像是透明的  
lighter   表现源图像 + 方针图像  
copy   表现源图像。忽略方针图像  
xor   行使异或操纵对源图像与方针图像举办组合  

下面是一个小示例,可以通过点击改变组合结果:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>图形组合</title> <style type="text/css"> #canvas{ border: 1px solid #1C0EFA; display: block; margin: 20px auto; } #buttons{ width: 1000px; margin: 5px auto; clear:both; } #buttons a{ font-size: 18px; display: block; float: left; margin-left: 20px; } </style> </head> <body> <canvas id="canvas" width="1000" height="800"> 你的赏识器还不支持canvas </canvas> <div id="buttons"> <a href="#">source-over</a> <a href="#">source-atop</a> <a href="#">source-in</a> <a href="#">source-out</a> <a href="#">destination-over</a> <a href="#">destination-atop</a> <a href="#">destination-in</a> <a href="#">destination-out</a> <a href="#">lighter</a> <a href="#">copy</a> <a href="#">xor</a> </div> </body> <script type="text/javascript"> window.onload = function(){ draw("source-over"); var buttons = document.getElementById("buttons").getElementsByTagName("a"); for (var i = 0; i < buttons.length; i++) { buttons[i].onclick = function(){ draw(this.text); return false; }; } }; function draw(compositeStyle){ var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); context.clearRect(0, 0, canvas.width, canvas.height); //draw title context.font = "bold 40px Arial"; context.textAlign = "center"; context.textBasedline = "middle"; context.fillStyle = "#150E0E"; context.fillText("globalCompositeOperation = "+compositeStyle, canvas.width/2, 60); //draw a rect context.fillStyle = "#F6082A"; context.fillRect(300, 150, 500, 500); //draw a triangle context.globalCompositeOperation = compositeStyle; context.fillStyle = "#1611F5"; context.beginPath(); context.moveTo(700, 250); context.lineTo(1000,750); context.lineTo(400, 750); context.closePath(); context.fill(); } </script> </html>

读者可以点击标签来调查差异的组合结果,结果如下:

剪辑地区:

clip()要领从原始画布中剪切恣不测形和尺寸。

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读