-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
78 lines (67 loc) · 2.08 KB
/
app.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
const circle = document.getElementById("circle");
const instruction = document.getElementById("instruction");
const startBtn = document.getElementById("start-btn");
const playIcon = document.getElementById("play-icon");
const stopIcon = document.getElementById("stop-icon");
let isRunning = false;
let breatheInTimeout, breatheOutTimeout;
let countdownInterval;
const totalTime = 300;
let remainingTime = totalTime;
function updateProgressBar() {
const elapsedPercentage = ((totalTime - remainingTime) / totalTime) * 100;
const progressCircle = document.querySelector("#progress-circle circle:last-child");
const circleLength = 2 * Math.PI * 45;
const progressOffset = circleLength * (1 - elapsedPercentage / 100);
progressCircle.setAttribute("stroke-dashoffset", progressOffset.toString());
}
function breatheIn() {
instruction.textContent = "Inspirer";
circle.style.animation = "breathe-in 5s linear infinite both";
}
function breatheOut() {
instruction.textContent = "Expirer";
circle.style.animation = "breathe-out 5s linear infinite both";
}
function startBreathing() {
isRunning = true;
playIcon.style.display = "none";
stopIcon.style.display = "inline-block";
breatheIn();
breatheInTimeout = setTimeout(() => {
breatheOut();
breatheOutTimeout = setTimeout(() => {
startBreathing();
}, 5000);
}, 5000);
clearInterval(countdownInterval);
countdownInterval = setInterval(() => {
if (remainingTime <= 0) {
clearInterval(countdownInterval);
stopBreathing();
return;
}
remainingTime--;
updateProgressBar();
}, 1000);
}
function stopBreathing() {
isRunning = false;
playIcon.style.display = "inline-block";
stopIcon.style.display = "none";
clearTimeout(breatheInTimeout);
clearTimeout(breatheOutTimeout);
clearInterval(countdownInterval);
remainingTime = totalTime;
updateProgressBar();
circle.style.animation = "";
instruction.textContent = "Terminé";
}
startBtn.addEventListener("click", () => {
if (!isRunning) {
startBreathing();
} else {
stopBreathing();
}
});
updateProgressBar();