39,084
社区成员
发帖
与我相关
我的任务
分享
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title> 页面名称 </title>
</head>
<body>
<canvas id="canvasId" width="400" height="350"></canvas>
<script type="text/javascript">
var canvas = document.getElementById("canvasId");
var ctx = canvas.getContext("2d");
var p = [
{x:200,y:50},
{x:100,y:200},
{x:300,y:200}
];
p.push(p[0]);
function go() {
if (p.length>1)
line(p[0],p[1]);
}
function line(p1,p2) {
var vx = p2.x-p1.x, vy = p2.y-p1.y;
var size = Math.max(Math.abs(vx),Math.abs(vy));
vx /= size;
vy /= size;
var x = p1.x, y = p1.y;
function run() {
ctx.beginPath();
ctx.moveTo(x,y);
x += vx;
y += vy;
ctx.lineTo(x,y);
ctx.stroke();
if (--size>0) {
setTimeout(run, 16);
} else {
p.shift();
go();
}
}
run();
}
go();
</script>
</body>
</html>