Spaces:
Sleeping
Sleeping
Elliot Sones
commited on
Commit
·
8a44149
1
Parent(s):
45278ff
Fix: Improve JS canvas initialization and event handling
Browse files
app.py
CHANGED
|
@@ -233,113 +233,116 @@ def predict(strokes_json):
|
|
| 233 |
# ============================================================================
|
| 234 |
|
| 235 |
CANVAS_HTML = """
|
| 236 |
-
<div id="canvas-container" style="display: flex; flex-direction: column; align-items: center;">
|
| 237 |
<canvas id="drawing-canvas" width="400" height="400"
|
| 238 |
-
style="border: 2px solid #333; border-radius: 8px; background: white; cursor: crosshair;"></canvas>
|
| 239 |
<div style="margin-top: 10px;">
|
| 240 |
-
<button onclick="clearCanvas()" style="padding: 8px 16px; margin-right: 10px; cursor: pointer;">Clear</button>
|
| 241 |
<button onclick="sendStrokes()" style="padding: 8px 16px; background: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer;">Predict</button>
|
| 242 |
</div>
|
| 243 |
<p style="color: #666; font-size: 12px; margin-top: 5px;">Draw an animal, then click Predict</p>
|
| 244 |
</div>
|
| 245 |
|
| 246 |
<script>
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
ctx
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
canvas.
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
}
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
| 343 |
</script>
|
| 344 |
"""
|
| 345 |
|
|
|
|
| 233 |
# ============================================================================
|
| 234 |
|
| 235 |
CANVAS_HTML = """
|
| 236 |
+
<div id="canvas-container" style="display: flex; flex-direction: column; align-items: center; position: relative; z-index: 10;">
|
| 237 |
<canvas id="drawing-canvas" width="400" height="400"
|
| 238 |
+
style="border: 2px solid #333; border-radius: 8px; background: white; cursor: crosshair; touch-action: none;"></canvas>
|
| 239 |
<div style="margin-top: 10px;">
|
| 240 |
+
<button onclick="clearCanvas()" style="padding: 8px 16px; margin-right: 10px; cursor: pointer; border: 1px solid #ccc; border-radius: 4px; background: #fff;">Clear</button>
|
| 241 |
<button onclick="sendStrokes()" style="padding: 8px 16px; background: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer;">Predict</button>
|
| 242 |
</div>
|
| 243 |
<p style="color: #666; font-size: 12px; margin-top: 5px;">Draw an animal, then click Predict</p>
|
| 244 |
</div>
|
| 245 |
|
| 246 |
<script>
|
| 247 |
+
(function() {
|
| 248 |
+
// Wait for DOM
|
| 249 |
+
setTimeout(() => {
|
| 250 |
+
const canvas = document.getElementById('drawing-canvas');
|
| 251 |
+
if (!canvas) {
|
| 252 |
+
console.error("Canvas not found!");
|
| 253 |
+
return;
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
const ctx = canvas.getContext('2d', { willReadFrequently: true });
|
| 257 |
+
let isDrawing = false;
|
| 258 |
+
let strokes = [];
|
| 259 |
+
let currentStroke = {x: [], y: []};
|
| 260 |
+
|
| 261 |
+
ctx.strokeStyle = '#000';
|
| 262 |
+
ctx.lineWidth = 3;
|
| 263 |
+
ctx.lineCap = 'round';
|
| 264 |
+
ctx.lineJoin = 'round';
|
| 265 |
+
|
| 266 |
+
// Mouse Events
|
| 267 |
+
canvas.addEventListener('mousedown', (e) => {
|
| 268 |
+
isDrawing = true;
|
| 269 |
+
const rect = canvas.getBoundingClientRect();
|
| 270 |
+
const x = e.clientX - rect.left;
|
| 271 |
+
const y = e.clientY - rect.top;
|
| 272 |
+
currentStroke = {x: [x], y: [y]};
|
| 273 |
+
ctx.beginPath();
|
| 274 |
+
ctx.moveTo(x, y);
|
| 275 |
+
});
|
| 276 |
+
|
| 277 |
+
canvas.addEventListener('mousemove', (e) => {
|
| 278 |
+
if (!isDrawing) return;
|
| 279 |
+
const rect = canvas.getBoundingClientRect();
|
| 280 |
+
const x = e.clientX - rect.left;
|
| 281 |
+
const y = e.clientY - rect.top;
|
| 282 |
+
currentStroke.x.push(x);
|
| 283 |
+
currentStroke.y.push(y);
|
| 284 |
+
ctx.lineTo(x, y);
|
| 285 |
+
ctx.stroke();
|
| 286 |
+
});
|
| 287 |
+
|
| 288 |
+
const endStroke = () => {
|
| 289 |
+
if (isDrawing && currentStroke.x.length > 0) {
|
| 290 |
+
strokes.push([currentStroke.x, currentStroke.y]);
|
| 291 |
+
}
|
| 292 |
+
isDrawing = false;
|
| 293 |
+
};
|
| 294 |
+
|
| 295 |
+
canvas.addEventListener('mouseup', endStroke);
|
| 296 |
+
canvas.addEventListener('mouseleave', endStroke);
|
| 297 |
+
|
| 298 |
+
// Touch Events
|
| 299 |
+
canvas.addEventListener('touchstart', (e) => {
|
| 300 |
+
e.preventDefault();
|
| 301 |
+
const touch = e.touches[0];
|
| 302 |
+
const rect = canvas.getBoundingClientRect();
|
| 303 |
+
const x = touch.clientX - rect.left;
|
| 304 |
+
const y = touch.clientY - rect.top;
|
| 305 |
+
isDrawing = true;
|
| 306 |
+
currentStroke = {x: [x], y: [y]};
|
| 307 |
+
ctx.beginPath();
|
| 308 |
+
ctx.moveTo(x, y);
|
| 309 |
+
}, { passive: false });
|
| 310 |
+
|
| 311 |
+
canvas.addEventListener('touchmove', (e) => {
|
| 312 |
+
e.preventDefault();
|
| 313 |
+
if (!isDrawing) return;
|
| 314 |
+
const touch = e.touches[0];
|
| 315 |
+
const rect = canvas.getBoundingClientRect();
|
| 316 |
+
const x = touch.clientX - rect.left;
|
| 317 |
+
const y = touch.clientY - rect.top;
|
| 318 |
+
currentStroke.x.push(x);
|
| 319 |
+
currentStroke.y.push(y);
|
| 320 |
+
ctx.lineTo(x, y);
|
| 321 |
+
ctx.stroke();
|
| 322 |
+
}, { passive: false });
|
| 323 |
+
|
| 324 |
+
canvas.addEventListener('touchend', endStroke);
|
| 325 |
+
|
| 326 |
+
// Global functions for buttons
|
| 327 |
+
window.clearCanvas = function() {
|
| 328 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 329 |
+
strokes = [];
|
| 330 |
+
};
|
| 331 |
+
|
| 332 |
+
window.sendStrokes = function() {
|
| 333 |
+
const strokesJson = JSON.stringify(strokes);
|
| 334 |
+
const textbox = document.querySelector('#strokes-input textarea');
|
| 335 |
+
if (textbox) {
|
| 336 |
+
textbox.value = strokesJson;
|
| 337 |
+
textbox.dispatchEvent(new Event('input', { bubbles: true }));
|
| 338 |
+
}
|
| 339 |
+
const btn = document.querySelector('#predict-btn');
|
| 340 |
+
if (btn) btn.click();
|
| 341 |
+
};
|
| 342 |
+
|
| 343 |
+
console.log("Canvas initialized!");
|
| 344 |
+
}, 500); // Small delay to ensure render
|
| 345 |
+
})();
|
| 346 |
</script>
|
| 347 |
"""
|
| 348 |
|