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

详解HTML5 canvas绘图基本使用方法

发布时间:2020-05-12 21:34:29 所属栏目:MySql教程 来源:站长网
导读:副标题#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()方法从原始画布中剪切任意形状和尺寸。

(编辑:威海站长网)

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

热点阅读