<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>Canvas基础</title>
</head>
<body>
<canvas id="myCanvas" style="border:1px solid #222;display:block;margin:50px auto;">
当前浏览器不支持Canvas 请更换浏览器后再试
</canvas>
<script>
var tangram =
[
{p:[{x:0,y:0},{x:800,y:0},{x:400,y:400}],color:"#ccc"},
{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},{x:600,y:200}],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},{x:200,y:600}],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"}
]
function draw(pObj,con){
con.beginPath();
con.moveTo(pObj.p[0].x,pObj.p[0].y);
for(var i=1;i < pObj.p.length;i++){
con.lineTo(pObj.p[i].x,pObj.p[i].y);
con.strokeStyle="blue";
con.stroke();
//con.closePath();
con.fillStyle=pObj.color;
con.fill();
con.closePath();
}
}
window.onload=function(){
var myCanvas = document.getElementById("myCanvas");
var context = myCanvas.getContext("2d");
myCanvas.width=800;
myCanvas.height=800;
for(var i=0;i<tangram.length;i++){
draw(tangram[i],context);
}
};
</script>
</body>
</html>