@@ -3,17 +3,20 @@ import { updateTimer } from "./utils.mjs";
33
44// Lecture du paramètre "duration" dans l'URL (en secondes)
55const params = new URLSearchParams ( globalThis . location . search ) ;
6- const div = document . getElementById ( 'expandingDiv' ) ;
6+ const background = document . getElementById ( 'expandingDiv' ) ;
77const timer = document . getElementById ( 'timer' ) ;
8- const startMessage = document . getElementById ( 'startMessage ' ) ;
8+ const startBtn = document . getElementById ( 'start-pause ' ) ;
99const settingsForm = document . getElementById ( "settingsForm" ) ;
1010const settingsModal = document . getElementById ( "settingsModal" ) ;
11- const closeSettings = document . getElementById ( "closeSettings" ) ;
11+ const closeSettingsBtn = document . getElementById ( "closeSettings" ) ;
1212const resetBtn = document . getElementById ( "resetBtn" ) ;
1313const submitBtn = document . getElementById ( "submitBtn" ) ;
1414const showSettingsBtn = document . getElementById ( "showSettings" ) ;
15- const startAnimationBtn = document . getElementById ( "startAnimation" ) ;
1615const totalHeight = window . innerHeight ;
16+ let elapsed = 0 ;
17+ let pause = true ;
18+ const pauseChar = '⏸️' ;
19+ const playChar = '▶️' ;
1720
1821const 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