TypeError: piece.p is undefined

来源:2-2 绘制直线、多边形和七巧板

summer_raining

2016-06-17 20:52

为什么运行不出来呢?

cxt.moveTo(piece.p[0].x,piece.p[0].y);这一行

总是提示:TypeError: piece.p is undefined



<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title></title>

</head>

<body>

<canvas id="mycanvas" width="500" height="500"></canvas>

<script type="text/javascript">

var myCanvas = document.getElementById("mycanvas");

var context = myCanvas.getContext("2d");

var tangram = [

{p:[{x:0, y:0}, {x:800, y:0},{x:400, y:400}], color:"red"},

{P:[{x:0,y:0},{x:400,y:400},{x:0,y:800}], color:"orange"},

{p:[{x:800,y:0},{x:800,y:400},{x:600,y:600},{x:600,y:200}],color:"yellow"},

{p:[{x:600,y:200},{x:600,y:600},{x:400,y:400}],color:"green"},

{p:[{x:400,y:400},{x:600,y:600},{x:400,y:800},{x:200,y:600}],color:"cyan"},

{p:[{x:200,y:600},{x:400,y:800},{x:0,y:800}],color:"blue"},

{p:[{x:800,y:400},{x:800,y:800},{x:400,y:800}],color:"purple"}

];

window.onload = function() {

for (var i = 0; i < tangram.length; i++) {

draw(tangram[i], context);

}

}

 

function draw (piece, cxt) {

cxt.beginPath();

cxt.moveTo(piece.p[0].x,piece.p[0].y);

for (var j = 0; j < piece.p.length; j++) {

cxt.lineTo(piece.p[j].x, piece.p[j].y);

}

cxt.closePath();

cxt.fillStyle = piece.color;

cxt.fill();

}

</script>

</body>

</html>


写回答 关注

1回答

  • 忽职守
    2016-06-18 19:36:25

    第二个循环应该是var j=1 开始,第一个是作为起点的。

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

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

96746 学习 · 1000 问题

查看课程

相似问题