<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{margin:0;}
#canvas{margin:0 auto;display: block; background: #ccc;}
</style>
</head>
<body>
<canvas id="canvas" width="600" height="600"></canvas>
<script>
var canvas=document.getElementById('canvas');
var cxt=canvas.getContext('2d');
cxt.save();
cxt.beginPath();
cxt.fillStyle="#000"
cxt.fillRect(0,0,600,600);
cxt.beginPath();
cxt.fillStyle="#f00"
cxt.fillRect(0,0,600,600);
cxt.globalCompositeOperation="destination-out";
cxt.beginPath();
cxt.fillStyle="#00f"
cxt.fillRect(200,200,200,200);
cxt.beginPath();
cxt.arc(200,200,50,0,2*Math.PI);
cxt.fillStyle="#0f0";
cxt.fill();
cxt.beginPath();
cxt.fillStyle="#00f"
cxt.fillRect(400,400,100,100);
cxt.restore();
</script>
</body>
</html>
cxt.beginPath(); cxt.fillStyle="#f00" cxt.fillRect(0,0,600,600);
被这里的红色背景覆盖掉了