'use strict';
// ═══════════════════════════════════════════════════════════════
// YOGAMATE - Premium AI Yoga Assistant
// ═══════════════════════════════════════════════════════════════
// ─── Configuration ───
const CONFIG = {
API_BASE: '',
DEFAULT_INTERVAL: 150,
CAMERA_WIDTH: 480,
CAMERA_HEIGHT: 360,
SNAPSHOT_QUALITY: 0.5,
REQUIRED_IMAGES: 5,
HISTORY_LIMIT: 50,
SCORE_HISTORY_LIMIT: 5,
};
// ─── State ───
let STATE = {
currentUser: null,
poses: [],
selectedPose: null,
currentView: 'home',
cameraStream: null,
isAnalysing: false,
liveInterval: CONFIG.DEFAULT_INTERVAL,
mirrorEnabled: true,
showFps: false,
isFullscreen: false,
isDark: localStorage.getItem('ym_dark') === 'true',
historyData: [],
scoreHistory: [],
currentLandmarks: null,
currentScore: 0,
currentStatus: '',
currentSuggestions: [],
lastFrameTime: 0,
animationFrameId: null,
liveLoopTimer: null,
sessionStartTime: null,
sessionScores: [],
favorites: [],
practiceCount: 0,
streak: 0,
lastPracticeDate: null,
uploadedImages: [],
generatedPose: null,
isGenerating: false,
searchQuery: '',
floatingSaveBtn: null,
achievements: [],
};
// ─── User‑prefixed localStorage helpers ───
function getUserKey(key) {
if (!STATE.currentUser) return `ym_${key}`;
return `ym_${STATE.currentUser.userId}_${key}`;
}
function getUserLocalStorage(key) {
return localStorage.getItem(getUserKey(key));
}
function setUserLocalStorage(key, value) {
localStorage.setItem(getUserKey(key), value);
}
function removeUserLocalStorage(key) {
localStorage.removeItem(getUserKey(key));
}
// ─── Load user data ───
function loadUserData() {
if (!STATE.currentUser) return;
STATE.historyData = JSON.parse(getUserLocalStorage('history') || '[]');
STATE.favorites = JSON.parse(getUserLocalStorage('favorites') || '[]');
STATE.practiceCount = parseInt(getUserLocalStorage('practice_count') || '0');
STATE.streak = parseInt(getUserLocalStorage('streak') || '0');
STATE.lastPracticeDate = getUserLocalStorage('last_practice_date') || null;
STATE.achievements = JSON.parse(getUserLocalStorage('achievements') || '[]');
STATE.isDark = getUserLocalStorage('dark') === 'true';
applyTheme(STATE.isDark);
}
function saveUserData() {
if (!STATE.currentUser) return;
setUserLocalStorage('history', JSON.stringify(STATE.historyData));
setUserLocalStorage('favorites', JSON.stringify(STATE.favorites));
setUserLocalStorage('practice_count', String(STATE.practiceCount));
setUserLocalStorage('streak', String(STATE.streak));
setUserLocalStorage('last_practice_date', STATE.lastPracticeDate || '');
setUserLocalStorage('achievements', JSON.stringify(STATE.achievements));
setUserLocalStorage('dark', String(STATE.isDark));
}
// ─── DOM Helpers ───
function el(id) {
const element = document.getElementById(id);
if (!element) {
return {
classList: { add: () => {}, remove: () => {}, toggle: () => {} },
style: {},
textContent: '',
innerHTML: '',
value: '',
addEventListener: () => {},
removeEventListener: () => {},
};
}
return element;
}
function $$(sel, ctx = document) {
return [...ctx.querySelectorAll(sel)];
}
// ─── Toast System ───
let toastTimeout;
function showToast(message, type = 'info', duration = 3000) {
const toast = el('toast');
const icons = { success: '✅', error: '❌', warning: '⚠️', info: '💡' };
toast.innerHTML = `${icons[type] || '💡'} ${message}`;
toast.className = `toast toast-${type}`;
toast.classList.remove('hidden');
clearTimeout(toastTimeout);
toastTimeout = setTimeout(() => toast.classList.add('hidden'), duration);
}
// ─── Theme ───
function applyTheme(dark) {
STATE.isDark = dark;
document.documentElement.setAttribute('data-theme', dark ? 'dark' : '');
setUserLocalStorage('dark', dark);
const btn = el('theme-toggle');
if (btn) btn.classList.toggle('on', dark);
}
function toggleTheme() {
applyTheme(!STATE.isDark);
showToast(STATE.isDark ? 'Dark mode enabled 🌙' : 'Light mode enabled ☀️');
}
function toggleMirror() {
STATE.mirrorEnabled = !STATE.mirrorEnabled;
const btn = el('mirror-toggle');
if (btn) btn.classList.toggle('on', STATE.mirrorEnabled);
showToast(STATE.mirrorEnabled ? 'Mirror mode ON' : 'Mirror mode OFF');
}
function toggleFps() {
STATE.showFps = !STATE.showFps;
const btn = el('fps-toggle');
if (btn) btn.classList.toggle('on', STATE.showFps);
const fpsBadge = el('fps-badge');
if (fpsBadge) {
fpsBadge.classList.toggle('hidden', !STATE.showFps || !STATE.cameraStream);
}
}
function updateInterval(value) {
STATE.liveInterval = parseInt(value);
const label = el('interval-val');
if (label) label.textContent = `${value} ms`;
if (STATE.liveLoopTimer) {
clearInterval(STATE.liveLoopTimer);
STATE.liveLoopTimer = setInterval(sendFrameToServer, STATE.liveInterval);
}
}
function selectDifficulty(btn) {
$$('.difficulty-pills .pill').forEach(p => p.classList.remove('active'));
btn.classList.add('active');
}
// ─── Debounce ───────────────────────────────────────────────
function debounce(func, wait) {
let timeout;
return function(...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), wait);
};
}
// ─── API ────────────────────────────────────────────────────
async function apiRequest(endpoint, options = {}) {
const headers = options.headers || {};
if (STATE.currentUser) {
headers['X-User-ID'] = STATE.currentUser.userId;
}
options.headers = headers;
options.credentials = 'include';
try {
const response = await fetch(`${CONFIG.API_BASE}${endpoint}`, options);
const data = await response.json();
if (!response.ok) {
throw new Error(data.error || `HTTP ${response.status}`);
}
return data;
} catch (error) {
console.error(`[API] ${endpoint} error:`, error);
throw error;
}
}
// ─── Authentication ────────────────────────────────────────
async function login(email, password) {
try {
const data = await apiRequest('/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password }),
});
STATE.currentUser = { userId: data.user_id, name: data.name, email };
loadUserData();
showApp();
showToast(`Welcome back, ${data.name}! 🧘`, 'success');
return true;
} catch (error) {
showToast(error.message || 'Login failed', 'error');
return false;
}
}
async function register(name, email, password) {
try {
const data = await apiRequest('/api/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, email, password }),
});
STATE.currentUser = { userId: data.user_id, name: data.name, email };
loadUserData();
showApp();
showToast(`Welcome, ${data.name}! 🎉`, 'success');
return true;
} catch (error) {
showToast(error.message || 'Registration failed', 'error');
return false;
}
}
async function logout() {
try {
await apiRequest('/api/logout', { method: 'POST' });
} catch (e) {}
STATE.currentUser = null;
document.getElementById('app-shell').style.display = 'none';
document.getElementById('login-overlay').style.display = 'flex';
showToast('Logged out', 'info');
}
function showApp() {
document.getElementById('login-overlay').style.display = 'none';
document.getElementById('app-shell').style.display = 'flex';
document.getElementById('user-name').textContent = STATE.currentUser.name;
document.getElementById('user-email').textContent = STATE.currentUser.email;
fetchPoses();
renderHistory();
renderProgress();
renderFavorites();
updateBadge();
}
// ─── Login/Register UI ────────────────────────────────────
function initAuthUI() {
const loginForm = document.getElementById('login-form');
const registerForm = document.getElementById('register-form');
const toggleRegister = document.getElementById('login-toggle-register');
const toggleLogin = document.getElementById('login-toggle-login');
const logoutBtn = document.getElementById('logout-btn');
loginForm.addEventListener('submit', async (e) => {
e.preventDefault();
const email = document.getElementById('login-email').value.trim();
const password = document.getElementById('login-password').value;
const errorEl = document.getElementById('login-error');
errorEl.classList.add('hidden');
const ok = await login(email, password);
if (!ok) {
errorEl.textContent = 'Invalid email or password';
errorEl.classList.remove('hidden');
}
});
registerForm.addEventListener('submit', async (e) => {
e.preventDefault();
const name = document.getElementById('register-name').value.trim();
const email = document.getElementById('register-email').value.trim();
const password = document.getElementById('register-password').value;
const errorEl = document.getElementById('register-error');
errorEl.classList.add('hidden');
if (!name || !email || !password) {
errorEl.textContent = 'All fields are required';
errorEl.classList.remove('hidden');
return;
}
const ok = await register(name, email, password);
if (!ok) {
errorEl.textContent = 'Registration failed. Email may already exist.';
errorEl.classList.remove('hidden');
}
});
toggleRegister.addEventListener('click', (e) => {
e.preventDefault();
loginForm.style.display = 'none';
registerForm.style.display = 'block';
});
toggleLogin.addEventListener('click', (e) => {
e.preventDefault();
registerForm.style.display = 'none';
loginForm.style.display = 'block';
});
logoutBtn.addEventListener('click', logout);
}
// ─── Fetch Poses ────────────────────────────────────
async function fetchPoses() {
const grid = el('pose-grid');
if (grid) {
grid.innerHTML = `
`;
}
try {
const poses = await apiRequest('/api/poses');
STATE.poses = poses;
console.log(`[YogaMate] Loaded ${poses.length} poses`);
renderPoseGrid();
renderFavorites();
updateSearchResults();
return poses;
} catch (error) {
console.error('[YogaMate] Error fetching poses:', error);
showToast('Failed to load poses', 'error');
if (grid) {
grid.innerHTML = `
⚠️
Failed to load poses.
`;
}
return [];
}
}
// ─── Render Pose Grid ─────────────────────────────────────
function renderPoseGrid() {
const grid = el('pose-grid');
if (!grid) return;
const poses = STATE.poses.slice(0, 10);
if (!poses || poses.length === 0) {
grid.innerHTML = `
🧘
No poses found
Click "Add Pose" to create custom poses
`;
return;
}
grid.innerHTML = poses.map(p => {
const isFav = STATE.favorites.includes(p.id);
const diffClass = `pose-diff-${(p.difficulty || 'beginner').toLowerCase()}`;
const isCustom = p.is_custom || false;
const customBadge = isCustom ? 'CUSTOM' : '';
return `
${p.emoji || '🧘'}
${p.name || p.id} ${customBadge}
${p.english || p.id}
${p.difficulty || 'Beginner'}
`;
}).join('');
const countEl = el('pose-count');
if (countEl) {
countEl.textContent = `Showing ${Math.min(STATE.poses.length, 10)} of ${STATE.poses.length} poses`;
}
}
// ─── Favorites ─────────────────────────────────────────────
function toggleFavorite(poseId) {
const index = STATE.favorites.indexOf(poseId);
if (index > -1) {
STATE.favorites.splice(index, 1);
showToast('Removed from favorites');
} else {
STATE.favorites.push(poseId);
showToast('Added to favorites ❤️');
}
saveUserData();
renderPoseGrid();
renderFavorites();
}
function renderFavorites() {
const container = el('favorites-grid');
if (!container) return;
const favs = STATE.poses.filter(p => STATE.favorites.includes(p.id));
if (favs.length === 0) {
container.innerHTML = 'No favorites yet
';
return;
}
container.innerHTML = favs.map(p => `
${p.emoji || '🧘'}
${p.name || p.id}
`).join('');
}
// ─── Search ──────────────────────────────────────────────
const debouncedSearch = debounce((query) => {
performSearch(query);
}, 300);
function setupSearch() {
const input = el('home-search-input');
const clearBtn = el('home-search-clear');
const panel = el('search-results-panel');
const list = el('search-results-list');
const label = el('search-results-label');
if (!input) return;
input.addEventListener('input', () => {
const query = input.value.trim();
STATE.searchQuery = query;
debouncedSearch(query);
});
input.addEventListener('focus', () => {
if (STATE.searchQuery) {
performSearch(STATE.searchQuery);
}
});
if (clearBtn) {
clearBtn.addEventListener('click', () => {
input.value = '';
STATE.searchQuery = '';
clearBtn.classList.add('hidden');
if (panel) {
panel.style.display = 'none';
panel.classList.remove('visible');
}
input.focus();
});
}
document.addEventListener('keydown', (e) => {
if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
e.preventDefault();
input.focus();
input.select();
}
if (e.key === 'Escape') {
input.blur();
if (panel) {
panel.style.display = 'none';
panel.classList.remove('visible');
}
}
});
document.addEventListener('click', (e) => {
if (panel && !panel.contains(e.target) && e.target !== input) {
panel.style.display = 'none';
panel.classList.remove('visible');
}
});
}
function performSearch(query) {
const clearBtn = el('home-search-clear');
const panel = el('search-results-panel');
const list = el('search-results-list');
const label = el('search-results-label');
if (clearBtn) {
if (query) clearBtn.classList.remove('hidden');
else clearBtn.classList.add('hidden');
}
if (!query) {
if (panel) {
panel.style.display = 'none';
panel.classList.remove('visible');
}
return;
}
const results = STATE.poses.filter(p =>
(p.name && p.name.toLowerCase().includes(query.toLowerCase())) ||
(p.english && p.english.toLowerCase().includes(query.toLowerCase())) ||
(p.category && p.category.toLowerCase().includes(query.toLowerCase()))
);
if (panel) {
panel.style.display = 'block';
panel.classList.add('visible');
}
if (label) {
label.textContent = results.length > 0 ? `Results (${results.length})` : 'No Results';
}
if (list) {
if (results.length > 0) {
list.innerHTML = results.map(p => {
const isCustom = p.is_custom || false;
const customBadge = isCustom ? ' CUSTOM' : '';
return `
${p.emoji || '🧘'}
${p.name || p.id}${customBadge}
${p.english || p.id}
`;
}).join('');
} else {
list.innerHTML = `
🔍 No poses found for "${query}"
`;
}
}
}
function updateSearchResults() {
const input = el('home-search-input');
if (input && input.value.trim()) {
performSearch(input.value.trim());
}
}
// ─── Reference Images ────────────────────────────────────
function displayReferenceImages(images) {
const container = el('reference-images-container');
if (!container) return;
container.innerHTML = '';
if (!images || images.length === 0) {
container.innerHTML = `
📷 No reference images found
`;
return;
}
images.forEach((img, index) => {
const card = document.createElement('div');
card.className = 'reference-image-card';
card.style.cursor = 'pointer';
const imgElement = document.createElement('img');
imgElement.src = `data:${img.mime};base64,${img.data}`;
imgElement.alt = `Reference ${index + 1}`;
imgElement.loading = 'lazy';
const label = document.createElement('div');
label.className = 'ref-label';
label.textContent = `#${index + 1}`;
card.appendChild(imgElement);
card.appendChild(label);
// Direct onclick for guaranteed execution
card.onclick = function(e) {
if (e.target.classList.contains('ref-label')) return;
openLightbox(imgElement.src);
};
container.appendChild(card);
});
}
async function loadReferenceImages(poseId) {
const container = el('reference-images-container');
if (!container) return;
container.innerHTML = `
Loading reference images...
`;
try {
const data = await apiRequest(`/api/pose/${poseId}/references`);
if (data.references && data.references.length > 0) {
displayReferenceImages(data.references);
} else {
container.innerHTML = `
📷 No reference images found
`;
}
} catch (error) {
console.error('[YogaMate] Error loading reference images:', error);
container.innerHTML = `
❌ Error loading images
`;
}
}
function refreshReferenceImages() {
if (!STATE.selectedPose) {
showToast('No pose selected');
return;
}
loadReferenceImages(STATE.selectedPose.id);
}
// ─── Open Practice ────────────────────────────────────────
function openPractice(poseId) {
const pose = STATE.poses.find(p => p.id === poseId);
if (!pose) {
showToast('Pose not found');
return;
}
STATE.selectedPose = pose;
const titleEl = el('practice-pose-title');
if (titleEl) titleEl.textContent = `Practice: ${pose.english || pose.name || pose.id}`;
const artEl = el('practice-pose-art');
if (artEl) artEl.textContent = pose.emoji || '🧘';
const nameEl = el('practice-pose-name');
if (nameEl) nameEl.textContent = pose.name || pose.id;
const sanskritEl = el('practice-pose-sanskrit');
if (sanskritEl) sanskritEl.textContent = pose.english || pose.id;
const diffEl = el('practice-pose-difficulty');
if (diffEl) {
diffEl.textContent = pose.difficulty || 'Beginner';
diffEl.className = `practice-pose-difficulty difficulty-${(pose.difficulty || 'beginner').toLowerCase()}`;
}
const benefits = pose.benefits || ['Practice this pose', 'Improve alignment', 'Build strength'];
const steps = pose.steps || ['Position yourself in the pose', 'Hold the posture', 'Breathe deeply'];
const benefitsEl = el('benefits-list');
if (benefitsEl) benefitsEl.innerHTML = benefits.map(b => `${b}`).join('');
const stepsEl = el('steps-list');
if (stepsEl) stepsEl.innerHTML = steps.map(s => `${s}`).join('');
loadReferenceImages(poseId);
resetCameraUI();
navigateTo('practice');
}
// ─── Navigation ────────────────────────────────────────────
function navigateTo(view) {
$$('.view').forEach(v => v.classList.remove('active'));
const target = el(`view-${view}`);
if (target) target.classList.add('active');
$$('.nav-item[data-view]').forEach(el => {
el.classList.toggle('active', el.dataset.view === view);
});
STATE.currentView = view;
if (view === 'history') renderHistory();
if (view === 'progress') renderProgress();
if (view === 'home') {
renderPoseGrid();
renderFavorites();
updateSearchResults();
}
}
function goHome() {
stopCamera();
navigateTo('home');
}
function openAddPose() {
resetAddPoseForm();
navigateTo('add-pose');
$$('.nav-item').forEach(el => el.classList.remove('active'));
}
// ─── Reset Add Pose Form ─────────────────────────────────
function resetAddPoseForm() {
STATE.uploadedImages = [];
STATE.generatedPose = null;
STATE.isGenerating = false;
removeFloatingSaveButton();
const container = el('uploaded-images-container');
if (container) container.innerHTML = '';
const preview = el('generated-pose-preview');
if (preview) preview.classList.add('hidden');
const results = el('generation-results');
if (results) results.classList.add('hidden');
const btn = el('generate-pose-btn');
if (btn) {
btn.textContent = '✨ Generate Pose Reference';
btn.disabled = true;
}
const progress = el('generation-progress');
if (progress) {
progress.classList.add('hidden');
const bar = el('generation-progress-bar');
if (bar) bar.style.width = '0%';
const text = el('generation-progress-text');
if (text) text.textContent = '';
}
const form = el('add-pose-form');
if (form) form.reset();
$$('.difficulty-pills .pill').forEach(p => p.classList.remove('active'));
const beginnerPill = document.querySelector('.difficulty-pills .pill[data-diff="Beginner"]');
if (beginnerPill) beginnerPill.classList.add('active');
const progressEl = el('upload-progress');
if (progressEl) progressEl.textContent = '0/5 uploaded';
const statusEl = el('upload-status');
if (statusEl) statusEl.textContent = 'Select images';
const bar = document.querySelector('.upload-progress-bar');
if (bar) bar.style.width = '0%';
}
// ─── Camera Management ────────────────────────────────────
async function startCamera() {
if (!STATE.selectedPose) {
showToast('Please select a pose first');
return;
}
try {
STATE.cameraStream = await navigator.mediaDevices.getUserMedia({
video: {
width: { ideal: CONFIG.CAMERA_WIDTH },
height: { ideal: CONFIG.CAMERA_HEIGHT },
facingMode: 'user',
},
audio: false,
});
} catch (err) {
showToast('Camera access denied. Please allow camera permission.', 'error');
console.error('[Camera]', err);
return;
}
const video = el('webcam-video');
const canvas = el('webcam-canvas');
if (!video || !canvas) return;
video.srcObject = STATE.cameraStream;
video.onloadedmetadata = () => {
video.play();
canvas.width = CONFIG.CAMERA_WIDTH;
canvas.height = CONFIG.CAMERA_HEIGHT;
el('camera-off-overlay').classList.add('hidden');
el('score-overlay').classList.remove('hidden');
el('live-badge').classList.remove('hidden');
el('start-camera-btn').classList.add('hidden');
el('stop-camera-btn').classList.remove('hidden');
el('snapshot-btn').classList.remove('hidden');
el('result-card').classList.remove('hidden');
el('suggestions-panel').classList.remove('hidden');
if (STATE.showFps) el('fps-badge').classList.remove('hidden');
STATE.sessionStartTime = Date.now();
STATE.sessionScores = [];
if (STATE.animationFrameId) cancelAnimationFrame(STATE.animationFrameId);
renderLoop();
sendFrameToServer();
STATE.liveLoopTimer = setInterval(sendFrameToServer, STATE.liveInterval);
showToast('Camera started! 🎥');
};
}
function stopCamera() {
if (STATE.liveLoopTimer) {
clearInterval(STATE.liveLoopTimer);
STATE.liveLoopTimer = null;
}
if (STATE.animationFrameId) {
cancelAnimationFrame(STATE.animationFrameId);
STATE.animationFrameId = null;
}
if (STATE.cameraStream) {
STATE.cameraStream.getTracks().forEach(t => t.stop());
STATE.cameraStream = null;
}
const video = el('webcam-video');
if (video) video.srcObject = null;
el('live-badge').classList.add('hidden');
el('fps-badge').classList.add('hidden');
el('score-overlay').classList.add('hidden');
el('camera-off-overlay').classList.remove('hidden');
el('start-camera-btn').classList.remove('hidden');
el('stop-camera-btn').classList.add('hidden');
el('snapshot-btn').classList.add('hidden');
el('suggestions-panel').classList.add('hidden');
el('fullscreen-suggestions').style.display = 'none';
STATE.currentLandmarks = null;
if (STATE.sessionScores.length > 0) {
showSessionSummary();
}
STATE.sessionStartTime = null;
}
function renderLoop() {
if (!STATE.cameraStream) {
STATE.animationFrameId = null;
return;
}
const video = el('webcam-video');
const canvas = el('webcam-canvas');
if (!video || !canvas) {
STATE.animationFrameId = requestAnimationFrame(renderLoop);
return;
}
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
if (STATE.mirrorEnabled) {
ctx.translate(canvas.width, 0);
ctx.scale(-1, 1);
}
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
ctx.restore();
if (STATE.currentLandmarks) {
drawSkeleton(ctx, STATE.currentLandmarks, canvas.width, canvas.height, STATE.mirrorEnabled);
}
if (STATE.showFps) {
const now = performance.now();
const fps = Math.round(1000 / (now - STATE.lastFrameTime));
STATE.lastFrameTime = now;
const fpsBadge = el('fps-badge');
if (fpsBadge) fpsBadge.textContent = `${fps} fps`;
}
STATE.animationFrameId = requestAnimationFrame(renderLoop);
}
function drawSkeleton(ctx, landmarks, width, height, mirror) {
if (!landmarks || landmarks.length === 0) return;
const scaleX = width;
const scaleY = height;
let color;
if (STATE.currentScore >= 80) color = '#22C55E';
else if (STATE.currentScore >= 65) color = '#F59E0B';
else color = '#EF4444';
const connections = [
[11, 12], [11, 13], [13, 15], [12, 14], [14, 16],
[11, 23], [12, 24], [23, 24], [23, 25], [25, 27],
[24, 26], [26, 28], [11, 0], [12, 0]
];
ctx.lineWidth = 2.5;
ctx.strokeStyle = color;
connections.forEach(([i, j]) => {
if (i < landmarks.length && j < landmarks.length) {
const p1 = landmarks[i];
const p2 = landmarks[j];
let x1 = p1[0] * scaleX;
let y1 = p1[1] * scaleY;
let x2 = p2[0] * scaleX;
let y2 = p2[1] * scaleY;
if (mirror) {
x1 = width - x1;
x2 = width - x2;
}
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
});
landmarks.forEach((lm, index) => {
let x = lm[0] * scaleX;
let y = lm[1] * scaleY;
if (mirror) x = width - x;
const isMajor = [11, 12, 13, 14, 15, 16, 23, 24, 25, 26, 27, 28].includes(index);
const radius = isMajor ? 5 : 3;
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2 * Math.PI);
ctx.fillStyle = color;
ctx.fill();
});
}
// ─── Send Frame to Server ─────────────────────────────────
async function sendFrameToServer() {
if (!STATE.cameraStream || STATE.isAnalysing || !STATE.selectedPose) return;
STATE.isAnalysing = true;
const video = el('webcam-video');
const canvas = el('webcam-canvas');
if (!video || !canvas) {
STATE.isAnalysing = false;
return;
}
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
const b64 = canvas.toDataURL('image/jpeg', CONFIG.SNAPSHOT_QUALITY);
try {
const data = await apiRequest('/api/live', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ image: b64, pose_id: STATE.selectedPose.id }),
});
if (data.error) {
console.warn('[Live]', data.error);
STATE.isAnalysing = false;
return;
}
if (data.landmarks && data.landmarks.length > 0) {
STATE.currentLandmarks = data.landmarks;
}
if (data.score !== undefined) {
STATE.currentScore = data.score;
STATE.currentStatus = data.status || '';
STATE.currentSuggestions = data.suggestions || [];
STATE.sessionScores.push(STATE.currentScore);
STATE.scoreHistory.push(STATE.currentScore);
if (STATE.scoreHistory.length > CONFIG.SCORE_HISTORY_LIMIT) {
STATE.scoreHistory.shift();
}
const smoothedScore = Math.round(
STATE.scoreHistory.reduce((a, b) => a + b, 0) / STATE.scoreHistory.length
);
updateLiveUI(smoothedScore, STATE.currentStatus, STATE.currentSuggestions);
if (STATE.isFullscreen) {
updateFullscreenSuggestions(STATE.currentSuggestions, smoothedScore);
}
}
} catch (err) {
console.error('[SendFrame]', err);
} finally {
STATE.isAnalysing = false;
}
}
// ─── Update Live UI ───────────────────────────────────────
function updateLiveUI(score, status, suggestions) {
const overlayScore = el('overlay-score');
const overlayStatus = el('overlay-status');
let colour;
if (score >= 80) colour = '#22C55E';
else if (score >= 65) colour = '#F59E0B';
else colour = '#EF4444';
if (overlayScore) {
overlayScore.textContent = `${score}%`;
overlayScore.style.color = colour;
}
if (overlayStatus) {
overlayStatus.textContent = status || '–';
overlayStatus.style.color = colour;
}
const badge = el('result-badge');
const scoreVal = el('score-value');
const barFill = el('score-bar-fill');
const suggList = el('suggestions-list');
if (scoreVal) {
scoreVal.textContent = `${score}%`;
scoreVal.style.color = colour;
}
if (barFill) {
barFill.style.width = `${score}%`;
barFill.style.background = colour;
}
let level, badgeClass;
if (score >= 80) {
level = 'Excellent';
badgeClass = 'badge-excellent';
} else if (score >= 65) {
level = 'Good';
badgeClass = 'badge-good';
} else {
level = 'Needs Correction';
badgeClass = 'badge-correction';
}
if (badge) {
badge.textContent = level;
badge.className = `result-badge ${badgeClass}`;
}
let displaySugg = suggestions;
if (!displaySugg || displaySugg.length === 0) {
if (score >= 80) displaySugg = ['🌟 Excellent posture!', 'Keep breathing deeply'];
else if (score >= 65) displaySugg = ['Almost there!', 'Small adjustments needed'];
else displaySugg = ['Focus on alignment', 'Practice this pose more'];
}
if (suggList) {
suggList.innerHTML = displaySugg.slice(0, 4).map(s => `
${s}
`).join('');
}
updateSuggestionsPanel(displaySugg, score);
}
function updateSuggestionsPanel(suggestions, score) {
const panel = el('suggestions-panel');
if (!panel) return;
let colour;
if (score >= 80) colour = '#22C55E';
else if (score >= 65) colour = '#F59E0B';
else colour = '#EF4444';
let displaySugg = suggestions;
if (!displaySugg || displaySugg.length === 0) {
if (score >= 80) displaySugg = ['🌟 Excellent posture!', 'Keep it up!'];
else if (score >= 65) displaySugg = ['Almost there!', 'Small adjustments'];
else displaySugg = ['Keep practicing', 'Focus on alignment'];
}
panel.innerHTML = `
${displaySugg.slice(0, 5).map((s, i) => `
${i + 1}
${s}
`).join('')}
`;
}
function updateFullscreenSuggestions(suggestions, score) {
const container = el('fullscreen-suggestions');
if (!container) return;
let colour;
if (score >= 80) colour = '#22C55E';
else if (score >= 65) colour = '#F59E0B';
else colour = '#EF4444';
let displaySugg = suggestions;
if (!displaySugg || displaySugg.length === 0) {
if (score >= 80) displaySugg = ['🌟 Excellent posture!', 'Keep it up!'];
else if (score >= 65) displaySugg = ['Almost there!', 'Small adjustments'];
else displaySugg = ['Keep practicing', 'Focus on alignment'];
}
container.innerHTML = `
💡 Pose Guidance
Accuracy
${score}%
${displaySugg.slice(0, 4).map(s => `
${s}
`).join('')}
`;
}
// ─── Fullscreen ────────────────────────────────────────────
function toggleFullscreen() {
const cameraPanel = document.querySelector('.camera-panel');
if (!cameraPanel) return;
if (!document.fullscreenElement) {
cameraPanel.requestFullscreen().catch(err => console.warn('Fullscreen error:', err));
} else {
document.exitFullscreen();
}
}
document.addEventListener('fullscreenchange', () => {
STATE.isFullscreen = !!document.fullscreenElement;
const btn = el('fullscreen-btn');
if (btn) {
btn.textContent = STATE.isFullscreen ? '⏹ Exit' : '⛶ Fullscreen';
}
});
// ─── Snapshot ──────────────────────────────────────────────
function takeSnapshot() {
if (!STATE.cameraStream) {
showToast('Start camera first', 'warning');
return;
}
const canvas = el('webcam-canvas');
if (!canvas) return;
const dataUrl = canvas.toDataURL('image/png');
const preview = el('snapshot-preview');
const imgEl = el('snapshot-img');
if (imgEl) imgEl.src = dataUrl;
if (preview) preview.classList.remove('hidden');
if (STATE.selectedPose && STATE.currentScore > 0) {
saveHistory(STATE.selectedPose, STATE.currentScore);
showToast('Snapshot saved 📸', 'success');
} else {
showToast('No pose data to save', 'warning');
}
}
// ─── Save History ──────────────────────────────────────────
function saveHistory(pose, score) {
const entry = {
id: Date.now(),
pose: pose.name || pose.id,
english: pose.english || pose.id,
emoji: pose.emoji || '🧘',
score,
date: new Date().toLocaleDateString('en-IN', { day: 'numeric', month: 'short', year: 'numeric' }),
time: new Date().toLocaleTimeString('en-IN', { hour: '2-digit', minute: '2-digit' }),
timestamp: new Date().toISOString(),
};
STATE.historyData.unshift(entry);
if (STATE.historyData.length > CONFIG.HISTORY_LIMIT) {
STATE.historyData = STATE.historyData.slice(0, CONFIG.HISTORY_LIMIT);
}
saveUserData();
updateStreak();
updatePracticeCount();
checkAchievements(score);
renderHistory();
updateBadge();
}
function updateStreak() {
const today = new Date().toDateString();
const lastPractice = STATE.lastPracticeDate;
if (lastPractice === today) return;
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
const yesterdayStr = yesterday.toDateString();
if (lastPractice === yesterdayStr) {
STATE.streak += 1;
} else {
STATE.streak = 1;
}
STATE.lastPracticeDate = today;
saveUserData();
}
function updatePracticeCount() {
STATE.practiceCount += 1;
saveUserData();
}
function showSessionSummary() {
const scores = STATE.sessionScores;
if (scores.length === 0) return;
const avg = Math.round(scores.reduce((a, b) => a + b, 0) / scores.length);
const max = Math.max(...scores);
const duration = Math.round((Date.now() - STATE.sessionStartTime) / 1000);
const minutes = Math.floor(duration / 60);
const seconds = duration % 60;
const calories = Math.round((duration / 60) * 3.5);
showToast(
`Session Complete! 🎉 Avg: ${avg}% | Best: ${max}% | Duration: ${minutes}m ${seconds}s | ${calories} kcal`,
'success',
5000
);
const session = {
date: new Date().toISOString(),
avg,
max,
duration,
calories,
pose: STATE.selectedPose?.name || 'Unknown',
};
const sessions = JSON.parse(getUserLocalStorage('sessions') || '[]');
sessions.unshift(session);
if (sessions.length > 20) sessions.pop();
setUserLocalStorage('sessions', JSON.stringify(sessions));
}
function checkAchievements(score) {
const achievements = [];
if (STATE.practiceCount === 1) {
achievements.push({ id: 'first-practice', name: 'First Practice 🧘', icon: '🌟' });
}
if (score >= 95) {
achievements.push({ id: 'perfect-pose', name: 'Perfect Pose ⭐', icon: '🏆' });
}
if (STATE.streak === 7) {
achievements.push({ id: 'week-streak', name: '7 Day Streak 🔥', icon: '🔥' });
}
if (STATE.streak === 30) {
achievements.push({ id: 'month-streak', name: '30 Day Streak 💪', icon: '💪' });
}
if (STATE.practiceCount === 10) {
achievements.push({ id: 'ten-sessions', name: '10 Sessions 🎯', icon: '🎯' });
}
if (STATE.practiceCount === 100) {
achievements.push({ id: 'hundred-sessions', name: '100 Sessions 🏅', icon: '🏅' });
}
const existing = STATE.achievements;
const newAchievements = achievements.filter(a => !existing.some(e => e.id === a.id));
if (newAchievements.length > 0) {
STATE.achievements = [...existing, ...newAchievements];
saveUserData();
newAchievements.forEach(a => {
showToast(`🏆 Achievement Unlocked: ${a.name}`, 'success', 4000);
});
}
}
// ─── Render History ────────────────────────────────────────
function renderHistory() {
const container = el('history-list');
if (!container) return;
if (!STATE.historyData.length) {
container.innerHTML = `
📜
No practice sessions yet
Select a pose, start the camera, and take a snapshot
`;
return;
}
container.innerHTML = STATE.historyData.map(entry => `
${entry.emoji || '🧘'}
${entry.pose}
${entry.english} — ${entry.date} at ${entry.time}
${entry.score}%
`).join('');
}
function clearHistory() {
if (!confirm('Clear all history?')) return;
STATE.historyData = [];
saveUserData();
renderHistory();
showToast('History cleared', 'warning');
}
function updateBadge() {
const badge = el('history-badge');
if (badge) badge.textContent = STATE.historyData.length;
}
// ─── Render Progress ────────────────────────────────────
function renderProgress() {
const statsEl = el('progress-stats');
const chartEl = el('chart-area');
if (!statsEl || !chartEl) return;
if (!STATE.historyData.length) {
statsEl.innerHTML = `
📊
No data yet
Practice some poses to see your progress
`;
chartEl.innerHTML = '';
return;
}
const scores = STATE.historyData.map(h => h.score);
const avg = Math.round(scores.reduce((a, b) => a + b, 0) / scores.length);
const best = Math.max(...scores);
const total = STATE.historyData.length;
const sessions = JSON.parse(getUserLocalStorage('sessions') || '[]');
const totalDuration = sessions.reduce((sum, s) => sum + (s.duration || 0), 0);
const hours = Math.floor(totalDuration / 3600);
const minutes = Math.floor((totalDuration % 3600) / 60);
const totalCalories = sessions.reduce((sum, s) => sum + (s.calories || 0), 0);
statsEl.innerHTML = `
Day Streak
${STATE.streak}d
Total Time
${hours}h ${minutes}m
Calories Burned
${totalCalories}kcal
`;
// Improved bar chart with labels
const recent = STATE.historyData.slice(0, 15).reverse();
chartEl.innerHTML = recent.map((entry, i) => {
const h = Math.round((entry.score / 100) * 140);
const color = entry.score >= 80 ? '#22C55E' : entry.score >= 65 ? '#F59E0B' : '#EF4444';
return `
`;
}).join('');
}
// ─── Custom Pose Generation ──────────────────────────────
function handlePoseImageUpload(event) {
const files = event.target.files;
if (!files || files.length === 0) return;
if (STATE.uploadedImages.length + files.length > CONFIG.REQUIRED_IMAGES) {
showToast(`Please upload exactly ${CONFIG.REQUIRED_IMAGES} images`, 'warning');
event.target.value = '';
return;
}
for (const file of files) {
if (STATE.uploadedImages.length >= CONFIG.REQUIRED_IMAGES) break;
const reader = new FileReader();
reader.onload = function(e) {
STATE.uploadedImages.push({
data: e.target.result,
name: file.name,
type: file.type,
});
renderUploadedImages();
if (STATE.uploadedImages.length === CONFIG.REQUIRED_IMAGES) {
showToast('All 5 images uploaded! Ready to generate pose 🎉', 'success');
const btn = el('generate-pose-btn');
if (btn) btn.disabled = false;
}
};
reader.readAsDataURL(file);
}
event.target.value = '';
}
function renderUploadedImages() {
const container = el('uploaded-images-container');
if (!container) return;
if (STATE.uploadedImages.length === 0) {
container.innerHTML = `
No images uploaded yet
`;
return;
}
container.innerHTML = STATE.uploadedImages.map((img, index) => `
${index + 1}
`).join('');
// Update progress
const progress = el('upload-progress');
if (progress) {
progress.textContent = `${STATE.uploadedImages.length}/${CONFIG.REQUIRED_IMAGES} uploaded`;
const bar = document.querySelector('.upload-progress-bar');
if (bar) bar.style.width = `${(STATE.uploadedImages.length / CONFIG.REQUIRED_IMAGES) * 100}%`;
}
const status = el('upload-status');
if (status) {
status.textContent = STATE.uploadedImages.length === CONFIG.REQUIRED_IMAGES ? '✅ Complete!' : 'Upload more...';
}
}
function removeUploadedImage(index) {
STATE.uploadedImages.splice(index, 1);
renderUploadedImages();
const btn = el('generate-pose-btn');
if (btn) {
btn.disabled = STATE.uploadedImages.length < CONFIG.REQUIRED_IMAGES;
}
}
// ─── generateCustomPose ──────────────────────────────────
async function generateCustomPose() {
if (STATE.uploadedImages.length !== CONFIG.REQUIRED_IMAGES) {
showToast(`Please upload exactly ${CONFIG.REQUIRED_IMAGES} images`, 'warning');
return;
}
if (STATE.isGenerating) return;
STATE.isGenerating = true;
const progress = el('generation-progress');
const progressBar = el('generation-progress-bar');
const progressText = el('generation-progress-text');
const progressPct = el('generation-progress-pct');
const btn = el('generate-pose-btn');
if (progress) progress.classList.remove('hidden');
if (btn) {
btn.textContent = '⏳ Generating...';
btn.disabled = true;
}
const name = el('asana-name')?.value.trim();
if (!name) {
showToast('Please enter a pose name', 'error');
STATE.isGenerating = false;
if (btn) {
btn.textContent = '✨ Generate Pose Reference';
btn.disabled = false;
}
if (progress) progress.classList.add('hidden');
return;
}
const category = el('asana-category')?.value || 'General';
const difficulty = document.querySelector('.difficulty-pills .pill.active')?.dataset.diff || 'Beginner';
if (progressText) progressText.textContent = 'Uploading images...';
if (progressPct) progressPct.textContent = '20%';
if (progressBar) progressBar.style.width = '20%';
try {
const formData = new FormData();
formData.append('name', name);
formData.append('category', category);
formData.append('difficulty', difficulty);
for (let i = 0; i < STATE.uploadedImages.length; i++) {
const imgData = STATE.uploadedImages[i];
const response = await fetch(imgData.data);
const blob = await response.blob();
const ext = imgData.name.split('.').pop() || 'jpg';
formData.append(`image_${i}`, blob, `pose_${i+1}.${ext}`);
}
if (progressText) progressText.textContent = 'Generating pose reference...';
if (progressPct) progressPct.textContent = '50%';
if (progressBar) progressBar.style.width = '50%';
const response = await fetch(`${CONFIG.API_BASE}/api/generate-pose`, {
method: 'POST',
body: formData,
});
const result = await response.json();
if (!response.ok) {
throw new Error(result.error || 'Generation failed');
}
if (result.error) {
throw new Error(result.error);
}
if (progressText) progressText.textContent = 'Processing results...';
if (progressPct) progressPct.textContent = '80%';
if (progressBar) progressBar.style.width = '80%';
STATE.generatedPose = result;
showGenerationResults(result);
if (progressText) progressText.textContent = '✅ Generation complete!';
if (progressPct) progressPct.textContent = '100%';
if (progressBar) progressBar.style.width = '100%';
showToast(`Pose "${name}" generated successfully! 🎉`, 'success');
} catch (error) {
console.error('[GeneratePose]', error);
showToast(`❌ Generation failed: ${error.message}`, 'error', 5000);
if (progressText) progressText.textContent = '❌ Generation failed';
if (progressPct) progressPct.textContent = 'Error';
if (progressBar) {
progressBar.style.width = '100%';
progressBar.style.background = '#EF4444';
}
} finally {
STATE.isGenerating = false;
if (btn) {
btn.textContent = '✨ Generate Pose Reference';
btn.disabled = false;
}
setTimeout(() => {
if (progress) progress.classList.add('hidden');
if (progressBar) {
progressBar.style.width = '0%';
progressBar.style.background = '';
}
if (progressPct) progressPct.textContent = '0%';
}, 3000);
}
}
// ─── Floating Save Button ─────────────────────────────────
function createFloatingSaveButton() {
removeFloatingSaveButton();
const btn = document.createElement('button');
btn.id = 'floating-save-btn';
btn.textContent = '💾 Save Now';
btn.style.cssText = `
position: fixed;
bottom: 30px;
right: 30px;
z-index: 9999;
background: #22C55E;
color: white;
padding: 16px 32px;
border: none;
border-radius: 12px;
font-size: 18px;
font-weight: bold;
cursor: pointer;
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
transition: transform 0.2s;
`;
btn.addEventListener('mouseenter', () => { btn.style.transform = 'scale(1.05)'; });
btn.addEventListener('mouseleave', () => { btn.style.transform = 'scale(1)'; });
btn.addEventListener('click', saveGeneratedPose);
document.body.appendChild(btn);
STATE.floatingSaveBtn = btn;
}
function removeFloatingSaveButton() {
if (STATE.floatingSaveBtn) {
STATE.floatingSaveBtn.remove();
STATE.floatingSaveBtn = null;
}
const existing = document.getElementById('floating-save-btn');
if (existing) existing.remove();
}
function showGenerationResults(result) {
const container = el('generation-results');
const preview = el('generated-pose-preview');
if (!container || !preview) return;
container.classList.remove('hidden');
preview.innerHTML = `
${result.pose_name || 'Generated Pose'}
${result.category || 'General'} • ${result.difficulty || 'Beginner'}
Similarity
${(result.similarity * 100).toFixed(1)}%
Samples
${result.sample_count || 5}
Angles
${result.avg_angles?.length || 15}
📊 Generated Reference Angles
${result.avg_angles?.slice(0, 15).map((angle, i) => `
J${i+1}: ${angle.toFixed(1)}°
`).join('')}
`;
preview.removeEventListener('click', handlePreviewClick);
preview.addEventListener('click', handlePreviewClick);
setTimeout(() => {
container.scrollIntoView({ behavior: 'smooth', block: 'center' });
}, 100);
createFloatingSaveButton();
}
function handlePreviewClick(e) {
const target = e.target.closest('[data-action]');
if (!target) return;
const action = target.dataset.action;
if (action === 'save-pose') {
saveGeneratedPose();
} else if (action === 'discard-pose') {
discardGeneratedPose();
}
}
// ─── saveGeneratedPose with image_paths ────────────────────
async function saveGeneratedPose() {
if (!STATE.generatedPose) {
showToast('No pose to save', 'error');
return;
}
const saveBtns = document.querySelectorAll('[data-action="save-pose"], #floating-save-btn');
saveBtns.forEach(btn => {
btn.disabled = true;
btn.textContent = '⏳ Saving...';
});
try {
const response = await fetch(`${CONFIG.API_BASE}/api/save-custom-pose`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-User-ID': STATE.currentUser ? STATE.currentUser.userId : '',
},
body: JSON.stringify({
pose_id: STATE.generatedPose.pose_id,
reference: STATE.generatedPose.reference,
name: STATE.generatedPose.pose_name,
images: STATE.generatedPose.image_paths || []
}),
});
const result = await response.json();
if (!response.ok) {
throw new Error(result.error || 'Save failed');
}
if (result.success) {
showToast(`✅ Pose "${STATE.generatedPose.pose_name}" saved!`, 'success', 4000);
removeFloatingSaveButton();
STATE.generatedPose = null;
await fetchPoses();
goHome();
const searchInput = el('home-search-input');
if (searchInput && STATE.generatedPose) {
const poseName = STATE.generatedPose.pose_name || '';
searchInput.value = poseName;
STATE.searchQuery = poseName;
performSearch(poseName);
searchInput.focus();
}
} else {
throw new Error(result.error || 'Unknown error');
}
} catch (error) {
console.error('❌ Save error:', error);
showToast(`❌ Save failed: ${error.message}`, 'error', 5000);
saveBtns.forEach(btn => {
btn.disabled = false;
btn.textContent = btn.id === 'floating-save-btn' ? '💾 Save Now' : '💾 Retry Save';
});
}
}
function discardGeneratedPose() {
STATE.generatedPose = null;
const container = el('generation-results');
if (container) container.classList.add('hidden');
const preview = el('generated-pose-preview');
if (preview) preview.innerHTML = '';
removeFloatingSaveButton();
showToast('Generation discarded', 'info');
}
// ─── Image Lightbox ──────────────────────────────────────────
function openLightbox(imageSrc) {
const lightbox = document.getElementById('image-lightbox');
const img = document.getElementById('lightbox-img');
if (!lightbox || !img || !imageSrc) return;
img.src = imageSrc;
lightbox.classList.remove('hidden');
document.body.style.overflow = 'hidden';
}
function closeLightbox() {
const lightbox = document.getElementById('image-lightbox');
if (!lightbox) return;
lightbox.classList.add('hidden');
document.body.style.overflow = '';
}
// Close lightbox on Escape key
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') closeLightbox();
});
// ─── Reset Camera UI ──────────────────────────────────────
function resetCameraUI() {
stopCamera();
STATE.scoreHistory = [];
STATE.currentLandmarks = null;
el('result-card').classList.add('hidden');
el('score-overlay').classList.add('hidden');
el('live-badge').classList.add('hidden');
el('fps-badge').classList.add('hidden');
el('snapshot-preview').classList.add('hidden');
el('camera-off-overlay').classList.remove('hidden');
el('start-camera-btn').classList.remove('hidden');
el('stop-camera-btn').classList.add('hidden');
el('snapshot-btn').classList.add('hidden');
el('suggestions-panel').classList.add('hidden');
el('fullscreen-suggestions').style.display = 'none';
const canvas = el('webcam-canvas');
if (canvas) {
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
}
// ─── Setup ──────────────────────────────────────────────────
function setupNav() {
$$('.nav-item[data-view]').forEach(el => {
el.addEventListener('click', (e) => {
e.preventDefault();
const view = el.dataset.view;
if (view === 'history') renderHistory();
if (view === 'progress') renderProgress();
if (view !== 'practice') stopCamera();
navigateTo(view);
});
});
}
function setupKeyboardShortcuts() {
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
if (STATE.isFullscreen) {
document.exitFullscreen();
}
closeLightbox();
}
if (e.target.tagName !== 'INPUT' && e.target.tagName !== 'TEXTAREA') {
if (e.key === ' ' && STATE.currentView === 'practice') {
e.preventDefault();
if (STATE.cameraStream) {
stopCamera();
} else {
startCamera();
}
}
if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.key === 'A') {
e.preventDefault();
openAddPose();
}
}
if (e.key === 's' && STATE.currentView === 'practice' && STATE.cameraStream) {
e.preventDefault();
takeSnapshot();
}
});
}
// ─── Initialize ─────────────────────────────────────────────
document.addEventListener('DOMContentLoaded', () => {
try {
apiRequest('/api/me')
.then(data => {
if (data.user_id) {
STATE.currentUser = { userId: data.user_id, name: data.name };
loadUserData();
showApp();
} else {
document.getElementById('login-overlay').style.display = 'flex';
}
})
.catch(() => {
document.getElementById('login-overlay').style.display = 'flex';
});
initAuthUI();
setupSearch();
setupNav();
setupKeyboardShortcuts();
console.log('[YogaMate] Initialized successfully 🧘');
} catch (err) {
console.error('[YogaMate] Startup error:', err);
showToast('Error loading application', 'error');
}
});
// ─── Expose Global Functions ──────────────────────────────
window.startCamera = startCamera;
window.stopCamera = stopCamera;
window.takeSnapshot = takeSnapshot;
window.toggleFullscreen = toggleFullscreen;
window.toggleMirror = toggleMirror;
window.toggleFps = toggleFps;
window.toggleTheme = toggleTheme;
window.updateInterval = updateInterval;
window.selectDifficulty = selectDifficulty;
window.openPractice = openPractice;
window.openAddPose = openAddPose;
window.goHome = goHome;
window.handlePoseImageUpload = handlePoseImageUpload;
window.removeUploadedImage = removeUploadedImage;
window.generateCustomPose = generateCustomPose;
window.saveGeneratedPose = saveGeneratedPose;
window.discardGeneratedPose = discardGeneratedPose;
window.clearHistory = clearHistory;
window.refreshReferenceImages = refreshReferenceImages;
window.toggleFavorite = toggleFavorite;
window.resetAddPoseForm = resetAddPoseForm;
window.performSearch = performSearch;
window.updateSearchResults = updateSearchResults;
window.openLightbox = openLightbox;
window.closeLightbox = closeLightbox;