问答详情
源自:8-2 globalAlpha和globalCompositeOperation

最先绘制的黑色背景怎么不显示了,求老师解答下 ,谢谢了

<!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>


提问者:xxAzz 2015-03-13 17:27

个回答

  • lymo
    2015-03-13 17:31:16

    cxt.beginPath();
    cxt.fillStyle="#f00" 
    cxt.fillRect(0,0,600,600);

    被这里的红色背景覆盖掉了