-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (70 loc) · 3.06 KB
/
index.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
const clock = document.querySelector('#clock');
const clockCenter = document.querySelector('#clock #center');
const hourHand = document.querySelector('#hour-hand');
const minuteHand = document.querySelector('#minute-hand');
const secondHand = document.querySelector('#second-hand');
const TICKS_PER_SECOND = 4;
const INTERVAL = 1000 / TICKS_PER_SECOND;
function removeMarksAndNumbers() {
const marksAndNumbers = document.querySelectorAll('.mark, .number');
for (const markOrNumber of marksAndNumbers) {
clock.removeChild(markOrNumber);
}
}
function createMarks() {
const clockDiameter = parseFloat(getComputedStyle(clock).width);
for (let i = 0; i < 60; i++) {
const mark = document.createElement('div');
mark.classList.add('mark');
mark.style.transform = `translate(-50%) rotate(${i * 6}deg) translate(0, calc(${clockDiameter / 2}px - var(--mark-length)))`;
if (i % 5 === 0) {
mark.classList.add('mark-thick');
mark.style.transform = `translate(-50%) rotate(${i * 6}deg) translate(0, calc(${
clockDiameter / 2
}px - var(--thick-mark-length)))`;
}
clock.appendChild(mark);
}
}
function createNumbers() {
for (let i = 1; i <= 12; i++) {
const number = document.createElement('div');
number.classList.add('number');
number.style.transform = `translate(-50%, -50%) rotate(${i * 30}deg) translate(0, -30vmin) rotate(-${i * 30}deg)`;
number.textContent = i;
clock.appendChild(number);
}
}
function displayTime() {
const currentDate = new Date();
const hour = currentDate.getHours();
const minute = currentDate.getMinutes();
const second = currentDate.getSeconds();
const hourHandDegrees = ((hour % 12) + minute / 60 + second / 3600) * (360 / 12) + 180;
const minuteHandDegrees = (minute + second / 60) * (360 / 60) + 180;
const secondHandDegrees = second * (360 / 60) + 180;
hourHand.style.transform = `translate(-50%, 0) rotate(${hourHandDegrees}deg)`;
minuteHand.style.transform = `translate(-50%, 0) rotate(${minuteHandDegrees}deg)`;
secondHand.style.transform = `translate(-50%, 0) rotate(${secondHandDegrees}deg)`;
setInterval(() => {
const currentDate = new Date();
const hour = currentDate.getHours();
const minute = currentDate.getMinutes();
const second = currentDate.getSeconds();
const hourHandDegrees = ((hour % 12) + minute / 60 + second / 3600) * (360 / 12) + 180;
const minuteHandDegrees = (minute + second / 60) * (360 / 60) + 180;
const secondHandDegrees = second * (360 / 60) + 180;
hourHand.style.transform = `translate(-50%, 0) rotate(${hourHandDegrees}deg)`;
minuteHand.style.transform = `translate(-50%, 0) rotate(${minuteHandDegrees}deg)`;
secondHand.style.transform = `translate(-50%, 0) rotate(${secondHandDegrees}deg)`;
}, INTERVAL);
}
// Re-compute marks and numbers positions on window resize
window.addEventListener('resize', (e) => {
removeMarksAndNumbers();
createMarks();
createNumbers();
});
createMarks();
createNumbers();
displayTime();