-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
25 lines (20 loc) · 1010 Bytes
/
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
function updateClock() {
const now = new Date();
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
const milliseconds = now.getMilliseconds();
// now calculating degrees with milli seconds for smoother movement
const hourDeg = (hours % 12) * 30 + (minutes / 60) * 30 + (seconds / 3600) * 30;
const minuteDeg = minutes * 6 + (seconds / 60) * 6;
const secondDeg = seconds * 6 + (milliseconds / 1000) * 6;
const hourHand = document.querySelector('.hour-hand');
const minuteHand = document.querySelector('.minute-hand');
const secondHand = document.querySelector('.second-hand');
hourHand.style.transform = `translateX(-50%) rotate(${hourDeg}deg)`;
minuteHand.style.transform = `translateX(-50%) rotate(${minuteDeg}deg)`;
secondHand.style.transform = `translateX(-50%) rotate(${secondDeg}deg)`;
// requesting next frame for smooth animation
requestAnimationFrame(updateClock);
}
updateClock();