Skip to content

Commit f7c040e

Browse files
committed
- make transition smoother
1 parent dd986f6 commit f7c040e

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

playground/pushup_timer.html

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
.progress-fill {
9494
height: 100%;
9595
background: white;
96-
transition: width 0.1s linear;
96+
transition: none;
9797
}
9898

9999
.controls {
@@ -154,8 +154,8 @@
154154
<script>
155155
let currentPushup = 0;
156156
let currentPhase = 'ready'; // ready, countdown, down, hold, up
157-
let timeLeft = 0;
158-
let totalTime = 0;
157+
let phaseStartTime = 0;
158+
let phaseDuration = 0;
159159
let intervalId = null;
160160
let isRunning = false;
161161

@@ -167,8 +167,13 @@
167167
};
168168

169169
function updateDisplay() {
170+
const now = Date.now();
171+
const elapsed = (now - phaseStartTime) / 1000;
172+
const timeLeft = Math.max(0, phaseDuration - elapsed);
173+
const displayTime = Math.ceil(timeLeft);
174+
170175
document.getElementById('currentPushup').textContent = currentPushup;
171-
document.getElementById('timer').textContent = timeLeft;
176+
document.getElementById('timer').textContent = displayTime;
172177

173178
const phaseElement = document.getElementById('phase');
174179
const progressFill = document.getElementById('progressFill');
@@ -183,18 +188,23 @@
183188
progressFill.style.width = '100%';
184189
document.getElementById('completed').style.display = 'block';
185190
} else if (currentPhase === 'countdown') {
186-
phaseElement.textContent = `GET READY - ${timeLeft}`;
191+
phaseElement.textContent = `GET READY - ${displayTime}`;
187192
phaseElement.className = 'phase countdown';
188-
const progress = ((totalTime - timeLeft) / totalTime) * 100;
193+
const progress = Math.min(100, (elapsed / phaseDuration) * 100);
189194
progressFill.style.width = `${progress}%`;
190195
} else {
191196
const phase = phases[currentPhase];
192197
phaseElement.textContent = phase.text;
193198
phaseElement.className = `phase ${phase.class}`;
194199

195-
const progress = ((totalTime - timeLeft) / totalTime) * 100;
200+
const progress = Math.min(100, (elapsed / phaseDuration) * 100);
196201
progressFill.style.width = `${progress}%`;
197202
}
203+
204+
// Check if phase should end
205+
if (elapsed >= phaseDuration && currentPhase !== 'ready' && currentPhase !== 'completed') {
206+
nextPhase();
207+
}
198208
}
199209

200210
function nextPhase() {
@@ -220,26 +230,17 @@
220230
}
221231
}
222232

223-
timeLeft = phases[currentPhase].duration;
224-
totalTime = timeLeft;
225-
updateDisplay();
226-
}
227-
228-
function tick() {
229-
timeLeft--;
233+
phaseDuration = phases[currentPhase].duration;
234+
phaseStartTime = Date.now();
230235
updateDisplay();
231-
232-
if (timeLeft <= 0) {
233-
nextPhase();
234-
}
235236
}
236237

237238
function startWorkout() {
238239
if (!isRunning) {
239240
if (currentPhase === 'ready') {
240241
nextPhase();
241242
}
242-
intervalId = setInterval(tick, 1000);
243+
intervalId = setInterval(updateDisplay, 50); // Update every 50ms for smooth animation
243244
isRunning = true;
244245
}
245246
}
@@ -256,8 +257,8 @@
256257
isRunning = false;
257258
currentPushup = 0;
258259
currentPhase = 'ready';
259-
timeLeft = 0;
260-
totalTime = 0;
260+
phaseStartTime = 0;
261+
phaseDuration = 0;
261262
document.getElementById('completed').style.display = 'none';
262263
updateDisplay();
263264
}

0 commit comments

Comments
 (0)