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

TypeError: piece.p is undefined

为什么运行不出来呢?

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

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

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

举报

0/150
提交
取消

TypeError: piece.p is undefined

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