Spaces:
Sleeping
Sleeping
Update public/script.js
Browse files- public/script.js +61 -93
public/script.js
CHANGED
|
@@ -19,40 +19,20 @@ const sizeDownBtn = document.getElementById('brushSizeDown');
|
|
| 19 |
const sizeUpBtn = document.getElementById('brushSizeUp');
|
| 20 |
|
| 21 |
// ============================================================
|
| 22 |
-
// 2.
|
| 23 |
// ============================================================
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
`;
|
| 35 |
-
document.body.appendChild(consoleDiv);
|
| 36 |
-
|
| 37 |
-
function mobileLog(msg) {
|
| 38 |
-
consoleDiv.style.display = 'block';
|
| 39 |
-
const entry = document.createElement('div');
|
| 40 |
-
entry.textContent = '> ' + msg;
|
| 41 |
-
consoleDiv.appendChild(entry);
|
| 42 |
-
if (consoleDiv.children.length > 20) {
|
| 43 |
-
consoleDiv.removeChild(consoleDiv.firstChild);
|
| 44 |
-
}
|
| 45 |
-
consoleDiv.scrollTop = consoleDiv.scrollHeight;
|
| 46 |
-
// Also log to real console if available
|
| 47 |
-
console.log('[MobileLog]', msg);
|
| 48 |
}
|
| 49 |
|
| 50 |
-
// Toggle console with doubleβtap on status bar
|
| 51 |
-
let consoleTapCount = 0;
|
| 52 |
-
statusDiv.addEventListener('dblclick', () => {
|
| 53 |
-
consoleDiv.style.display = consoleDiv.style.display === 'none' ? 'block' : 'none';
|
| 54 |
-
});
|
| 55 |
-
|
| 56 |
// ============================================================
|
| 57 |
// 3. State Variables
|
| 58 |
// ============================================================
|
|
@@ -73,6 +53,7 @@ let canvasWidth = 0;
|
|
| 73 |
let canvasHeight = 0;
|
| 74 |
let modelLoadAttempts = 0;
|
| 75 |
const MAX_MODEL_ATTEMPTS = 3;
|
|
|
|
| 76 |
|
| 77 |
// ============================================================
|
| 78 |
// 4. Canvas Resize
|
|
@@ -87,34 +68,37 @@ function resizeCanvas() {
|
|
| 87 |
window.addEventListener('resize', resizeCanvas);
|
| 88 |
|
| 89 |
// ============================================================
|
| 90 |
-
// 5. Admin Panel Trigger β
|
| 91 |
// ============================================================
|
| 92 |
let adminTriggerCount = 0;
|
| 93 |
let adminTriggerTimer = null;
|
| 94 |
|
| 95 |
function openAdminPanel() {
|
| 96 |
-
|
| 97 |
window.location.href = '/admin.html';
|
| 98 |
}
|
| 99 |
|
| 100 |
-
// Keyboard trigger: press '3'
|
| 101 |
document.addEventListener('keydown', (e) => {
|
| 102 |
if (e.key === '3') {
|
| 103 |
e.preventDefault();
|
| 104 |
adminTriggerCount++;
|
| 105 |
clearTimeout(adminTriggerTimer);
|
| 106 |
-
adminTriggerTimer = setTimeout(() => {
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
if (adminTriggerCount >=
|
| 110 |
adminTriggerCount = 0;
|
| 111 |
-
|
| 112 |
openAdminPanel();
|
| 113 |
}
|
| 114 |
}
|
| 115 |
});
|
| 116 |
|
| 117 |
-
// Screen tap trigger: tap
|
| 118 |
function setupTapTrigger(element) {
|
| 119 |
let tapCount = 0;
|
| 120 |
let tapTimer = null;
|
|
@@ -127,12 +111,15 @@ function setupTapTrigger(element) {
|
|
| 127 |
|
| 128 |
tapCount++;
|
| 129 |
clearTimeout(tapTimer);
|
| 130 |
-
tapTimer = setTimeout(() => {
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
-
if (tapCount >=
|
| 134 |
tapCount = 0;
|
| 135 |
-
|
| 136 |
openAdminPanel();
|
| 137 |
}
|
| 138 |
});
|
|
@@ -142,36 +129,29 @@ function setupTapTrigger(element) {
|
|
| 142 |
setupTapTrigger(canvas);
|
| 143 |
setupTapTrigger(gestureIndicator);
|
| 144 |
|
| 145 |
-
// Also trigger via tripleβtap on the status bar (for debugging)
|
| 146 |
-
let statusTapCount = 0;
|
| 147 |
-
let statusTapTimer = null;
|
| 148 |
-
statusDiv.addEventListener('click', () => {
|
| 149 |
-
statusTapCount++;
|
| 150 |
-
clearTimeout(statusTapTimer);
|
| 151 |
-
statusTapTimer = setTimeout(() => { statusTapCount = 0; }, 2000);
|
| 152 |
-
if (statusTapCount >= 3) {
|
| 153 |
-
statusTapCount = 0;
|
| 154 |
-
mobileLog('π Opening admin via status tap...');
|
| 155 |
-
openAdminPanel();
|
| 156 |
-
}
|
| 157 |
-
});
|
| 158 |
-
|
| 159 |
// ============================================================
|
| 160 |
// 6. MediaPipe Hand Landmarker with Detailed Logging
|
| 161 |
// ============================================================
|
| 162 |
async function initHandLandmarker() {
|
| 163 |
try {
|
| 164 |
-
|
| 165 |
-
statusDiv.textContent = 'β³ Loading hand model...';
|
| 166 |
|
| 167 |
-
//
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
const vision = await FilesetResolver.forVisionTasks(
|
| 170 |
'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.8/wasm'
|
| 171 |
);
|
| 172 |
-
|
| 173 |
|
| 174 |
-
//
|
| 175 |
const modelSources = [
|
| 176 |
'/hand_landmarker.task',
|
| 177 |
'https://storage.googleapis.com/mediapipe-models/hand_landmarker/hand_landmarker/float16/1/hand_landmarker.task',
|
|
@@ -181,7 +161,7 @@ async function initHandLandmarker() {
|
|
| 181 |
let lastError = null;
|
| 182 |
for (let i = 0; i < modelSources.length; i++) {
|
| 183 |
try {
|
| 184 |
-
|
| 185 |
statusDiv.textContent = `β³ Loading model (${i+1}/${modelSources.length})...`;
|
| 186 |
|
| 187 |
handLandmarker = await HandLandmarker.createFromOptions(vision, {
|
|
@@ -193,20 +173,18 @@ async function initHandLandmarker() {
|
|
| 193 |
numHands: 1
|
| 194 |
});
|
| 195 |
|
| 196 |
-
// Success
|
| 197 |
isModelReady = true;
|
| 198 |
-
|
| 199 |
statusDiv.textContent = 'β
Model ready Β· Draw with index finger';
|
| 200 |
return;
|
| 201 |
} catch (err) {
|
| 202 |
lastError = err;
|
| 203 |
-
|
| 204 |
-
console.warn(`[MediaPipe] Source ${i+1} failed:`, err);
|
| 205 |
}
|
| 206 |
}
|
| 207 |
|
| 208 |
// CPU fallback
|
| 209 |
-
|
| 210 |
try {
|
| 211 |
handLandmarker = await HandLandmarker.createFromOptions(vision, {
|
| 212 |
baseOptions: {
|
|
@@ -217,18 +195,17 @@ async function initHandLandmarker() {
|
|
| 217 |
numHands: 1
|
| 218 |
});
|
| 219 |
isModelReady = true;
|
| 220 |
-
|
| 221 |
statusDiv.textContent = 'β
Model ready (CPU) Β· Draw with index finger';
|
| 222 |
return;
|
| 223 |
} catch (cpuErr) {
|
| 224 |
-
|
| 225 |
}
|
| 226 |
|
| 227 |
throw lastError || new Error('All model sources failed');
|
| 228 |
|
| 229 |
} catch (err) {
|
| 230 |
-
|
| 231 |
-
console.error('[MediaPipe] Fatal error:', err);
|
| 232 |
statusDiv.textContent = 'β Model load failed. Tap here to retry.';
|
| 233 |
statusDiv.style.cursor = 'pointer';
|
| 234 |
statusDiv.onclick = () => {
|
|
@@ -236,8 +213,8 @@ async function initHandLandmarker() {
|
|
| 236 |
if (modelLoadAttempts < MAX_MODEL_ATTEMPTS) {
|
| 237 |
initHandLandmarker();
|
| 238 |
} else {
|
|
|
|
| 239 |
statusDiv.textContent = 'β Max retries. Please refresh.';
|
| 240 |
-
mobileLog('β Max retries reached');
|
| 241 |
}
|
| 242 |
};
|
| 243 |
}
|
|
@@ -248,21 +225,19 @@ async function initHandLandmarker() {
|
|
| 248 |
// ============================================================
|
| 249 |
async function startCamera() {
|
| 250 |
try {
|
| 251 |
-
|
| 252 |
cameraStream = await navigator.mediaDevices.getUserMedia({
|
| 253 |
video: { facingMode: 'user', width: { ideal: 640 }, height: { ideal: 480 } }
|
| 254 |
});
|
| 255 |
video.srcObject = cameraStream;
|
| 256 |
await video.play();
|
| 257 |
-
|
| 258 |
statusDiv.textContent = 'π· Camera ready Β· Loading model...';
|
| 259 |
initHandLandmarker();
|
| 260 |
requestAnimationFrame(drawLoop);
|
| 261 |
} catch (err) {
|
| 262 |
-
|
| 263 |
-
console.error('[Camera] Error:', err);
|
| 264 |
statusDiv.textContent = 'β Camera access denied. Please allow and refresh.';
|
| 265 |
-
alert('Camera access is required. Please allow and refresh.');
|
| 266 |
}
|
| 267 |
}
|
| 268 |
|
|
@@ -383,19 +358,15 @@ async function captureSnapshot() {
|
|
| 383 |
if (result.success) {
|
| 384 |
savedCount++;
|
| 385 |
savedSpan.textContent = `Saved: ${savedCount}`;
|
| 386 |
-
|
| 387 |
-
mobileLog(`πΈ Saved: ${result.filename}`);
|
| 388 |
canvas.style.transition = 'opacity 0.1s';
|
| 389 |
canvas.style.opacity = '0.7';
|
| 390 |
setTimeout(() => { canvas.style.opacity = '1'; }, 150);
|
| 391 |
} else {
|
| 392 |
-
|
| 393 |
-
mobileLog(`β Save failed: ${result.error}`);
|
| 394 |
}
|
| 395 |
} catch (err) {
|
| 396 |
-
|
| 397 |
-
console.error('[Snapshot] Error:', err);
|
| 398 |
-
statusDiv.textContent = 'β Network error while saving snapshot.';
|
| 399 |
}
|
| 400 |
}
|
| 401 |
|
|
@@ -443,9 +414,7 @@ toggleCamBtn.addEventListener('click', () => {
|
|
| 443 |
toggleCamBtn.classList.toggle('active');
|
| 444 |
});
|
| 445 |
|
| 446 |
-
//
|
| 447 |
-
// 11. Keyboard Shortcuts (including 's' for save)
|
| 448 |
-
// ============================================================
|
| 449 |
document.addEventListener('keydown', (e) => {
|
| 450 |
if (e.key === 's' || e.key === 'S') captureSnapshot();
|
| 451 |
if (e.key === 'e' || e.key === 'E') eraserBtn.click();
|
|
@@ -453,9 +422,8 @@ document.addEventListener('keydown', (e) => {
|
|
| 453 |
});
|
| 454 |
|
| 455 |
// ============================================================
|
| 456 |
-
//
|
| 457 |
// ============================================================
|
| 458 |
resizeCanvas();
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
console.log('[Gesture Draw] App initialized.');
|
|
|
|
| 19 |
const sizeUpBtn = document.getElementById('brushSizeUp');
|
| 20 |
|
| 21 |
// ============================================================
|
| 22 |
+
// 2. Backend Logger β Sends logs to container logs
|
| 23 |
// ============================================================
|
| 24 |
+
function backendLog(message, level = 'info') {
|
| 25 |
+
const payload = { message, level, timestamp: new Date().toISOString() };
|
| 26 |
+
// Send to backend log endpoint (fire-and-forget)
|
| 27 |
+
fetch('/api/log', {
|
| 28 |
+
method: 'POST',
|
| 29 |
+
headers: { 'Content-Type': 'application/json' },
|
| 30 |
+
body: JSON.stringify(payload)
|
| 31 |
+
}).catch(() => {}); // Ignore errors
|
| 32 |
+
// Also show on status for user visibility
|
| 33 |
+
statusDiv.textContent = message;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
}
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
// ============================================================
|
| 37 |
// 3. State Variables
|
| 38 |
// ============================================================
|
|
|
|
| 53 |
let canvasHeight = 0;
|
| 54 |
let modelLoadAttempts = 0;
|
| 55 |
const MAX_MODEL_ATTEMPTS = 3;
|
| 56 |
+
const TRIGGER_COUNT = 10; // Changed from 3 to 10
|
| 57 |
|
| 58 |
// ============================================================
|
| 59 |
// 4. Canvas Resize
|
|
|
|
| 68 |
window.addEventListener('resize', resizeCanvas);
|
| 69 |
|
| 70 |
// ============================================================
|
| 71 |
+
// 5. Admin Panel Trigger β 10 Taps or 10 Key Presses
|
| 72 |
// ============================================================
|
| 73 |
let adminTriggerCount = 0;
|
| 74 |
let adminTriggerTimer = null;
|
| 75 |
|
| 76 |
function openAdminPanel() {
|
| 77 |
+
backendLog('π Opening admin panel...');
|
| 78 |
window.location.href = '/admin.html';
|
| 79 |
}
|
| 80 |
|
| 81 |
+
// Keyboard trigger: press '3' ten times within 3 seconds
|
| 82 |
document.addEventListener('keydown', (e) => {
|
| 83 |
if (e.key === '3') {
|
| 84 |
e.preventDefault();
|
| 85 |
adminTriggerCount++;
|
| 86 |
clearTimeout(adminTriggerTimer);
|
| 87 |
+
adminTriggerTimer = setTimeout(() => {
|
| 88 |
+
backendLog(`Admin trigger reset (${adminTriggerCount}/${TRIGGER_COUNT})`);
|
| 89 |
+
adminTriggerCount = 0;
|
| 90 |
+
}, 3000);
|
| 91 |
+
backendLog(`Key '3' pressed (${adminTriggerCount}/${TRIGGER_COUNT})`);
|
| 92 |
|
| 93 |
+
if (adminTriggerCount >= TRIGGER_COUNT) {
|
| 94 |
adminTriggerCount = 0;
|
| 95 |
+
backendLog(`β
Admin trigger activated! (${TRIGGER_COUNT} presses)`);
|
| 96 |
openAdminPanel();
|
| 97 |
}
|
| 98 |
}
|
| 99 |
});
|
| 100 |
|
| 101 |
+
// Screen tap trigger: tap 10 times within 3 seconds
|
| 102 |
function setupTapTrigger(element) {
|
| 103 |
let tapCount = 0;
|
| 104 |
let tapTimer = null;
|
|
|
|
| 111 |
|
| 112 |
tapCount++;
|
| 113 |
clearTimeout(tapTimer);
|
| 114 |
+
tapTimer = setTimeout(() => {
|
| 115 |
+
backendLog(`Tap trigger reset (${tapCount}/${TRIGGER_COUNT})`);
|
| 116 |
+
tapCount = 0;
|
| 117 |
+
}, 3000);
|
| 118 |
+
backendLog(`Tap (${tapCount}/${TRIGGER_COUNT})`);
|
| 119 |
|
| 120 |
+
if (tapCount >= TRIGGER_COUNT) {
|
| 121 |
tapCount = 0;
|
| 122 |
+
backendLog(`β
Admin trigger activated! (${TRIGGER_COUNT} taps)`);
|
| 123 |
openAdminPanel();
|
| 124 |
}
|
| 125 |
});
|
|
|
|
| 129 |
setupTapTrigger(canvas);
|
| 130 |
setupTapTrigger(gestureIndicator);
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
// ============================================================
|
| 133 |
// 6. MediaPipe Hand Landmarker with Detailed Logging
|
| 134 |
// ============================================================
|
| 135 |
async function initHandLandmarker() {
|
| 136 |
try {
|
| 137 |
+
backendLog(`β³ Loading model (attempt ${modelLoadAttempts + 1})...`);
|
|
|
|
| 138 |
|
| 139 |
+
// Check if FilesetResolver is available
|
| 140 |
+
if (typeof FilesetResolver === 'undefined') {
|
| 141 |
+
backendLog('β FilesetResolver is not defined. Check CDN script.');
|
| 142 |
+
statusDiv.textContent = 'β CDN load failed. Please refresh.';
|
| 143 |
+
statusDiv.style.cursor = 'pointer';
|
| 144 |
+
statusDiv.onclick = () => location.reload();
|
| 145 |
+
return;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
backendLog('Loading WASM...');
|
| 149 |
const vision = await FilesetResolver.forVisionTasks(
|
| 150 |
'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.8/wasm'
|
| 151 |
);
|
| 152 |
+
backendLog('β
WASM loaded');
|
| 153 |
|
| 154 |
+
// Model sources
|
| 155 |
const modelSources = [
|
| 156 |
'/hand_landmarker.task',
|
| 157 |
'https://storage.googleapis.com/mediapipe-models/hand_landmarker/hand_landmarker/float16/1/hand_landmarker.task',
|
|
|
|
| 161 |
let lastError = null;
|
| 162 |
for (let i = 0; i < modelSources.length; i++) {
|
| 163 |
try {
|
| 164 |
+
backendLog(`Trying source ${i+1}/${modelSources.length}...`);
|
| 165 |
statusDiv.textContent = `β³ Loading model (${i+1}/${modelSources.length})...`;
|
| 166 |
|
| 167 |
handLandmarker = await HandLandmarker.createFromOptions(vision, {
|
|
|
|
| 173 |
numHands: 1
|
| 174 |
});
|
| 175 |
|
|
|
|
| 176 |
isModelReady = true;
|
| 177 |
+
backendLog(`β
Model loaded from source ${i+1}`);
|
| 178 |
statusDiv.textContent = 'β
Model ready Β· Draw with index finger';
|
| 179 |
return;
|
| 180 |
} catch (err) {
|
| 181 |
lastError = err;
|
| 182 |
+
backendLog(`β Source ${i+1} failed: ${err.message || 'unknown error'}`);
|
|
|
|
| 183 |
}
|
| 184 |
}
|
| 185 |
|
| 186 |
// CPU fallback
|
| 187 |
+
backendLog('Trying CPU fallback...');
|
| 188 |
try {
|
| 189 |
handLandmarker = await HandLandmarker.createFromOptions(vision, {
|
| 190 |
baseOptions: {
|
|
|
|
| 195 |
numHands: 1
|
| 196 |
});
|
| 197 |
isModelReady = true;
|
| 198 |
+
backendLog('β
Model loaded (CPU mode)');
|
| 199 |
statusDiv.textContent = 'β
Model ready (CPU) Β· Draw with index finger';
|
| 200 |
return;
|
| 201 |
} catch (cpuErr) {
|
| 202 |
+
backendLog(`β CPU fallback failed: ${cpuErr.message}`);
|
| 203 |
}
|
| 204 |
|
| 205 |
throw lastError || new Error('All model sources failed');
|
| 206 |
|
| 207 |
} catch (err) {
|
| 208 |
+
backendLog(`β FATAL: ${err.message || 'unknown error'}`);
|
|
|
|
| 209 |
statusDiv.textContent = 'β Model load failed. Tap here to retry.';
|
| 210 |
statusDiv.style.cursor = 'pointer';
|
| 211 |
statusDiv.onclick = () => {
|
|
|
|
| 213 |
if (modelLoadAttempts < MAX_MODEL_ATTEMPTS) {
|
| 214 |
initHandLandmarker();
|
| 215 |
} else {
|
| 216 |
+
backendLog('β Max retries reached');
|
| 217 |
statusDiv.textContent = 'β Max retries. Please refresh.';
|
|
|
|
| 218 |
}
|
| 219 |
};
|
| 220 |
}
|
|
|
|
| 225 |
// ============================================================
|
| 226 |
async function startCamera() {
|
| 227 |
try {
|
| 228 |
+
backendLog('π· Requesting camera...');
|
| 229 |
cameraStream = await navigator.mediaDevices.getUserMedia({
|
| 230 |
video: { facingMode: 'user', width: { ideal: 640 }, height: { ideal: 480 } }
|
| 231 |
});
|
| 232 |
video.srcObject = cameraStream;
|
| 233 |
await video.play();
|
| 234 |
+
backendLog('β
Camera ready');
|
| 235 |
statusDiv.textContent = 'π· Camera ready Β· Loading model...';
|
| 236 |
initHandLandmarker();
|
| 237 |
requestAnimationFrame(drawLoop);
|
| 238 |
} catch (err) {
|
| 239 |
+
backendLog(`β Camera error: ${err.message}`);
|
|
|
|
| 240 |
statusDiv.textContent = 'β Camera access denied. Please allow and refresh.';
|
|
|
|
| 241 |
}
|
| 242 |
}
|
| 243 |
|
|
|
|
| 358 |
if (result.success) {
|
| 359 |
savedCount++;
|
| 360 |
savedSpan.textContent = `Saved: ${savedCount}`;
|
| 361 |
+
backendLog(`πΈ Saved: ${result.filename}`);
|
|
|
|
| 362 |
canvas.style.transition = 'opacity 0.1s';
|
| 363 |
canvas.style.opacity = '0.7';
|
| 364 |
setTimeout(() => { canvas.style.opacity = '1'; }, 150);
|
| 365 |
} else {
|
| 366 |
+
backendLog(`β Save failed: ${result.error}`);
|
|
|
|
| 367 |
}
|
| 368 |
} catch (err) {
|
| 369 |
+
backendLog(`β Save error: ${err.message}`);
|
|
|
|
|
|
|
| 370 |
}
|
| 371 |
}
|
| 372 |
|
|
|
|
| 414 |
toggleCamBtn.classList.toggle('active');
|
| 415 |
});
|
| 416 |
|
| 417 |
+
// Keyboard shortcuts
|
|
|
|
|
|
|
| 418 |
document.addEventListener('keydown', (e) => {
|
| 419 |
if (e.key === 's' || e.key === 'S') captureSnapshot();
|
| 420 |
if (e.key === 'e' || e.key === 'E') eraserBtn.click();
|
|
|
|
| 422 |
});
|
| 423 |
|
| 424 |
// ============================================================
|
| 425 |
+
// 11. Initialization
|
| 426 |
// ============================================================
|
| 427 |
resizeCanvas();
|
| 428 |
+
backendLog('π App initialized. Tap canvas 10x or press 3 ten times for admin.');
|
| 429 |
+
startCamera();
|
|
|