设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 数据 创业者 手机
当前位置: 首页 > 站长学院 > MySql教程 > 正文

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

发布时间:2020-05-11 23:29 所属栏目:115 来源:站长网
导读:值描述 source-over 在目标图像上显示源图像(默认) source-atop 在目标图像顶部显示源图像。源图像位于目标图像之外的部分是不可见的 source-in 在目标图像中显示源图像。只有目标图像之内的源图像部分会显示,目

      值 描述
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()方法从原始画布中剪切任意形状和尺寸。

(编辑:ASP站长网)

网友评论
推荐文章
    热点阅读