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>