canvas save、restore及图片组合

重回此篇文章,已经时长n长时间了,简单了看了一下,发现对一些知识的介绍及描述不多,主要还是本人的文采不照啊,谁叫me上学期间没有好好重视语文这门课程呢,真到实战的时候才知书到用时方恨少,一直不及格造成的困挠。希望大家还要凑活看吧,在这里只能说纯干货,哈哈哈。。

 

一、save()和restore()

 

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="red";
ctx.fillRect(20,20,75,50);

ctx.save();  // 保存当前环境的状态

ctx.globalAlpha=0.2;
ctx.fillStyle="green";
ctx.fillRect(50,50,75,50);

ctx.restore();  // 返回之前保存过的路径状态和属性
ctx.fillStyle="blue";
ctx.fillRect(80,80,75,50);

 

 

 

 

二、globalCompositeOperation 属性设置或返回如何将一个源(新的)图像绘制到目标(已有)的图像上。

 

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

 

1、source-over (红色区域是目标图像,蓝色区域是源图像)

 

 

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");

ctx.fillStyle="red";
ctx.fillRect(20,20,75,50);
ctx.globalCompositeOperation="source-over";
ctx.fillStyle="blue";
ctx.fillRect(50,50,75,50);

 

 

 

 

2、source-atop

 

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");

ctx.fillStyle="red";
ctx.fillRect(20,20,75,50);
ctx.globalCompositeOperation="source-atop";
ctx.fillStyle="blue";
ctx.fillRect(50,50,75,50);

 

 

 

 

3、source-in

 

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");

ctx.fillStyle="red";
ctx.fillRect(20,20,75,50);
ctx.globalCompositeOperation="source-in";
ctx.fillStyle="blue";
ctx.fillRect(50,50,75,50);

 

 

4、source-out

 

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");

ctx.fillStyle="red";
ctx.fillRect(20,20,75,50);
ctx.globalCompositeOperation="source-out";
ctx.fillStyle="blue";
ctx.fillRect(50,50,75,50);

 

 

 

5、lighter

 

 

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");

ctx.fillStyle="red";
ctx.fillRect(20,20,75,50);
ctx.globalCompositeOperation="lighter";
ctx.fillStyle="blue";
ctx.fillRect(50,50,75,50);

 

 

 

 

6、copy

 

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");

ctx.fillStyle="red";
ctx.fillRect(20,20,75,50);
ctx.globalCompositeOperation="copy";
ctx.fillStyle="blue";
ctx.fillRect(50,50,75,50);

 

7、destination-atop ( 蓝色区域是目标图片,红色区域是源图片 )

 

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");

ctx.fillStyle="red";
ctx.fillRect(20,20,75,50);
ctx.globalCompositeOperation="destination-atop";
ctx.fillStyle="blue";
ctx.fillRect(50,50,75,50);

 

 

 

 

欢迎关注分享录:http://fenxianglu.cn/

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天空还下着雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值