html5 canvas画布上合成

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

以上是HTML 5 canvas globalCompositeOperation 属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!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;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!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>
复制代码

 

posted @   一点点白  阅读(1722)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示