Skip to content

Commit 4b9a124

Browse files
committed
✨ Pause timer on click
1 parent acb993e commit 4b9a124

File tree

3 files changed

+68
-31
lines changed

3 files changed

+68
-31
lines changed

css/style.css

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,14 @@ body {
6262
color: white;
6363
text-shadow: 2px 2px 6px rgba(0, 0, 0, 0.8);
6464
z-index: 10;
65-
user-select: none;
66-
pointer-events: none;
65+
66+
&.pause {
67+
animation: blink 5s infinite;
68+
}
6769
}
6870

69-
#startMessage {
71+
72+
.action-button {
7073
position: fixed;
7174
bottom: 10%;
7275
left: 50%;
@@ -75,6 +78,10 @@ body {
7578
animation: pulse 1.2s infinite alternate;
7679
transition: opacity 0.6s ease;
7780
user-select: none;
81+
82+
&.hide{
83+
display: none;
84+
}
7885
}
7986

8087
#settingsForm {
@@ -170,14 +177,13 @@ body {
170177
opacity: 0.75;
171178
}
172179

173-
#toggleSettings {
180+
#showSettings {
174181
position: absolute;
175182
bottom: 0;
176183
right: 0;
177184
z-index: 20;
178185
}
179186

180-
181187
/* Animation de pulsation */
182188
@keyframes pulse {
183189
0% {transform: translate(-50%, -50%) scale(1);}
@@ -187,7 +193,7 @@ body {
187193
/* Animation de clignotement */
188194
@keyframes blink {
189195
0%, 50%, 100% { opacity: 1;}
190-
25%, 75% { opacity: 0;}
196+
25%, 75% { opacity: 0.1;}
191197
}
192198

193199
.blinking {
@@ -214,4 +220,5 @@ body {
214220
font-weight: bold;
215221
text-align: center;
216222
padding: 0.2em;
223+
background: none;
217224
}

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ <h3>Settings</h3>
7878
</dialog>
7979

8080

81-
<div id="startMessage"><button id="startAnimation" class="btn">START</button></div>
81+
<button id="start-pause" class="btn action-button"></button>
8282
<div id="timer">00:00</div>
8383
<div id="expandingDiv"></div>
84-
<div id="toggleSettings"><button id="showSettings" class="btn">⚙️</button></div>
84+
<button id="showSettings" class="btn">⚙️</button>
8585
<div id="credits">made with ❤️ by Zenika</div>
8686

8787
<script src="./js/utils.mjs" type="module"></script>

js/app.mjs

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ import { updateTimer } from "./utils.mjs";
33

44
// Lecture du paramètre "duration" dans l'URL (en secondes)
55
const params = new URLSearchParams(globalThis.location.search);
6-
const div = document.getElementById('expandingDiv');
6+
const background = document.getElementById('expandingDiv');
77
const timer = document.getElementById('timer');
8-
const startMessage = document.getElementById('startMessage');
8+
const startBtn = document.getElementById('start-pause');
99
const settingsForm = document.getElementById("settingsForm");
1010
const settingsModal = document.getElementById("settingsModal");
11-
const closeSettings = document.getElementById("closeSettings");
11+
const closeSettingsBtn = document.getElementById("closeSettings");
1212
const resetBtn = document.getElementById("resetBtn");
1313
const submitBtn = document.getElementById("submitBtn");
1414
const showSettingsBtn = document.getElementById("showSettings");
15-
const startAnimationBtn = document.getElementById("startAnimation");
1615
const totalHeight = window.innerHeight;
16+
let elapsed = 0;
17+
let pause = true;
18+
const pauseChar = '⏸️';
19+
const playChar = '▶️';
1720

1821
const defaultSettings = {
1922
"durationInSeconds": 600,
@@ -136,25 +139,53 @@ function getClassByProgress(p) {
136139
return 'ending';
137140
}
138141

139-
function startAnimation() {
140-
requestWakeLock();
141-
startMessage.style.opacity = 0;
142-
setTimeout(() => startMessage.style.display = 'none', 600);
142+
function switchPause() {
143+
pause = !pause;
144+
if (pause) {
145+
displayStart();
146+
pause = true
147+
timer.classList.add("blinking");
148+
timer.classList.add("pause");
149+
timer.setAttribute('title', "Click to continue");
150+
} else {
151+
startAnimation(performance.now() - elapsed)
152+
timer.classList.remove("blinking");
153+
timer.classList.remove("pause");
154+
timer.title = "Click to pause";
155+
}
156+
}
143157

144-
const startTime = performance.now();
158+
function displayPause() {
159+
startBtn.innerHTML = pauseChar;
160+
startBtn.title = 'pause';
161+
}
162+
163+
function displayStart() {
164+
startBtn.innerHTML = playChar;
165+
startBtn.title = 'start';
166+
}
145167

146-
function animate(time) {
147-
const elapsed = time - startTime;
148-
const progress = Math.min(elapsed / (settings.durationInSeconds * 1000), 1); // 0 → 1
149-
const currentHeight = Math.floor(totalHeight * progress);
168+
function updateBackground(progress) {
169+
const currentHeight = Math.floor(totalHeight * progress);
170+
background.style.height = `${currentHeight}px`;
171+
background.classList = getClassByProgress(progress);
172+
}
150173

151-
const remaining = settings.durationInSeconds - (elapsed / 1000);
174+
function startAnimation(startTime = performance.now()) {
175+
requestWakeLock();
176+
displayPause();
177+
178+
function animate(time, durationInSeconds = settings.durationInSeconds) {
179+
elapsed = time - startTime;
180+
const progress = Math.min(elapsed / (durationInSeconds * 1000), 1); // 0 → 1
181+
const remaining = durationInSeconds - (elapsed / 1000);
152182
timer.textContent = updateTimer(remaining);
153183

154-
div.style.height = `${currentHeight}px`;
155-
div.classList = getClassByProgress(progress);
184+
updateBackground(progress)
156185

157-
if (progress < 1) {
186+
if (pause) {
187+
return;
188+
} else if (progress < 1) {
158189
requestAnimationFrame(animate);
159190
} else {
160191
timer.textContent = "00:00";
@@ -214,14 +245,13 @@ export function init() {
214245
}
215246
});
216247

217-
settingsForm.addEventListener('submit', submitSettings);
218-
closeSettings.addEventListener('click', closeSettings);
248+
249+
showSettingsBtn.addEventListener('click', showSettings);
250+
closeSettingsBtn.addEventListener('click', hideSettings);
219251
resetBtn.addEventListener('click', resetDefaultSettings);
220252
submitBtn.addEventListener('click', submitSettings);
221-
startAnimationBtn.addEventListener('click', startAnimation);
222-
showSettingsBtn.addEventListener('click', showSettings);
223-
224-
253+
startBtn.addEventListener('click', switchPause);
254+
displayStart();
225255

226256
applySettings();
227257
}

0 commit comments

Comments
 (0)