-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
64 lines (55 loc) · 2.13 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
// Neumorphic Digital Clock - JavaScript Code
// Author: bufferwise
// Join the Discord community:
// https://discord.gg/26MMXRHgZB
// Query all strip elements (hour, minute, second)
const strips = [...document.querySelectorAll(".strip")];
const numberSize = 8; // Number size in 'vmin' for the sliding effect
/**
* Highlights a specific digit on the given strip.
* Adds a "pop" effect briefly for a visual highlight.
*
* @param {number} stripIndex - The index of the strip (hour, minute, second).
* @param {number} digit - The digit to highlight (0-9).
*/
function highlight(stripIndex, digit) {
const selectedNumber = strips[stripIndex].querySelector(`.number:nth-of-type(${digit + 1})`);
selectedNumber.classList.add("pop");
// Remove the highlight class after 950ms to create a ticking effect
setTimeout(() => {
selectedNumber.classList.remove("pop");
}, 950);
}
/**
* Slides the strip to show the current digit.
*
* @param {number} stripIndex - The index of the strip to update (0 for hours, 2 for minutes, 4 for seconds).
* @param {number} number - The two-digit number to display on the strip.
*/
function stripSlider(stripIndex, number) {
// Split number into two digits
const firstDigit = Math.floor(number / 10);
const secondDigit = number % 10;
// Slide the first digit
strips[stripIndex].style.transform = `translateY(${firstDigit * -numberSize}vmin)`;
highlight(stripIndex, firstDigit);
// Slide the second digit
strips[stripIndex + 1].style.transform = `translateY(${secondDigit * -numberSize}vmin)`;
highlight(stripIndex + 1, secondDigit);
}
/**
* Updates the clock every second.
* Retrieves the current time and updates the hour, minute, and second strips accordingly.
*/
function updateClock() {
const time = new Date();
const hours = time.getHours();
const minutes = time.getMinutes();
const seconds = time.getSeconds();
// Update the hour, minute, and second strips
stripSlider(0, hours);
stripSlider(2, minutes);
stripSlider(4, seconds);
}
// Set an interval to update the clock every second
setInterval(updateClock, 1000);