html5 canvas画布上合成

 2023-09-05 阅读 29 评论 0

摘要:source-over默认。在目标图像上显示源图像。source-atop在目标图像顶部显示源图像。源图像位于目标图像之外的部分是不可见的。source-in在目标图像中显示源图像。只有目标图像内的源图像部分会显示,目标图像是透明的。source-out在目标图像之外显示源图像。只会显示目
source-over默认。在目标图像上显示源图像。
source-atop在目标图像顶部显示源图像。源图像位于目标图像之外的部分是不可见的。
source-in在目标图像中显示源图像。只有目标图像内的源图像部分会显示,目标图像是透明的。
source-out在目标图像之外显示源图像。只会显示目标图像之外源图像部分,目标图像是透明的。
destination-over在源图像上方显示目标图像。
destination-atop在源图像顶部显示目标图像。源图像之外的目标图像部分不会被显示。
destination-in在源图像中显示目标图像。只有源图像内的目标图像部分会被显示,源图像是透明的。
destination-out在源图像外显示目标图像。只有源图像外的目标图像部分会被显示,源图像是透明的。
lighter显示源图像 + 目标图像。
copy显示源图像。忽略目标图像。
source-over使用异或操作对源图像与目标图像进行组合。

以上是HTML 5 canvas globalCompositeOperation 属性

<!DOCTYPE html>
<html>
<body><canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.
</canvas><script>var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="red";
ctx.fillRect(20,20,75,50);
ctx.fillStyle="blue";	
ctx.globalCompositeOperation="source-over";
ctx.fillRect(50,50,75,50);	
ctx.fillStyle="red";
ctx.fillRect(150,20,75,50);
ctx.fillStyle="blue";	
ctx.globalCompositeOperation="destination-over";
ctx.fillRect(180,50,75,50);	</script></body>
</html>

 

HTML 5 canvas globalAlpha 属性    globalAlpha=0-1;

<!DOCTYPE html>
<html>
<body><canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.
</canvas><script>var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="red";
ctx.fillRect(20,20,75,50);//Turn transparency on
ctx.globalAlpha=0.2;
ctx.fillStyle="green"; 
ctx.fillRect(50,50,75,50); 
ctx.fillStyle="blue"; 
ctx.fillRect(80,80,75,50);
</script></body>
</html>

 以上2段为W3SCHOOL上的说明:昨天我写到了,API经过国外翻译之后可能翻译的结果让人感觉无语 所以多实践 才能提高

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>canvas合成</title>
<script src="js/modernizr.js"></script>
</head><body>
<script type="text/javascript">
window.addEventListener('load',eventWindowLoaded,false);
function eventWindowLoaded(){canvasApp();
}
function canvasSupport(){return Modernizr.canvas;
}
function canvasApp(){if(!canvasSupport()){return;}else{var theCanvas = document.getElementById('canvas')var context = theCanvas.getContext("2d")}drawScreen();function drawScreen(){//在屏幕上绘制一个大方块
        context.fillStyle = "black";context.fillRect(10,10,200,200);//保留globalCompositeOperation原有值不变//现在绘制一个红色正方形
        context.fillStyle = "#ff0000";context.fillRect(1,1,50,50);//现在设置为source-over
        context.globalCompositeOperation = "source-over";context.fillRect(60,1,50,50);//现在设置为destination-over
        context.globalCompositeOperation = "destination-over";context.fillRect(1,60,50,50);//现在设置globalAlpha
        context.globalAlpha = .5;//现在设置source-atop
        context.globalCompositeOperation = "source-atop";context.fillRect(60,60,50,50);}}</script>
<canvas id="canvas" width="500" height="500">
你的浏览器无法使用canvas
如有疑问加QQ:1035417613;小白童鞋;你的支持是我最大的快乐!!
</canvas>
</body>
</html>

 

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/1508.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息