-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
228 lines (200 loc) · 10.5 KB
/
script.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.canvas.width = ((window.innerHeight / 3) * 2) - 80;
ctx.canvas.height = window.innerHeight - 120;
const numbers = {
0: [
[{hours: 3, minutes: 30}, {hours: 3, minutes: 45}, {hours: 3, minutes: 45}, {hours: 9, minutes: 30}],
[{hours: 12, minutes: 30}, {hours: 3, minutes: 30}, {hours: 9, minutes: 30}, {hours: 12, minutes: 30}],
[{hours: 12, minutes: 30}, {hours: 12, minutes: 30}, {hours: 12, minutes: 30}, {hours: 12, minutes: 30}],
[{hours: 12, minutes: 30}, {hours: 12, minutes: 30}, {hours: 12, minutes: 30}, {hours: 12, minutes: 30}],
[{hours: 12, minutes: 30}, {hours: 12, minutes: 15}, {hours: 12, minutes: 45}, {hours: 12, minutes: 30}],
[{hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 3, minutes: 45}, {hours: 12, minutes: 45}],
],
1: [
[{hours: 3, minutes: 30}, {hours: 3, minutes: 45}, {hours: 6, minutes: 45}, {hours: 7, minutes: 35}],
[{hours: 12, minutes: 15}, {hours: 6, minutes: 45}, {hours: 12, minutes: 30}, {hours: 7, minutes: 35}],
[{hours: 7, minutes: 35}, {hours: 12, minutes: 30}, {hours: 12, minutes: 30}, {hours: 7, minutes: 35}],
[{hours: 7, minutes: 35}, {hours: 12, minutes: 30}, {hours: 12, minutes: 30}, {hours: 7, minutes: 35}],
[{hours: 3, minutes: 30}, {hours: 12, minutes: 45}, {hours: 12, minutes: 15}, {hours: 6, minutes: 45}],
[{hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 3, minutes: 45}, {hours: 12, minutes: 45}],
],
2: [
[{hours: 3, minutes: 30}, {hours: 3, minutes: 45}, {hours: 3, minutes: 45}, {hours: 6, minutes: 45}],
[{hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 6, minutes: 45}, {hours: 12, minutes: 30}],
[{hours: 3, minutes: 30}, {hours: 3, minutes: 45}, {hours: 12, minutes: 45}, {hours: 12, minutes: 30}],
[{hours: 12, minutes: 30}, {hours: 3, minutes: 30}, {hours: 3, minutes: 45}, {hours: 12, minutes: 45}],
[{hours: 12, minutes: 30}, {hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 9, minutes: 30}],
[{hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 3, minutes: 45}, {hours: 12, minutes: 45}],
],
3: [
[{hours: 3, minutes: 30}, {hours: 3, minutes: 45}, {hours: 3, minutes: 45}, {hours: 6, minutes: 45}],
[{hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 6, minutes: 45}, {hours: 12, minutes: 30}],
[{hours: 6, minutes: 15}, {hours: 3, minutes: 45}, {hours: 12, minutes: 45}, {hours: 12, minutes: 30}],
[{hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 6, minutes: 45}, {hours: 12, minutes: 30}],
[{hours: 3, minutes: 30}, {hours: 3, minutes: 45}, {hours: 12, minutes: 45}, {hours: 12, minutes: 30}],
[{hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 3, minutes: 45}, {hours: 12, minutes: 45}],
],
4: [
[{hours: 3, minutes: 30}, {hours: 6, minutes: 45}, {hours: 3, minutes: 30}, {hours: 9, minutes: 30}],
[{hours: 12, minutes: 30}, {hours: 12, minutes: 30}, {hours: 12, minutes: 30}, {hours: 12, minutes: 30}],
[{hours: 12, minutes: 30}, {hours: 12, minutes: 15}, {hours: 12, minutes: 45}, {hours: 12, minutes: 30}],
[{hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 6, minutes: 45}, {hours: 12, minutes: 30}],
[{hours: 7, minutes: 35}, {hours: 7, minutes: 35}, {hours: 12, minutes: 30}, {hours: 12, minutes: 30}],
[{hours: 7, minutes: 35}, {hours: 7, minutes: 35}, {hours: 12, minutes: 15}, {hours: 12, minutes: 45}],
],
5: [
[{hours: 3, minutes: 30}, {hours: 3, minutes: 45}, {hours: 3, minutes: 45}, {hours: 6, minutes: 45}],
[{hours: 12, minutes: 30}, {hours: 3, minutes: 30}, {hours: 3, minutes: 45}, {hours: 12, minutes: 45}],
[{hours: 12, minutes: 30}, {hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 9, minutes: 30}],
[{hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 6, minutes: 45}, {hours: 12, minutes: 30}],
[{hours: 3, minutes: 30}, {hours: 3, minutes: 45}, {hours: 12, minutes: 45}, {hours: 12, minutes: 30}],
[{hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 3, minutes: 45}, {hours: 12, minutes: 45}],
],
6: [
[{hours: 3, minutes: 30}, {hours: 3, minutes: 45}, {hours: 3, minutes: 45}, {hours: 6, minutes: 45}],
[{hours: 12, minutes: 30}, {hours: 3, minutes: 30}, {hours: 3, minutes: 45}, {hours: 12, minutes: 45}],
[{hours: 12, minutes: 30}, {hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 9, minutes: 30}],
[{hours: 12, minutes: 30}, {hours: 3, minutes: 30}, {hours: 6, minutes: 45}, {hours: 12, minutes: 30}],
[{hours: 12, minutes: 30}, {hours: 12, minutes: 15}, {hours: 12, minutes: 45}, {hours: 12, minutes: 30}],
[{hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 3, minutes: 45}, {hours: 12, minutes: 45}],
],
7: [
[{hours: 3, minutes: 30}, {hours: 3, minutes: 45}, {hours: 3, minutes: 45}, {hours: 6, minutes: 45}],
[{hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 9, minutes: 30}, {hours: 12, minutes: 30}],
[{hours: 7, minutes: 35}, {hours: 7, minutes: 35}, {hours: 12, minutes: 30}, {hours: 12, minutes: 30}],
[{hours: 7, minutes: 35}, {hours: 7, minutes: 35}, {hours: 12, minutes: 30}, {hours: 12, minutes: 30}],
[{hours: 7, minutes: 35}, {hours: 7, minutes: 35}, {hours: 12, minutes: 30}, {hours: 12, minutes: 30}],
[{hours: 7, minutes: 35}, {hours: 7, minutes: 35}, {hours: 12, minutes: 15}, {hours: 12, minutes: 45}],
],
8: [
[{hours: 3, minutes: 30}, {hours: 3, minutes: 45}, {hours: 3, minutes: 45}, {hours: 9, minutes: 30}],
[{hours: 12, minutes: 30}, {hours: 3, minutes: 30}, {hours: 9, minutes: 30}, {hours: 12, minutes: 30}],
[{hours: 12, minutes: 23}, {hours: 12, minutes: 15}, {hours: 12, minutes: 45}, {hours: 12, minutes: 37}],
[{hours: 6, minutes: 7}, {hours: 3, minutes: 30}, {hours: 9, minutes: 30}, {hours: 6, minutes: 53}],
[{hours: 12, minutes: 30}, {hours: 12, minutes: 15}, {hours: 12, minutes: 45}, {hours: 12, minutes: 30}],
[{hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 3, minutes: 45}, {hours: 12, minutes: 45}],
],
9: [
[{hours: 3, minutes: 30}, {hours: 3, minutes: 45}, {hours: 3, minutes: 45}, {hours: 6, minutes: 45}],
[{hours: 12, minutes: 30}, {hours: 6, minutes: 15}, {hours: 9, minutes: 30}, {hours: 12, minutes: 30}],
[{hours: 12, minutes: 30}, {hours: 12, minutes: 15}, {hours: 12, minutes: 45}, {hours: 12, minutes: 30}],
[{hours: 12, minutes: 15}, {hours: 3, minutes: 45}, {hours: 9, minutes: 30}, {hours: 12, minutes: 30}],
[{hours: 7, minutes: 35}, {hours: 7, minutes: 35}, {hours: 12, minutes: 30}, {hours: 12, minutes: 30}],
[{hours: 7, minutes: 35}, {hours: 7, minutes: 35}, {hours: 12, minutes: 15}, {hours: 12, minutes: 45}],
],
};
class Clock {
constructor(radius, centerX, centerY) {
this.strokeWidth = 3;
this.strokeColor = '#333';
this.armColor = 'white';
this.armWidth = 3;
this.radius = radius - this.strokeWidth;
this.centerX = centerX;
this.centerY = centerY;
this.hours = Math.floor((Math.random() * 12) + 1);
this.minutes = Math.floor((Math.random() * 60) + 1);
}
draw() {
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
ctx.lineWidth = this.strokeWidth;
ctx.strokeStyle = this.strokeColor;
ctx.beginPath();
ctx.arc(this.centerX, this.centerY, this.radius, 0, 2 * Math.PI, false);
ctx.stroke();
}
setTime(hours, minutes) {
this.hours = parseFloat(hours.toFixed(2));
this.minutes = parseFloat(minutes.toFixed(2));
this.drawArm(this.hours / 12);
this.drawArm(this.minutes / 60);
}
get time() {
return {hours: this.hours, minutes: this.minutes};
}
drawArm(progress) {
const armRadians = ((2 * Math.PI) * progress) - ((2 * Math.PI) / 4);
const armLength = this.radius - this.strokeWidth * 2;
const targetX = this.centerX + Math.cos(armRadians) * armLength;
const targetY = this.centerY + Math.sin(armRadians) * armLength;
ctx.lineWidth = this.armWidth;
ctx.strokeStyle = this.armColor;
ctx.beginPath();
ctx.moveTo(this.centerX, this.centerY);
ctx.lineTo(targetX, targetY);
ctx.stroke();
}
}
class ClockPanel {
constructor(height, posX, posY) {
this.height = height;
this.width = (height / 3) * 2;
this.posX = posX;
this.posY = posY;
this.rows = 6;
this.columns = 4;
this.clockSpacing = 2;
this.clocks = Array.from({length: this.rows}, i => []);
const colWidth = this.width / this.columns;
const rowHeight = this.height / this.rows;
let cX = this.posX - (colWidth / 2);
let cY = this.posY - (rowHeight / 2);
for (let row = 0; row < this.rows; row++) {
cX = this.posX - (colWidth / 2);
cY = cY + rowHeight;
for (let col = 0; col < this.columns; col++) {
cX = cX + colWidth;
const clockSize = ((this.height / this.rows) / 2) - this.clockSpacing / 2;
const clock = new Clock(clockSize, cX, cY);
this.clocks[row].push(clock);
}
}
}
draw() {
this.clocks.forEach(row => row.forEach(clock => clock.draw()));
}
displayNumber(numberArray) {
numberArray.forEach((row, rowIndex) => {
row.forEach((col, colIndex) => {
const clock = this.clocks[rowIndex][colIndex];
let h = clock.time.hours;
let m = clock.time.minutes;
if (clock.time.minutes !== col.minutes) {
m = clock.time.minutes === 60 ? 0 : clock.time.minutes + 1;
}
if (clock.time.hours !== col.hours) {
h = clock.time.hours === 12 ? 0 : clock.time.hours + 0.1;
}
clock.setTime(h, m);
});
});
}
}
const rows = 2;
const columns = 2;
const panels = [];
for (let r = 0; r < rows; r++) {
for (let c = 0; c < columns; c++) {
const panelPosX = (ctx.canvas.width / columns) * c;
const panelPosY = (ctx.canvas.height / rows) * r;
panels.push(new ClockPanel(ctx.canvas.height / 2, panelPosX, panelPosY));
}
}
const draw = () => {
const today = new Date();
const hours = (`0${today.getHours()}`).slice(-2);
const minutes = (`0${today.getMinutes()}`).slice(-2);
ctx.canvas.width = ((window.innerHeight / 3) * 2) - 80;
ctx.canvas.height = window.innerHeight - 120;
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
panels.forEach(p => p.draw());
panels[0].displayNumber(numbers[String(hours).charAt(0)]);
panels[1].displayNumber(numbers[String(hours).charAt(1)]);
panels[2].displayNumber(numbers[String(minutes).charAt(0)]);
panels[3].displayNumber(numbers[String(minutes).charAt(1)]);
requestAnimationFrame(draw);
}
const init = () => requestAnimationFrame(draw);
init();