求教,七巧板只有一个框

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

慕粉1859536052

2017-05-07 19:32

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Document</title>

</head>

<body>

<canvas id="myCanvas" style="border:1px solid #aaa; display:block; margin: 50px auto;">

当前浏览器不支持Cancas,请更换浏览器再试。

</canvas>

<script>



window.onload = function(){

var sev = [

{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:400},{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:"blue"},

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

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

]


var canvas = document.getElementById('myCanvas');

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


canvas.width = 800;

canvas.height = 800;




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

draw(sev[i],context);

}



function draw(piece,cxt){

cxt.beginPath();

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

for(var j=1; j<piece.length; j++);

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

cxt.closePath();


cxt.fillStyle = piece.color;

cxt.fill();


}


}

</script>


</body>

</html>


写回答 关注

2回答

  • 慕粉1859536052
    2017-05-09 11:29:24

    啊,找到了,是piece.p.length没有分号

  • 唯一602
    2017-05-08 15:28:14

    一行代码,两个问题,红色部分已标出

    http://img.mukewang.com/59101e080001722403280236.jpg

    慕粉1859...

    咦,我用控制台检查是完全没问题... 如果算是自动断行的话,划出来的分号去掉了还是只有一个框诶~

    2017-05-09 10:00:12

    共 1 条回复 >

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

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

96746 学习 · 1000 问题

查看课程

相似问题