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

来源:8-2 globalAlpha和globalCompositeOperation

xxAzz

2015-03-13 17:27

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


写回答 关注

1回答

  • lymo
    2015-03-13 17:31:16
    cxt.beginPath();
    cxt.fillStyle="#f00" 
    cxt.fillRect(0,0,600,600);

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

Canvas绘图详解

Canvas系列教程第二课,详解Canvas各接口,让同学彻底掌握Canvas绘图

72881 学习 · 422 问题

查看课程

相似问题