Spaces:
Sleeping
Sleeping
Update static/app.js
Browse files- static/app.js +286 -242
static/app.js
CHANGED
|
@@ -18,7 +18,7 @@ const CONFIG = {
|
|
| 18 |
|
| 19 |
// βββ State βββ
|
| 20 |
let STATE = {
|
| 21 |
-
currentUser: null,
|
| 22 |
poses: [],
|
| 23 |
selectedPose: null,
|
| 24 |
currentView: 'home',
|
|
@@ -70,7 +70,7 @@ function removeUserLocalStorage(key) {
|
|
| 70 |
localStorage.removeItem(getUserKey(key));
|
| 71 |
}
|
| 72 |
|
| 73 |
-
// βββ Load user data
|
| 74 |
function loadUserData() {
|
| 75 |
if (!STATE.currentUser) return;
|
| 76 |
STATE.historyData = JSON.parse(getUserLocalStorage('history') || '[]');
|
|
@@ -80,7 +80,6 @@ function loadUserData() {
|
|
| 80 |
STATE.lastPracticeDate = getUserLocalStorage('last_practice_date') || null;
|
| 81 |
STATE.achievements = JSON.parse(getUserLocalStorage('achievements') || '[]');
|
| 82 |
STATE.isDark = getUserLocalStorage('dark') === 'true';
|
| 83 |
-
// Theme
|
| 84 |
applyTheme(STATE.isDark);
|
| 85 |
}
|
| 86 |
|
|
@@ -122,11 +121,11 @@ let toastTimeout;
|
|
| 122 |
function showToast(message, type = 'info', duration = 3000) {
|
| 123 |
const toast = el('toast');
|
| 124 |
const icons = { success: 'β
', error: 'β', warning: 'β οΈ', info: 'π‘' };
|
| 125 |
-
|
| 126 |
toast.innerHTML = `<span>${icons[type] || 'π‘'}</span> ${message}`;
|
| 127 |
toast.className = `toast toast-${type}`;
|
| 128 |
toast.classList.remove('hidden');
|
| 129 |
-
|
| 130 |
clearTimeout(toastTimeout);
|
| 131 |
toastTimeout = setTimeout(() => toast.classList.add('hidden'), duration);
|
| 132 |
}
|
|
@@ -166,7 +165,7 @@ function updateInterval(value) {
|
|
| 166 |
STATE.liveInterval = parseInt(value);
|
| 167 |
const label = el('interval-val');
|
| 168 |
if (label) label.textContent = `${value} ms`;
|
| 169 |
-
|
| 170 |
if (STATE.liveLoopTimer) {
|
| 171 |
clearInterval(STATE.liveLoopTimer);
|
| 172 |
STATE.liveLoopTimer = setInterval(sendFrameToServer, STATE.liveInterval);
|
|
@@ -178,7 +177,16 @@ function selectDifficulty(btn) {
|
|
| 178 |
btn.classList.add('active');
|
| 179 |
}
|
| 180 |
|
| 181 |
-
// βββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
async function apiRequest(endpoint, options = {}) {
|
| 183 |
const headers = options.headers || {};
|
| 184 |
if (STATE.currentUser) {
|
|
@@ -200,7 +208,7 @@ async function apiRequest(endpoint, options = {}) {
|
|
| 200 |
}
|
| 201 |
}
|
| 202 |
|
| 203 |
-
// βββ Authentication βββ
|
| 204 |
async function login(email, password) {
|
| 205 |
try {
|
| 206 |
const data = await apiRequest('/api/login', {
|
|
@@ -242,8 +250,6 @@ async function logout() {
|
|
| 242 |
await apiRequest('/api/logout', { method: 'POST' });
|
| 243 |
} catch (e) {}
|
| 244 |
STATE.currentUser = null;
|
| 245 |
-
// Clear user data from memory but keep localStorage (will be overwritten on next login)
|
| 246 |
-
// We'll just hide the app and show login
|
| 247 |
document.getElementById('app-shell').style.display = 'none';
|
| 248 |
document.getElementById('login-overlay').style.display = 'flex';
|
| 249 |
showToast('Logged out', 'info');
|
|
@@ -252,19 +258,16 @@ async function logout() {
|
|
| 252 |
function showApp() {
|
| 253 |
document.getElementById('login-overlay').style.display = 'none';
|
| 254 |
document.getElementById('app-shell').style.display = 'flex';
|
| 255 |
-
// Update UI with user info
|
| 256 |
document.getElementById('user-name').textContent = STATE.currentUser.name;
|
| 257 |
document.getElementById('user-email').textContent = STATE.currentUser.email;
|
| 258 |
-
// Load data
|
| 259 |
fetchPoses();
|
| 260 |
renderHistory();
|
| 261 |
renderProgress();
|
| 262 |
renderFavorites();
|
| 263 |
updateBadge();
|
| 264 |
-
// Re-render if needed
|
| 265 |
}
|
| 266 |
|
| 267 |
-
// βββ Login/Register UI
|
| 268 |
function initAuthUI() {
|
| 269 |
const loginForm = document.getElementById('login-form');
|
| 270 |
const registerForm = document.getElementById('register-form');
|
|
@@ -319,8 +322,17 @@ function initAuthUI() {
|
|
| 319 |
logoutBtn.addEventListener('click', logout);
|
| 320 |
}
|
| 321 |
|
| 322 |
-
// βββ Fetch Poses βββ
|
| 323 |
async function fetchPoses() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
try {
|
| 325 |
const poses = await apiRequest('/api/poses');
|
| 326 |
STATE.poses = poses;
|
|
@@ -332,17 +344,26 @@ async function fetchPoses() {
|
|
| 332 |
} catch (error) {
|
| 333 |
console.error('[YogaMate] Error fetching poses:', error);
|
| 334 |
showToast('Failed to load poses', 'error');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
return [];
|
| 336 |
}
|
| 337 |
}
|
| 338 |
|
| 339 |
-
// βββ Render Pose Grid βββ
|
| 340 |
function renderPoseGrid() {
|
| 341 |
const grid = el('pose-grid');
|
| 342 |
if (!grid) return;
|
| 343 |
-
|
| 344 |
const poses = STATE.poses.slice(0, 10);
|
| 345 |
-
|
| 346 |
if (!poses || poses.length === 0) {
|
| 347 |
grid.innerHTML = `
|
| 348 |
<div style="grid-column:1/-1;text-align:center;padding:60px 20px;color:var(--text-secondary);">
|
|
@@ -353,13 +374,13 @@ function renderPoseGrid() {
|
|
| 353 |
`;
|
| 354 |
return;
|
| 355 |
}
|
| 356 |
-
|
| 357 |
grid.innerHTML = poses.map(p => {
|
| 358 |
const isFav = STATE.favorites.includes(p.id);
|
| 359 |
const diffClass = `pose-diff-${(p.difficulty || 'beginner').toLowerCase()}`;
|
| 360 |
const isCustom = p.is_custom || false;
|
| 361 |
const customBadge = isCustom ? '<span class="custom-badge">CUSTOM</span>' : '';
|
| 362 |
-
|
| 363 |
return `
|
| 364 |
<div class="pose-card" data-pose-id="${p.id}" onclick="openPractice('${p.id}')">
|
| 365 |
<button class="pose-fav ${isFav ? 'active' : ''}" onclick="event.stopPropagation();toggleFavorite('${p.id}')">
|
|
@@ -372,14 +393,14 @@ function renderPoseGrid() {
|
|
| 372 |
</div>
|
| 373 |
`;
|
| 374 |
}).join('');
|
| 375 |
-
|
| 376 |
const countEl = el('pose-count');
|
| 377 |
if (countEl) {
|
| 378 |
countEl.textContent = `Showing ${Math.min(STATE.poses.length, 10)} of ${STATE.poses.length} poses`;
|
| 379 |
}
|
| 380 |
}
|
| 381 |
|
| 382 |
-
// βββ Favorites βββ
|
| 383 |
function toggleFavorite(poseId) {
|
| 384 |
const index = STATE.favorites.indexOf(poseId);
|
| 385 |
if (index > -1) {
|
|
@@ -397,13 +418,13 @@ function toggleFavorite(poseId) {
|
|
| 397 |
function renderFavorites() {
|
| 398 |
const container = el('favorites-grid');
|
| 399 |
if (!container) return;
|
| 400 |
-
|
| 401 |
const favs = STATE.poses.filter(p => STATE.favorites.includes(p.id));
|
| 402 |
if (favs.length === 0) {
|
| 403 |
container.innerHTML = '<div style="grid-column:1/-1;text-align:center;padding:20px;color:var(--text-muted);font-size:13px;">No favorites yet</div>';
|
| 404 |
return;
|
| 405 |
}
|
| 406 |
-
|
| 407 |
container.innerHTML = favs.map(p => `
|
| 408 |
<div class="pose-card" onclick="openPractice('${p.id}')" style="padding:16px;">
|
| 409 |
<div class="pose-emoji" style="font-size:32px;">${p.emoji || 'π§'}</div>
|
|
@@ -412,7 +433,11 @@ function renderFavorites() {
|
|
| 412 |
`).join('');
|
| 413 |
}
|
| 414 |
|
| 415 |
-
// βββ Search βββ
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
function setupSearch() {
|
| 417 |
const input = el('home-search-input');
|
| 418 |
const clearBtn = el('home-search-clear');
|
|
@@ -425,7 +450,7 @@ function setupSearch() {
|
|
| 425 |
input.addEventListener('input', () => {
|
| 426 |
const query = input.value.trim();
|
| 427 |
STATE.searchQuery = query;
|
| 428 |
-
|
| 429 |
});
|
| 430 |
|
| 431 |
input.addEventListener('focus', () => {
|
|
@@ -477,13 +502,10 @@ function performSearch(query) {
|
|
| 477 |
const label = el('search-results-label');
|
| 478 |
|
| 479 |
if (clearBtn) {
|
| 480 |
-
if (query)
|
| 481 |
-
|
| 482 |
-
} else {
|
| 483 |
-
clearBtn.classList.add('hidden');
|
| 484 |
-
}
|
| 485 |
}
|
| 486 |
-
|
| 487 |
if (!query) {
|
| 488 |
if (panel) {
|
| 489 |
panel.style.display = 'none';
|
|
@@ -491,22 +513,22 @@ function performSearch(query) {
|
|
| 491 |
}
|
| 492 |
return;
|
| 493 |
}
|
| 494 |
-
|
| 495 |
const results = STATE.poses.filter(p =>
|
| 496 |
(p.name && p.name.toLowerCase().includes(query.toLowerCase())) ||
|
| 497 |
(p.english && p.english.toLowerCase().includes(query.toLowerCase())) ||
|
| 498 |
(p.category && p.category.toLowerCase().includes(query.toLowerCase()))
|
| 499 |
);
|
| 500 |
-
|
| 501 |
if (panel) {
|
| 502 |
panel.style.display = 'block';
|
| 503 |
panel.classList.add('visible');
|
| 504 |
}
|
| 505 |
-
|
| 506 |
if (label) {
|
| 507 |
label.textContent = results.length > 0 ? `Results (${results.length})` : 'No Results';
|
| 508 |
}
|
| 509 |
-
|
| 510 |
if (list) {
|
| 511 |
if (results.length > 0) {
|
| 512 |
list.innerHTML = results.map(p => {
|
|
@@ -540,13 +562,13 @@ function updateSearchResults() {
|
|
| 540 |
}
|
| 541 |
}
|
| 542 |
|
| 543 |
-
// βββ Reference Images βββ
|
| 544 |
function displayReferenceImages(images) {
|
| 545 |
const container = el('reference-images-container');
|
| 546 |
if (!container) return;
|
| 547 |
-
|
| 548 |
container.innerHTML = '';
|
| 549 |
-
|
| 550 |
if (!images || images.length === 0) {
|
| 551 |
container.innerHTML = `
|
| 552 |
<div style="grid-column:1/-1;text-align:center;padding:20px;color:var(--text-secondary);font-size:13px;">
|
|
@@ -555,22 +577,30 @@ function displayReferenceImages(images) {
|
|
| 555 |
`;
|
| 556 |
return;
|
| 557 |
}
|
| 558 |
-
|
| 559 |
images.forEach((img, index) => {
|
| 560 |
const card = document.createElement('div');
|
| 561 |
card.className = 'reference-image-card';
|
| 562 |
-
|
|
|
|
| 563 |
const imgElement = document.createElement('img');
|
| 564 |
imgElement.src = `data:${img.mime};base64,${img.data}`;
|
| 565 |
imgElement.alt = `Reference ${index + 1}`;
|
| 566 |
imgElement.loading = 'lazy';
|
| 567 |
-
|
| 568 |
const label = document.createElement('div');
|
| 569 |
label.className = 'ref-label';
|
| 570 |
label.textContent = `#${index + 1}`;
|
| 571 |
-
|
| 572 |
card.appendChild(imgElement);
|
| 573 |
card.appendChild(label);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 574 |
container.appendChild(card);
|
| 575 |
});
|
| 576 |
}
|
|
@@ -578,17 +608,17 @@ function displayReferenceImages(images) {
|
|
| 578 |
async function loadReferenceImages(poseId) {
|
| 579 |
const container = el('reference-images-container');
|
| 580 |
if (!container) return;
|
| 581 |
-
|
| 582 |
container.innerHTML = `
|
| 583 |
<div style="grid-column:1/-1;text-align:center;padding:20px;color:var(--text-secondary);font-size:13px;">
|
| 584 |
<div class="spinner" style="margin:0 auto 12px;"></div>
|
| 585 |
Loading reference images...
|
| 586 |
</div>
|
| 587 |
`;
|
| 588 |
-
|
| 589 |
try {
|
| 590 |
const data = await apiRequest(`/api/pose/${poseId}/references`);
|
| 591 |
-
|
| 592 |
if (data.references && data.references.length > 0) {
|
| 593 |
displayReferenceImages(data.references);
|
| 594 |
} else {
|
|
@@ -616,60 +646,60 @@ function refreshReferenceImages() {
|
|
| 616 |
loadReferenceImages(STATE.selectedPose.id);
|
| 617 |
}
|
| 618 |
|
| 619 |
-
// βββ Open Practice βββ
|
| 620 |
function openPractice(poseId) {
|
| 621 |
const pose = STATE.poses.find(p => p.id === poseId);
|
| 622 |
if (!pose) {
|
| 623 |
showToast('Pose not found');
|
| 624 |
return;
|
| 625 |
}
|
| 626 |
-
|
| 627 |
STATE.selectedPose = pose;
|
| 628 |
-
|
| 629 |
const titleEl = el('practice-pose-title');
|
| 630 |
if (titleEl) titleEl.textContent = `Practice: ${pose.english || pose.name || pose.id}`;
|
| 631 |
-
|
| 632 |
const artEl = el('practice-pose-art');
|
| 633 |
if (artEl) artEl.textContent = pose.emoji || 'π§';
|
| 634 |
-
|
| 635 |
const nameEl = el('practice-pose-name');
|
| 636 |
if (nameEl) nameEl.textContent = pose.name || pose.id;
|
| 637 |
-
|
| 638 |
const sanskritEl = el('practice-pose-sanskrit');
|
| 639 |
if (sanskritEl) sanskritEl.textContent = pose.english || pose.id;
|
| 640 |
-
|
| 641 |
const diffEl = el('practice-pose-difficulty');
|
| 642 |
if (diffEl) {
|
| 643 |
diffEl.textContent = pose.difficulty || 'Beginner';
|
| 644 |
diffEl.className = `practice-pose-difficulty difficulty-${(pose.difficulty || 'beginner').toLowerCase()}`;
|
| 645 |
}
|
| 646 |
-
|
| 647 |
const benefits = pose.benefits || ['Practice this pose', 'Improve alignment', 'Build strength'];
|
| 648 |
const steps = pose.steps || ['Position yourself in the pose', 'Hold the posture', 'Breathe deeply'];
|
| 649 |
-
|
| 650 |
const benefitsEl = el('benefits-list');
|
| 651 |
if (benefitsEl) benefitsEl.innerHTML = benefits.map(b => `<li>${b}</li>`).join('');
|
| 652 |
-
|
| 653 |
const stepsEl = el('steps-list');
|
| 654 |
if (stepsEl) stepsEl.innerHTML = steps.map(s => `<li>${s}</li>`).join('');
|
| 655 |
-
|
| 656 |
loadReferenceImages(poseId);
|
| 657 |
resetCameraUI();
|
| 658 |
navigateTo('practice');
|
| 659 |
}
|
| 660 |
|
| 661 |
-
// βββ Navigation βββ
|
| 662 |
function navigateTo(view) {
|
| 663 |
$$('.view').forEach(v => v.classList.remove('active'));
|
| 664 |
const target = el(`view-${view}`);
|
| 665 |
if (target) target.classList.add('active');
|
| 666 |
-
|
| 667 |
$$('.nav-item[data-view]').forEach(el => {
|
| 668 |
el.classList.toggle('active', el.dataset.view === view);
|
| 669 |
});
|
| 670 |
-
|
| 671 |
STATE.currentView = view;
|
| 672 |
-
|
| 673 |
if (view === 'history') renderHistory();
|
| 674 |
if (view === 'progress') renderProgress();
|
| 675 |
if (view === 'home') {
|
|
@@ -690,28 +720,28 @@ function openAddPose() {
|
|
| 690 |
$$('.nav-item').forEach(el => el.classList.remove('active'));
|
| 691 |
}
|
| 692 |
|
| 693 |
-
// βββ Reset Add Pose Form βββ
|
| 694 |
function resetAddPoseForm() {
|
| 695 |
STATE.uploadedImages = [];
|
| 696 |
STATE.generatedPose = null;
|
| 697 |
STATE.isGenerating = false;
|
| 698 |
removeFloatingSaveButton();
|
| 699 |
-
|
| 700 |
const container = el('uploaded-images-container');
|
| 701 |
if (container) container.innerHTML = '';
|
| 702 |
-
|
| 703 |
const preview = el('generated-pose-preview');
|
| 704 |
if (preview) preview.classList.add('hidden');
|
| 705 |
-
|
| 706 |
const results = el('generation-results');
|
| 707 |
if (results) results.classList.add('hidden');
|
| 708 |
-
|
| 709 |
const btn = el('generate-pose-btn');
|
| 710 |
if (btn) {
|
| 711 |
btn.textContent = 'β¨ Generate Pose Reference';
|
| 712 |
btn.disabled = true;
|
| 713 |
}
|
| 714 |
-
|
| 715 |
const progress = el('generation-progress');
|
| 716 |
if (progress) {
|
| 717 |
progress.classList.add('hidden');
|
|
@@ -720,14 +750,14 @@ function resetAddPoseForm() {
|
|
| 720 |
const text = el('generation-progress-text');
|
| 721 |
if (text) text.textContent = '';
|
| 722 |
}
|
| 723 |
-
|
| 724 |
const form = el('add-pose-form');
|
| 725 |
if (form) form.reset();
|
| 726 |
-
|
| 727 |
$$('.difficulty-pills .pill').forEach(p => p.classList.remove('active'));
|
| 728 |
const beginnerPill = document.querySelector('.difficulty-pills .pill[data-diff="Beginner"]');
|
| 729 |
if (beginnerPill) beginnerPill.classList.add('active');
|
| 730 |
-
|
| 731 |
const progressEl = el('upload-progress');
|
| 732 |
if (progressEl) progressEl.textContent = '0/5 uploaded';
|
| 733 |
const statusEl = el('upload-status');
|
|
@@ -736,13 +766,13 @@ function resetAddPoseForm() {
|
|
| 736 |
if (bar) bar.style.width = '0%';
|
| 737 |
}
|
| 738 |
|
| 739 |
-
// βββ Camera Management βββ
|
| 740 |
async function startCamera() {
|
| 741 |
if (!STATE.selectedPose) {
|
| 742 |
showToast('Please select a pose first');
|
| 743 |
return;
|
| 744 |
}
|
| 745 |
-
|
| 746 |
try {
|
| 747 |
STATE.cameraStream = await navigator.mediaDevices.getUserMedia({
|
| 748 |
video: {
|
|
@@ -757,18 +787,18 @@ async function startCamera() {
|
|
| 757 |
console.error('[Camera]', err);
|
| 758 |
return;
|
| 759 |
}
|
| 760 |
-
|
| 761 |
const video = el('webcam-video');
|
| 762 |
const canvas = el('webcam-canvas');
|
| 763 |
if (!video || !canvas) return;
|
| 764 |
-
|
| 765 |
video.srcObject = STATE.cameraStream;
|
| 766 |
-
|
| 767 |
video.onloadedmetadata = () => {
|
| 768 |
video.play();
|
| 769 |
canvas.width = CONFIG.CAMERA_WIDTH;
|
| 770 |
canvas.height = CONFIG.CAMERA_HEIGHT;
|
| 771 |
-
|
| 772 |
el('camera-off-overlay').classList.add('hidden');
|
| 773 |
el('score-overlay').classList.remove('hidden');
|
| 774 |
el('live-badge').classList.remove('hidden');
|
|
@@ -777,17 +807,17 @@ async function startCamera() {
|
|
| 777 |
el('snapshot-btn').classList.remove('hidden');
|
| 778 |
el('result-card').classList.remove('hidden');
|
| 779 |
el('suggestions-panel').classList.remove('hidden');
|
| 780 |
-
|
| 781 |
if (STATE.showFps) el('fps-badge').classList.remove('hidden');
|
| 782 |
-
|
| 783 |
STATE.sessionStartTime = Date.now();
|
| 784 |
STATE.sessionScores = [];
|
| 785 |
-
|
| 786 |
if (STATE.animationFrameId) cancelAnimationFrame(STATE.animationFrameId);
|
| 787 |
renderLoop();
|
| 788 |
sendFrameToServer();
|
| 789 |
STATE.liveLoopTimer = setInterval(sendFrameToServer, STATE.liveInterval);
|
| 790 |
-
|
| 791 |
showToast('Camera started! π₯');
|
| 792 |
};
|
| 793 |
}
|
|
@@ -805,10 +835,10 @@ function stopCamera() {
|
|
| 805 |
STATE.cameraStream.getTracks().forEach(t => t.stop());
|
| 806 |
STATE.cameraStream = null;
|
| 807 |
}
|
| 808 |
-
|
| 809 |
const video = el('webcam-video');
|
| 810 |
if (video) video.srcObject = null;
|
| 811 |
-
|
| 812 |
el('live-badge').classList.add('hidden');
|
| 813 |
el('fps-badge').classList.add('hidden');
|
| 814 |
el('score-overlay').classList.add('hidden');
|
|
@@ -818,13 +848,13 @@ function stopCamera() {
|
|
| 818 |
el('snapshot-btn').classList.add('hidden');
|
| 819 |
el('suggestions-panel').classList.add('hidden');
|
| 820 |
el('fullscreen-suggestions').style.display = 'none';
|
| 821 |
-
|
| 822 |
STATE.currentLandmarks = null;
|
| 823 |
-
|
| 824 |
if (STATE.sessionScores.length > 0) {
|
| 825 |
showSessionSummary();
|
| 826 |
}
|
| 827 |
-
|
| 828 |
STATE.sessionStartTime = null;
|
| 829 |
}
|
| 830 |
|
|
@@ -833,17 +863,17 @@ function renderLoop() {
|
|
| 833 |
STATE.animationFrameId = null;
|
| 834 |
return;
|
| 835 |
}
|
| 836 |
-
|
| 837 |
const video = el('webcam-video');
|
| 838 |
const canvas = el('webcam-canvas');
|
| 839 |
if (!video || !canvas) {
|
| 840 |
STATE.animationFrameId = requestAnimationFrame(renderLoop);
|
| 841 |
return;
|
| 842 |
}
|
| 843 |
-
|
| 844 |
const ctx = canvas.getContext('2d');
|
| 845 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 846 |
-
|
| 847 |
ctx.save();
|
| 848 |
if (STATE.mirrorEnabled) {
|
| 849 |
ctx.translate(canvas.width, 0);
|
|
@@ -851,11 +881,11 @@ function renderLoop() {
|
|
| 851 |
}
|
| 852 |
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
| 853 |
ctx.restore();
|
| 854 |
-
|
| 855 |
if (STATE.currentLandmarks) {
|
| 856 |
drawSkeleton(ctx, STATE.currentLandmarks, canvas.width, canvas.height, STATE.mirrorEnabled);
|
| 857 |
}
|
| 858 |
-
|
| 859 |
if (STATE.showFps) {
|
| 860 |
const now = performance.now();
|
| 861 |
const fps = Math.round(1000 / (now - STATE.lastFrameTime));
|
|
@@ -863,61 +893,61 @@ function renderLoop() {
|
|
| 863 |
const fpsBadge = el('fps-badge');
|
| 864 |
if (fpsBadge) fpsBadge.textContent = `${fps} fps`;
|
| 865 |
}
|
| 866 |
-
|
| 867 |
STATE.animationFrameId = requestAnimationFrame(renderLoop);
|
| 868 |
}
|
| 869 |
|
| 870 |
function drawSkeleton(ctx, landmarks, width, height, mirror) {
|
| 871 |
if (!landmarks || landmarks.length === 0) return;
|
| 872 |
-
|
| 873 |
const scaleX = width;
|
| 874 |
const scaleY = height;
|
| 875 |
-
|
| 876 |
let color;
|
| 877 |
if (STATE.currentScore >= 80) color = '#22C55E';
|
| 878 |
else if (STATE.currentScore >= 65) color = '#F59E0B';
|
| 879 |
else color = '#EF4444';
|
| 880 |
-
|
| 881 |
const connections = [
|
| 882 |
[11, 12], [11, 13], [13, 15], [12, 14], [14, 16],
|
| 883 |
[11, 23], [12, 24], [23, 24], [23, 25], [25, 27],
|
| 884 |
[24, 26], [26, 28], [11, 0], [12, 0]
|
| 885 |
];
|
| 886 |
-
|
| 887 |
ctx.lineWidth = 2.5;
|
| 888 |
ctx.strokeStyle = color;
|
| 889 |
-
|
| 890 |
connections.forEach(([i, j]) => {
|
| 891 |
if (i < landmarks.length && j < landmarks.length) {
|
| 892 |
const p1 = landmarks[i];
|
| 893 |
const p2 = landmarks[j];
|
| 894 |
-
|
| 895 |
let x1 = p1[0] * scaleX;
|
| 896 |
let y1 = p1[1] * scaleY;
|
| 897 |
let x2 = p2[0] * scaleX;
|
| 898 |
let y2 = p2[1] * scaleY;
|
| 899 |
-
|
| 900 |
if (mirror) {
|
| 901 |
x1 = width - x1;
|
| 902 |
x2 = width - x2;
|
| 903 |
}
|
| 904 |
-
|
| 905 |
ctx.beginPath();
|
| 906 |
ctx.moveTo(x1, y1);
|
| 907 |
ctx.lineTo(x2, y2);
|
| 908 |
ctx.stroke();
|
| 909 |
}
|
| 910 |
});
|
| 911 |
-
|
| 912 |
landmarks.forEach((lm, index) => {
|
| 913 |
let x = lm[0] * scaleX;
|
| 914 |
let y = lm[1] * scaleY;
|
| 915 |
-
|
| 916 |
if (mirror) x = width - x;
|
| 917 |
-
|
| 918 |
const isMajor = [11, 12, 13, 14, 15, 16, 23, 24, 25, 26, 27, 28].includes(index);
|
| 919 |
const radius = isMajor ? 5 : 3;
|
| 920 |
-
|
| 921 |
ctx.beginPath();
|
| 922 |
ctx.arc(x, y, radius, 0, 2 * Math.PI);
|
| 923 |
ctx.fillStyle = color;
|
|
@@ -925,58 +955,58 @@ function drawSkeleton(ctx, landmarks, width, height, mirror) {
|
|
| 925 |
});
|
| 926 |
}
|
| 927 |
|
| 928 |
-
// βββ Send Frame to Server βββ
|
| 929 |
async function sendFrameToServer() {
|
| 930 |
if (!STATE.cameraStream || STATE.isAnalysing || !STATE.selectedPose) return;
|
| 931 |
STATE.isAnalysing = true;
|
| 932 |
-
|
| 933 |
const video = el('webcam-video');
|
| 934 |
const canvas = el('webcam-canvas');
|
| 935 |
if (!video || !canvas) {
|
| 936 |
STATE.isAnalysing = false;
|
| 937 |
return;
|
| 938 |
}
|
| 939 |
-
|
| 940 |
const ctx = canvas.getContext('2d');
|
| 941 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 942 |
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
| 943 |
-
|
| 944 |
const b64 = canvas.toDataURL('image/jpeg', CONFIG.SNAPSHOT_QUALITY);
|
| 945 |
-
|
| 946 |
try {
|
| 947 |
const data = await apiRequest('/api/live', {
|
| 948 |
method: 'POST',
|
| 949 |
headers: { 'Content-Type': 'application/json' },
|
| 950 |
body: JSON.stringify({ image: b64, pose_id: STATE.selectedPose.id }),
|
| 951 |
});
|
| 952 |
-
|
| 953 |
if (data.error) {
|
| 954 |
console.warn('[Live]', data.error);
|
| 955 |
STATE.isAnalysing = false;
|
| 956 |
return;
|
| 957 |
}
|
| 958 |
-
|
| 959 |
if (data.landmarks && data.landmarks.length > 0) {
|
| 960 |
STATE.currentLandmarks = data.landmarks;
|
| 961 |
}
|
| 962 |
-
|
| 963 |
if (data.score !== undefined) {
|
| 964 |
STATE.currentScore = data.score;
|
| 965 |
STATE.currentStatus = data.status || '';
|
| 966 |
STATE.currentSuggestions = data.suggestions || [];
|
| 967 |
-
|
| 968 |
STATE.sessionScores.push(STATE.currentScore);
|
| 969 |
STATE.scoreHistory.push(STATE.currentScore);
|
| 970 |
if (STATE.scoreHistory.length > CONFIG.SCORE_HISTORY_LIMIT) {
|
| 971 |
STATE.scoreHistory.shift();
|
| 972 |
}
|
| 973 |
-
|
| 974 |
const smoothedScore = Math.round(
|
| 975 |
STATE.scoreHistory.reduce((a, b) => a + b, 0) / STATE.scoreHistory.length
|
| 976 |
);
|
| 977 |
-
|
| 978 |
updateLiveUI(smoothedScore, STATE.currentStatus, STATE.currentSuggestions);
|
| 979 |
-
|
| 980 |
if (STATE.isFullscreen) {
|
| 981 |
updateFullscreenSuggestions(STATE.currentSuggestions, smoothedScore);
|
| 982 |
}
|
|
@@ -988,16 +1018,16 @@ async function sendFrameToServer() {
|
|
| 988 |
}
|
| 989 |
}
|
| 990 |
|
| 991 |
-
// βββ Update Live UI βββ
|
| 992 |
function updateLiveUI(score, status, suggestions) {
|
| 993 |
const overlayScore = el('overlay-score');
|
| 994 |
const overlayStatus = el('overlay-status');
|
| 995 |
-
|
| 996 |
let colour;
|
| 997 |
if (score >= 80) colour = '#22C55E';
|
| 998 |
else if (score >= 65) colour = '#F59E0B';
|
| 999 |
else colour = '#EF4444';
|
| 1000 |
-
|
| 1001 |
if (overlayScore) {
|
| 1002 |
overlayScore.textContent = `${score}%`;
|
| 1003 |
overlayScore.style.color = colour;
|
|
@@ -1006,12 +1036,12 @@ function updateLiveUI(score, status, suggestions) {
|
|
| 1006 |
overlayStatus.textContent = status || 'β';
|
| 1007 |
overlayStatus.style.color = colour;
|
| 1008 |
}
|
| 1009 |
-
|
| 1010 |
const badge = el('result-badge');
|
| 1011 |
const scoreVal = el('score-value');
|
| 1012 |
const barFill = el('score-bar-fill');
|
| 1013 |
const suggList = el('suggestions-list');
|
| 1014 |
-
|
| 1015 |
if (scoreVal) {
|
| 1016 |
scoreVal.textContent = `${score}%`;
|
| 1017 |
scoreVal.style.color = colour;
|
|
@@ -1020,7 +1050,7 @@ function updateLiveUI(score, status, suggestions) {
|
|
| 1020 |
barFill.style.width = `${score}%`;
|
| 1021 |
barFill.style.background = colour;
|
| 1022 |
}
|
| 1023 |
-
|
| 1024 |
let level, badgeClass;
|
| 1025 |
if (score >= 80) {
|
| 1026 |
level = 'Excellent';
|
|
@@ -1032,19 +1062,19 @@ function updateLiveUI(score, status, suggestions) {
|
|
| 1032 |
level = 'Needs Correction';
|
| 1033 |
badgeClass = 'badge-correction';
|
| 1034 |
}
|
| 1035 |
-
|
| 1036 |
if (badge) {
|
| 1037 |
badge.textContent = level;
|
| 1038 |
badge.className = `result-badge ${badgeClass}`;
|
| 1039 |
}
|
| 1040 |
-
|
| 1041 |
let displaySugg = suggestions;
|
| 1042 |
if (!displaySugg || displaySugg.length === 0) {
|
| 1043 |
if (score >= 80) displaySugg = ['π Excellent posture!', 'Keep breathing deeply'];
|
| 1044 |
else if (score >= 65) displaySugg = ['Almost there!', 'Small adjustments needed'];
|
| 1045 |
else displaySugg = ['Focus on alignment', 'Practice this pose more'];
|
| 1046 |
}
|
| 1047 |
-
|
| 1048 |
if (suggList) {
|
| 1049 |
suggList.innerHTML = displaySugg.slice(0, 4).map(s => `
|
| 1050 |
<li>
|
|
@@ -1053,26 +1083,26 @@ function updateLiveUI(score, status, suggestions) {
|
|
| 1053 |
</li>
|
| 1054 |
`).join('');
|
| 1055 |
}
|
| 1056 |
-
|
| 1057 |
updateSuggestionsPanel(displaySugg, score);
|
| 1058 |
}
|
| 1059 |
|
| 1060 |
function updateSuggestionsPanel(suggestions, score) {
|
| 1061 |
const panel = el('suggestions-panel');
|
| 1062 |
if (!panel) return;
|
| 1063 |
-
|
| 1064 |
let colour;
|
| 1065 |
if (score >= 80) colour = '#22C55E';
|
| 1066 |
else if (score >= 65) colour = '#F59E0B';
|
| 1067 |
else colour = '#EF4444';
|
| 1068 |
-
|
| 1069 |
let displaySugg = suggestions;
|
| 1070 |
if (!displaySugg || displaySugg.length === 0) {
|
| 1071 |
if (score >= 80) displaySugg = ['π Excellent posture!', 'Keep it up!'];
|
| 1072 |
else if (score >= 65) displaySugg = ['Almost there!', 'Small adjustments'];
|
| 1073 |
else displaySugg = ['Keep practicing', 'Focus on alignment'];
|
| 1074 |
}
|
| 1075 |
-
|
| 1076 |
panel.innerHTML = `
|
| 1077 |
<div class="suggestions-panel-header" style="border-bottom-color: ${colour};">
|
| 1078 |
<span style="font-weight:700;">π‘ Pose Guidance</span>
|
|
@@ -1095,19 +1125,19 @@ function updateSuggestionsPanel(suggestions, score) {
|
|
| 1095 |
function updateFullscreenSuggestions(suggestions, score) {
|
| 1096 |
const container = el('fullscreen-suggestions');
|
| 1097 |
if (!container) return;
|
| 1098 |
-
|
| 1099 |
let colour;
|
| 1100 |
if (score >= 80) colour = '#22C55E';
|
| 1101 |
else if (score >= 65) colour = '#F59E0B';
|
| 1102 |
else colour = '#EF4444';
|
| 1103 |
-
|
| 1104 |
let displaySugg = suggestions;
|
| 1105 |
if (!displaySugg || displaySugg.length === 0) {
|
| 1106 |
if (score >= 80) displaySugg = ['π Excellent posture!', 'Keep it up!'];
|
| 1107 |
else if (score >= 65) displaySugg = ['Almost there!', 'Small adjustments'];
|
| 1108 |
else displaySugg = ['Keep practicing', 'Focus on alignment'];
|
| 1109 |
}
|
| 1110 |
-
|
| 1111 |
container.innerHTML = `
|
| 1112 |
<div class="fs-title">π‘ Pose Guidance</div>
|
| 1113 |
<div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:6px;">
|
|
@@ -1123,11 +1153,11 @@ function updateFullscreenSuggestions(suggestions, score) {
|
|
| 1123 |
`;
|
| 1124 |
}
|
| 1125 |
|
| 1126 |
-
// βββ Fullscreen βββ
|
| 1127 |
function toggleFullscreen() {
|
| 1128 |
const cameraPanel = document.querySelector('.camera-panel');
|
| 1129 |
if (!cameraPanel) return;
|
| 1130 |
-
|
| 1131 |
if (!document.fullscreenElement) {
|
| 1132 |
cameraPanel.requestFullscreen().catch(err => console.warn('Fullscreen error:', err));
|
| 1133 |
} else {
|
|
@@ -1143,23 +1173,23 @@ document.addEventListener('fullscreenchange', () => {
|
|
| 1143 |
}
|
| 1144 |
});
|
| 1145 |
|
| 1146 |
-
// βββ Snapshot βββ
|
| 1147 |
function takeSnapshot() {
|
| 1148 |
if (!STATE.cameraStream) {
|
| 1149 |
showToast('Start camera first', 'warning');
|
| 1150 |
return;
|
| 1151 |
}
|
| 1152 |
-
|
| 1153 |
const canvas = el('webcam-canvas');
|
| 1154 |
if (!canvas) return;
|
| 1155 |
-
|
| 1156 |
const dataUrl = canvas.toDataURL('image/png');
|
| 1157 |
const preview = el('snapshot-preview');
|
| 1158 |
const imgEl = el('snapshot-img');
|
| 1159 |
-
|
| 1160 |
if (imgEl) imgEl.src = dataUrl;
|
| 1161 |
if (preview) preview.classList.remove('hidden');
|
| 1162 |
-
|
| 1163 |
if (STATE.selectedPose && STATE.currentScore > 0) {
|
| 1164 |
saveHistory(STATE.selectedPose, STATE.currentScore);
|
| 1165 |
showToast('Snapshot saved πΈ', 'success');
|
|
@@ -1168,7 +1198,7 @@ function takeSnapshot() {
|
|
| 1168 |
}
|
| 1169 |
}
|
| 1170 |
|
| 1171 |
-
// βββ Save History βββ
|
| 1172 |
function saveHistory(pose, score) {
|
| 1173 |
const entry = {
|
| 1174 |
id: Date.now(),
|
|
@@ -1180,13 +1210,13 @@ function saveHistory(pose, score) {
|
|
| 1180 |
time: new Date().toLocaleTimeString('en-IN', { hour: '2-digit', minute: '2-digit' }),
|
| 1181 |
timestamp: new Date().toISOString(),
|
| 1182 |
};
|
| 1183 |
-
|
| 1184 |
STATE.historyData.unshift(entry);
|
| 1185 |
if (STATE.historyData.length > CONFIG.HISTORY_LIMIT) {
|
| 1186 |
STATE.historyData = STATE.historyData.slice(0, CONFIG.HISTORY_LIMIT);
|
| 1187 |
}
|
| 1188 |
saveUserData();
|
| 1189 |
-
|
| 1190 |
updateStreak();
|
| 1191 |
updatePracticeCount();
|
| 1192 |
checkAchievements(score);
|
|
@@ -1197,19 +1227,19 @@ function saveHistory(pose, score) {
|
|
| 1197 |
function updateStreak() {
|
| 1198 |
const today = new Date().toDateString();
|
| 1199 |
const lastPractice = STATE.lastPracticeDate;
|
| 1200 |
-
|
| 1201 |
if (lastPractice === today) return;
|
| 1202 |
-
|
| 1203 |
const yesterday = new Date();
|
| 1204 |
yesterday.setDate(yesterday.getDate() - 1);
|
| 1205 |
const yesterdayStr = yesterday.toDateString();
|
| 1206 |
-
|
| 1207 |
if (lastPractice === yesterdayStr) {
|
| 1208 |
STATE.streak += 1;
|
| 1209 |
} else {
|
| 1210 |
STATE.streak = 1;
|
| 1211 |
}
|
| 1212 |
-
|
| 1213 |
STATE.lastPracticeDate = today;
|
| 1214 |
saveUserData();
|
| 1215 |
}
|
|
@@ -1222,20 +1252,20 @@ function updatePracticeCount() {
|
|
| 1222 |
function showSessionSummary() {
|
| 1223 |
const scores = STATE.sessionScores;
|
| 1224 |
if (scores.length === 0) return;
|
| 1225 |
-
|
| 1226 |
const avg = Math.round(scores.reduce((a, b) => a + b, 0) / scores.length);
|
| 1227 |
const max = Math.max(...scores);
|
| 1228 |
const duration = Math.round((Date.now() - STATE.sessionStartTime) / 1000);
|
| 1229 |
const minutes = Math.floor(duration / 60);
|
| 1230 |
const seconds = duration % 60;
|
| 1231 |
const calories = Math.round((duration / 60) * 3.5);
|
| 1232 |
-
|
| 1233 |
showToast(
|
| 1234 |
`Session Complete! π Avg: ${avg}% | Best: ${max}% | Duration: ${minutes}m ${seconds}s | ${calories} kcal`,
|
| 1235 |
'success',
|
| 1236 |
5000
|
| 1237 |
);
|
| 1238 |
-
|
| 1239 |
const session = {
|
| 1240 |
date: new Date().toISOString(),
|
| 1241 |
avg,
|
|
@@ -1244,7 +1274,7 @@ function showSessionSummary() {
|
|
| 1244 |
calories,
|
| 1245 |
pose: STATE.selectedPose?.name || 'Unknown',
|
| 1246 |
};
|
| 1247 |
-
|
| 1248 |
const sessions = JSON.parse(getUserLocalStorage('sessions') || '[]');
|
| 1249 |
sessions.unshift(session);
|
| 1250 |
if (sessions.length > 20) sessions.pop();
|
|
@@ -1253,7 +1283,7 @@ function showSessionSummary() {
|
|
| 1253 |
|
| 1254 |
function checkAchievements(score) {
|
| 1255 |
const achievements = [];
|
| 1256 |
-
|
| 1257 |
if (STATE.practiceCount === 1) {
|
| 1258 |
achievements.push({ id: 'first-practice', name: 'First Practice π§', icon: 'π' });
|
| 1259 |
}
|
|
@@ -1272,25 +1302,25 @@ function checkAchievements(score) {
|
|
| 1272 |
if (STATE.practiceCount === 100) {
|
| 1273 |
achievements.push({ id: 'hundred-sessions', name: '100 Sessions π
', icon: 'π
' });
|
| 1274 |
}
|
| 1275 |
-
|
| 1276 |
const existing = STATE.achievements;
|
| 1277 |
const newAchievements = achievements.filter(a => !existing.some(e => e.id === a.id));
|
| 1278 |
-
|
| 1279 |
if (newAchievements.length > 0) {
|
| 1280 |
STATE.achievements = [...existing, ...newAchievements];
|
| 1281 |
saveUserData();
|
| 1282 |
-
|
| 1283 |
newAchievements.forEach(a => {
|
| 1284 |
showToast(`π Achievement Unlocked: ${a.name}`, 'success', 4000);
|
| 1285 |
});
|
| 1286 |
}
|
| 1287 |
}
|
| 1288 |
|
| 1289 |
-
// βββ Render History βββ
|
| 1290 |
function renderHistory() {
|
| 1291 |
const container = el('history-list');
|
| 1292 |
if (!container) return;
|
| 1293 |
-
|
| 1294 |
if (!STATE.historyData.length) {
|
| 1295 |
container.innerHTML = `
|
| 1296 |
<div class="history-empty">
|
|
@@ -1301,7 +1331,7 @@ function renderHistory() {
|
|
| 1301 |
`;
|
| 1302 |
return;
|
| 1303 |
}
|
| 1304 |
-
|
| 1305 |
container.innerHTML = STATE.historyData.map(entry => `
|
| 1306 |
<div class="history-item">
|
| 1307 |
<div class="history-emoji">${entry.emoji || 'π§'}</div>
|
|
@@ -1329,12 +1359,12 @@ function updateBadge() {
|
|
| 1329 |
if (badge) badge.textContent = STATE.historyData.length;
|
| 1330 |
}
|
| 1331 |
|
| 1332 |
-
// βββ Render Progress βββ
|
| 1333 |
function renderProgress() {
|
| 1334 |
const statsEl = el('progress-stats');
|
| 1335 |
const chartEl = el('chart-area');
|
| 1336 |
if (!statsEl || !chartEl) return;
|
| 1337 |
-
|
| 1338 |
if (!STATE.historyData.length) {
|
| 1339 |
statsEl.innerHTML = `
|
| 1340 |
<div class="history-empty" style="grid-column:1/-1;">
|
|
@@ -1346,72 +1376,75 @@ function renderProgress() {
|
|
| 1346 |
chartEl.innerHTML = '';
|
| 1347 |
return;
|
| 1348 |
}
|
| 1349 |
-
|
| 1350 |
const scores = STATE.historyData.map(h => h.score);
|
| 1351 |
const avg = Math.round(scores.reduce((a, b) => a + b, 0) / scores.length);
|
| 1352 |
const best = Math.max(...scores);
|
| 1353 |
const total = STATE.historyData.length;
|
| 1354 |
-
|
| 1355 |
const sessions = JSON.parse(getUserLocalStorage('sessions') || '[]');
|
| 1356 |
const totalDuration = sessions.reduce((sum, s) => sum + (s.duration || 0), 0);
|
| 1357 |
const hours = Math.floor(totalDuration / 3600);
|
| 1358 |
const minutes = Math.floor((totalDuration % 3600) / 60);
|
| 1359 |
const totalCalories = sessions.reduce((sum, s) => sum + (s.calories || 0), 0);
|
| 1360 |
-
|
| 1361 |
statsEl.innerHTML = `
|
| 1362 |
-
<div class="stat-card">
|
| 1363 |
<div class="stat-label">Total Sessions</div>
|
| 1364 |
<div class="stat-value">${total}</div>
|
| 1365 |
</div>
|
| 1366 |
-
<div class="stat-card">
|
| 1367 |
<div class="stat-label">Average Score</div>
|
| 1368 |
<div class="stat-value">${avg}<span class="stat-unit">%</span></div>
|
| 1369 |
</div>
|
| 1370 |
-
<div class="stat-card">
|
| 1371 |
<div class="stat-label">Best Score</div>
|
| 1372 |
<div class="stat-value">${best}<span class="stat-unit">%</span></div>
|
| 1373 |
</div>
|
| 1374 |
-
<div class="stat-card">
|
| 1375 |
<div class="stat-label">Day Streak</div>
|
| 1376 |
<div class="stat-value">${STATE.streak}<span class="stat-unit">d</span></div>
|
| 1377 |
</div>
|
| 1378 |
-
<div class="stat-card">
|
| 1379 |
<div class="stat-label">Total Time</div>
|
| 1380 |
<div class="stat-value">${hours}h ${minutes}m</div>
|
| 1381 |
</div>
|
| 1382 |
-
<div class="stat-card">
|
| 1383 |
<div class="stat-label">Calories Burned</div>
|
| 1384 |
<div class="stat-value">${totalCalories}<span class="stat-unit">kcal</span></div>
|
| 1385 |
</div>
|
| 1386 |
`;
|
| 1387 |
-
|
|
|
|
| 1388 |
const recent = STATE.historyData.slice(0, 15).reverse();
|
| 1389 |
chartEl.innerHTML = recent.map((entry, i) => {
|
| 1390 |
const h = Math.round((entry.score / 100) * 140);
|
|
|
|
| 1391 |
return `
|
| 1392 |
-
<div class="chart-bar-wrap">
|
| 1393 |
-
<div class="chart-bar" style="height:${h}px;background:${
|
| 1394 |
-
|
| 1395 |
-
<
|
|
|
|
| 1396 |
</div>
|
| 1397 |
`;
|
| 1398 |
}).join('');
|
| 1399 |
}
|
| 1400 |
|
| 1401 |
-
// βββ Custom Pose Generation βββ
|
| 1402 |
function handlePoseImageUpload(event) {
|
| 1403 |
const files = event.target.files;
|
| 1404 |
if (!files || files.length === 0) return;
|
| 1405 |
-
|
| 1406 |
if (STATE.uploadedImages.length + files.length > CONFIG.REQUIRED_IMAGES) {
|
| 1407 |
showToast(`Please upload exactly ${CONFIG.REQUIRED_IMAGES} images`, 'warning');
|
| 1408 |
event.target.value = '';
|
| 1409 |
return;
|
| 1410 |
}
|
| 1411 |
-
|
| 1412 |
for (const file of files) {
|
| 1413 |
if (STATE.uploadedImages.length >= CONFIG.REQUIRED_IMAGES) break;
|
| 1414 |
-
|
| 1415 |
const reader = new FileReader();
|
| 1416 |
reader.onload = function(e) {
|
| 1417 |
STATE.uploadedImages.push({
|
|
@@ -1419,9 +1452,9 @@ function handlePoseImageUpload(event) {
|
|
| 1419 |
name: file.name,
|
| 1420 |
type: file.type,
|
| 1421 |
});
|
| 1422 |
-
|
| 1423 |
renderUploadedImages();
|
| 1424 |
-
|
| 1425 |
if (STATE.uploadedImages.length === CONFIG.REQUIRED_IMAGES) {
|
| 1426 |
showToast('All 5 images uploaded! Ready to generate pose π', 'success');
|
| 1427 |
const btn = el('generate-pose-btn');
|
|
@@ -1430,14 +1463,14 @@ function handlePoseImageUpload(event) {
|
|
| 1430 |
};
|
| 1431 |
reader.readAsDataURL(file);
|
| 1432 |
}
|
| 1433 |
-
|
| 1434 |
event.target.value = '';
|
| 1435 |
}
|
| 1436 |
|
| 1437 |
function renderUploadedImages() {
|
| 1438 |
const container = el('uploaded-images-container');
|
| 1439 |
if (!container) return;
|
| 1440 |
-
|
| 1441 |
if (STATE.uploadedImages.length === 0) {
|
| 1442 |
container.innerHTML = `
|
| 1443 |
<div class="upload-placeholder" style="grid-column:1/-1;text-align:center;padding:20px;color:var(--text-muted);font-size:13px;">
|
|
@@ -1446,15 +1479,16 @@ function renderUploadedImages() {
|
|
| 1446 |
`;
|
| 1447 |
return;
|
| 1448 |
}
|
| 1449 |
-
|
| 1450 |
container.innerHTML = STATE.uploadedImages.map((img, index) => `
|
| 1451 |
-
<div class="uploaded-image-card">
|
| 1452 |
<img src="${img.data}" alt="Upload ${index + 1}">
|
| 1453 |
<div class="uploaded-image-number">${index + 1}</div>
|
| 1454 |
-
<button class="remove-image-btn" onclick="removeUploadedImage(${index})">β</button>
|
| 1455 |
</div>
|
| 1456 |
`).join('');
|
| 1457 |
-
|
|
|
|
| 1458 |
const progress = el('upload-progress');
|
| 1459 |
if (progress) {
|
| 1460 |
progress.textContent = `${STATE.uploadedImages.length}/${CONFIG.REQUIRED_IMAGES} uploaded`;
|
|
@@ -1470,34 +1504,35 @@ function renderUploadedImages() {
|
|
| 1470 |
function removeUploadedImage(index) {
|
| 1471 |
STATE.uploadedImages.splice(index, 1);
|
| 1472 |
renderUploadedImages();
|
| 1473 |
-
|
| 1474 |
const btn = el('generate-pose-btn');
|
| 1475 |
if (btn) {
|
| 1476 |
btn.disabled = STATE.uploadedImages.length < CONFIG.REQUIRED_IMAGES;
|
| 1477 |
}
|
| 1478 |
}
|
| 1479 |
|
|
|
|
| 1480 |
async function generateCustomPose() {
|
| 1481 |
if (STATE.uploadedImages.length !== CONFIG.REQUIRED_IMAGES) {
|
| 1482 |
showToast(`Please upload exactly ${CONFIG.REQUIRED_IMAGES} images`, 'warning');
|
| 1483 |
return;
|
| 1484 |
}
|
| 1485 |
-
|
| 1486 |
if (STATE.isGenerating) return;
|
| 1487 |
STATE.isGenerating = true;
|
| 1488 |
-
|
| 1489 |
const progress = el('generation-progress');
|
| 1490 |
const progressBar = el('generation-progress-bar');
|
| 1491 |
const progressText = el('generation-progress-text');
|
| 1492 |
const progressPct = el('generation-progress-pct');
|
| 1493 |
const btn = el('generate-pose-btn');
|
| 1494 |
-
|
| 1495 |
if (progress) progress.classList.remove('hidden');
|
| 1496 |
if (btn) {
|
| 1497 |
btn.textContent = 'β³ Generating...';
|
| 1498 |
btn.disabled = true;
|
| 1499 |
}
|
| 1500 |
-
|
| 1501 |
const name = el('asana-name')?.value.trim();
|
| 1502 |
if (!name) {
|
| 1503 |
showToast('Please enter a pose name', 'error');
|
|
@@ -1509,20 +1544,20 @@ async function generateCustomPose() {
|
|
| 1509 |
if (progress) progress.classList.add('hidden');
|
| 1510 |
return;
|
| 1511 |
}
|
| 1512 |
-
|
| 1513 |
const category = el('asana-category')?.value || 'General';
|
| 1514 |
const difficulty = document.querySelector('.difficulty-pills .pill.active')?.dataset.diff || 'Beginner';
|
| 1515 |
-
|
| 1516 |
if (progressText) progressText.textContent = 'Uploading images...';
|
| 1517 |
if (progressPct) progressPct.textContent = '20%';
|
| 1518 |
if (progressBar) progressBar.style.width = '20%';
|
| 1519 |
-
|
| 1520 |
try {
|
| 1521 |
const formData = new FormData();
|
| 1522 |
formData.append('name', name);
|
| 1523 |
formData.append('category', category);
|
| 1524 |
formData.append('difficulty', difficulty);
|
| 1525 |
-
|
| 1526 |
for (let i = 0; i < STATE.uploadedImages.length; i++) {
|
| 1527 |
const imgData = STATE.uploadedImages[i];
|
| 1528 |
const response = await fetch(imgData.data);
|
|
@@ -1530,42 +1565,42 @@ async function generateCustomPose() {
|
|
| 1530 |
const ext = imgData.name.split('.').pop() || 'jpg';
|
| 1531 |
formData.append(`image_${i}`, blob, `pose_${i+1}.${ext}`);
|
| 1532 |
}
|
| 1533 |
-
|
| 1534 |
if (progressText) progressText.textContent = 'Generating pose reference...';
|
| 1535 |
if (progressPct) progressPct.textContent = '50%';
|
| 1536 |
if (progressBar) progressBar.style.width = '50%';
|
| 1537 |
-
|
| 1538 |
const response = await fetch(`${CONFIG.API_BASE}/api/generate-pose`, {
|
| 1539 |
method: 'POST',
|
| 1540 |
body: formData,
|
| 1541 |
});
|
| 1542 |
const result = await response.json();
|
| 1543 |
-
|
| 1544 |
if (!response.ok) {
|
| 1545 |
throw new Error(result.error || 'Generation failed');
|
| 1546 |
}
|
| 1547 |
-
|
| 1548 |
if (result.error) {
|
| 1549 |
throw new Error(result.error);
|
| 1550 |
}
|
| 1551 |
-
|
| 1552 |
if (progressText) progressText.textContent = 'Processing results...';
|
| 1553 |
if (progressPct) progressPct.textContent = '80%';
|
| 1554 |
if (progressBar) progressBar.style.width = '80%';
|
| 1555 |
-
|
| 1556 |
STATE.generatedPose = result;
|
| 1557 |
showGenerationResults(result);
|
| 1558 |
-
|
| 1559 |
if (progressText) progressText.textContent = 'β
Generation complete!';
|
| 1560 |
if (progressPct) progressPct.textContent = '100%';
|
| 1561 |
if (progressBar) progressBar.style.width = '100%';
|
| 1562 |
-
|
| 1563 |
showToast(`Pose "${name}" generated successfully! π`, 'success');
|
| 1564 |
-
|
| 1565 |
} catch (error) {
|
| 1566 |
console.error('[GeneratePose]', error);
|
| 1567 |
showToast(`β Generation failed: ${error.message}`, 'error', 5000);
|
| 1568 |
-
|
| 1569 |
if (progressText) progressText.textContent = 'β Generation failed';
|
| 1570 |
if (progressPct) progressPct.textContent = 'Error';
|
| 1571 |
if (progressBar) {
|
|
@@ -1578,7 +1613,7 @@ async function generateCustomPose() {
|
|
| 1578 |
btn.textContent = 'β¨ Generate Pose Reference';
|
| 1579 |
btn.disabled = false;
|
| 1580 |
}
|
| 1581 |
-
|
| 1582 |
setTimeout(() => {
|
| 1583 |
if (progress) progress.classList.add('hidden');
|
| 1584 |
if (progressBar) {
|
|
@@ -1590,7 +1625,7 @@ async function generateCustomPose() {
|
|
| 1590 |
}
|
| 1591 |
}
|
| 1592 |
|
| 1593 |
-
// βββ Floating Save Button βββ
|
| 1594 |
function createFloatingSaveButton() {
|
| 1595 |
removeFloatingSaveButton();
|
| 1596 |
const btn = document.createElement('button');
|
|
@@ -1629,13 +1664,9 @@ function removeFloatingSaveButton() {
|
|
| 1629 |
}
|
| 1630 |
|
| 1631 |
function showGenerationResults(result) {
|
| 1632 |
-
console.log('π’ showGenerationResults called with result:', result);
|
| 1633 |
const container = el('generation-results');
|
| 1634 |
const preview = el('generated-pose-preview');
|
| 1635 |
-
if (!container || !preview)
|
| 1636 |
-
console.error('β Container or preview not found');
|
| 1637 |
-
return;
|
| 1638 |
-
}
|
| 1639 |
|
| 1640 |
container.classList.remove('hidden');
|
| 1641 |
|
|
@@ -1662,18 +1693,10 @@ function showGenerationResults(result) {
|
|
| 1662 |
</div>
|
| 1663 |
</div>
|
| 1664 |
<div style="display:flex;gap:8px;">
|
| 1665 |
-
<button
|
| 1666 |
-
data-action="save-pose"
|
| 1667 |
-
onclick="saveGeneratedPose()"
|
| 1668 |
-
class="btn-primary"
|
| 1669 |
-
style="font-size:13px;padding:10px 28px;">
|
| 1670 |
πΎ Save Pose
|
| 1671 |
</button>
|
| 1672 |
-
<button
|
| 1673 |
-
data-action="discard-pose"
|
| 1674 |
-
onclick="discardGeneratedPose()"
|
| 1675 |
-
class="btn-outline"
|
| 1676 |
-
style="font-size:13px;padding:10px 20px;">
|
| 1677 |
Discard
|
| 1678 |
</button>
|
| 1679 |
</div>
|
|
@@ -1690,18 +1713,14 @@ function showGenerationResults(result) {
|
|
| 1690 |
</div>
|
| 1691 |
`;
|
| 1692 |
|
| 1693 |
-
// Event delegation (backup)
|
| 1694 |
preview.removeEventListener('click', handlePreviewClick);
|
| 1695 |
preview.addEventListener('click', handlePreviewClick);
|
| 1696 |
|
| 1697 |
-
// Auto-scroll
|
| 1698 |
setTimeout(() => {
|
| 1699 |
container.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
| 1700 |
}, 100);
|
| 1701 |
|
| 1702 |
-
// Floating save button
|
| 1703 |
createFloatingSaveButton();
|
| 1704 |
-
console.log('β
Floating save button created');
|
| 1705 |
}
|
| 1706 |
|
| 1707 |
function handlePreviewClick(e) {
|
|
@@ -1715,17 +1734,13 @@ function handlePreviewClick(e) {
|
|
| 1715 |
}
|
| 1716 |
}
|
| 1717 |
|
|
|
|
| 1718 |
async function saveGeneratedPose() {
|
| 1719 |
-
console.log('π΅ saveGeneratedPose called');
|
| 1720 |
-
|
| 1721 |
if (!STATE.generatedPose) {
|
| 1722 |
showToast('No pose to save', 'error');
|
| 1723 |
-
console.warn('β οΈ STATE.generatedPose is null/undefined');
|
| 1724 |
return;
|
| 1725 |
}
|
| 1726 |
|
| 1727 |
-
console.log('π¦ Pose data:', STATE.generatedPose);
|
| 1728 |
-
|
| 1729 |
const saveBtns = document.querySelectorAll('[data-action="save-pose"], #floating-save-btn');
|
| 1730 |
saveBtns.forEach(btn => {
|
| 1731 |
btn.disabled = true;
|
|
@@ -1743,11 +1758,11 @@ async function saveGeneratedPose() {
|
|
| 1743 |
pose_id: STATE.generatedPose.pose_id,
|
| 1744 |
reference: STATE.generatedPose.reference,
|
| 1745 |
name: STATE.generatedPose.pose_name,
|
|
|
|
| 1746 |
}),
|
| 1747 |
});
|
| 1748 |
|
| 1749 |
const result = await response.json();
|
| 1750 |
-
console.log('π₯ Save response:', result);
|
| 1751 |
|
| 1752 |
if (!response.ok) {
|
| 1753 |
throw new Error(result.error || 'Save failed');
|
|
@@ -1756,6 +1771,7 @@ async function saveGeneratedPose() {
|
|
| 1756 |
if (result.success) {
|
| 1757 |
showToast(`β
Pose "${STATE.generatedPose.pose_name}" saved!`, 'success', 4000);
|
| 1758 |
removeFloatingSaveButton();
|
|
|
|
| 1759 |
await fetchPoses();
|
| 1760 |
goHome();
|
| 1761 |
const searchInput = el('home-search-input');
|
|
@@ -1789,12 +1805,34 @@ function discardGeneratedPose() {
|
|
| 1789 |
showToast('Generation discarded', 'info');
|
| 1790 |
}
|
| 1791 |
|
| 1792 |
-
// βββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1793 |
function resetCameraUI() {
|
| 1794 |
stopCamera();
|
| 1795 |
STATE.scoreHistory = [];
|
| 1796 |
STATE.currentLandmarks = null;
|
| 1797 |
-
|
| 1798 |
el('result-card').classList.add('hidden');
|
| 1799 |
el('score-overlay').classList.add('hidden');
|
| 1800 |
el('live-badge').classList.add('hidden');
|
|
@@ -1806,7 +1844,7 @@ function resetCameraUI() {
|
|
| 1806 |
el('snapshot-btn').classList.add('hidden');
|
| 1807 |
el('suggestions-panel').classList.add('hidden');
|
| 1808 |
el('fullscreen-suggestions').style.display = 'none';
|
| 1809 |
-
|
| 1810 |
const canvas = el('webcam-canvas');
|
| 1811 |
if (canvas) {
|
| 1812 |
const ctx = canvas.getContext('2d');
|
|
@@ -1814,7 +1852,7 @@ function resetCameraUI() {
|
|
| 1814 |
}
|
| 1815 |
}
|
| 1816 |
|
| 1817 |
-
// βββ Setup βββ
|
| 1818 |
function setupNav() {
|
| 1819 |
$$('.nav-item[data-view]').forEach(el => {
|
| 1820 |
el.addEventListener('click', (e) => {
|
|
@@ -1834,8 +1872,9 @@ function setupKeyboardShortcuts() {
|
|
| 1834 |
if (STATE.isFullscreen) {
|
| 1835 |
document.exitFullscreen();
|
| 1836 |
}
|
|
|
|
| 1837 |
}
|
| 1838 |
-
|
| 1839 |
if (e.target.tagName !== 'INPUT' && e.target.tagName !== 'TEXTAREA') {
|
| 1840 |
if (e.key === ' ' && STATE.currentView === 'practice') {
|
| 1841 |
e.preventDefault();
|
|
@@ -1845,8 +1884,12 @@ function setupKeyboardShortcuts() {
|
|
| 1845 |
startCamera();
|
| 1846 |
}
|
| 1847 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1848 |
}
|
| 1849 |
-
|
| 1850 |
if (e.key === 's' && STATE.currentView === 'practice' && STATE.cameraStream) {
|
| 1851 |
e.preventDefault();
|
| 1852 |
takeSnapshot();
|
|
@@ -1854,10 +1897,9 @@ function setupKeyboardShortcuts() {
|
|
| 1854 |
});
|
| 1855 |
}
|
| 1856 |
|
| 1857 |
-
// βββ Initialize βββ
|
| 1858 |
document.addEventListener('DOMContentLoaded', () => {
|
| 1859 |
try {
|
| 1860 |
-
// Check if user is already logged in via session
|
| 1861 |
apiRequest('/api/me')
|
| 1862 |
.then(data => {
|
| 1863 |
if (data.user_id) {
|
|
@@ -1871,12 +1913,12 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 1871 |
.catch(() => {
|
| 1872 |
document.getElementById('login-overlay').style.display = 'flex';
|
| 1873 |
});
|
| 1874 |
-
|
| 1875 |
initAuthUI();
|
| 1876 |
setupSearch();
|
| 1877 |
setupNav();
|
| 1878 |
setupKeyboardShortcuts();
|
| 1879 |
-
|
| 1880 |
console.log('[YogaMate] Initialized successfully π§');
|
| 1881 |
} catch (err) {
|
| 1882 |
console.error('[YogaMate] Startup error:', err);
|
|
@@ -1884,7 +1926,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 1884 |
}
|
| 1885 |
});
|
| 1886 |
|
| 1887 |
-
// βββ Expose Global Functions βββ
|
| 1888 |
window.startCamera = startCamera;
|
| 1889 |
window.stopCamera = stopCamera;
|
| 1890 |
window.takeSnapshot = takeSnapshot;
|
|
@@ -1907,4 +1949,6 @@ window.refreshReferenceImages = refreshReferenceImages;
|
|
| 1907 |
window.toggleFavorite = toggleFavorite;
|
| 1908 |
window.resetAddPoseForm = resetAddPoseForm;
|
| 1909 |
window.performSearch = performSearch;
|
| 1910 |
-
window.updateSearchResults = updateSearchResults;
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
// βββ State βββ
|
| 20 |
let STATE = {
|
| 21 |
+
currentUser: null,
|
| 22 |
poses: [],
|
| 23 |
selectedPose: null,
|
| 24 |
currentView: 'home',
|
|
|
|
| 70 |
localStorage.removeItem(getUserKey(key));
|
| 71 |
}
|
| 72 |
|
| 73 |
+
// βββ Load user data βββ
|
| 74 |
function loadUserData() {
|
| 75 |
if (!STATE.currentUser) return;
|
| 76 |
STATE.historyData = JSON.parse(getUserLocalStorage('history') || '[]');
|
|
|
|
| 80 |
STATE.lastPracticeDate = getUserLocalStorage('last_practice_date') || null;
|
| 81 |
STATE.achievements = JSON.parse(getUserLocalStorage('achievements') || '[]');
|
| 82 |
STATE.isDark = getUserLocalStorage('dark') === 'true';
|
|
|
|
| 83 |
applyTheme(STATE.isDark);
|
| 84 |
}
|
| 85 |
|
|
|
|
| 121 |
function showToast(message, type = 'info', duration = 3000) {
|
| 122 |
const toast = el('toast');
|
| 123 |
const icons = { success: 'β
', error: 'β', warning: 'β οΈ', info: 'π‘' };
|
| 124 |
+
|
| 125 |
toast.innerHTML = `<span>${icons[type] || 'π‘'}</span> ${message}`;
|
| 126 |
toast.className = `toast toast-${type}`;
|
| 127 |
toast.classList.remove('hidden');
|
| 128 |
+
|
| 129 |
clearTimeout(toastTimeout);
|
| 130 |
toastTimeout = setTimeout(() => toast.classList.add('hidden'), duration);
|
| 131 |
}
|
|
|
|
| 165 |
STATE.liveInterval = parseInt(value);
|
| 166 |
const label = el('interval-val');
|
| 167 |
if (label) label.textContent = `${value} ms`;
|
| 168 |
+
|
| 169 |
if (STATE.liveLoopTimer) {
|
| 170 |
clearInterval(STATE.liveLoopTimer);
|
| 171 |
STATE.liveLoopTimer = setInterval(sendFrameToServer, STATE.liveInterval);
|
|
|
|
| 177 |
btn.classList.add('active');
|
| 178 |
}
|
| 179 |
|
| 180 |
+
// βββ Debounce βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 181 |
+
function debounce(func, wait) {
|
| 182 |
+
let timeout;
|
| 183 |
+
return function(...args) {
|
| 184 |
+
clearTimeout(timeout);
|
| 185 |
+
timeout = setTimeout(() => func.apply(this, args), wait);
|
| 186 |
+
};
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
// βββ API ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 190 |
async function apiRequest(endpoint, options = {}) {
|
| 191 |
const headers = options.headers || {};
|
| 192 |
if (STATE.currentUser) {
|
|
|
|
| 208 |
}
|
| 209 |
}
|
| 210 |
|
| 211 |
+
// βββ Authentication ββββββββββββββββββββββββββββββββββββββββ
|
| 212 |
async function login(email, password) {
|
| 213 |
try {
|
| 214 |
const data = await apiRequest('/api/login', {
|
|
|
|
| 250 |
await apiRequest('/api/logout', { method: 'POST' });
|
| 251 |
} catch (e) {}
|
| 252 |
STATE.currentUser = null;
|
|
|
|
|
|
|
| 253 |
document.getElementById('app-shell').style.display = 'none';
|
| 254 |
document.getElementById('login-overlay').style.display = 'flex';
|
| 255 |
showToast('Logged out', 'info');
|
|
|
|
| 258 |
function showApp() {
|
| 259 |
document.getElementById('login-overlay').style.display = 'none';
|
| 260 |
document.getElementById('app-shell').style.display = 'flex';
|
|
|
|
| 261 |
document.getElementById('user-name').textContent = STATE.currentUser.name;
|
| 262 |
document.getElementById('user-email').textContent = STATE.currentUser.email;
|
|
|
|
| 263 |
fetchPoses();
|
| 264 |
renderHistory();
|
| 265 |
renderProgress();
|
| 266 |
renderFavorites();
|
| 267 |
updateBadge();
|
|
|
|
| 268 |
}
|
| 269 |
|
| 270 |
+
// βββ Login/Register UI ββββββββββββββββββββββββββββββββββββ
|
| 271 |
function initAuthUI() {
|
| 272 |
const loginForm = document.getElementById('login-form');
|
| 273 |
const registerForm = document.getElementById('register-form');
|
|
|
|
| 322 |
logoutBtn.addEventListener('click', logout);
|
| 323 |
}
|
| 324 |
|
| 325 |
+
// βββ Fetch Poses ββββββββββββββββββββββββββββββββββββ
|
| 326 |
async function fetchPoses() {
|
| 327 |
+
const grid = el('pose-grid');
|
| 328 |
+
if (grid) {
|
| 329 |
+
grid.innerHTML = `
|
| 330 |
+
<div style="grid-column:1/-1;text-align:center;padding:60px 20px;">
|
| 331 |
+
<div class="spinner" style="margin:0 auto 16px;"></div>
|
| 332 |
+
<p style="color:var(--text-muted);">Loading poses...</p>
|
| 333 |
+
</div>
|
| 334 |
+
`;
|
| 335 |
+
}
|
| 336 |
try {
|
| 337 |
const poses = await apiRequest('/api/poses');
|
| 338 |
STATE.poses = poses;
|
|
|
|
| 344 |
} catch (error) {
|
| 345 |
console.error('[YogaMate] Error fetching poses:', error);
|
| 346 |
showToast('Failed to load poses', 'error');
|
| 347 |
+
if (grid) {
|
| 348 |
+
grid.innerHTML = `
|
| 349 |
+
<div style="grid-column:1/-1;text-align:center;padding:60px 20px;">
|
| 350 |
+
<div style="font-size:48px;margin-bottom:16px;">β οΈ</div>
|
| 351 |
+
<p style="color:var(--text-secondary);">Failed to load poses. <br>
|
| 352 |
+
<button class="btn-primary" onclick="fetchPoses()" style="margin-top:12px;">Retry</button></p>
|
| 353 |
+
</div>
|
| 354 |
+
`;
|
| 355 |
+
}
|
| 356 |
return [];
|
| 357 |
}
|
| 358 |
}
|
| 359 |
|
| 360 |
+
// βββ Render Pose Grid βββββββββββββββββββββββββββββββββββββ
|
| 361 |
function renderPoseGrid() {
|
| 362 |
const grid = el('pose-grid');
|
| 363 |
if (!grid) return;
|
| 364 |
+
|
| 365 |
const poses = STATE.poses.slice(0, 10);
|
| 366 |
+
|
| 367 |
if (!poses || poses.length === 0) {
|
| 368 |
grid.innerHTML = `
|
| 369 |
<div style="grid-column:1/-1;text-align:center;padding:60px 20px;color:var(--text-secondary);">
|
|
|
|
| 374 |
`;
|
| 375 |
return;
|
| 376 |
}
|
| 377 |
+
|
| 378 |
grid.innerHTML = poses.map(p => {
|
| 379 |
const isFav = STATE.favorites.includes(p.id);
|
| 380 |
const diffClass = `pose-diff-${(p.difficulty || 'beginner').toLowerCase()}`;
|
| 381 |
const isCustom = p.is_custom || false;
|
| 382 |
const customBadge = isCustom ? '<span class="custom-badge">CUSTOM</span>' : '';
|
| 383 |
+
|
| 384 |
return `
|
| 385 |
<div class="pose-card" data-pose-id="${p.id}" onclick="openPractice('${p.id}')">
|
| 386 |
<button class="pose-fav ${isFav ? 'active' : ''}" onclick="event.stopPropagation();toggleFavorite('${p.id}')">
|
|
|
|
| 393 |
</div>
|
| 394 |
`;
|
| 395 |
}).join('');
|
| 396 |
+
|
| 397 |
const countEl = el('pose-count');
|
| 398 |
if (countEl) {
|
| 399 |
countEl.textContent = `Showing ${Math.min(STATE.poses.length, 10)} of ${STATE.poses.length} poses`;
|
| 400 |
}
|
| 401 |
}
|
| 402 |
|
| 403 |
+
// βββ Favorites βββββββββββββββββββββββββββββββββββββββββββββ
|
| 404 |
function toggleFavorite(poseId) {
|
| 405 |
const index = STATE.favorites.indexOf(poseId);
|
| 406 |
if (index > -1) {
|
|
|
|
| 418 |
function renderFavorites() {
|
| 419 |
const container = el('favorites-grid');
|
| 420 |
if (!container) return;
|
| 421 |
+
|
| 422 |
const favs = STATE.poses.filter(p => STATE.favorites.includes(p.id));
|
| 423 |
if (favs.length === 0) {
|
| 424 |
container.innerHTML = '<div style="grid-column:1/-1;text-align:center;padding:20px;color:var(--text-muted);font-size:13px;">No favorites yet</div>';
|
| 425 |
return;
|
| 426 |
}
|
| 427 |
+
|
| 428 |
container.innerHTML = favs.map(p => `
|
| 429 |
<div class="pose-card" onclick="openPractice('${p.id}')" style="padding:16px;">
|
| 430 |
<div class="pose-emoji" style="font-size:32px;">${p.emoji || 'π§'}</div>
|
|
|
|
| 433 |
`).join('');
|
| 434 |
}
|
| 435 |
|
| 436 |
+
// βββ Search ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 437 |
+
const debouncedSearch = debounce((query) => {
|
| 438 |
+
performSearch(query);
|
| 439 |
+
}, 300);
|
| 440 |
+
|
| 441 |
function setupSearch() {
|
| 442 |
const input = el('home-search-input');
|
| 443 |
const clearBtn = el('home-search-clear');
|
|
|
|
| 450 |
input.addEventListener('input', () => {
|
| 451 |
const query = input.value.trim();
|
| 452 |
STATE.searchQuery = query;
|
| 453 |
+
debouncedSearch(query);
|
| 454 |
});
|
| 455 |
|
| 456 |
input.addEventListener('focus', () => {
|
|
|
|
| 502 |
const label = el('search-results-label');
|
| 503 |
|
| 504 |
if (clearBtn) {
|
| 505 |
+
if (query) clearBtn.classList.remove('hidden');
|
| 506 |
+
else clearBtn.classList.add('hidden');
|
|
|
|
|
|
|
|
|
|
| 507 |
}
|
| 508 |
+
|
| 509 |
if (!query) {
|
| 510 |
if (panel) {
|
| 511 |
panel.style.display = 'none';
|
|
|
|
| 513 |
}
|
| 514 |
return;
|
| 515 |
}
|
| 516 |
+
|
| 517 |
const results = STATE.poses.filter(p =>
|
| 518 |
(p.name && p.name.toLowerCase().includes(query.toLowerCase())) ||
|
| 519 |
(p.english && p.english.toLowerCase().includes(query.toLowerCase())) ||
|
| 520 |
(p.category && p.category.toLowerCase().includes(query.toLowerCase()))
|
| 521 |
);
|
| 522 |
+
|
| 523 |
if (panel) {
|
| 524 |
panel.style.display = 'block';
|
| 525 |
panel.classList.add('visible');
|
| 526 |
}
|
| 527 |
+
|
| 528 |
if (label) {
|
| 529 |
label.textContent = results.length > 0 ? `Results (${results.length})` : 'No Results';
|
| 530 |
}
|
| 531 |
+
|
| 532 |
if (list) {
|
| 533 |
if (results.length > 0) {
|
| 534 |
list.innerHTML = results.map(p => {
|
|
|
|
| 562 |
}
|
| 563 |
}
|
| 564 |
|
| 565 |
+
// βββ Reference Images ββββββββββββββββββββββββββββββββββββ
|
| 566 |
function displayReferenceImages(images) {
|
| 567 |
const container = el('reference-images-container');
|
| 568 |
if (!container) return;
|
| 569 |
+
|
| 570 |
container.innerHTML = '';
|
| 571 |
+
|
| 572 |
if (!images || images.length === 0) {
|
| 573 |
container.innerHTML = `
|
| 574 |
<div style="grid-column:1/-1;text-align:center;padding:20px;color:var(--text-secondary);font-size:13px;">
|
|
|
|
| 577 |
`;
|
| 578 |
return;
|
| 579 |
}
|
| 580 |
+
|
| 581 |
images.forEach((img, index) => {
|
| 582 |
const card = document.createElement('div');
|
| 583 |
card.className = 'reference-image-card';
|
| 584 |
+
card.style.cursor = 'pointer';
|
| 585 |
+
|
| 586 |
const imgElement = document.createElement('img');
|
| 587 |
imgElement.src = `data:${img.mime};base64,${img.data}`;
|
| 588 |
imgElement.alt = `Reference ${index + 1}`;
|
| 589 |
imgElement.loading = 'lazy';
|
| 590 |
+
|
| 591 |
const label = document.createElement('div');
|
| 592 |
label.className = 'ref-label';
|
| 593 |
label.textContent = `#${index + 1}`;
|
| 594 |
+
|
| 595 |
card.appendChild(imgElement);
|
| 596 |
card.appendChild(label);
|
| 597 |
+
|
| 598 |
+
// Direct onclick for guaranteed execution
|
| 599 |
+
card.onclick = function(e) {
|
| 600 |
+
if (e.target.classList.contains('ref-label')) return;
|
| 601 |
+
openLightbox(imgElement.src);
|
| 602 |
+
};
|
| 603 |
+
|
| 604 |
container.appendChild(card);
|
| 605 |
});
|
| 606 |
}
|
|
|
|
| 608 |
async function loadReferenceImages(poseId) {
|
| 609 |
const container = el('reference-images-container');
|
| 610 |
if (!container) return;
|
| 611 |
+
|
| 612 |
container.innerHTML = `
|
| 613 |
<div style="grid-column:1/-1;text-align:center;padding:20px;color:var(--text-secondary);font-size:13px;">
|
| 614 |
<div class="spinner" style="margin:0 auto 12px;"></div>
|
| 615 |
Loading reference images...
|
| 616 |
</div>
|
| 617 |
`;
|
| 618 |
+
|
| 619 |
try {
|
| 620 |
const data = await apiRequest(`/api/pose/${poseId}/references`);
|
| 621 |
+
|
| 622 |
if (data.references && data.references.length > 0) {
|
| 623 |
displayReferenceImages(data.references);
|
| 624 |
} else {
|
|
|
|
| 646 |
loadReferenceImages(STATE.selectedPose.id);
|
| 647 |
}
|
| 648 |
|
| 649 |
+
// βββ Open Practice ββββββββββββββββββββββββββββββββββββββββ
|
| 650 |
function openPractice(poseId) {
|
| 651 |
const pose = STATE.poses.find(p => p.id === poseId);
|
| 652 |
if (!pose) {
|
| 653 |
showToast('Pose not found');
|
| 654 |
return;
|
| 655 |
}
|
| 656 |
+
|
| 657 |
STATE.selectedPose = pose;
|
| 658 |
+
|
| 659 |
const titleEl = el('practice-pose-title');
|
| 660 |
if (titleEl) titleEl.textContent = `Practice: ${pose.english || pose.name || pose.id}`;
|
| 661 |
+
|
| 662 |
const artEl = el('practice-pose-art');
|
| 663 |
if (artEl) artEl.textContent = pose.emoji || 'π§';
|
| 664 |
+
|
| 665 |
const nameEl = el('practice-pose-name');
|
| 666 |
if (nameEl) nameEl.textContent = pose.name || pose.id;
|
| 667 |
+
|
| 668 |
const sanskritEl = el('practice-pose-sanskrit');
|
| 669 |
if (sanskritEl) sanskritEl.textContent = pose.english || pose.id;
|
| 670 |
+
|
| 671 |
const diffEl = el('practice-pose-difficulty');
|
| 672 |
if (diffEl) {
|
| 673 |
diffEl.textContent = pose.difficulty || 'Beginner';
|
| 674 |
diffEl.className = `practice-pose-difficulty difficulty-${(pose.difficulty || 'beginner').toLowerCase()}`;
|
| 675 |
}
|
| 676 |
+
|
| 677 |
const benefits = pose.benefits || ['Practice this pose', 'Improve alignment', 'Build strength'];
|
| 678 |
const steps = pose.steps || ['Position yourself in the pose', 'Hold the posture', 'Breathe deeply'];
|
| 679 |
+
|
| 680 |
const benefitsEl = el('benefits-list');
|
| 681 |
if (benefitsEl) benefitsEl.innerHTML = benefits.map(b => `<li>${b}</li>`).join('');
|
| 682 |
+
|
| 683 |
const stepsEl = el('steps-list');
|
| 684 |
if (stepsEl) stepsEl.innerHTML = steps.map(s => `<li>${s}</li>`).join('');
|
| 685 |
+
|
| 686 |
loadReferenceImages(poseId);
|
| 687 |
resetCameraUI();
|
| 688 |
navigateTo('practice');
|
| 689 |
}
|
| 690 |
|
| 691 |
+
// βββ Navigation ββββββββββββββββββββββββββββββββββββββββββββ
|
| 692 |
function navigateTo(view) {
|
| 693 |
$$('.view').forEach(v => v.classList.remove('active'));
|
| 694 |
const target = el(`view-${view}`);
|
| 695 |
if (target) target.classList.add('active');
|
| 696 |
+
|
| 697 |
$$('.nav-item[data-view]').forEach(el => {
|
| 698 |
el.classList.toggle('active', el.dataset.view === view);
|
| 699 |
});
|
| 700 |
+
|
| 701 |
STATE.currentView = view;
|
| 702 |
+
|
| 703 |
if (view === 'history') renderHistory();
|
| 704 |
if (view === 'progress') renderProgress();
|
| 705 |
if (view === 'home') {
|
|
|
|
| 720 |
$$('.nav-item').forEach(el => el.classList.remove('active'));
|
| 721 |
}
|
| 722 |
|
| 723 |
+
// βββ Reset Add Pose Form βββββββββββββββββββββββββββββββββ
|
| 724 |
function resetAddPoseForm() {
|
| 725 |
STATE.uploadedImages = [];
|
| 726 |
STATE.generatedPose = null;
|
| 727 |
STATE.isGenerating = false;
|
| 728 |
removeFloatingSaveButton();
|
| 729 |
+
|
| 730 |
const container = el('uploaded-images-container');
|
| 731 |
if (container) container.innerHTML = '';
|
| 732 |
+
|
| 733 |
const preview = el('generated-pose-preview');
|
| 734 |
if (preview) preview.classList.add('hidden');
|
| 735 |
+
|
| 736 |
const results = el('generation-results');
|
| 737 |
if (results) results.classList.add('hidden');
|
| 738 |
+
|
| 739 |
const btn = el('generate-pose-btn');
|
| 740 |
if (btn) {
|
| 741 |
btn.textContent = 'β¨ Generate Pose Reference';
|
| 742 |
btn.disabled = true;
|
| 743 |
}
|
| 744 |
+
|
| 745 |
const progress = el('generation-progress');
|
| 746 |
if (progress) {
|
| 747 |
progress.classList.add('hidden');
|
|
|
|
| 750 |
const text = el('generation-progress-text');
|
| 751 |
if (text) text.textContent = '';
|
| 752 |
}
|
| 753 |
+
|
| 754 |
const form = el('add-pose-form');
|
| 755 |
if (form) form.reset();
|
| 756 |
+
|
| 757 |
$$('.difficulty-pills .pill').forEach(p => p.classList.remove('active'));
|
| 758 |
const beginnerPill = document.querySelector('.difficulty-pills .pill[data-diff="Beginner"]');
|
| 759 |
if (beginnerPill) beginnerPill.classList.add('active');
|
| 760 |
+
|
| 761 |
const progressEl = el('upload-progress');
|
| 762 |
if (progressEl) progressEl.textContent = '0/5 uploaded';
|
| 763 |
const statusEl = el('upload-status');
|
|
|
|
| 766 |
if (bar) bar.style.width = '0%';
|
| 767 |
}
|
| 768 |
|
| 769 |
+
// βββ Camera Management ββββββββββββββββββββββββββββββββββββ
|
| 770 |
async function startCamera() {
|
| 771 |
if (!STATE.selectedPose) {
|
| 772 |
showToast('Please select a pose first');
|
| 773 |
return;
|
| 774 |
}
|
| 775 |
+
|
| 776 |
try {
|
| 777 |
STATE.cameraStream = await navigator.mediaDevices.getUserMedia({
|
| 778 |
video: {
|
|
|
|
| 787 |
console.error('[Camera]', err);
|
| 788 |
return;
|
| 789 |
}
|
| 790 |
+
|
| 791 |
const video = el('webcam-video');
|
| 792 |
const canvas = el('webcam-canvas');
|
| 793 |
if (!video || !canvas) return;
|
| 794 |
+
|
| 795 |
video.srcObject = STATE.cameraStream;
|
| 796 |
+
|
| 797 |
video.onloadedmetadata = () => {
|
| 798 |
video.play();
|
| 799 |
canvas.width = CONFIG.CAMERA_WIDTH;
|
| 800 |
canvas.height = CONFIG.CAMERA_HEIGHT;
|
| 801 |
+
|
| 802 |
el('camera-off-overlay').classList.add('hidden');
|
| 803 |
el('score-overlay').classList.remove('hidden');
|
| 804 |
el('live-badge').classList.remove('hidden');
|
|
|
|
| 807 |
el('snapshot-btn').classList.remove('hidden');
|
| 808 |
el('result-card').classList.remove('hidden');
|
| 809 |
el('suggestions-panel').classList.remove('hidden');
|
| 810 |
+
|
| 811 |
if (STATE.showFps) el('fps-badge').classList.remove('hidden');
|
| 812 |
+
|
| 813 |
STATE.sessionStartTime = Date.now();
|
| 814 |
STATE.sessionScores = [];
|
| 815 |
+
|
| 816 |
if (STATE.animationFrameId) cancelAnimationFrame(STATE.animationFrameId);
|
| 817 |
renderLoop();
|
| 818 |
sendFrameToServer();
|
| 819 |
STATE.liveLoopTimer = setInterval(sendFrameToServer, STATE.liveInterval);
|
| 820 |
+
|
| 821 |
showToast('Camera started! π₯');
|
| 822 |
};
|
| 823 |
}
|
|
|
|
| 835 |
STATE.cameraStream.getTracks().forEach(t => t.stop());
|
| 836 |
STATE.cameraStream = null;
|
| 837 |
}
|
| 838 |
+
|
| 839 |
const video = el('webcam-video');
|
| 840 |
if (video) video.srcObject = null;
|
| 841 |
+
|
| 842 |
el('live-badge').classList.add('hidden');
|
| 843 |
el('fps-badge').classList.add('hidden');
|
| 844 |
el('score-overlay').classList.add('hidden');
|
|
|
|
| 848 |
el('snapshot-btn').classList.add('hidden');
|
| 849 |
el('suggestions-panel').classList.add('hidden');
|
| 850 |
el('fullscreen-suggestions').style.display = 'none';
|
| 851 |
+
|
| 852 |
STATE.currentLandmarks = null;
|
| 853 |
+
|
| 854 |
if (STATE.sessionScores.length > 0) {
|
| 855 |
showSessionSummary();
|
| 856 |
}
|
| 857 |
+
|
| 858 |
STATE.sessionStartTime = null;
|
| 859 |
}
|
| 860 |
|
|
|
|
| 863 |
STATE.animationFrameId = null;
|
| 864 |
return;
|
| 865 |
}
|
| 866 |
+
|
| 867 |
const video = el('webcam-video');
|
| 868 |
const canvas = el('webcam-canvas');
|
| 869 |
if (!video || !canvas) {
|
| 870 |
STATE.animationFrameId = requestAnimationFrame(renderLoop);
|
| 871 |
return;
|
| 872 |
}
|
| 873 |
+
|
| 874 |
const ctx = canvas.getContext('2d');
|
| 875 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 876 |
+
|
| 877 |
ctx.save();
|
| 878 |
if (STATE.mirrorEnabled) {
|
| 879 |
ctx.translate(canvas.width, 0);
|
|
|
|
| 881 |
}
|
| 882 |
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
| 883 |
ctx.restore();
|
| 884 |
+
|
| 885 |
if (STATE.currentLandmarks) {
|
| 886 |
drawSkeleton(ctx, STATE.currentLandmarks, canvas.width, canvas.height, STATE.mirrorEnabled);
|
| 887 |
}
|
| 888 |
+
|
| 889 |
if (STATE.showFps) {
|
| 890 |
const now = performance.now();
|
| 891 |
const fps = Math.round(1000 / (now - STATE.lastFrameTime));
|
|
|
|
| 893 |
const fpsBadge = el('fps-badge');
|
| 894 |
if (fpsBadge) fpsBadge.textContent = `${fps} fps`;
|
| 895 |
}
|
| 896 |
+
|
| 897 |
STATE.animationFrameId = requestAnimationFrame(renderLoop);
|
| 898 |
}
|
| 899 |
|
| 900 |
function drawSkeleton(ctx, landmarks, width, height, mirror) {
|
| 901 |
if (!landmarks || landmarks.length === 0) return;
|
| 902 |
+
|
| 903 |
const scaleX = width;
|
| 904 |
const scaleY = height;
|
| 905 |
+
|
| 906 |
let color;
|
| 907 |
if (STATE.currentScore >= 80) color = '#22C55E';
|
| 908 |
else if (STATE.currentScore >= 65) color = '#F59E0B';
|
| 909 |
else color = '#EF4444';
|
| 910 |
+
|
| 911 |
const connections = [
|
| 912 |
[11, 12], [11, 13], [13, 15], [12, 14], [14, 16],
|
| 913 |
[11, 23], [12, 24], [23, 24], [23, 25], [25, 27],
|
| 914 |
[24, 26], [26, 28], [11, 0], [12, 0]
|
| 915 |
];
|
| 916 |
+
|
| 917 |
ctx.lineWidth = 2.5;
|
| 918 |
ctx.strokeStyle = color;
|
| 919 |
+
|
| 920 |
connections.forEach(([i, j]) => {
|
| 921 |
if (i < landmarks.length && j < landmarks.length) {
|
| 922 |
const p1 = landmarks[i];
|
| 923 |
const p2 = landmarks[j];
|
| 924 |
+
|
| 925 |
let x1 = p1[0] * scaleX;
|
| 926 |
let y1 = p1[1] * scaleY;
|
| 927 |
let x2 = p2[0] * scaleX;
|
| 928 |
let y2 = p2[1] * scaleY;
|
| 929 |
+
|
| 930 |
if (mirror) {
|
| 931 |
x1 = width - x1;
|
| 932 |
x2 = width - x2;
|
| 933 |
}
|
| 934 |
+
|
| 935 |
ctx.beginPath();
|
| 936 |
ctx.moveTo(x1, y1);
|
| 937 |
ctx.lineTo(x2, y2);
|
| 938 |
ctx.stroke();
|
| 939 |
}
|
| 940 |
});
|
| 941 |
+
|
| 942 |
landmarks.forEach((lm, index) => {
|
| 943 |
let x = lm[0] * scaleX;
|
| 944 |
let y = lm[1] * scaleY;
|
| 945 |
+
|
| 946 |
if (mirror) x = width - x;
|
| 947 |
+
|
| 948 |
const isMajor = [11, 12, 13, 14, 15, 16, 23, 24, 25, 26, 27, 28].includes(index);
|
| 949 |
const radius = isMajor ? 5 : 3;
|
| 950 |
+
|
| 951 |
ctx.beginPath();
|
| 952 |
ctx.arc(x, y, radius, 0, 2 * Math.PI);
|
| 953 |
ctx.fillStyle = color;
|
|
|
|
| 955 |
});
|
| 956 |
}
|
| 957 |
|
| 958 |
+
// βββ Send Frame to Server βββββββββββββββββββββββββββββββββ
|
| 959 |
async function sendFrameToServer() {
|
| 960 |
if (!STATE.cameraStream || STATE.isAnalysing || !STATE.selectedPose) return;
|
| 961 |
STATE.isAnalysing = true;
|
| 962 |
+
|
| 963 |
const video = el('webcam-video');
|
| 964 |
const canvas = el('webcam-canvas');
|
| 965 |
if (!video || !canvas) {
|
| 966 |
STATE.isAnalysing = false;
|
| 967 |
return;
|
| 968 |
}
|
| 969 |
+
|
| 970 |
const ctx = canvas.getContext('2d');
|
| 971 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 972 |
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
| 973 |
+
|
| 974 |
const b64 = canvas.toDataURL('image/jpeg', CONFIG.SNAPSHOT_QUALITY);
|
| 975 |
+
|
| 976 |
try {
|
| 977 |
const data = await apiRequest('/api/live', {
|
| 978 |
method: 'POST',
|
| 979 |
headers: { 'Content-Type': 'application/json' },
|
| 980 |
body: JSON.stringify({ image: b64, pose_id: STATE.selectedPose.id }),
|
| 981 |
});
|
| 982 |
+
|
| 983 |
if (data.error) {
|
| 984 |
console.warn('[Live]', data.error);
|
| 985 |
STATE.isAnalysing = false;
|
| 986 |
return;
|
| 987 |
}
|
| 988 |
+
|
| 989 |
if (data.landmarks && data.landmarks.length > 0) {
|
| 990 |
STATE.currentLandmarks = data.landmarks;
|
| 991 |
}
|
| 992 |
+
|
| 993 |
if (data.score !== undefined) {
|
| 994 |
STATE.currentScore = data.score;
|
| 995 |
STATE.currentStatus = data.status || '';
|
| 996 |
STATE.currentSuggestions = data.suggestions || [];
|
| 997 |
+
|
| 998 |
STATE.sessionScores.push(STATE.currentScore);
|
| 999 |
STATE.scoreHistory.push(STATE.currentScore);
|
| 1000 |
if (STATE.scoreHistory.length > CONFIG.SCORE_HISTORY_LIMIT) {
|
| 1001 |
STATE.scoreHistory.shift();
|
| 1002 |
}
|
| 1003 |
+
|
| 1004 |
const smoothedScore = Math.round(
|
| 1005 |
STATE.scoreHistory.reduce((a, b) => a + b, 0) / STATE.scoreHistory.length
|
| 1006 |
);
|
| 1007 |
+
|
| 1008 |
updateLiveUI(smoothedScore, STATE.currentStatus, STATE.currentSuggestions);
|
| 1009 |
+
|
| 1010 |
if (STATE.isFullscreen) {
|
| 1011 |
updateFullscreenSuggestions(STATE.currentSuggestions, smoothedScore);
|
| 1012 |
}
|
|
|
|
| 1018 |
}
|
| 1019 |
}
|
| 1020 |
|
| 1021 |
+
// βββ Update Live UI βββββββββββββββββββββββββββββββββββββββ
|
| 1022 |
function updateLiveUI(score, status, suggestions) {
|
| 1023 |
const overlayScore = el('overlay-score');
|
| 1024 |
const overlayStatus = el('overlay-status');
|
| 1025 |
+
|
| 1026 |
let colour;
|
| 1027 |
if (score >= 80) colour = '#22C55E';
|
| 1028 |
else if (score >= 65) colour = '#F59E0B';
|
| 1029 |
else colour = '#EF4444';
|
| 1030 |
+
|
| 1031 |
if (overlayScore) {
|
| 1032 |
overlayScore.textContent = `${score}%`;
|
| 1033 |
overlayScore.style.color = colour;
|
|
|
|
| 1036 |
overlayStatus.textContent = status || 'β';
|
| 1037 |
overlayStatus.style.color = colour;
|
| 1038 |
}
|
| 1039 |
+
|
| 1040 |
const badge = el('result-badge');
|
| 1041 |
const scoreVal = el('score-value');
|
| 1042 |
const barFill = el('score-bar-fill');
|
| 1043 |
const suggList = el('suggestions-list');
|
| 1044 |
+
|
| 1045 |
if (scoreVal) {
|
| 1046 |
scoreVal.textContent = `${score}%`;
|
| 1047 |
scoreVal.style.color = colour;
|
|
|
|
| 1050 |
barFill.style.width = `${score}%`;
|
| 1051 |
barFill.style.background = colour;
|
| 1052 |
}
|
| 1053 |
+
|
| 1054 |
let level, badgeClass;
|
| 1055 |
if (score >= 80) {
|
| 1056 |
level = 'Excellent';
|
|
|
|
| 1062 |
level = 'Needs Correction';
|
| 1063 |
badgeClass = 'badge-correction';
|
| 1064 |
}
|
| 1065 |
+
|
| 1066 |
if (badge) {
|
| 1067 |
badge.textContent = level;
|
| 1068 |
badge.className = `result-badge ${badgeClass}`;
|
| 1069 |
}
|
| 1070 |
+
|
| 1071 |
let displaySugg = suggestions;
|
| 1072 |
if (!displaySugg || displaySugg.length === 0) {
|
| 1073 |
if (score >= 80) displaySugg = ['π Excellent posture!', 'Keep breathing deeply'];
|
| 1074 |
else if (score >= 65) displaySugg = ['Almost there!', 'Small adjustments needed'];
|
| 1075 |
else displaySugg = ['Focus on alignment', 'Practice this pose more'];
|
| 1076 |
}
|
| 1077 |
+
|
| 1078 |
if (suggList) {
|
| 1079 |
suggList.innerHTML = displaySugg.slice(0, 4).map(s => `
|
| 1080 |
<li>
|
|
|
|
| 1083 |
</li>
|
| 1084 |
`).join('');
|
| 1085 |
}
|
| 1086 |
+
|
| 1087 |
updateSuggestionsPanel(displaySugg, score);
|
| 1088 |
}
|
| 1089 |
|
| 1090 |
function updateSuggestionsPanel(suggestions, score) {
|
| 1091 |
const panel = el('suggestions-panel');
|
| 1092 |
if (!panel) return;
|
| 1093 |
+
|
| 1094 |
let colour;
|
| 1095 |
if (score >= 80) colour = '#22C55E';
|
| 1096 |
else if (score >= 65) colour = '#F59E0B';
|
| 1097 |
else colour = '#EF4444';
|
| 1098 |
+
|
| 1099 |
let displaySugg = suggestions;
|
| 1100 |
if (!displaySugg || displaySugg.length === 0) {
|
| 1101 |
if (score >= 80) displaySugg = ['π Excellent posture!', 'Keep it up!'];
|
| 1102 |
else if (score >= 65) displaySugg = ['Almost there!', 'Small adjustments'];
|
| 1103 |
else displaySugg = ['Keep practicing', 'Focus on alignment'];
|
| 1104 |
}
|
| 1105 |
+
|
| 1106 |
panel.innerHTML = `
|
| 1107 |
<div class="suggestions-panel-header" style="border-bottom-color: ${colour};">
|
| 1108 |
<span style="font-weight:700;">π‘ Pose Guidance</span>
|
|
|
|
| 1125 |
function updateFullscreenSuggestions(suggestions, score) {
|
| 1126 |
const container = el('fullscreen-suggestions');
|
| 1127 |
if (!container) return;
|
| 1128 |
+
|
| 1129 |
let colour;
|
| 1130 |
if (score >= 80) colour = '#22C55E';
|
| 1131 |
else if (score >= 65) colour = '#F59E0B';
|
| 1132 |
else colour = '#EF4444';
|
| 1133 |
+
|
| 1134 |
let displaySugg = suggestions;
|
| 1135 |
if (!displaySugg || displaySugg.length === 0) {
|
| 1136 |
if (score >= 80) displaySugg = ['π Excellent posture!', 'Keep it up!'];
|
| 1137 |
else if (score >= 65) displaySugg = ['Almost there!', 'Small adjustments'];
|
| 1138 |
else displaySugg = ['Keep practicing', 'Focus on alignment'];
|
| 1139 |
}
|
| 1140 |
+
|
| 1141 |
container.innerHTML = `
|
| 1142 |
<div class="fs-title">π‘ Pose Guidance</div>
|
| 1143 |
<div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:6px;">
|
|
|
|
| 1153 |
`;
|
| 1154 |
}
|
| 1155 |
|
| 1156 |
+
// βββ Fullscreen ββββββββββββββββββββββββββββββββββββββββββββ
|
| 1157 |
function toggleFullscreen() {
|
| 1158 |
const cameraPanel = document.querySelector('.camera-panel');
|
| 1159 |
if (!cameraPanel) return;
|
| 1160 |
+
|
| 1161 |
if (!document.fullscreenElement) {
|
| 1162 |
cameraPanel.requestFullscreen().catch(err => console.warn('Fullscreen error:', err));
|
| 1163 |
} else {
|
|
|
|
| 1173 |
}
|
| 1174 |
});
|
| 1175 |
|
| 1176 |
+
// βββ Snapshot ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1177 |
function takeSnapshot() {
|
| 1178 |
if (!STATE.cameraStream) {
|
| 1179 |
showToast('Start camera first', 'warning');
|
| 1180 |
return;
|
| 1181 |
}
|
| 1182 |
+
|
| 1183 |
const canvas = el('webcam-canvas');
|
| 1184 |
if (!canvas) return;
|
| 1185 |
+
|
| 1186 |
const dataUrl = canvas.toDataURL('image/png');
|
| 1187 |
const preview = el('snapshot-preview');
|
| 1188 |
const imgEl = el('snapshot-img');
|
| 1189 |
+
|
| 1190 |
if (imgEl) imgEl.src = dataUrl;
|
| 1191 |
if (preview) preview.classList.remove('hidden');
|
| 1192 |
+
|
| 1193 |
if (STATE.selectedPose && STATE.currentScore > 0) {
|
| 1194 |
saveHistory(STATE.selectedPose, STATE.currentScore);
|
| 1195 |
showToast('Snapshot saved πΈ', 'success');
|
|
|
|
| 1198 |
}
|
| 1199 |
}
|
| 1200 |
|
| 1201 |
+
// βββ Save History ββββββββββββββββββββββββββββββββββββββββββ
|
| 1202 |
function saveHistory(pose, score) {
|
| 1203 |
const entry = {
|
| 1204 |
id: Date.now(),
|
|
|
|
| 1210 |
time: new Date().toLocaleTimeString('en-IN', { hour: '2-digit', minute: '2-digit' }),
|
| 1211 |
timestamp: new Date().toISOString(),
|
| 1212 |
};
|
| 1213 |
+
|
| 1214 |
STATE.historyData.unshift(entry);
|
| 1215 |
if (STATE.historyData.length > CONFIG.HISTORY_LIMIT) {
|
| 1216 |
STATE.historyData = STATE.historyData.slice(0, CONFIG.HISTORY_LIMIT);
|
| 1217 |
}
|
| 1218 |
saveUserData();
|
| 1219 |
+
|
| 1220 |
updateStreak();
|
| 1221 |
updatePracticeCount();
|
| 1222 |
checkAchievements(score);
|
|
|
|
| 1227 |
function updateStreak() {
|
| 1228 |
const today = new Date().toDateString();
|
| 1229 |
const lastPractice = STATE.lastPracticeDate;
|
| 1230 |
+
|
| 1231 |
if (lastPractice === today) return;
|
| 1232 |
+
|
| 1233 |
const yesterday = new Date();
|
| 1234 |
yesterday.setDate(yesterday.getDate() - 1);
|
| 1235 |
const yesterdayStr = yesterday.toDateString();
|
| 1236 |
+
|
| 1237 |
if (lastPractice === yesterdayStr) {
|
| 1238 |
STATE.streak += 1;
|
| 1239 |
} else {
|
| 1240 |
STATE.streak = 1;
|
| 1241 |
}
|
| 1242 |
+
|
| 1243 |
STATE.lastPracticeDate = today;
|
| 1244 |
saveUserData();
|
| 1245 |
}
|
|
|
|
| 1252 |
function showSessionSummary() {
|
| 1253 |
const scores = STATE.sessionScores;
|
| 1254 |
if (scores.length === 0) return;
|
| 1255 |
+
|
| 1256 |
const avg = Math.round(scores.reduce((a, b) => a + b, 0) / scores.length);
|
| 1257 |
const max = Math.max(...scores);
|
| 1258 |
const duration = Math.round((Date.now() - STATE.sessionStartTime) / 1000);
|
| 1259 |
const minutes = Math.floor(duration / 60);
|
| 1260 |
const seconds = duration % 60;
|
| 1261 |
const calories = Math.round((duration / 60) * 3.5);
|
| 1262 |
+
|
| 1263 |
showToast(
|
| 1264 |
`Session Complete! π Avg: ${avg}% | Best: ${max}% | Duration: ${minutes}m ${seconds}s | ${calories} kcal`,
|
| 1265 |
'success',
|
| 1266 |
5000
|
| 1267 |
);
|
| 1268 |
+
|
| 1269 |
const session = {
|
| 1270 |
date: new Date().toISOString(),
|
| 1271 |
avg,
|
|
|
|
| 1274 |
calories,
|
| 1275 |
pose: STATE.selectedPose?.name || 'Unknown',
|
| 1276 |
};
|
| 1277 |
+
|
| 1278 |
const sessions = JSON.parse(getUserLocalStorage('sessions') || '[]');
|
| 1279 |
sessions.unshift(session);
|
| 1280 |
if (sessions.length > 20) sessions.pop();
|
|
|
|
| 1283 |
|
| 1284 |
function checkAchievements(score) {
|
| 1285 |
const achievements = [];
|
| 1286 |
+
|
| 1287 |
if (STATE.practiceCount === 1) {
|
| 1288 |
achievements.push({ id: 'first-practice', name: 'First Practice π§', icon: 'π' });
|
| 1289 |
}
|
|
|
|
| 1302 |
if (STATE.practiceCount === 100) {
|
| 1303 |
achievements.push({ id: 'hundred-sessions', name: '100 Sessions π
', icon: 'π
' });
|
| 1304 |
}
|
| 1305 |
+
|
| 1306 |
const existing = STATE.achievements;
|
| 1307 |
const newAchievements = achievements.filter(a => !existing.some(e => e.id === a.id));
|
| 1308 |
+
|
| 1309 |
if (newAchievements.length > 0) {
|
| 1310 |
STATE.achievements = [...existing, ...newAchievements];
|
| 1311 |
saveUserData();
|
| 1312 |
+
|
| 1313 |
newAchievements.forEach(a => {
|
| 1314 |
showToast(`π Achievement Unlocked: ${a.name}`, 'success', 4000);
|
| 1315 |
});
|
| 1316 |
}
|
| 1317 |
}
|
| 1318 |
|
| 1319 |
+
// βββ Render History ββββββββββββββββββββββββββββββββββββββββ
|
| 1320 |
function renderHistory() {
|
| 1321 |
const container = el('history-list');
|
| 1322 |
if (!container) return;
|
| 1323 |
+
|
| 1324 |
if (!STATE.historyData.length) {
|
| 1325 |
container.innerHTML = `
|
| 1326 |
<div class="history-empty">
|
|
|
|
| 1331 |
`;
|
| 1332 |
return;
|
| 1333 |
}
|
| 1334 |
+
|
| 1335 |
container.innerHTML = STATE.historyData.map(entry => `
|
| 1336 |
<div class="history-item">
|
| 1337 |
<div class="history-emoji">${entry.emoji || 'π§'}</div>
|
|
|
|
| 1359 |
if (badge) badge.textContent = STATE.historyData.length;
|
| 1360 |
}
|
| 1361 |
|
| 1362 |
+
// βββ Render Progress ββββββββββββββββββββββββββββββββββββ
|
| 1363 |
function renderProgress() {
|
| 1364 |
const statsEl = el('progress-stats');
|
| 1365 |
const chartEl = el('chart-area');
|
| 1366 |
if (!statsEl || !chartEl) return;
|
| 1367 |
+
|
| 1368 |
if (!STATE.historyData.length) {
|
| 1369 |
statsEl.innerHTML = `
|
| 1370 |
<div class="history-empty" style="grid-column:1/-1;">
|
|
|
|
| 1376 |
chartEl.innerHTML = '';
|
| 1377 |
return;
|
| 1378 |
}
|
| 1379 |
+
|
| 1380 |
const scores = STATE.historyData.map(h => h.score);
|
| 1381 |
const avg = Math.round(scores.reduce((a, b) => a + b, 0) / scores.length);
|
| 1382 |
const best = Math.max(...scores);
|
| 1383 |
const total = STATE.historyData.length;
|
| 1384 |
+
|
| 1385 |
const sessions = JSON.parse(getUserLocalStorage('sessions') || '[]');
|
| 1386 |
const totalDuration = sessions.reduce((sum, s) => sum + (s.duration || 0), 0);
|
| 1387 |
const hours = Math.floor(totalDuration / 3600);
|
| 1388 |
const minutes = Math.floor((totalDuration % 3600) / 60);
|
| 1389 |
const totalCalories = sessions.reduce((sum, s) => sum + (s.calories || 0), 0);
|
| 1390 |
+
|
| 1391 |
statsEl.innerHTML = `
|
| 1392 |
+
<div class="stat-card" title="Total practice sessions">
|
| 1393 |
<div class="stat-label">Total Sessions</div>
|
| 1394 |
<div class="stat-value">${total}</div>
|
| 1395 |
</div>
|
| 1396 |
+
<div class="stat-card" title="Average accuracy over all sessions">
|
| 1397 |
<div class="stat-label">Average Score</div>
|
| 1398 |
<div class="stat-value">${avg}<span class="stat-unit">%</span></div>
|
| 1399 |
</div>
|
| 1400 |
+
<div class="stat-card" title="Your best single practice">
|
| 1401 |
<div class="stat-label">Best Score</div>
|
| 1402 |
<div class="stat-value">${best}<span class="stat-unit">%</span></div>
|
| 1403 |
</div>
|
| 1404 |
+
<div class="stat-card" title="Current consecutive days with practice">
|
| 1405 |
<div class="stat-label">Day Streak</div>
|
| 1406 |
<div class="stat-value">${STATE.streak}<span class="stat-unit">d</span></div>
|
| 1407 |
</div>
|
| 1408 |
+
<div class="stat-card" title="Total time spent practicing">
|
| 1409 |
<div class="stat-label">Total Time</div>
|
| 1410 |
<div class="stat-value">${hours}h ${minutes}m</div>
|
| 1411 |
</div>
|
| 1412 |
+
<div class="stat-card" title="Estimated calories burned">
|
| 1413 |
<div class="stat-label">Calories Burned</div>
|
| 1414 |
<div class="stat-value">${totalCalories}<span class="stat-unit">kcal</span></div>
|
| 1415 |
</div>
|
| 1416 |
`;
|
| 1417 |
+
|
| 1418 |
+
// Improved bar chart with labels
|
| 1419 |
const recent = STATE.historyData.slice(0, 15).reverse();
|
| 1420 |
chartEl.innerHTML = recent.map((entry, i) => {
|
| 1421 |
const h = Math.round((entry.score / 100) * 140);
|
| 1422 |
+
const color = entry.score >= 80 ? '#22C55E' : entry.score >= 65 ? '#F59E0B' : '#EF4444';
|
| 1423 |
return `
|
| 1424 |
+
<div class="chart-bar-wrap" title="${entry.pose} β ${entry.score}%">
|
| 1425 |
+
<div class="chart-bar" style="height:${h}px;background:${color};">
|
| 1426 |
+
<span class="chart-bar-tooltip">${entry.score}%</span>
|
| 1427 |
+
</div>
|
| 1428 |
+
<div class="chart-bar-label">${i+1}</div>
|
| 1429 |
</div>
|
| 1430 |
`;
|
| 1431 |
}).join('');
|
| 1432 |
}
|
| 1433 |
|
| 1434 |
+
// βββ Custom Pose Generation ββββββββββββββββββββββββββββββ
|
| 1435 |
function handlePoseImageUpload(event) {
|
| 1436 |
const files = event.target.files;
|
| 1437 |
if (!files || files.length === 0) return;
|
| 1438 |
+
|
| 1439 |
if (STATE.uploadedImages.length + files.length > CONFIG.REQUIRED_IMAGES) {
|
| 1440 |
showToast(`Please upload exactly ${CONFIG.REQUIRED_IMAGES} images`, 'warning');
|
| 1441 |
event.target.value = '';
|
| 1442 |
return;
|
| 1443 |
}
|
| 1444 |
+
|
| 1445 |
for (const file of files) {
|
| 1446 |
if (STATE.uploadedImages.length >= CONFIG.REQUIRED_IMAGES) break;
|
| 1447 |
+
|
| 1448 |
const reader = new FileReader();
|
| 1449 |
reader.onload = function(e) {
|
| 1450 |
STATE.uploadedImages.push({
|
|
|
|
| 1452 |
name: file.name,
|
| 1453 |
type: file.type,
|
| 1454 |
});
|
| 1455 |
+
|
| 1456 |
renderUploadedImages();
|
| 1457 |
+
|
| 1458 |
if (STATE.uploadedImages.length === CONFIG.REQUIRED_IMAGES) {
|
| 1459 |
showToast('All 5 images uploaded! Ready to generate pose π', 'success');
|
| 1460 |
const btn = el('generate-pose-btn');
|
|
|
|
| 1463 |
};
|
| 1464 |
reader.readAsDataURL(file);
|
| 1465 |
}
|
| 1466 |
+
|
| 1467 |
event.target.value = '';
|
| 1468 |
}
|
| 1469 |
|
| 1470 |
function renderUploadedImages() {
|
| 1471 |
const container = el('uploaded-images-container');
|
| 1472 |
if (!container) return;
|
| 1473 |
+
|
| 1474 |
if (STATE.uploadedImages.length === 0) {
|
| 1475 |
container.innerHTML = `
|
| 1476 |
<div class="upload-placeholder" style="grid-column:1/-1;text-align:center;padding:20px;color:var(--text-muted);font-size:13px;">
|
|
|
|
| 1479 |
`;
|
| 1480 |
return;
|
| 1481 |
}
|
| 1482 |
+
|
| 1483 |
container.innerHTML = STATE.uploadedImages.map((img, index) => `
|
| 1484 |
+
<div class="uploaded-image-card" data-index="${index}" onclick="openLightbox(this.querySelector('img').src)">
|
| 1485 |
<img src="${img.data}" alt="Upload ${index + 1}">
|
| 1486 |
<div class="uploaded-image-number">${index + 1}</div>
|
| 1487 |
+
<button class="remove-image-btn" onclick="event.stopPropagation();removeUploadedImage(${index})">β</button>
|
| 1488 |
</div>
|
| 1489 |
`).join('');
|
| 1490 |
+
|
| 1491 |
+
// Update progress
|
| 1492 |
const progress = el('upload-progress');
|
| 1493 |
if (progress) {
|
| 1494 |
progress.textContent = `${STATE.uploadedImages.length}/${CONFIG.REQUIRED_IMAGES} uploaded`;
|
|
|
|
| 1504 |
function removeUploadedImage(index) {
|
| 1505 |
STATE.uploadedImages.splice(index, 1);
|
| 1506 |
renderUploadedImages();
|
| 1507 |
+
|
| 1508 |
const btn = el('generate-pose-btn');
|
| 1509 |
if (btn) {
|
| 1510 |
btn.disabled = STATE.uploadedImages.length < CONFIG.REQUIRED_IMAGES;
|
| 1511 |
}
|
| 1512 |
}
|
| 1513 |
|
| 1514 |
+
// βββ generateCustomPose ββββββββββββββββββββββββββββββββββ
|
| 1515 |
async function generateCustomPose() {
|
| 1516 |
if (STATE.uploadedImages.length !== CONFIG.REQUIRED_IMAGES) {
|
| 1517 |
showToast(`Please upload exactly ${CONFIG.REQUIRED_IMAGES} images`, 'warning');
|
| 1518 |
return;
|
| 1519 |
}
|
| 1520 |
+
|
| 1521 |
if (STATE.isGenerating) return;
|
| 1522 |
STATE.isGenerating = true;
|
| 1523 |
+
|
| 1524 |
const progress = el('generation-progress');
|
| 1525 |
const progressBar = el('generation-progress-bar');
|
| 1526 |
const progressText = el('generation-progress-text');
|
| 1527 |
const progressPct = el('generation-progress-pct');
|
| 1528 |
const btn = el('generate-pose-btn');
|
| 1529 |
+
|
| 1530 |
if (progress) progress.classList.remove('hidden');
|
| 1531 |
if (btn) {
|
| 1532 |
btn.textContent = 'β³ Generating...';
|
| 1533 |
btn.disabled = true;
|
| 1534 |
}
|
| 1535 |
+
|
| 1536 |
const name = el('asana-name')?.value.trim();
|
| 1537 |
if (!name) {
|
| 1538 |
showToast('Please enter a pose name', 'error');
|
|
|
|
| 1544 |
if (progress) progress.classList.add('hidden');
|
| 1545 |
return;
|
| 1546 |
}
|
| 1547 |
+
|
| 1548 |
const category = el('asana-category')?.value || 'General';
|
| 1549 |
const difficulty = document.querySelector('.difficulty-pills .pill.active')?.dataset.diff || 'Beginner';
|
| 1550 |
+
|
| 1551 |
if (progressText) progressText.textContent = 'Uploading images...';
|
| 1552 |
if (progressPct) progressPct.textContent = '20%';
|
| 1553 |
if (progressBar) progressBar.style.width = '20%';
|
| 1554 |
+
|
| 1555 |
try {
|
| 1556 |
const formData = new FormData();
|
| 1557 |
formData.append('name', name);
|
| 1558 |
formData.append('category', category);
|
| 1559 |
formData.append('difficulty', difficulty);
|
| 1560 |
+
|
| 1561 |
for (let i = 0; i < STATE.uploadedImages.length; i++) {
|
| 1562 |
const imgData = STATE.uploadedImages[i];
|
| 1563 |
const response = await fetch(imgData.data);
|
|
|
|
| 1565 |
const ext = imgData.name.split('.').pop() || 'jpg';
|
| 1566 |
formData.append(`image_${i}`, blob, `pose_${i+1}.${ext}`);
|
| 1567 |
}
|
| 1568 |
+
|
| 1569 |
if (progressText) progressText.textContent = 'Generating pose reference...';
|
| 1570 |
if (progressPct) progressPct.textContent = '50%';
|
| 1571 |
if (progressBar) progressBar.style.width = '50%';
|
| 1572 |
+
|
| 1573 |
const response = await fetch(`${CONFIG.API_BASE}/api/generate-pose`, {
|
| 1574 |
method: 'POST',
|
| 1575 |
body: formData,
|
| 1576 |
});
|
| 1577 |
const result = await response.json();
|
| 1578 |
+
|
| 1579 |
if (!response.ok) {
|
| 1580 |
throw new Error(result.error || 'Generation failed');
|
| 1581 |
}
|
| 1582 |
+
|
| 1583 |
if (result.error) {
|
| 1584 |
throw new Error(result.error);
|
| 1585 |
}
|
| 1586 |
+
|
| 1587 |
if (progressText) progressText.textContent = 'Processing results...';
|
| 1588 |
if (progressPct) progressPct.textContent = '80%';
|
| 1589 |
if (progressBar) progressBar.style.width = '80%';
|
| 1590 |
+
|
| 1591 |
STATE.generatedPose = result;
|
| 1592 |
showGenerationResults(result);
|
| 1593 |
+
|
| 1594 |
if (progressText) progressText.textContent = 'β
Generation complete!';
|
| 1595 |
if (progressPct) progressPct.textContent = '100%';
|
| 1596 |
if (progressBar) progressBar.style.width = '100%';
|
| 1597 |
+
|
| 1598 |
showToast(`Pose "${name}" generated successfully! π`, 'success');
|
| 1599 |
+
|
| 1600 |
} catch (error) {
|
| 1601 |
console.error('[GeneratePose]', error);
|
| 1602 |
showToast(`β Generation failed: ${error.message}`, 'error', 5000);
|
| 1603 |
+
|
| 1604 |
if (progressText) progressText.textContent = 'β Generation failed';
|
| 1605 |
if (progressPct) progressPct.textContent = 'Error';
|
| 1606 |
if (progressBar) {
|
|
|
|
| 1613 |
btn.textContent = 'β¨ Generate Pose Reference';
|
| 1614 |
btn.disabled = false;
|
| 1615 |
}
|
| 1616 |
+
|
| 1617 |
setTimeout(() => {
|
| 1618 |
if (progress) progress.classList.add('hidden');
|
| 1619 |
if (progressBar) {
|
|
|
|
| 1625 |
}
|
| 1626 |
}
|
| 1627 |
|
| 1628 |
+
// βββ Floating Save Button βββββββββββββββββββββββββββββββββ
|
| 1629 |
function createFloatingSaveButton() {
|
| 1630 |
removeFloatingSaveButton();
|
| 1631 |
const btn = document.createElement('button');
|
|
|
|
| 1664 |
}
|
| 1665 |
|
| 1666 |
function showGenerationResults(result) {
|
|
|
|
| 1667 |
const container = el('generation-results');
|
| 1668 |
const preview = el('generated-pose-preview');
|
| 1669 |
+
if (!container || !preview) return;
|
|
|
|
|
|
|
|
|
|
| 1670 |
|
| 1671 |
container.classList.remove('hidden');
|
| 1672 |
|
|
|
|
| 1693 |
</div>
|
| 1694 |
</div>
|
| 1695 |
<div style="display:flex;gap:8px;">
|
| 1696 |
+
<button data-action="save-pose" onclick="saveGeneratedPose()" class="btn-primary" style="font-size:13px;padding:10px 28px;">
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1697 |
πΎ Save Pose
|
| 1698 |
</button>
|
| 1699 |
+
<button data-action="discard-pose" onclick="discardGeneratedPose()" class="btn-outline" style="font-size:13px;padding:10px 20px;">
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1700 |
Discard
|
| 1701 |
</button>
|
| 1702 |
</div>
|
|
|
|
| 1713 |
</div>
|
| 1714 |
`;
|
| 1715 |
|
|
|
|
| 1716 |
preview.removeEventListener('click', handlePreviewClick);
|
| 1717 |
preview.addEventListener('click', handlePreviewClick);
|
| 1718 |
|
|
|
|
| 1719 |
setTimeout(() => {
|
| 1720 |
container.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
| 1721 |
}, 100);
|
| 1722 |
|
|
|
|
| 1723 |
createFloatingSaveButton();
|
|
|
|
| 1724 |
}
|
| 1725 |
|
| 1726 |
function handlePreviewClick(e) {
|
|
|
|
| 1734 |
}
|
| 1735 |
}
|
| 1736 |
|
| 1737 |
+
// βββ saveGeneratedPose with image_paths ββββββββββββββββββββ
|
| 1738 |
async function saveGeneratedPose() {
|
|
|
|
|
|
|
| 1739 |
if (!STATE.generatedPose) {
|
| 1740 |
showToast('No pose to save', 'error');
|
|
|
|
| 1741 |
return;
|
| 1742 |
}
|
| 1743 |
|
|
|
|
|
|
|
| 1744 |
const saveBtns = document.querySelectorAll('[data-action="save-pose"], #floating-save-btn');
|
| 1745 |
saveBtns.forEach(btn => {
|
| 1746 |
btn.disabled = true;
|
|
|
|
| 1758 |
pose_id: STATE.generatedPose.pose_id,
|
| 1759 |
reference: STATE.generatedPose.reference,
|
| 1760 |
name: STATE.generatedPose.pose_name,
|
| 1761 |
+
images: STATE.generatedPose.image_paths || []
|
| 1762 |
}),
|
| 1763 |
});
|
| 1764 |
|
| 1765 |
const result = await response.json();
|
|
|
|
| 1766 |
|
| 1767 |
if (!response.ok) {
|
| 1768 |
throw new Error(result.error || 'Save failed');
|
|
|
|
| 1771 |
if (result.success) {
|
| 1772 |
showToast(`β
Pose "${STATE.generatedPose.pose_name}" saved!`, 'success', 4000);
|
| 1773 |
removeFloatingSaveButton();
|
| 1774 |
+
STATE.generatedPose = null;
|
| 1775 |
await fetchPoses();
|
| 1776 |
goHome();
|
| 1777 |
const searchInput = el('home-search-input');
|
|
|
|
| 1805 |
showToast('Generation discarded', 'info');
|
| 1806 |
}
|
| 1807 |
|
| 1808 |
+
// βββ Image Lightbox ββββββββββββββββββββββββββββββββββββββββββ
|
| 1809 |
+
function openLightbox(imageSrc) {
|
| 1810 |
+
const lightbox = document.getElementById('image-lightbox');
|
| 1811 |
+
const img = document.getElementById('lightbox-img');
|
| 1812 |
+
if (!lightbox || !img || !imageSrc) return;
|
| 1813 |
+
img.src = imageSrc;
|
| 1814 |
+
lightbox.classList.remove('hidden');
|
| 1815 |
+
document.body.style.overflow = 'hidden';
|
| 1816 |
+
}
|
| 1817 |
+
|
| 1818 |
+
function closeLightbox() {
|
| 1819 |
+
const lightbox = document.getElementById('image-lightbox');
|
| 1820 |
+
if (!lightbox) return;
|
| 1821 |
+
lightbox.classList.add('hidden');
|
| 1822 |
+
document.body.style.overflow = '';
|
| 1823 |
+
}
|
| 1824 |
+
|
| 1825 |
+
// Close lightbox on Escape key
|
| 1826 |
+
document.addEventListener('keydown', (e) => {
|
| 1827 |
+
if (e.key === 'Escape') closeLightbox();
|
| 1828 |
+
});
|
| 1829 |
+
|
| 1830 |
+
// βββ Reset Camera UI ββββββββββββββββββββββββββββββββββββββ
|
| 1831 |
function resetCameraUI() {
|
| 1832 |
stopCamera();
|
| 1833 |
STATE.scoreHistory = [];
|
| 1834 |
STATE.currentLandmarks = null;
|
| 1835 |
+
|
| 1836 |
el('result-card').classList.add('hidden');
|
| 1837 |
el('score-overlay').classList.add('hidden');
|
| 1838 |
el('live-badge').classList.add('hidden');
|
|
|
|
| 1844 |
el('snapshot-btn').classList.add('hidden');
|
| 1845 |
el('suggestions-panel').classList.add('hidden');
|
| 1846 |
el('fullscreen-suggestions').style.display = 'none';
|
| 1847 |
+
|
| 1848 |
const canvas = el('webcam-canvas');
|
| 1849 |
if (canvas) {
|
| 1850 |
const ctx = canvas.getContext('2d');
|
|
|
|
| 1852 |
}
|
| 1853 |
}
|
| 1854 |
|
| 1855 |
+
// βββ Setup ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1856 |
function setupNav() {
|
| 1857 |
$$('.nav-item[data-view]').forEach(el => {
|
| 1858 |
el.addEventListener('click', (e) => {
|
|
|
|
| 1872 |
if (STATE.isFullscreen) {
|
| 1873 |
document.exitFullscreen();
|
| 1874 |
}
|
| 1875 |
+
closeLightbox();
|
| 1876 |
}
|
| 1877 |
+
|
| 1878 |
if (e.target.tagName !== 'INPUT' && e.target.tagName !== 'TEXTAREA') {
|
| 1879 |
if (e.key === ' ' && STATE.currentView === 'practice') {
|
| 1880 |
e.preventDefault();
|
|
|
|
| 1884 |
startCamera();
|
| 1885 |
}
|
| 1886 |
}
|
| 1887 |
+
if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.key === 'A') {
|
| 1888 |
+
e.preventDefault();
|
| 1889 |
+
openAddPose();
|
| 1890 |
+
}
|
| 1891 |
}
|
| 1892 |
+
|
| 1893 |
if (e.key === 's' && STATE.currentView === 'practice' && STATE.cameraStream) {
|
| 1894 |
e.preventDefault();
|
| 1895 |
takeSnapshot();
|
|
|
|
| 1897 |
});
|
| 1898 |
}
|
| 1899 |
|
| 1900 |
+
// βββ Initialize βββββββββββββββββββββββββββββββββββββββββββββ
|
| 1901 |
document.addEventListener('DOMContentLoaded', () => {
|
| 1902 |
try {
|
|
|
|
| 1903 |
apiRequest('/api/me')
|
| 1904 |
.then(data => {
|
| 1905 |
if (data.user_id) {
|
|
|
|
| 1913 |
.catch(() => {
|
| 1914 |
document.getElementById('login-overlay').style.display = 'flex';
|
| 1915 |
});
|
| 1916 |
+
|
| 1917 |
initAuthUI();
|
| 1918 |
setupSearch();
|
| 1919 |
setupNav();
|
| 1920 |
setupKeyboardShortcuts();
|
| 1921 |
+
|
| 1922 |
console.log('[YogaMate] Initialized successfully π§');
|
| 1923 |
} catch (err) {
|
| 1924 |
console.error('[YogaMate] Startup error:', err);
|
|
|
|
| 1926 |
}
|
| 1927 |
});
|
| 1928 |
|
| 1929 |
+
// βββ Expose Global Functions ββββββββββββββββββββββββββββββ
|
| 1930 |
window.startCamera = startCamera;
|
| 1931 |
window.stopCamera = stopCamera;
|
| 1932 |
window.takeSnapshot = takeSnapshot;
|
|
|
|
| 1949 |
window.toggleFavorite = toggleFavorite;
|
| 1950 |
window.resetAddPoseForm = resetAddPoseForm;
|
| 1951 |
window.performSearch = performSearch;
|
| 1952 |
+
window.updateSearchResults = updateSearchResults;
|
| 1953 |
+
window.openLightbox = openLightbox;
|
| 1954 |
+
window.closeLightbox = closeLightbox;
|