<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title></title>

</head>

<body>

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

</canvas>

</body>

<script type="text/javascript">

//定义一个数组变量,分别代表七巧板的七块

//每一部分是一个类的对象,每一部分包含一个p,

//p也是一个数组,分别代表七巧板的每一块的顶点坐标

//color属性代表每一块的颜色

var tangram = [{

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 : "blue"

}, {

p: [{

x: 800,

y: 0

}, {

x: 800,

y: 400

}, {

x: 600,

y: 600

}, {

x: 600,

y: 200

}],

color : "yellow"

}, {

p: [{

x: 600,

y: 200

}, {

x: 600,

y: 600

}, {

x: 400,

y: 400

}],

color : "pink"

}, {

p: [{

x: 400,

y: 400

}, {

x: 600,

y: 600

}, {

x: 400,

y: 800

}, {

x: 200,

y: 600

}],

color : "gray"

}, {

p: [{

x: 200,

y: 600

}, {

x: 400,

y: 800

}, {

x: 0,

y: 800

}],

color : "green"

}, {

p: [{

x: 800,

y: 400

}, {

x: 800,

y: 800

}, {

x: 400,

y: 800

}],

color : "orange"

}]

window.onload = function() {

//获取Canvas

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

//定义宽高

canvas.width = 800;

canvas.height = 800;

//获取context

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

//遍历七巧板数组

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

//每次遍历调用函数draw()

draw(tangram[i], context);

//draw()函数传入两个参数,第一个是七巧板中的一块,

//第二个是我们获得的绘图的上下文环境context

}

function draw(piece, cxt) {

cxt.beginPath();

//用moveTo移动到顶点坐标的第一个坐标的位置

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();

//定义颜色,调用piece.color

cxt.fillStyle = piece.color;

cxt.fill();

//给七巧板的每一块加一个外边框

cxt.strokeStyle="black";

cxt.lineWidth=3;

cxt.stroke();

}

</script>

</html>

最新文章

  1. Android 数据存储之 SQLite数据库存储
  2. 【XLL API 函数】xlGetBinaryName
  3. duilib进阶教程 -- 扩展duilib的消息 (11)
  4. 【未解决】eclipse未自动引入maven依赖
  5. 【Android测试】【第二节】性能——CPU时间片
  6. C++求平均数
  7. DBParameter比拼接字符串慢的解决办法
  8. UIViewController的生命周期(图解)
  9. SQL Server 数据库所有者
  10. stm32基础入门
  11. Go-GRPC 初体验
  12. Mysql 查询昨天16:00至今天16:00之间的信息
  13. 「TJOI2015」组合数学 解题报告
  14. Browser Render Engine &amp; Javascript Engine
  15. Ajax请求导出Excel的问题
  16. 《Python》hashlib模块、configparser模块、logging模块
  17. zabbix3.4web界面添加第一台被监控服务器图文教程
  18. Win7删除缓存垃圾文件
  19. 【struts2】Struts2的系统架构
  20. TOM带你玩充电 篇三:15款5号电池横评及选购建议——南孚金霸王小米宜家耐时品胜一个都逃不了

热门文章

  1. Windows 运行中的命令
  2. mac系统卸载mono
  3. poj 1300 欧拉图
  4. 51nod 1255 贪心/构造
  5. hive 遇到的问题及解决方法
  6. UVA 11605 Lights inside a 3d Grid
  7. loj #6247. 九个太阳
  8. 《Javascript高级程序设计》阅读记录(三):第五章 上
  9. luogu P4848 崂山白花蛇草水
  10. CentOS7中配置基于Nginx+Supervisor+Gunicorn的Flask项目