-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_rotation.html
51 lines (42 loc) · 1.16 KB
/
5_rotation.html
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
47
48
49
50
51
<!DOCTYPE html><html><head><meta charset="utf-8"><title>Skookum Labs</title></head><body><canvas id="surface" width="600" height="400"></canvas><pre><code id="source">var line = {
x: 300,
y: 200,
length: 100,
angle: 0
};
|
// ...
|
line.angle += (Math.PI*2) / 100;
var x = line.x + line.length * Math.cos(line.angle);
var y = line.y + line.length * Math.sin(line.angle);
</code></pre><script>// Version 0: Rotation - angle known, need X,Y
var line = {
x: 300,
y: 200,
length: 100,
angle: 0
};
var canvas = document.getElementById("surface");
var ctx = canvas.getContext('2d');
setInterval(function() {
// Clear display
ctx.save();
ctx.fillStyle = "rgba(0, 0, 0, .2)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.restore();
// Update angle - with geometry! =)
line.angle += (Math.PI * 2) / 100;
var x = line.x + line.length * Math.cos(line.angle);
var y = line.y + line.length * Math.sin(line.angle);
// Draw line
ctx.save();
ctx.strokeStyle = "#fff";
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(line.x, line.y);
ctx.lineTo(x, y);
ctx.stroke();
ctx.restore();
}, 1000 / 77);
</script></body></html>