-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
41 lines (28 loc) · 1.2 KB
/
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Learn how to code this watch step by step on YouTube:
https://www.youtube.com/watch?v=ULomsOSk4JA
Follow me on twitter for more: https://twitter.com/HunorBorbely
*/
const textElement = document.getElementById("text");
const hoursElement = document.getElementById("hour_hand");
const minutesElement = document.getElementById("minute_hand");
const secondsElement = document.getElementById("second_hand");
let showDate = true;
function animate() {
const date = new Date();
const day = date.getDate();
const ampm = date.getHours() >= 12 ? "PM" : "AM";
const hour = date.getHours() + date.getMinutes() / 60;
const minute = date.getMinutes() + date.getSeconds() / 60;
const second = date.getSeconds() + date.getMilliseconds() / 1000;
textElement.textContent = showDate ? day : ampm;
hoursElement.setAttribute("transform", `rotate(${(360 / 12) * hour})`);
minutesElement.setAttribute("transform", `rotate(${(360 / 60) * minute})`);
secondsElement.setAttribute("transform", `rotate(${(360 / 60) * second})`);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
textElement.addEventListener("click", () => {
showDate = !showDate;
});
document.querySelector("svg").style.display = "block";