##静态七巧板
个人感觉canvas绘图有种小学信息技术课上学的pc logo的感觉
复习去年一个晚自习跟着视频做的canvas绘图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>七巧板</title>
</head>
<body>
<canvas id="canvas" width="400" height="400" style="border:2px solid black;display:block;margin: 0 auto;"></canvas><!--这里是画图的区域-->
<script type="text/javascript">
var tangram=[
{p:[{x:0,y:0},{x:100,y:100},{x:100,y:300},{x:0,y:200}],color:"rgb(229,0,205)"},
{p:[{x:0,y:0},{x:200,y:200},{x:400,y:0}],color:"rgb(237,129,31)"},
{p:[{x:100,y:100},{x:200,y:200},{x:100,y:300}],color:"rgb(1,64,229)"},
{p:[{x:0,y:200},{x:200,y:400},{x:0,y:400}],color:"rgb(14,160,15)"},
{p:[{x:100,y:300},{x:200,y:200},{x:300,y:300},{x:200,y:400}],color:"rgb(0,228,165)"},
{p:[{x:400,y:0},{x:400,y:400},{x:200,y:200}],color:"rgb(105,188,232)"},
{p:[{x:200,y:400},{x:400,y:400},{x:300,y:300}],color:"rgb(233,229,0)"}
]//定义一个数组里面装着每一块板的颜色和顶点的坐标
window.onload=function()
{
var canvas=document.getElementById('canvas');
var context=canvas.getContext('2d');//设置一块二维画布
for (var i = 0;i<tangram.length;i++)
{
draw(tangram[i],context);
}
}
function draw(arr,con)
{
con.beginPath();//开始绘制
con.moveTo(arr.p[0].x,arr.p[0].y);//初始点
for (var i=1;i<arr.p.length;i++)
{
con.lineTo(arr.p[i].x,arr.p[i].y);//依次绘制到各个坐标
}
con.closePath();//结束绘制,并将不封闭的图形自行封闭
con.fillStyle=arr.color; //填充样式
con.fill();//填充
con.strokeStyle="black";//线条样式
con.lineWidth="3px";//线条粗细
con.stroke();//画线
}//去年花了1节晚自习跟着视频写成,现在重新看感觉又不会了,于是通过笔记加上了一点注释(突然理解了组长让我们在代码中加注释的原因)
</script>
</body>
</html>