介绍
在默认情况下,将一个图像绘制在另一个图像之上,那么浏览器会简单地把源物体的图像叠放到目标图像上面。
然而可以通过修改Canvas绘图对象的globalCompositeOperation属性修改这种默认的叠放,该属性可以取表中所列的任意一个值。
这些值叫做Porter-Duff操作符,它们被描述在一篇由LucasFilm Ltd的Thomas Porter与Tom Duff所写的文章中,该文章发表在1984年7月的《Computer Graphics》杂志上。原文链接
属性值
值 | 描述 |
---|---|
source-over | 默认。在目标图像上显示源图像。 |
source-atop | 在目标图像顶部显示源图像。源图像位于目标图像之外的部分是不可见的。 |
source-in | 在目标图像中显示源图像。只有目标图像之内的源图像部分会显示,目标图像是透明的。 |
source-out | 在目标图像之外显示源图像。只有目标图像之外的源图像部分会显示,目标图像是透明的。 |
destination-over | 在源图像上显示目标图像。 |
destination-atop | 在源图像顶部显示目标图像。目标图像位于源图像之外的部分是不可见的。 |
destination-in | 在源图像中显示目标图像。只有源图像之内的目标图像部分会被显示,源图像是透明的。 |
destination-out | 在源图像之外显示目标图像。只有源图像之外的目标图像部分会被显示,源图像是透明的。 |
lighter | 显示源图像 + 目标图像。 |
copy | 显示源图像。忽略目标图像。 |
xor | 使用异或操作对源图像与目标图像进行组合。 |
Demo演示
源码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图像合成</title>
<style>
#canvas {
border: 1px solid cornflowerblue;
position: absolute;
left: 150px;
top: 10px;
background: #eeeeee;
border: thin solid #aaaaaa;
cursor: pointer;
-webkit-box-shadow: rgba(200, 200, 255, 0.9) 5px 5px 10px;
-moz-box-shadow: rgba(200, 200, 255, 0.9) 5px 5px 10px;
box-shadow: rgba(200, 200, 255, 0.9) 5px 5px 10px;
}
</style>
</head>
<body>
<select size="11" id="compositingSelect">
<option value="source-atop">source-atop</option>
<option value='source-in'>source-in</option>
<option value='source-out'>source-out</option>
<option value='source-over'>source-over (default)</option>
<option value='destination-atop'>destination-atop</option>
<option value='destination-in'>destination-in</option>
<option value='destination-out'>destination-out</option>
<option value='destination-over'>destination-over</option>
<option value='lighter'>lighter</option>
<option value='copy'>copy</option>
<option value='xor'>xor</option>
</select>
<canvas id="canvas" width="600" height="420">Canvas not supports</canvas>
</body>
<script>
'use strict';
let context = document.getElementById('canvas').getContext('2d'),
selectElement = document.getElementById('compositingSelect');
//Function……
/**
* 绘制文字
*/
let drawText = () => {
context.save();
context.shadowColor = 'rgba(100, 100, 150, 0.8)';
context.shadowOffsetX = 5;
context.shadowOffsetY = 5;
context.shadowBlur = 10;
context.fillStyle = 'cornflowerblue';
context.fillText('HTML', 20, 250);
context.strokeStyle = 'yellow';
context.strokeText('HTML', 20, 250);
context.restore();
};
//Event handlers……
let windowToCanvas = (canvas, x, y) => {
let bbox = canvas.getBoundingClientRect();
return {
x:x-bbox.left*(canvas.width/bbox.width),
y:y-bbox.top*(canvas.height/bbox.height)
};
};
context.canvas.onmousemove=e=>{
let loc = windowToCanvas(context.canvas,e.clientX,e.clientY);
context.clearRect(0,0,context.canvas.width,context.canvas.height);
drawText();
context.save();
//设置选项即为全局合成属性
context.globalCompositeOperation=selectElement.value;
context.beginPath();
context.arc(loc.x,loc.y,100,0,Math.PI*2,false);
context.fillStyle='orange';
context.stroke();
context.fill();
context.restore();
}
//Init……
selectElement.selectedIndex=3;
context.lineWidth=0.5;
context.font='128pt Comic-sans';
drawText();
</script>
</html>