Khunndev Cursor commited on
Commit
7a0d096
·
1 Parent(s): 5fa7413

Fix canvas redraw on tap and circle overflow

Browse files

Co-authored-by: Cursor <cursoragent@cursor.com>

frontend/src/components/ResultCanvas.tsx CHANGED
@@ -296,6 +296,47 @@ export function ResultCanvas({
296
  }
297
  }, [animating, image, detections, groups])
298
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299
  // -------------------------------------------------------------------------
300
  // zoom / pan helpers (interactive phase only)
301
  // -------------------------------------------------------------------------
@@ -434,32 +475,52 @@ export function ResultCanvas({
434
 
435
  if (areaStartRef.current && tool === 'area') {
436
  const { x, y } = toImageCoords(e.clientX, e.clientY)
437
- areaRef.current = { x1: Math.min(areaStartRef.current.x, x), y1: Math.min(areaStartRef.current.y, y), x2: Math.max(areaStartRef.current.x, x), y2: Math.max(areaStartRef.current.y, y) }
438
- onUpdate([...detections])
 
 
 
 
 
 
439
  }
440
  }
441
 
442
  const handlePointerUp = (e: React.PointerEvent) => {
443
  if (e.currentTarget.hasPointerCapture(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId)
444
- if (!animating && tool === 'area' && areaRef.current && areaStartRef.current) {
 
445
  const a = areaRef.current
446
- if (a.x2 > a.x1 && a.y2 > a.y1) {
 
 
 
 
447
  onUpdate(detections.map((d) => {
448
  const cx = (d.xmin + d.xmax) / 2, cy = (d.ymin + d.ymax) / 2
449
- return cx >= a.x1 && cx <= a.x2 && cy >= a.y1 && cy <= a.y2 ? { ...d, groupId: selectedGroupId, manual: true } : d
 
 
450
  }))
 
 
 
451
  }
452
  }
 
453
  activeRef.current.delete(e.pointerId)
454
  if (activeRef.current.size < 2) pinchRef.current = null
455
- if (activeRef.current.size === 0) { panStartRef.current = null; areaStartRef.current = null; areaRef.current = null; if (!animating) onUpdate([...detections]) }
 
 
 
456
  }
457
 
458
  return (
459
  <div ref={containerRef} className="relative overflow-hidden rounded-2xl border border-slate-700/80 bg-black shadow-lg">
460
  <canvas
461
  ref={canvasRef}
462
- style={{ transformOrigin: '0 0', display: 'block', width: '100%' }}
463
  className="select-none touch-none"
464
  onPointerDown={handlePointerDown}
465
  onPointerMove={handlePointerMove}
 
296
  }
297
  }, [animating, image, detections, groups])
298
 
299
+ // -------------------------------------------------------------------------
300
+ // direct canvas redraw — does NOT go through React state / onUpdate
301
+ // use this for transient visual updates (area rect, post-pan) to avoid
302
+ // triggering React re-renders and the canvas useEffect.
303
+ // -------------------------------------------------------------------------
304
+ const redrawCanvas = () => {
305
+ const canvas = canvasRef.current
306
+ if (!canvas || !image || animating) return
307
+ const ctx = canvas.getContext('2d')
308
+ if (!ctx) return
309
+
310
+ const displayW = canvas.parentElement?.clientWidth ?? image.naturalWidth
311
+ const displayPx = displayW / image.naturalWidth
312
+ const dense = detections.length > 300
313
+ const showLabels = detections.length <= 500
314
+ const minR = (dense ? 5 : 7) / displayPx
315
+
316
+ ctx.clearRect(0, 0, canvas.width, canvas.height)
317
+ ctx.drawImage(image, 0, 0, canvas.width, canvas.height)
318
+
319
+ detections.forEach((d, idx) => {
320
+ const group = groups.find((g) => g.id === d.groupId) ?? groups[0]
321
+ const rgb = hexToRgb(group?.color ?? '#ef4444')
322
+ const pw = d.xmax - d.xmin
323
+ const ph = d.ymax - d.ymin
324
+ drawCircle(ctx, d.xmin + pw / 2, d.ymin + ph / 2, Math.max(minR, Math.min(pw, ph) / 2), rgb, 1, String(idx + 1), showLabels, displayPx)
325
+ })
326
+
327
+ const area = areaRef.current
328
+ if (area) {
329
+ ctx.save()
330
+ ctx.strokeStyle = 'rgba(255,255,255,0.9)'
331
+ ctx.fillStyle = 'rgba(255,255,255,0.08)'
332
+ ctx.lineWidth = Math.max(2 / displayPx, 1)
333
+ ctx.setLineDash([6 / displayPx, 4 / displayPx])
334
+ ctx.fillRect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1)
335
+ ctx.strokeRect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1)
336
+ ctx.restore()
337
+ }
338
+ }
339
+
340
  // -------------------------------------------------------------------------
341
  // zoom / pan helpers (interactive phase only)
342
  // -------------------------------------------------------------------------
 
475
 
476
  if (areaStartRef.current && tool === 'area') {
477
  const { x, y } = toImageCoords(e.clientX, e.clientY)
478
+ areaRef.current = {
479
+ x1: Math.min(areaStartRef.current.x, x),
480
+ y1: Math.min(areaStartRef.current.y, y),
481
+ x2: Math.max(areaStartRef.current.x, x),
482
+ y2: Math.max(areaStartRef.current.y, y),
483
+ }
484
+ // draw the selection rect directly — no React state update needed
485
+ redrawCanvas()
486
  }
487
  }
488
 
489
  const handlePointerUp = (e: React.PointerEvent) => {
490
  if (e.currentTarget.hasPointerCapture(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId)
491
+
492
+ if (!animating && tool === 'area' && areaStartRef.current) {
493
  const a = areaRef.current
494
+ areaRef.current = null
495
+ areaStartRef.current = null
496
+
497
+ if (a && a.x2 > a.x1 && a.y2 > a.y1) {
498
+ // area was drawn — update detections (this triggers re-render + canvas useEffect)
499
  onUpdate(detections.map((d) => {
500
  const cx = (d.xmin + d.xmax) / 2, cy = (d.ymin + d.ymax) / 2
501
+ return cx >= a.x1 && cx <= a.x2 && cy >= a.y1 && cy <= a.y2
502
+ ? { ...d, groupId: selectedGroupId, manual: true }
503
+ : d
504
  }))
505
+ } else {
506
+ // no area drawn — just clear the rect from canvas without a state update
507
+ redrawCanvas()
508
  }
509
  }
510
+
511
  activeRef.current.delete(e.pointerId)
512
  if (activeRef.current.size < 2) pinchRef.current = null
513
+ if (activeRef.current.size === 0) {
514
+ panStartRef.current = null
515
+ // ← removed onUpdate([...detections]) — that was causing redraws on every tap
516
+ }
517
  }
518
 
519
  return (
520
  <div ref={containerRef} className="relative overflow-hidden rounded-2xl border border-slate-700/80 bg-black shadow-lg">
521
  <canvas
522
  ref={canvasRef}
523
+ style={{ transformOrigin: '0 0', display: 'block', width: '100%', height: 'auto' }}
524
  className="select-none touch-none"
525
  onPointerDown={handlePointerDown}
526
  onPointerMove={handlePointerMove}