Skip to content

Instantly share code, notes, and snippets.

@cheng470
Created November 4, 2014 15:42
Show Gist options
  • Save cheng470/7f98607eabf55400d9a2 to your computer and use it in GitHub Desktop.
Save cheng470/7f98607eabf55400d9a2 to your computer and use it in GitHub Desktop.
用 canvas 绘制的一个旋转图案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Twister</title>
<style>
body { background: black }
canvas { margin: 0 auto; display: block; padding: 1px; }
</style>
</head>
<body>
<canvas id="canvas1" width="420" height="420"></canvas>
<script type="text/javascript">
var canvas = document.getElementById("canvas1");
var ctx = canvas.getContext('2d');
ctx.lineCap = 'round';
ctx.strokeStyle = '#FFFF00';
ctx.lineWidth = 4;
function turnIt() {
ctx.beginPath();
ctx.moveTo(0, 0);
moveNum = 2;
rotateNum = 0.1
rads = rotateNum * 2 * Math.PI;
for (i = 1; i <= 100; i++) {
distance = i * moveNum;
turn = i * rads;
x = Math.cos(turn) * distance;
y = Math.sin(turn) * distance;
ctx.lineTo(x, y);
}
}
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2);
setInterval(function() {
ctx.clearRect(-ctx.canvas.width / 2, -ctx.canvas.height / 2,
ctx.canvas.width, ctx.canvas.height);
ctx.rotate(.01);
turnIt.apply();
ctx.stroke();
}, 10);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment