-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
67 lines (57 loc) · 1.81 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
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
// DOM elements
const animationContainer = document.querySelector(".breath-animation");
const startBtn = document.getElementById("start-btn");
const pauseBtn = document.getElementById("pause-btn");
const resetBtn = document.getElementById("reset-btn");
const animationSpeedInput = document.getElementById("animation-speed");
// Animation variables
let animationInterval;
let isAnimating = false;
let animationSpeed = 5; // Default animation speed
// Sound effect
const audio = new Audio("background_sound.mp3");
// Start the animation
function startAnimation() {
if (!isAnimating) {
isAnimating = true;
animationInterval = setInterval(breathAnimation, 1000 / animationSpeed);
audio.loop = true;
audio.play();
}
}
// Pause the animation
function pauseAnimation() {
if (isAnimating) {
isAnimating = false;
clearInterval(animationInterval);
audio.pause();
}
}
// Reset the animation
function resetAnimation() {
pauseAnimation();
animationContainer.style.transform = "scale(1)";
}
// Perform the breathing animation
function breathAnimation() {
const progressBar = document.querySelector(".progress-bar");
progressBar.style.transition = `width ${animationSpeed / 2}s linear`;
progressBar.style.width = "100%";
animationContainer.style.transform = "scale(1.2)";
setTimeout(() => {
progressBar.style.width = "0";
animationContainer.style.transform = "scale(1)";
}, animationSpeed * 500);
}
// Event listeners for buttons
startBtn.addEventListener("click", startAnimation);
pauseBtn.addEventListener("click", pauseAnimation);
resetBtn.addEventListener("click", resetAnimation);
// Event listener for animation speed input
animationSpeedInput.addEventListener("input", () => {
animationSpeed = animationSpeedInput.value;
if (isAnimating) {
pauseAnimation();
startAnimation();
}
});