<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> </head> <body> <canvas id="canvas" style="border:1px solid #777;margin:50px auto;display:block;" ></canvas> <script> var tangram=[ {p[{x:0,y:0},{x:300,y:300},{x:600,y:0}],color:"#777"}, {p[{x:0,y:0},{x:300,y:300},{x:0,y:600}],color:"blue"}, {p[{x:0,y:600},{x:150,y:450},{x:300,y:600}],color:"black"}, {p[{x:150,y:450},{x:300,y:600},{x:450,y:450},{x:300,y:300}],color:"yellow"}, {p[{x:450,y:450},{x:300,y:300},{x:450,y:150}],color:"green"}, {p[{x:450,y:450},{x:450,y:150},{x:600,y:0},{x:600,y:300}],color:"pink"}, {p[{x:600,y:300},{x:600,y:600},{x:300,y:600}],color:"grey"} ]; window.onload=function(){ var canvas=document.getElementById("canvas"); canvas.width=800; canvas.height=800; var context=canvas.getContext("2d"); 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 i = 1; i < piece.p.length; i++) { cxt.lineTo(piece.p[i].x,piece.p[i].y); }; cxt.closePath(); cxt.fillStyle=piece.color; cxt.fill(); } </script> </body> </html>
七巧板写不出来,canvas内总是空白一片,好忧伤啊