为了账号安全,请及时绑定邮箱和手机立即绑定

和老师抄的一样,怎么不填充呢?

<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>Canvas基础</title>
</head>
<body>
	<canvas id="myCanvas" style="border:1px solid #222;display:block;margin:50px auto;">
		当前浏览器不支持Canvas 请更换浏览器后再试
	</canvas>
	
	<script>

		var tangram =
		[
			{p:[{x:0,y:0},{x:800,y:0},{x:400,y:400}],color:"#ccc"},
			{p:[{x:0,y:0},{x:400,y:400},{x:0,y:800}],color:"#67becf"},
			{p:[{x:800,y:0},{x:800,y:400},{x:600,y:600},{x:600,y:200}],color:"#ef3d61"},
			{p:[{x:600,y:200},{x:600,y:600},{x:400,y:400}],color:"#f9f51a"},
			{p:[{x:400,y:400},{x:600,y:600},{x:400,y:800},{x:200,y:600}],color:"#a594c0"},
			{p:[{x:200,y:600},{x:400,y:800},{x:0,y:800}],color:"#fa8ecc"},
			{p:[{x:800,y:400},{x:800,y:800},{x:400,y:800}],color:"#f6ca29"}
		]

		function draw(pObj,con){
			con.beginPath();
			con.moveTo(pObj.p[0].x,pObj.p[0].y);

			for(var i=1;i < pObj.p.length;i++){
				con.lineTo(pObj.p[i].x,pObj.p[i].y);
				con.strokeStyle="blue";
				con.stroke();
				//con.closePath();
				con.fillStyle=pObj.color;
				con.fill();
				con.closePath();
			}
		}
		window.onload=function(){
			var myCanvas = document.getElementById("myCanvas");
			
			var context = myCanvas.getContext("2d");
			myCanvas.width=800;
			myCanvas.height=800;
			for(var i=0;i<tangram.length;i++){
				draw(tangram[i],context);
			}
		};
	
	</script>
</body>
</html>


正在回答

1 回答

哎,找到了,原来是

     con.closePath();

con.fillStyle=pObj.color;

                con.fill();

要放在循环外面。。。。。。

           


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

和老师抄的一样,怎么不填充呢?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信