这篇文章主要说明一下html5中 合成 的使用方法
先把公有部分的代码放出来,规定之后的代码块默认之前都有的代码
<!--html-->
<canvas width='500' height='500'></canvas>
// javaScript
const oC = document.querySelector('canvas')
const ctx = oC.getContext( "2d" )
合成的两个操作
1. globalAlpha 设置或返回绘图的当前 alpha 或透明值
2.globalCompositeOperation 设置或返回新图像如何绘制到已有的图像上
例子
先说一下globalAlpha
其默认值为1,然而要对某图样设置透明度时其要放在该图样的前面
ctx.globalAlpha = .2
ctx.beginPath();
ctx.fillStyle = 'blue'
ctx.arc( 200,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
console.log(ctx.globalAlpha)
而globalCompositeOperation类似,可以放在任意位置输出,其默认值是"source-over"
接下来详细说一下 globalCompositeOperation 的操作
source-over 默认 在目标图像上显示源图像
ctx.beginPath();
ctx.fillStyle = 'blue'
ctx.arc( 200,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
ctx.globalCompositeOperation = "source-over"
ctx.beginPath();
ctx.fillStyle = 'red'
ctx.arc( 250,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
从效果图可以看出蓝色圆是目标图像,红色圆是源图像
由此可以得出结论,先写的图像为目标图像,后写的图像为源图像
source-atop 在目标图像顶部显示源图像。源图像位于目标图像之外的部分是不可见的
ctx.beginPath();
ctx.fillStyle = 'blue'
ctx.arc( 200,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
ctx.globalCompositeOperation = "source-atop"
ctx.beginPath();
ctx.fillStyle = 'red'
ctx.arc( 250,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
PS:注意这里的顶部是指在 从屏幕往你指向 的方向上更靠近你的位置,而不是屏幕的上下左右 ,我刚刚学到的时候就以为是屏幕的上下左右,希望你们不要犯一样的错误
source-in 在目标图像中显示源图像。只有目标图像内的源图像部分会显示,目标图像是透明的
ctx.beginPath();
ctx.fillStyle = 'blue'
ctx.arc( 200,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
ctx.globalCompositeOperation = "source-in"
ctx.beginPath();
ctx.fillStyle = 'red'
ctx.arc( 250,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
source-out 在目标图像之外显示源图像。只会显示目标图像之外源图像部分,目标图像是透明的
ctx.beginPath();
ctx.fillStyle = 'blue'
ctx.arc( 200,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
ctx.globalCompositeOperation = "source-out"
ctx.beginPath();
ctx.fillStyle = 'red'
ctx.arc( 250,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
destination-over 在源图像上方显示目标图像
ctx.beginPath();
ctx.fillStyle = 'blue'
ctx.arc( 200,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
ctx.globalCompositeOperation = "destination-over"
ctx.beginPath();
ctx.fillStyle = 'red'
ctx.arc( 250,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
注意和 source-over 效果的对比
destination-atop 在源图像顶部显示目标图像,源图像之外的目标图像部分不会被显示
ctx.beginPath();
ctx.fillStyle = 'blue'
ctx.arc( 200,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
ctx.globalCompositeOperation = "destination-atop"
ctx.beginPath();
ctx.fillStyle = 'red'
ctx.arc( 250,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
注意和 source-atop 的对比
destination-in 在源图像中显示目标图像。只有源图像内的目标图像部分会被显示,源图像是透明的
ctx.beginPath();
ctx.fillStyle = 'blue'
ctx.arc( 200,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
ctx.globalCompositeOperation = "destination-in"
ctx.beginPath();
ctx.fillStyle = 'red'
ctx.arc( 250,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
注意和 source-in 的对比
destination-out 在源图像外显示目标图像。只有源图像外的目标图像部分会被显示,源图像是透明的
ctx.beginPath();
ctx.fillStyle = 'blue'
ctx.arc( 200,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
ctx.globalCompositeOperation = "destination-out "
ctx.beginPath();
ctx.fillStyle = 'red'
ctx.arc( 250,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
注意和 source-out 的对比
lighter 显示源图像 + 目标图像
ctx.beginPath();
ctx.fillStyle = 'blue'
ctx.arc( 200,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
ctx.globalCompositeOperation = "lighter"
ctx.beginPath();
ctx.fillStyle = 'red'
ctx.arc( 250,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
从效果图可以看出,两图像重叠的地方会进行颜色的叠加
copy 显示源图像 忽略目标图像
ctx.beginPath();
ctx.fillStyle = 'blue'
ctx.arc( 200,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
ctx.globalCompositeOperation = "copy"
ctx.beginPath();
ctx.fillStyle = 'red'
ctx.arc( 250,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
xor 使用异或操作对源图像与目标图像进行组合
ctx.beginPath();
ctx.fillStyle = 'blue'
ctx.arc( 200,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
ctx.globalCompositeOperation = "xor"
ctx.beginPath();
ctx.fillStyle = 'red'
ctx.arc( 250,200,100,0,6.3,false )
ctx.fill()
ctx.closePath()
这个可能比较难理解,我稍稍解释一下
首先简单说一下异或是怎么操作的
0⊕0=0,1⊕0=1,0⊕1=1,1⊕1=0(同为0,异为1)
类比一下,在这里两图像重叠的地方消失,只显示不重叠的部分~~
最后汇总一下所有 globalCompositeOperation 的操作
globalAlpha 设置或返回绘图的当前 alpha 或透明值
globalCompositeOperation 设置或返回新图像如何绘制到已有的图像上
source-over 默认 在目标图像上显示源图像
source-atop 在目标图像顶部显示源图像 源图像位于目标图像之外的部分是不可见的
source-in 在目标图像中显示源图像 只有目标图像内的源图像部分会显示,目标图像是透明的
source-out 在目标图像之外显示源图像 只会显示目标图像之外源图像部分,目标图像是透明的
destination-over 在源图像上方显示目标图像
destination-atop 在源图像顶部显示目标图像 源图像之外的目标图像部分不会被显示
destination-in 在源图像中显示目标图像 只有源图像内的目标图像部分会被显示,源图像是透明的
destination-out 在源图像外显示目标图像 只有源图像外的目标图像部分会被显示,源图像是透明的
lighter 显示源图像 + 目标图像
copy 显示源图像。忽略目标图像
xor 使用异或操作对源图像与目标图像进行组合
好了,关于html5的合成就讲这么多了,这篇文章只讲了实现效果,具体要怎么应用到实际中还是要靠平时多看案例,多思考
希望这篇文章对你有帮助!
Wish you godspeed!