画不出七巧板,是不是代码出错了?

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title></title>

</head>

<body>

<canvas id="canvas1" style = "border:1px solid #aaa"></canvas>


<script>

var tangram = [

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

{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},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},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"]}

]//设置七块板


window.onload = function(){

var canvas = document.getElementById("canvas1");

canvas.width = 1024;

canvas.height = 1024;

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

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

drwa(tangram[i],context);

}

}

function drwa(ps,ctx){


ctx.beginPath();

ctx.moveTo(ps.p[0].x,ps.p[0].y);

for(var i = 1; i < ps.p.length;i++){

ctx.lineTo(ps.p[i].x,ps.p[i].y);

}

ctx.closePath();

ctx.fillStyle = ps.color;

ctx.fill();

}//画图函数

</script>

</body>

</html>


游赛腾
浏览 1512回答 3
3回答

蜂之谷

中括号的位置错了var tangram = [ {p:[{x:0,y:0},{x:800,y:0},{x:400,y:400}],color:"#caff67"}, {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}],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}],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"} ]//设置七块板
打开App,查看更多内容
随时随地看视频慕课网APP