-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
311 lines (251 loc) · 9.52 KB
/
script.js
File metadata and controls
311 lines (251 loc) · 9.52 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
const clockCanvas = document.getElementById('clockCanvas');
const clockContext = clockCanvas.getContext('2d');
let circleColor = "gray";
let clockCanvasWidth = clockCanvas.width;
let clockCanvasHeight = clockCanvas.height;
const timeDisplay = document.getElementById("timeDisplay")
const secondsCircleRadius = clockCanvasHeight / 2;
const minutesCircleRadius = clockCanvasHeight / 3.25;
const hoursCircleRadius = clockCanvasHeight / 8;
let secondBlobs = [], minuteBlobs = [], hourBlobs = [];
const secondsSound = new Audio('assets/secondsSound.mp3'); //https://pixabay.com/de/sound-effects/slow-cinematic-clock-ticking-tension-2-323078/
const minutesSound = new Audio('assets/minutesSound.mp3'); //https://pixabay.com/de/sound-effects/tibetan-gong-sound-effect-311179/
const hoursSound = new Audio('assets/hoursSound.mp3'); //https://pixabay.com/de/sound-effects/black-gong-28936/
let intervalId = null;
// Uhr passt sich an Fenstergröße an
function resizeCanvasToFullScreen() {
const dpr = window.devicePixelRatio || 1; // z. B. 2 bei Retina
// CSS-Größe in logischen Pixeln
clockCanvasWidth = window.innerWidth;
clockCanvasHeight = window.innerHeight;
// Setze tatsächliche Zeichenfläche auf physische Pixel
clockCanvas.width = clockCanvasWidth * dpr;
clockCanvas.height = clockCanvasHeight * dpr;
// Skaliere den Canvas-Inhalt runter, damit er optisch passt
clockCanvas.style.width = clockCanvasWidth + "px";
clockCanvas.style.height = clockCanvasHeight + "px";
// Skaliere den Zeichenkontext für scharfes Zeichnen
clockContext.setTransform(dpr, 0, 0, dpr, 0, 0);
}
resizeCanvasToFullScreen();
function clearCanvas(bg = "black") {
clockContext.fillStyle = bg;
clockContext.fillRect(0, 0, clockCanvas.width, clockCanvas.height);
}
class Blob {
constructor(startX, startY, blobType) {
this.x = startX;
this.y = startY;
this.blobType = blobType;
}
updatePosition(speed = 0.05) {
const centerX = clockCanvasWidth / 2;
const centerY = clockCanvasHeight / 2;
const currentDate = new Date();
let count, radius;
if (this.blobType === "secondBlob") {
count = Math.max(currentDate.getSeconds(), 1);
radius = secondsCircleRadius;
} else if (this.blobType === "minuteBlob") {
count = Math.max(currentDate.getMinutes(), 1);
radius = minutesCircleRadius;
} else {
count = Math.max(currentDate.getHours(), 1);
radius = hoursCircleRadius;
}
const spacing = (2 * Math.PI) / count;
// Aktueller Winkel (aus Position berechnet)
const dx = this.x - centerX;
const dy = centerY - this.y;
let currentAngle = Math.atan2(dy, dx);
// Zielwinkel (aus targetX/Y berechnet)
const targetDx = this.targetX - centerX;
const targetDy = centerY - this.targetY;
let targetAngle = Math.atan2(targetDy, targetDx);
// Winkel-Differenz berechnen
let deltaAngle = targetAngle - currentAngle;
// Kürzesten Weg auf dem Kreis nehmen
if (deltaAngle > Math.PI) deltaAngle -= 2 * Math.PI;
if (deltaAngle < -Math.PI) deltaAngle += 2 * Math.PI;
// Interpolation Richtung Zielwinkel
currentAngle += deltaAngle * speed;
// Neue x/y-Koordinaten auf dem Kreis
this.x = centerX + radius * Math.cos(currentAngle);
this.y = centerY - radius * Math.sin(currentAngle);
}
draw() {
let color
switch (this.blobType) {
case "secondBlob":
color = "white";
break;
case "minuteBlob":
color = "lightblue";
break;
case "hourBlob":
color = "slateblue";
break;
default:
color = "pink";
}
clockContext.fillStyle = color;
clockContext.beginPath();
clockContext.arc(this.x, this.y, 6, 0, 2 * Math.PI);
clockContext.fill();
clockContext.closePath();
}
}
class SkippingBlob extends Blob{
constructor(startX, startY, blobType, skippingTargetY) {
super(startX, startY, blobType);
this.skippingTargetX = clockCanvasWidth / 2;
this.skippingTargetY = skippingTargetY;
}
updateSkippingPosition(speed = 0.15){
this.x = this.x + ((this.skippingTargetX - this.x) * speed);
this.y = this.y + ((this.skippingTargetY - this.y) * speed);
}
}
function getTopOfCircle(centerX, centerY, radius) {
return {
x: centerX,
y: centerY - radius
};
}
function updateBlobs(array, count, radius, blobType) {
const centerX = clockCanvasWidth / 2;
const centerY = clockCanvasHeight / 2;
const spacing = (2 * Math.PI) / Math.max(count, 1);
while (array.length < count) {
const top = getTopOfCircle(centerX, centerY, radius);
array.push(new Blob(top.x, top.y, blobType));
}
for (let i = 0; i < count; i++) {
const angle = Math.PI / 2 + (i+1) * spacing;
const targetX = centerX + radius * Math.cos(angle);
const targetY = centerY - radius * Math.sin(angle);
array[i].targetX = targetX;
array[i].targetY = targetY;
}
array.splice(count);
}
let skippingBlob;
function circleSkippingBlobUpdate(blobType){
const centerX = clockCanvasWidth / 2;
const centerY = clockCanvasHeight / 2;
let startPosition = {
x: centerX,
y: -1
}
let targetPositionY;
if (blobType == "secondBlob"){
targetPositionY = centerY - secondsCircleRadius;
}else if (blobType == "minuteBlob"){
startPosition = getTopOfCircle (centerX, centerY, secondsCircleRadius);
targetPositionY = centerY - minutesCircleRadius;
}else{
startPosition = getTopOfCircle(centerX, centerY, minutesCircleRadius);
targetPositionY = centerY - hoursCircleRadius;
}
skippingBlob = new SkippingBlob (startPosition.x, startPosition.y, blobType, targetPositionY)
}
function drawCircles() {
clockContext.strokeStyle = circleColor;
//Draw Seconds Circle
clockContext.beginPath();
clockContext.arc((clockCanvasWidth / 2), (clockCanvasHeight / 2), secondsCircleRadius, 0, 2 * Math.PI, true);
clockContext.stroke();
clockContext.closePath();
//Draw Minutes Circle
clockContext.beginPath();
clockContext.arc((clockCanvasWidth / 2), (clockCanvasHeight / 2), minutesCircleRadius, 0, 2 * Math.PI, true);
clockContext.stroke();
clockContext.closePath();
//Draw Hours Circle
clockContext.beginPath();
clockContext.arc((clockCanvasWidth / 2), (clockCanvasHeight / 2), hoursCircleRadius, 0, 2 * Math.PI, true);
clockContext.stroke();
clockContext.closePath();
}
function clockLoop() {
const currentDate = new Date;
let blobType = "hourBlob";
if (currentDate.getSeconds() !== 0) {
secondsSound.currentTime = 0;
secondsSound.play();
blobType = "secondBlob";
} else if (currentDate.getMinutes() !== 0) {
minutesSound.currentTime = 0;
minutesSound.play();
blobType = "minuteBlob";
} else {
hoursSound.currentTime = 0;
hoursSound.play();
}
circleSkippingBlobUpdate(blobType);
updateBlobs(secondBlobs, currentDate.getSeconds(), secondsCircleRadius, "secondBlob");
updateBlobs(minuteBlobs, currentDate.getMinutes(), minutesCircleRadius, "minuteBlob");
updateBlobs(hourBlobs, currentDate.getHours(), hoursCircleRadius, "hourBlob");
timeDisplay.innerHTML = "Es ist " + currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds() + " Uhr.";
}
function animate() {
clearCanvas();
drawCircles();
const currentDate = new Date;
const currentSeconds = currentDate.getSeconds();
const currentMinutes = currentDate.getMinutes();
const currentHours = currentDate.getHours();
secondBlobs.forEach((b,i) => {
b.updatePosition();
if(i != currentSeconds-1){b.draw();}
});
if(currentSeconds == 0 && currentMinutes != 0){
minuteBlobs.forEach((b, i) => {
b.updatePosition();
if (i != currentMinutes-1){b.draw()};
})
}else{
minuteBlobs.forEach(b => { b.updatePosition(); b.draw(); });
}
if(currentSeconds == 0 && currentMinutes == 0){
hourBlobs.forEach((b, i) => {
b.updatePosition();
if (i != currentHours-1){b.draw();}
})
}else{
hourBlobs.forEach(b => { b.updatePosition(); b.draw(); });
}
skippingBlob.updateSkippingPosition();
skippingBlob.draw();
requestAnimationFrame(animate);
}
/* User inputs / Tastenkürzel:
"Enter" = beginnt das Programm und startet die Uhr
"w" = färbt die Kreise weiß ein
"g" = färbt die Kreise grau ein
"b" = färbt die Kreise schwarz ein
*/
window.addEventListener("resize", resizeCanvasToFullScreen);
document.addEventListener("keydown", function (event) {
if (event.key === "w") {
circleColor = "white";
}
if (event.key === "b") {
circleColor = "black";
}
if (event.key === "g") {
circleColor = "grey";
}
if (event.key === "Shift") {
timeDisplay.style.visibility =
timeDisplay.style.visibility === 'hidden' ? 'visible' : 'hidden';
}
}
);
//starte programm
resizeCanvasToFullScreen();
clockLoop();
if (intervalId === null) {
intervalId = setInterval(clockLoop, 1000);
}
animate();