-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
121 lines (108 loc) · 3.68 KB
/
main.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const TAU = Math.PI * 2 // 360 degrees, but in radians
const DEG_45 = TAU / 8 // 45 degrees, but in radians
const DEG_60 = TAU / 6 // 60 degrees, but in radians
const canvasEle = document.getElementById('canvas')
const context = canvasEle.getContext('2d')
const width = canvasEle.clientWidth
const height = canvasEle.clientHeight
const halfWidth = width / 2
const halfHeight = height / 2
const squareSize = Math.min(width, height)
const drawCircle = (x, y, radius, color, lineWidth) => {
context.save();
context.translate(x, y);
context.fillStyle = color || '#f00';
context.strokeStyle = color || '#f00';
context.lineWidth = lineWidth || 1;
context.beginPath();
context.arc(0, 0, radius, 0, TAU);
context.stroke();
context.restore();
};
const drawLine = (x1, y1, x2, y2, color, lineWidth) => {
context.save();
context.strokeStyle = color || '#f00';
context.lineWidth = lineWidth || 1;
context.beginPath();
context.moveTo(x1, y1);
context.lineTo(x2, y2);
context.stroke();
context.restore();
};
const drawLeg = ({x, y, angle}) => {
drawCircle(x, y, 0.1, '#ffffff', 0.05)
const radian = angle * DEG_45
const legLength = 0.5
// Cosines ALWAYS give you X values!
// Sines ALWAYS give you Y values!
// BOTH always return from -1 to +1
// 1.) Start at offset, (x or y) then
// 2.) Add radian
// 3.) Multiply by fractions to make large/small
const nextX = x + (Math.cos(radian) * legLength)
const nextY = y + (Math.sin(radian) * legLength)
drawLine(x, y, nextX, nextY, '#999999', 0.05)
}
const roboSpider = [
{x:1.000000, y:-1.400000, angle: 0},
{x:1.000000, y:-1.000000, angle: 0},
{x:1.000000, y:-0.600000, angle: 0},
{x:1.000000, y:-0.200000, angle: 0},
{x:1.000000, y:0.200000, angle: 0},
{x:1.000000, y:0.600000, angle: 0},
{x:1.000000, y:1.000000, angle: 0},
{x:1.000000, y:1.400000, angle: 0},
{x:-1.000000, y:-1.400000, angle: 4},
{x:-1.000000, y:-1.000000, angle: 4},
{x:-1.000000, y:-0.600000, angle: 4},
{x:-1.000000, y:-0.200000, angle: 4},
{x:-1.000000, y:0.200000, angle: 4},
{x:-1.000000, y:0.600000, angle: 4},
{x:-1.000000, y:1.000000, angle: 4},
{x:-1.000000, y:1.400000, angle: 4},
]
const squareBot = [
{x:1.200000, y:0.800000, angle: 0},
{x:1.200000, y:0.400000, angle: 0},
{x:1.200000, y:0.000000, angle: 0},
{x:1.200000, y:-0.400000, angle: 0},
{x:1.200000, y:-0.800000, angle: 0},
{x:-1.200000, y:0.800000, angle: 4},
{x:-1.200000, y:0.400000, angle: 4},
{x:-1.200000, y:0.000000, angle: 4},
{x:-1.200000, y:-0.400000, angle: 4},
{x:-1.200000, y:-0.800000, angle: 4},
{x: 0.800000, y:1.200000, angle: 2},
{x: 0.400000, y:1.200000, angle: 2},
{x: 0.000000, y:1.200000, angle: 2},
{x:-0.400000, y:1.200000, angle: 2},
{x:-0.800000, y:1.200000, angle: 2},
{x: 0.8000000, y:-1.20000, angle: 6},
{x: 0.4000000, y:-1.20000, angle: 6},
{x: 0.0000000, y:-1.20000, angle: 6},
{x:-0.4000000, y:-1.20000, angle: 6},
{x:-0.8000000, y:-1.20000, angle: 6},
]
const youSpinMeRightRound = (timestamp) => {
requestAnimationFrame(youSpinMeRightRound)
context.save()
context.clearRect(0,0, width, height)
context.translate(halfWidth, halfHeight)
context.scale(squareSize / 12, squareSize / 12)
context.save()
context.rotate(TAU * timestamp / 8000)
roboSpider.forEach(drawLeg)
context.restore()
context.save()
context.rotate(TAU * timestamp / -4000)
squareBot.forEach(drawLeg)
context.restore()
context.restore()
}
requestAnimationFrame(youSpinMeRightRound)
// context.clearRect(0,0, width, height)
// context.translate(halfWidth, halfHeight)
// context.scale(squareSize / 12, squareSize / 12)
// // context.rotate(TAU * timestamp / 4000)
// roboSpider.forEach(drawLeg)
// squareBot.forEach(drawLeg)