-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswiss-station-clock-screensave-good-1.html
373 lines (328 loc) · 15.7 KB
/
swiss-station-clock-screensave-good-1.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Swiss Railway Clock with Night Mode</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f2f2f2;
transition: background-color 0.3s;
}
#clock {
width: 1000px;
height: 1000px;
}
body.night-mode {
background-color: #000;
}
</style>
</head>
<body>
<svg id="clock" viewBox="-5 -5 110 110">
<defs>
<filter id="dropShadow" x="-20%" y="-20%" width="200%" height="200%">
<feGaussianBlur in="SourceAlpha" stddeviation="1"></feGaussianBlur>
<feOffset dx="0.5" dy="0.5" result="offsetblur"></feOffset>
<feFlood flood-color="#000000" flood-opacity="0.3"></feFlood>
<feComposite in2="offsetblur" operator="in"></feComposite>
<feMerge>
<feMergeNode></feMergeNode>
<feMergeNode in="SourceGraphic"></feMergeNode>
</feMerge>
</filter>
<linearGradient id="rimGradient" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" style="stop-color:#ffffff;stop-opacity:1" />
<stop offset="100%" style="stop-color:#e0e0e0;stop-opacity:1" />
</linearGradient>
<radialGradient id="faceGradient" cx="50%" cy="50%" r="50%" fx="30%" fy="30%">
<stop offset="0%" style="stop-color:#ffffff;stop-opacity:1" />
<stop offset="100%" style="stop-color:#f8f8f8;stop-opacity:1" />
</radialGradient>
</defs>
<g filter="url(#dropShadow)" id="clockFace">
<circle cx="50" cy="50" r="48" fill="url(#rimGradient)" stroke="#c0c0c0" stroke-width="2" id="rimCircle"></circle>
<circle cx="50" cy="50" r="46" fill="url(#faceGradient)" id="faceCircle"></circle>
</g>
<g id="ticks"></g>
<g id="hourHand"></g>
<g id="minuteHand"></g>
<g id="secondHand"></g>
<circle cx="50" cy="50" r="1.5" fill="#C23132"></circle>
</svg>
<script>
// Color constants
const DAY_COLORS = {
BACKGROUND: '#f2f2f2',
RIM: 'url(#rimGradient)',
RIM_STROKE: '#c0c0c0',
FACE: 'url(#faceGradient)',
HANDS_TICKS: 'black',
SECOND_HAND: '#C23132',
CENTER_DOT: '#C23132'
};
// Constants
const BRIGHTNESS_STEP = 10;
const MAX_BRIGHTNESS = 200;
const MIN_BRIGHTNESS = 10;
const SPRING_FREQUENCY = 5; // Controls number of oscillations
const SPRING_DECAY = 2; // Controls how quickly oscillations die out
const SPRING_DURATION = 500; // Animation duration in ms
const MAX_OVERSHOOT = 12; // Maximum overshoot angle in degrees
// Variables
let currentBrightness = 18;
let isNightMode = isNightTime();
// Color objects
const NIGHT_COLORS = {
BACKGROUND: '#000000',
RIM: '#000000',
RIM_STROKE: '#000000',
FACE: '#000000',
HANDS_TICKS: getBrightnessColor(currentBrightness),
SECOND_HAND: getBrightnessColor(currentBrightness),
CENTER_DOT: getBrightnessColor(currentBrightness)
};
function getBrightnessColor(brightness) {
const hex = Math.min(255, Math.max(0, brightness)).toString(16).padStart(2, '0');
return `#${hex}${hex}${hex}`;
}
const clockRadius = 48;
const centerX = 50, centerY = 50;
function isNightTime() {
const currentHour = new Date().getHours();
return currentHour >= 23 || currentHour < 7;
}
function createHand(params) {
const { baseWidth, tipWidth, length, stickOut } = params;
const hand = document.createElementNS("http://www.w3.org/2000/svg", "path");
const d = `
M ${centerX - baseWidth/2} ${centerY + stickOut}
L ${centerX - tipWidth/2} ${centerY - length + stickOut}
L ${centerX + tipWidth/2} ${centerY - length + stickOut}
L ${centerX + baseWidth/2} ${centerY + stickOut}
Z
`;
hand.setAttribute('d', d);
hand.setAttribute('fill', 'black');
return hand;
}
function createSecondHand(params) {
const { length, circleRadius, strokeWidth, stickOut } = params;
const group = document.createElementNS("http://www.w3.org/2000/svg", "g");
const line = document.createElementNS("http://www.w3.org/2000/svg", "line");
line.setAttribute('x1', centerX);
line.setAttribute('y1', centerY + stickOut);
line.setAttribute('x2', centerX);
line.setAttribute('y2', centerY - length + stickOut + circleRadius-1);
line.setAttribute('stroke', '#C23132');
line.setAttribute('stroke-width', strokeWidth);
const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
circle.setAttribute('cx', centerX);
circle.setAttribute('cy', centerY - length + stickOut);
circle.setAttribute('r', circleRadius);
circle.setAttribute('fill', '#C23132');
group.appendChild(line);
group.appendChild(circle);
return group;
}
function createTicks() {
const ticks = document.getElementById('ticks');
for (let i = 0; i < 60; i++) {
const tick = document.createElementNS("http://www.w3.org/2000/svg", "line");
const angle = i * 6;
const isHour = i % 5 === 0;
const length = isHour ? 11 : 3.2;
const width = isHour ? 3 : 1.2;
tick.setAttribute('x1', centerX + 44 * Math.cos((angle - 90) * Math.PI / 180));
tick.setAttribute('y1', centerY + 44 * Math.sin((angle - 90) * Math.PI / 180));
tick.setAttribute('x2', centerX + (44 - length) * Math.cos((angle - 90) * Math.PI / 180));
tick.setAttribute('y2', centerY + (44 - length) * Math.sin((angle - 90) * Math.PI / 180));
tick.setAttribute('stroke', 'black');
tick.setAttribute('stroke-width', width);
ticks.appendChild(tick);
}
}
let minuteHandAnimationStart = 0;
let isMinuteHandAnimating = false;
function springEasing(t) {
if (t >= 1) return 1;
const decay = Math.pow(1 - t, SPRING_DECAY);
const oscillation = Math.cos(SPRING_FREQUENCY * Math.PI * t);
return 1 + decay * oscillation;
}
function updateClock() {
const now = new Date();
const hours = now.getHours() % 12;
const minutes = now.getMinutes();
const seconds = now.getSeconds();
const milliseconds = now.getMilliseconds();
// Hour hand
const hourAngle = (hours * 30 + minutes * 0.5) % 360;
document.getElementById('hourHand').setAttribute('transform', `rotate(${hourAngle}, ${centerX}, ${centerY})`);
// Second hand
let secondAngle;
const totalMilliseconds = seconds * 1000 + milliseconds;
if (totalMilliseconds <= 58500) {
secondAngle = (totalMilliseconds / 58500) * 360;
} else {
secondAngle = 0;
}
document.getElementById('secondHand').setAttribute('transform', `rotate(${secondAngle}, ${centerX}, ${centerY})`);
// Minute hand
let minuteAngle = minutes * 6;
if (seconds === 0 && milliseconds <= SPRING_DURATION) {
if (!isMinuteHandAnimating) {
isMinuteHandAnimating = true;
minuteHandAnimationStart = performance.now();
}
const elapsed = performance.now() - minuteHandAnimationStart;
if (elapsed <= SPRING_DURATION) {
const progress = elapsed / SPRING_DURATION;
const springProgress = springEasing(progress);
minuteAngle = minutes * 6 + (MAX_OVERSHOOT * (1 - springProgress));
} else {
isMinuteHandAnimating = false;
minuteAngle = minutes * 6;
}
} else {
isMinuteHandAnimating = false;
}
document.getElementById('minuteHand').setAttribute('transform', `rotate(${minuteAngle}, ${centerX}, ${centerY})`);
requestAnimationFrame(updateClock);
}
function toggleNightMode() {
isNightMode = !isNightMode;
document.body.classList.toggle('night-mode');
if (isNightMode) {
applyNightModeStyles();
} else {
applyDayModeStyles();
}
console.log("Night mode toggled. Is night mode:", isNightMode);
}
function applyNightModeStyles() {
console.log("Applying night mode styles. Current brightness:", currentBrightness);
const clockFace = document.getElementById('clockFace');
const rimCircle = document.getElementById('rimCircle');
const faceCircle = document.getElementById('faceCircle');
const ticks = document.getElementById('ticks');
const hourHand = document.getElementById('hourHand').firstChild;
const minuteHand = document.getElementById('minuteHand').firstChild;
const secondHand = document.getElementById('secondHand');
const centerDot = document.querySelector('circle[cx="50"][cy="50"][r="1.5"]');
NIGHT_COLORS.HANDS_TICKS = getBrightnessColor(currentBrightness);
NIGHT_COLORS.SECOND_HAND = NIGHT_COLORS.HANDS_TICKS; // Set second hand color to match other hands
NIGHT_COLORS.CENTER_DOT = NIGHT_COLORS.HANDS_TICKS; // Set center dot color to match hands
document.body.style.backgroundColor = NIGHT_COLORS.BACKGROUND;
rimCircle.style.fill = NIGHT_COLORS.RIM;
rimCircle.style.stroke = NIGHT_COLORS.RIM_STROKE;
faceCircle.style.fill = NIGHT_COLORS.FACE;
clockFace.setAttribute('filter', 'none');
ticks.querySelectorAll('line').forEach(tick => tick.style.stroke = NIGHT_COLORS.HANDS_TICKS);
hourHand.style.fill = NIGHT_COLORS.HANDS_TICKS;
minuteHand.style.fill = NIGHT_COLORS.HANDS_TICKS;
secondHand.querySelector('line').style.stroke = NIGHT_COLORS.SECOND_HAND;
secondHand.querySelector('circle').style.fill = NIGHT_COLORS.SECOND_HAND;
centerDot.style.fill = NIGHT_COLORS.CENTER_DOT;
console.log("Night mode styles applied. Hands color:", NIGHT_COLORS.HANDS_TICKS);
}
function applyDayModeStyles() {
console.log("Applying day mode styles");
const clockFace = document.getElementById('clockFace');
const rimCircle = document.getElementById('rimCircle');
const faceCircle = document.getElementById('faceCircle');
const ticks = document.getElementById('ticks');
const hourHand = document.getElementById('hourHand').firstChild;
const minuteHand = document.getElementById('minuteHand').firstChild;
const secondHand = document.getElementById('secondHand');
const centerDot = document.querySelector('circle[cx="50"][cy="50"][r="1.5"]');
// Reset background
document.body.style.backgroundColor = DAY_COLORS.BACKGROUND;
// Reset clock face
rimCircle.setAttribute('fill', DAY_COLORS.RIM);
rimCircle.setAttribute('stroke', DAY_COLORS.RIM_STROKE);
faceCircle.setAttribute('fill', DAY_COLORS.FACE);
// Remove any inline styles
rimCircle.removeAttribute('style');
faceCircle.removeAttribute('style');
// Reapply Gaussian blur
clockFace.setAttribute('filter', 'url(#dropShadow)');
// Reset ticks
ticks.querySelectorAll('line').forEach(tick => {
tick.setAttribute('stroke', DAY_COLORS.HANDS_TICKS);
tick.removeAttribute('style');
});
// Reset hands
hourHand.setAttribute('fill', DAY_COLORS.HANDS_TICKS);
hourHand.removeAttribute('style');
minuteHand.setAttribute('fill', DAY_COLORS.HANDS_TICKS);
minuteHand.removeAttribute('style');
// Reset second hand
const secondHandLine = secondHand.querySelector('line');
const secondHandCircle = secondHand.querySelector('circle');
secondHandLine.setAttribute('stroke', DAY_COLORS.SECOND_HAND);
secondHandLine.removeAttribute('style');
secondHandCircle.setAttribute('fill', DAY_COLORS.SECOND_HAND);
secondHandCircle.removeAttribute('style');
// Reset center dot
centerDot.setAttribute('fill', DAY_COLORS.CENTER_DOT);
centerDot.removeAttribute('style');
console.log("Day mode styles applied");
}
function adjustBrightness(increase) {
if (!isNightMode) {
console.log("Not in night mode, brightness adjustment skipped");
return;
}
if (increase) {
currentBrightness = Math.min(MAX_BRIGHTNESS, currentBrightness + BRIGHTNESS_STEP);
console.log("Increasing brightness to:", currentBrightness);
} else {
currentBrightness = Math.max(MIN_BRIGHTNESS, currentBrightness - BRIGHTNESS_STEP);
console.log("Decreasing brightness to:", currentBrightness);
}
applyNightModeStyles();
}
// Update the CSS for night mode
const styleElement = document.createElement('style');
styleElement.textContent = `
body.night-mode {
background-color: ${NIGHT_COLORS.BACKGROUND};
}
`;
document.head.appendChild(styleElement);
// Initialize the clock
createTicks();
document.getElementById('hourHand').appendChild(createHand({ baseWidth: 6, tipWidth: 4.5, length: 38, stickOut: 9 }));
document.getElementById('minuteHand').appendChild(createHand({ baseWidth: 5.7, tipWidth: 3.5, length: 52.2, stickOut: 9 }));
document.getElementById('secondHand').appendChild(createSecondHand({ length: 42, circleRadius: 5, strokeWidth: 1.4, stickOut: 13 }));
updateClock();
// Set initial mode
if (isNightMode) {
document.body.classList.add('night-mode');
applyNightModeStyles();
} else {
applyDayModeStyles();
}
document.addEventListener('keydown', (event) => {
console.log("Key pressed:", event.key);
if (event.key === 'n' || event.key === 'N') {
toggleNightMode();
} else if (isNightMode) {
if (event.key === 'b' || event.key === 'B') {
console.log("Brightness increase requested");
adjustBrightness(true);
} else if (event.key === 'd' || event.key === 'D') {
console.log("Brightness decrease requested");
adjustBrightness(false);
}
}
});
</script>
</body>
</html>