forked from trys/clock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
87 lines (69 loc) · 2.1 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const lerp = (x, y, a) => x * (1 - a) + y * a;
const clamp = (a, min = 0, max = 1) => Math.min(max, Math.max(min, a));
const invlerp = (x, y, a) => clamp((a - x) / (y - x));
const range = (x1, y1, x2, y2, a) => lerp(x2, y2, invlerp(x1, y1, a));
(() => {
try {
const test = document.createElement('div')
test.style.background = 'conic-gradient(red, blue)';
document.body.appendChild(test);
const styles = window.getComputedStyle(test);
if (styles.backgroundImage === 'none') {
document.body.classList.add('no-gradient');
document.body.removeChild(test);
}
} catch(e){}
const timeInput = document.querySelector('[name="time"]');
const timerEl = document.querySelector('.timer');
const form = document.querySelector('.input-group');
const ding = document.querySelector('.ding');
let timeRemaining = Number(timeInput.value);
setTimeCSS();
let timer;
const MINUTE = 1 * 1000 * 60;
timerEl.addEventListener('click', toggleTimer);
form.addEventListener('submit', (event) => {
event.preventDefault();
toggleTimer();
})
function toggleTimer(params) {
timer ? stopTimer() : startTimer();
}
function startTimer() {
document.body.classList.add('timer-running');
document.body.classList.remove('timer-paused');
document.body.classList.remove('timer-finished');
timeRemaining = Number(timeInput.value) + 1;
tick();
}
function tick() {
timeRemaining--;
setTimeCSS();
if (!timeRemaining) {
stopTimer();
return;
}
timer = window.setTimeout(tick, MINUTE);
}
function setTimeCSS() {
document.documentElement.style.setProperty(
'--time',
360 - range(0, 60, 0, 360, Number(timeRemaining))
);
}
function stopTimer() {
document.body.classList.add('timer-paused');
document.body.classList.add('timer-finished');
document.body.classList.remove('timer-running');
if (ding) {
ding.play();
}
if (timer) {
window.clearTimeout(timer);
timer = null;
}
setTimeout(() => {
document.body.classList.remove('timer-finished');
}, 1000);
}
})();