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

来源:2-1 创建canvas

爱吃豆角的虫子

2015-11-11 22:41

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

  • 爱吃豆角的虫子
    2015-11-11 22:49:30

    哎,找到了,原来是

         con.closePath();

    con.fillStyle=pObj.color;

                    con.fill();

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

               


炫丽的倒计时效果Canvas绘图与动画基础

学习HTML5中最激动人心的技术Canvas,彻底释放自己的创造力

96746 学习 · 1000 问题

查看课程

相似问题