canvas擦除多边形,利用globalCompositeOperation

 canvas擦除多边形,利用globalCompositeOperation 
1.画出之前应有的区域(大)
利用     ctx.globalCompositeOperation="destination-out";
2.画出擦除后需要保留的区域,填充颜色用rgba 透明度设置为1
ctx.globalCompositeOperation="source-over";
3.再次画擦除后需要保留区域,颜色为需要展示的颜色
由于globalCompositeOperation操作已经把外层(第一次展示,需要画的图形与擦除后图像交集剪切,所以展示图像颜色不受其影响

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
    #myCanvas{
        margin-left: 500px;
    }
</style>
</head>
<body>

<canvas id="myCanvas" width="300" height="350" style="border:1px solid #d3d3d3;">
您的浏览器不支持 HTML5 canvas 标签。
</canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="rgba(0, 225, 0,1)";
ctx.fillRect(20,20,75,50);
	ctx.globalCompositeOperation="destination-out";
    ctx.fillStyle="rgba(255,255, 255,1)";	
ctx.fillRect(30,30,55,30);	
ctx.globalCompositeOperation="source-over";
ctx.fillStyle="rgba(255, 0, 0,0.5)";	
ctx.fillRect(30,30,55,30);	

ctx.globalCompositeOperation="destination-out";
    ctx.fillStyle="rgba(255,255, 255,1)";	
    ctx.globalCompositeOperation="source-over";
// ctx.fillStyle="rgba(0, 225, 0,0.5)";	
// ctx.fillRect(130,130,55,30);	
// ctx.globalCompositeOperation="destination-in";
// ctx.fillStyle="rgba(255, 0, 0,1)";
// ctx.fillRect(120,120,75,50);
</script>

</body>
</html>

例子方便用矩形复现三次画法加上  globalCompositeOperation 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值