覆盖原则
globalCompositeOperation
可以有遮盖指定区域,清除画布中的某些部分
globalCompositeOperation = type
这个属性设定了在画新图形时采用的遮盖策略,其值是一个标识12种遮盖方式的字符串。
例子:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body onload="draw();">
<canvas id="canvas" width="1000" height="1000"></canvas>
</body>
<script type="application/javascript">
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
//设置覆盖原则
ctx.globalCompositeOperation = "xor";
//设置颜色
ctx.fillStyle = "blue";
// 绘制正方形
ctx.fillRect(10, 10, 100, 100);
//设置颜色
ctx.fillStyle = "red";
// 绘制正方形
ctx.fillRect(50, 50, 100, 100);
</script>
</html>