-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurya-namaskar-app.js
More file actions
769 lines (653 loc) · 27.5 KB
/
surya-namaskar-app.js
File metadata and controls
769 lines (653 loc) · 27.5 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
// Surya Namaskar App - Main JavaScript
class SuryaNamaskarApp {
constructor() {
this.currentPose = 0;
this.currentSet = 1; // Current set number (1-indexed)
this.totalSets = 1; // Total number of sets to complete
this.poseDuration = 15; // Default 15 seconds per pose
this.timeRemaining = 15;
this.timerInterval = null;
this.isPaused = false;
this.audioContext = null;
this.imageCache = new Map(); // Cache for preloaded images
this.wakeLock = null; // Wake Lock to prevent screen from sleeping
this.breathingSoundNodes = []; // Track active breathing sound oscillators
this.initElements();
this.loadPreferences(); // Load saved preferences from localStorage
this.initAudio();
this.bindEvents();
this.createProgressDots();
this.preloadImages(); // Preload all pose images for smooth transitions
}
/**
* Preload all pose images into memory for instant display during practice.
* Images are stored in a Map cache for quick retrieval.
*/
preloadImages() {
POSES.forEach(pose => {
const img = new Image();
img.src = pose.image;
img.onload = () => {
console.log(`✓ Cached: ${pose.name}`);
};
img.onerror = () => {
console.warn(`✗ Failed to cache: ${pose.name}`);
};
this.imageCache.set(pose.image, img);
});
}
/**
* Get a cached image element or create a new one if not cached.
* @param {string} imagePath - Path to the image
* @returns {HTMLImageElement} - The cached or new image element
*/
getCachedImage(imagePath) {
if (this.imageCache.has(imagePath)) {
// Return a clone of the cached image to avoid DOM conflicts
return this.imageCache.get(imagePath).cloneNode(true);
}
// Fallback: create new image if not in cache
const img = new Image();
img.src = imagePath;
return img;
}
initElements() {
// Screens
this.startScreen = document.getElementById('start-screen');
this.practiceScreen = document.getElementById('practice-screen');
this.completeScreen = document.getElementById('complete-screen');
// Buttons
this.startBtn = document.getElementById('start-btn');
this.pauseBtn = document.getElementById('pause-btn');
this.skipBtn = document.getElementById('skip-btn');
this.restartBtn = document.getElementById('restart-btn');
this.restartPracticeBtn = document.getElementById('restart-practice-btn');
// Icons
this.pauseIcon = document.getElementById('pause-icon');
this.playIcon = document.getElementById('play-icon');
// Display elements
this.poseNumber = document.getElementById('pose-number');
this.poseName = document.getElementById('pose-name');
this.poseSubtitle = document.getElementById('pose-subtitle');
this.poseDescription = document.getElementById('pose-description');
this.poseSvg = document.getElementById('pose-svg');
this.breathingIndicator = document.getElementById('breathing-indicator');
this.breathVisual = document.getElementById('breath-visual');
this.setIndicator = document.getElementById('set-indicator');
this.currentSetDisplay = document.getElementById('current-set');
this.totalSetsDisplay = document.getElementById('total-sets');
// Timer elements
this.timerSeconds = document.getElementById('timer-seconds');
this.timerProgress = document.getElementById('timer-progress');
this.progressFill = document.getElementById('progress-fill');
this.progressDots = document.getElementById('progress-dots');
// Time configuration elements
this.timeValue = document.getElementById('time-value');
this.timeDecrease = document.getElementById('time-decrease');
this.timeIncrease = document.getElementById('time-increase');
this.presetBtns = document.querySelectorAll('.preset-btn');
// Sets configuration elements
this.setsValue = document.getElementById('sets-value');
this.setsDecrease = document.getElementById('sets-decrease');
this.setsIncrease = document.getElementById('sets-increase');
this.setsPresetBtns = document.querySelectorAll('.sets-preset-btn');
// Settings modal elements
this.settingsBtn = document.getElementById('settings-btn');
this.settingsModal = document.getElementById('settings-modal');
this.closeSettings = document.getElementById('close-settings');
this.saveSettings = document.getElementById('save-settings');
}
initAudio() {
// Create AudioContext for beep sounds
try {
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
} catch (e) {
console.log('Web Audio API not supported');
}
}
playBeep(frequency = 800, duration = 150, type = 'sine') {
if (!this.audioContext) return;
try {
// Resume audio context if suspended (browser requirement)
if (this.audioContext.state === 'suspended') {
this.audioContext.resume();
}
const oscillator = this.audioContext.createOscillator();
const gainNode = this.audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(this.audioContext.destination);
oscillator.frequency.value = frequency;
oscillator.type = type;
gainNode.gain.setValueAtTime(0.3, this.audioContext.currentTime);
gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + duration / 1000);
oscillator.start(this.audioContext.currentTime);
oscillator.stop(this.audioContext.currentTime + duration / 1000);
} catch (e) {
console.log('Audio playback error:', e);
}
}
playCountdownBeep() {
// Higher pitched short beep for countdown
this.playBeep(1000, 100, 'sine');
}
playFinalBeep() {
// Lower pitched longer beep for pose change
this.playBeep(600, 300, 'square');
}
/**
* Stop any currently playing breathing sounds gracefully.
*/
stopBreathingSound() {
const now = this.audioContext ? this.audioContext.currentTime : 0;
this.breathingSoundNodes.forEach(({ gain, oscillator }) => {
try {
gain.gain.cancelScheduledValues(now);
gain.gain.setValueAtTime(gain.gain.value, now);
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.3);
oscillator.stop(now + 0.35);
} catch (e) { /* already stopped */ }
});
this.breathingSoundNodes = [];
}
/**
* Play a singing bowl style breathing cue.
* @param {'INHALE'|'EXHALE'|'HOLD'|'START'} type - Breathing type
*/
playBreathingSound(type) {
if (!this.audioContext) return;
try {
if (this.audioContext.state === 'suspended') {
this.audioContext.resume();
}
// Stop previous breathing sound
this.stopBreathingSound();
const ctx = this.audioContext;
const now = ctx.currentTime;
const duration = 3; // 3 seconds
if (type === 'INHALE') {
this._playSingingBowlInhale(ctx, now, duration);
} else if (type === 'EXHALE') {
this._playSingingBowlExhale(ctx, now, duration);
} else if (type === 'HOLD') {
this._playSingingBowlHold(ctx, now, duration);
} else {
// START - gentle singing bowl strike
this._playSingingBowlStart(ctx, now, duration);
}
} catch (e) {
console.log('Breathing sound error:', e);
}
}
/**
* Inhale: Warm rising harmonics - like a singing bowl being circled.
* Two layered tones rising in pitch with gentle volume swell.
*/
_playSingingBowlInhale(ctx, now, duration) {
const layers = [
{ startFreq: 220, endFreq: 330, type: 'sine', vol: 0.12 },
{ startFreq: 440, endFreq: 660, type: 'sine', vol: 0.06 },
{ startFreq: 330, endFreq: 495, type: 'triangle', vol: 0.04 },
];
layers.forEach(layer => {
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.type = layer.type;
osc.frequency.setValueAtTime(layer.startFreq, now);
osc.frequency.exponentialRampToValueAtTime(layer.endFreq, now + duration);
// Volume swells gently upward
gain.gain.setValueAtTime(0.001, now);
gain.gain.exponentialRampToValueAtTime(layer.vol, now + duration * 0.6);
gain.gain.exponentialRampToValueAtTime(layer.vol * 0.5, now + duration);
osc.connect(gain);
gain.connect(ctx.destination);
osc.start(now);
osc.stop(now + duration + 0.1);
this.breathingSoundNodes.push({ oscillator: osc, gain });
});
}
/**
* Exhale: Warm descending harmonics - like a singing bowl fading.
* Tones descend in pitch with natural volume decay.
*/
_playSingingBowlExhale(ctx, now, duration) {
const layers = [
{ startFreq: 330, endFreq: 220, type: 'sine', vol: 0.12 },
{ startFreq: 660, endFreq: 440, type: 'sine', vol: 0.06 },
{ startFreq: 495, endFreq: 330, type: 'triangle', vol: 0.04 },
];
layers.forEach(layer => {
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.type = layer.type;
osc.frequency.setValueAtTime(layer.startFreq, now);
osc.frequency.exponentialRampToValueAtTime(layer.endFreq, now + duration);
// Volume starts present and fades out
gain.gain.setValueAtTime(layer.vol, now);
gain.gain.exponentialRampToValueAtTime(layer.vol * 0.7, now + duration * 0.4);
gain.gain.exponentialRampToValueAtTime(0.001, now + duration);
osc.connect(gain);
gain.connect(ctx.destination);
osc.start(now);
osc.stop(now + duration + 0.1);
this.breathingSoundNodes.push({ oscillator: osc, gain });
});
}
/**
* Hold: Sustained resonant hum - like a singing bowl ringing steady.
* Soft, steady tone with gentle pulsing.
*/
_playSingingBowlHold(ctx, now, duration) {
const layers = [
{ freq: 264, type: 'sine', vol: 0.08 },
{ freq: 528, type: 'sine', vol: 0.03 },
];
layers.forEach(layer => {
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.type = layer.type;
osc.frequency.setValueAtTime(layer.freq, now);
// Gentle fade in, sustain, gentle fade out
gain.gain.setValueAtTime(0.001, now);
gain.gain.exponentialRampToValueAtTime(layer.vol, now + 0.5);
gain.gain.setValueAtTime(layer.vol, now + duration - 0.8);
gain.gain.exponentialRampToValueAtTime(0.001, now + duration);
osc.connect(gain);
gain.connect(ctx.destination);
osc.start(now);
osc.stop(now + duration + 0.1);
this.breathingSoundNodes.push({ oscillator: osc, gain });
});
// Add subtle LFO tremolo for "alive" feel
const lfo = ctx.createOscillator();
const lfoGain = ctx.createGain();
lfo.type = 'sine';
lfo.frequency.setValueAtTime(3, now); // 3 Hz pulse
lfoGain.gain.setValueAtTime(0.02, now);
lfo.connect(lfoGain);
lfoGain.connect(ctx.destination);
lfo.start(now);
lfo.stop(now + duration + 0.1);
this.breathingSoundNodes.push({ oscillator: lfo, gain: lfoGain });
}
/**
* Start: Single gentle singing bowl strike.
*/
_playSingingBowlStart(ctx, now, duration) {
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(396, now); // Solfeggio frequency - liberation
// Sharp attack, long warm decay like a bowl being struck
gain.gain.setValueAtTime(0.001, now);
gain.gain.exponentialRampToValueAtTime(0.15, now + 0.05);
gain.gain.exponentialRampToValueAtTime(0.08, now + 0.3);
gain.gain.exponentialRampToValueAtTime(0.001, now + duration);
osc.connect(gain);
gain.connect(ctx.destination);
osc.start(now);
osc.stop(now + duration + 0.1);
this.breathingSoundNodes.push({ oscillator: osc, gain });
// Add harmonic overtone
const osc2 = ctx.createOscillator();
const gain2 = ctx.createGain();
osc2.type = 'sine';
osc2.frequency.setValueAtTime(792, now); // Octave above
gain2.gain.setValueAtTime(0.001, now);
gain2.gain.exponentialRampToValueAtTime(0.06, now + 0.05);
gain2.gain.exponentialRampToValueAtTime(0.001, now + duration * 0.7);
osc2.connect(gain2);
gain2.connect(ctx.destination);
osc2.start(now);
osc2.stop(now + duration + 0.1);
this.breathingSoundNodes.push({ oscillator: osc2, gain: gain2 });
}
bindEvents() {
this.startBtn.addEventListener('click', () => this.startPractice());
this.pauseBtn.addEventListener('click', () => this.togglePause());
this.skipBtn.addEventListener('click', () => this.skipPose());
this.restartBtn.addEventListener('click', () => this.restartPractice());
this.restartPracticeBtn.addEventListener('click', () => this.restartPractice());
// Time configuration events
this.timeDecrease.addEventListener('click', () => this.adjustTime(-5));
this.timeIncrease.addEventListener('click', () => this.adjustTime(5));
this.presetBtns.forEach(btn => {
btn.addEventListener('click', () => {
const time = parseInt(btn.dataset.time);
this.setDuration(time);
});
});
// Sets configuration events
this.setsDecrease.addEventListener('click', () => this.adjustSets(-1));
this.setsIncrease.addEventListener('click', () => this.adjustSets(1));
this.setsPresetBtns.forEach(btn => {
btn.addEventListener('click', () => {
const sets = parseInt(btn.dataset.sets);
this.setTotalSets(sets);
});
});
// Settings modal events
this.settingsBtn.addEventListener('click', () => this.openSettings());
this.closeSettings.addEventListener('click', () => this.closeSettingsModal());
this.saveSettings.addEventListener('click', () => this.saveAndCloseSettings());
// Close modal on overlay click
this.settingsModal.addEventListener('click', (e) => {
if (e.target === this.settingsModal) {
this.closeSettingsModal();
}
});
// Resume audio context on any user interaction
document.addEventListener('click', () => {
if (this.audioContext && this.audioContext.state === 'suspended') {
this.audioContext.resume();
}
}, { once: true });
// Handle visibility change - re-acquire wake lock if page becomes visible again
document.addEventListener('visibilitychange', async () => {
if (this.wakeLock !== null && document.visibilityState === 'visible') {
await this.requestWakeLock();
}
});
}
adjustTime(delta) {
const newTime = Math.max(5, Math.min(120, this.poseDuration + delta));
this.setDuration(newTime);
}
setDuration(seconds) {
this.poseDuration = seconds;
this.timeValue.textContent = seconds;
// Update preset buttons active state
this.presetBtns.forEach(btn => {
btn.classList.toggle('active', parseInt(btn.dataset.time) === seconds);
});
}
adjustSets(delta) {
const newSets = Math.max(1, Math.min(10, this.totalSets + delta));
this.setTotalSets(newSets);
}
setTotalSets(sets) {
this.totalSets = sets;
this.setsValue.textContent = sets;
// Update preset buttons active state
this.setsPresetBtns.forEach(btn => {
btn.classList.toggle('active', parseInt(btn.dataset.sets) === sets);
});
}
// Wake Lock API to prevent screen from sleeping
async requestWakeLock() {
try {
if ('wakeLock' in navigator) {
this.wakeLock = await navigator.wakeLock.request('screen');
console.log('✓ Wake Lock activated - screen will stay on');
this.wakeLock.addEventListener('release', () => {
console.log('Wake Lock released');
});
} else {
console.log('Wake Lock API not supported on this device');
}
} catch (err) {
console.log(`Wake Lock error: ${err.name}, ${err.message}`);
}
}
async releaseWakeLock() {
if (this.wakeLock !== null) {
await this.wakeLock.release();
this.wakeLock = null;
console.log('Wake Lock manually released');
}
}
// LocalStorage methods
loadPreferences() {
const savedDuration = localStorage.getItem('suryaNamaskar_poseDuration');
if (savedDuration) {
const duration = parseInt(savedDuration);
this.poseDuration = duration;
this.timeRemaining = duration;
this.timeValue.textContent = duration;
// Update preset buttons
this.presetBtns.forEach(btn => {
btn.classList.toggle('active', parseInt(btn.dataset.time) === duration);
});
}
const savedSets = localStorage.getItem('suryaNamaskar_totalSets');
if (savedSets) {
const sets = parseInt(savedSets);
this.totalSets = sets;
this.setsValue.textContent = sets;
// Update preset buttons
this.setsPresetBtns.forEach(btn => {
btn.classList.toggle('active', parseInt(btn.dataset.sets) === sets);
});
}
}
savePreferences() {
localStorage.setItem('suryaNamaskar_poseDuration', this.poseDuration.toString());
localStorage.setItem('suryaNamaskar_totalSets', this.totalSets.toString());
}
// Settings modal methods
openSettings() {
this.settingsModal.classList.add('active');
}
closeSettingsModal() {
this.settingsModal.classList.remove('active');
}
saveAndCloseSettings() {
this.savePreferences();
this.closeSettingsModal();
}
createProgressDots() {
this.progressDots.innerHTML = '';
for (let i = 0; i < POSES.length; i++) {
const dot = document.createElement('span');
dot.className = 'progress-dot';
dot.dataset.index = i;
this.progressDots.appendChild(dot);
}
}
updateProgressDots() {
const dots = this.progressDots.querySelectorAll('.progress-dot');
dots.forEach((dot, index) => {
dot.classList.remove('active', 'completed');
if (index < this.currentPose) {
dot.classList.add('completed');
} else if (index === this.currentPose) {
dot.classList.add('active');
}
});
}
showScreen(screen) {
this.startScreen.classList.remove('active');
this.practiceScreen.classList.remove('active');
this.completeScreen.classList.remove('active');
screen.classList.add('active');
}
async startPractice() {
this.currentPose = 0;
this.currentSet = 1;
await this.requestWakeLock(); // Prevent screen from sleeping
this.showScreen(this.practiceScreen);
this.updateSetIndicator();
this.loadPose();
this.startTimer();
}
updateSetIndicator() {
if (this.totalSets > 1) {
this.setIndicator.style.display = 'flex';
this.currentSetDisplay.textContent = this.currentSet;
this.totalSetsDisplay.textContent = this.totalSets;
} else {
this.setIndicator.style.display = 'none';
}
}
loadPose() {
const pose = POSES[this.currentPose];
// Update display
this.poseNumber.textContent = pose.id;
this.poseName.textContent = pose.name;
this.poseSubtitle.textContent = pose.subtitle;
this.poseDescription.textContent = pose.description;
// Use cached image for instant display
const cachedImg = this.getCachedImage(pose.image);
cachedImg.alt = pose.name;
cachedImg.className = 'pose-image';
this.poseSvg.innerHTML = '';
this.poseSvg.appendChild(cachedImg);
// Update breathing indicator
this.breathingIndicator.className = 'breathing-indicator';
if (pose.breathing === 'INHALE') {
this.breathingIndicator.classList.add('inhale');
} else if (pose.breathing === 'EXHALE') {
this.breathingIndicator.classList.add('exhale');
} else {
this.breathingIndicator.classList.add('hold');
}
// Update breath visual (large indicator)
this.breathVisual.className = 'breath-visual';
if (pose.breathing === 'INHALE') {
this.breathVisual.classList.add('inhale');
} else if (pose.breathing === 'EXHALE') {
this.breathVisual.classList.add('exhale');
} else {
this.breathVisual.classList.add('hold');
}
this.breathingIndicator.querySelector('.breath-text').textContent = pose.breathing;
// Play breathing sound cue
this.playBreathingSound(pose.breathing);
// Update progress
this.updateProgressDots();
const progressPercent = ((this.currentPose) / POSES.length) * 100;
this.progressFill.style.width = `${progressPercent}%`;
// Reset timer display
this.timeRemaining = this.poseDuration;
this.updateTimerDisplay();
}
startTimer() {
this.isPaused = false;
this.updatePauseButton();
this.timerInterval = setInterval(() => {
if (!this.isPaused) {
this.timeRemaining--;
this.updateTimerDisplay();
// Play beep for last 5 seconds
if (this.timeRemaining <= 5 && this.timeRemaining > 0) {
this.playCountdownBeep();
this.timerSeconds.classList.add('warning');
setTimeout(() => this.timerSeconds.classList.remove('warning'), 300);
}
// Time's up
if (this.timeRemaining <= 0) {
this.playFinalBeep();
this.nextPose();
}
}
}, 1000);
}
updateTimerDisplay() {
this.timerSeconds.textContent = this.timeRemaining;
// Update circular progress
const circumference = 2 * Math.PI * 54; // r = 54
const offset = circumference - (this.timeRemaining / this.poseDuration) * circumference;
this.timerProgress.style.strokeDashoffset = offset;
// Change color for last 5 seconds
if (this.timeRemaining <= 5) {
this.timerProgress.style.stroke = '#FF6B6B';
} else {
this.timerProgress.style.stroke = '#FF8C00';
}
}
nextPose() {
clearInterval(this.timerInterval);
if (this.currentPose < POSES.length - 1) {
this.currentPose++;
this.loadPose();
this.startTimer();
} else {
// Check if there are more sets to complete
if (this.currentSet < this.totalSets) {
this.currentSet++;
// Skip pose 0 (step 1 - Pranamasana) since it's the same as
// step 12 (Pranamasana). Sequence: ...12 → 2 → 3...
this.currentPose = 1;
this.updateSetIndicator();
this.loadPose();
this.startTimer();
// Play a special sound for new set
this.playSetCompleteSound();
} else {
this.completePractice();
}
}
}
playSetCompleteSound() {
// Play ascending tones to indicate new set
setTimeout(() => this.playBeep(440, 150), 0);
setTimeout(() => this.playBeep(550, 150), 150);
setTimeout(() => this.playBeep(660, 200), 300);
}
togglePause() {
this.isPaused = !this.isPaused;
this.updatePauseButton();
}
updatePauseButton() {
if (this.isPaused) {
this.pauseIcon.style.display = 'none';
this.playIcon.style.display = 'block';
} else {
this.pauseIcon.style.display = 'block';
this.playIcon.style.display = 'none';
}
}
skipPose() {
this.playFinalBeep();
this.nextPose();
}
async restartPractice() {
clearInterval(this.timerInterval);
this.currentPose = 0;
this.currentSet = 1;
this.timeRemaining = this.poseDuration;
this.isPaused = false;
await this.requestWakeLock(); // Ensure wake lock is active
this.showScreen(this.practiceScreen);
this.updateSetIndicator();
this.loadPose();
this.startTimer();
}
async completePractice() {
clearInterval(this.timerInterval);
await this.releaseWakeLock(); // Release wake lock when practice is complete
this.progressFill.style.width = '100%';
// Calculate and display actual practice time (including all sets)
// First set has all 12 poses, subsequent sets have 11 (step 1 is skipped)
const totalPoses = POSES.length + (this.totalSets - 1) * (POSES.length - 1);
const totalSeconds = this.poseDuration * totalPoses;
const statTime = document.getElementById('stat-time');
const statTimeLabel = document.getElementById('stat-time-label');
const statPoses = document.getElementById('stat-poses');
const statSets = document.getElementById('stat-sets');
const statSetsLabel = document.getElementById('stat-sets-label');
// Update poses count
statPoses.textContent = totalPoses;
// Update sets count
if (statSets && statSetsLabel) {
statSets.textContent = this.totalSets;
statSetsLabel.textContent = this.totalSets === 1 ? 'Set' : 'Sets';
}
if (totalSeconds >= 60) {
const minutes = Math.round(totalSeconds / 60 * 10) / 10; // Round to 1 decimal
statTime.textContent = minutes % 1 === 0 ? minutes.toFixed(0) : minutes.toFixed(1);
statTimeLabel.textContent = minutes === 1 ? 'Minute' : 'Minutes';
} else {
statTime.textContent = totalSeconds;
statTimeLabel.textContent = totalSeconds === 1 ? 'Second' : 'Seconds';
}
// Play completion sound
setTimeout(() => this.playBeep(523, 200), 0);
setTimeout(() => this.playBeep(659, 200), 200);
setTimeout(() => this.playBeep(784, 400), 400);
this.showScreen(this.completeScreen);
}
}
// Initialize app when DOM is ready
document.addEventListener('DOMContentLoaded', () => {
new SuryaNamaskarApp();
});