Spaces:
Runtime error
Runtime error
Khunndev Cursor commited on
Commit ·
5fa7413
1
Parent(s): fa9c7b9
Draw circles on full-resolution original photo (fix blurry result)
Browse filesPreviously canvas.width was set to the display container width (~390px), compressing the image and circles. Now canvas.width = image.naturalWidth so the photo is drawn at full resolution and CSS width:100% scales it down for display. All circle/stroke/font sizes are expressed in natural-image pixels computed from a displayPx factor so they remain visually correct on any screen size.
Co-authored-by: Cursor <cursoragent@cursor.com>
frontend/src/components/ResultCanvas.tsx
CHANGED
|
@@ -42,6 +42,10 @@ function computeStagger(count: number): { staggerMs: number; durationMs: number
|
|
| 42 |
return { staggerMs: 2, durationMs: 100 }
|
| 43 |
}
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
function drawCircle(
|
| 46 |
ctx: CanvasRenderingContext2D,
|
| 47 |
cx: number,
|
|
@@ -51,10 +55,12 @@ function drawCircle(
|
|
| 51 |
progress: number,
|
| 52 |
label: string,
|
| 53 |
showLabel: boolean,
|
|
|
|
| 54 |
) {
|
| 55 |
const [r, g, b] = rgb
|
| 56 |
const eased = easeOut(Math.min(1, progress))
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
ctx.save()
|
| 60 |
|
|
@@ -66,23 +72,28 @@ function drawCircle(
|
|
| 66 |
|
| 67 |
// stroke arc sweeps clockwise from 12 o'clock
|
| 68 |
ctx.beginPath()
|
| 69 |
-
const endAngle =
|
| 70 |
-
progress >= 1 ? Math.PI * 2 : eased * Math.PI * 2
|
| 71 |
ctx.arc(cx, cy, radius, -Math.PI / 2, -Math.PI / 2 + endAngle)
|
| 72 |
ctx.strokeStyle = `rgba(${r},${g},${b},0.92)`
|
| 73 |
ctx.lineWidth = lw
|
| 74 |
ctx.lineCap = 'round'
|
| 75 |
ctx.stroke()
|
| 76 |
|
| 77 |
-
// number label —
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
ctx.font = `bold ${fs}px sans-serif`
|
| 82 |
ctx.textAlign = 'center'
|
| 83 |
ctx.textBaseline = 'middle'
|
|
|
|
|
|
|
| 84 |
ctx.fillStyle = `rgba(0,0,0,${0.55 * alpha})`
|
| 85 |
-
ctx.fillText(label, cx +
|
| 86 |
ctx.fillStyle = `rgba(255,255,255,${alpha})`
|
| 87 |
ctx.fillText(label, cx, cy)
|
| 88 |
}
|
|
@@ -150,10 +161,12 @@ export function ResultCanvas({
|
|
| 150 |
const ctx = canvas.getContext('2d')
|
| 151 |
if (!ctx) return
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
| 157 |
|
| 158 |
onCanvasReady?.(canvas)
|
| 159 |
|
|
@@ -162,6 +175,8 @@ export function ResultCanvas({
|
|
| 162 |
const totalMs = (sorted.length - 1) * staggerMs + durationMs
|
| 163 |
const dense = detections.length > 300
|
| 164 |
const showLabels = detections.length <= 500
|
|
|
|
|
|
|
| 165 |
|
| 166 |
let startTs: number | null = null
|
| 167 |
skipRef.current = false
|
|
@@ -185,19 +200,18 @@ export function ResultCanvas({
|
|
| 185 |
const color = group?.color ?? '#ef4444'
|
| 186 |
const rgb = hexToRgb(color)
|
| 187 |
|
| 188 |
-
|
| 189 |
-
const
|
| 190 |
-
const
|
| 191 |
-
const
|
| 192 |
-
const
|
| 193 |
-
const
|
| 194 |
-
const radius = Math.max(dense ? 4 : 6, Math.min(pw, ph) / 2)
|
| 195 |
|
| 196 |
-
drawCircle(ctx!, cx, cy, radius, rgb, progress, String(origIdx + 1), showLabels)
|
| 197 |
visibleCount++
|
| 198 |
})
|
| 199 |
|
| 200 |
-
// live count update (DOM-direct for perf)
|
| 201 |
if (countDisplayRef?.current) {
|
| 202 |
countDisplayRef.current.textContent = String(visibleCount)
|
| 203 |
}
|
|
@@ -212,14 +226,12 @@ export function ResultCanvas({
|
|
| 212 |
const group = groups.find((g) => g.id === d.groupId) ?? groups[0]
|
| 213 |
const color = group?.color ?? '#ef4444'
|
| 214 |
const rgb = hexToRgb(color)
|
| 215 |
-
const
|
| 216 |
-
const
|
| 217 |
-
const
|
| 218 |
-
const
|
| 219 |
-
const
|
| 220 |
-
|
| 221 |
-
const radius = Math.max(dense ? 4 : 6, Math.min(pw, ph) / 2)
|
| 222 |
-
drawCircle(ctx!, cx, cy, radius, rgb, 1, String(origIdx + 1), showLabels)
|
| 223 |
})
|
| 224 |
if (countDisplayRef?.current) {
|
| 225 |
countDisplayRef.current.textContent = String(detections.length)
|
|
@@ -244,31 +256,31 @@ export function ResultCanvas({
|
|
| 244 |
const ctx = canvas.getContext('2d')
|
| 245 |
if (!ctx) return
|
| 246 |
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
canvas.
|
| 251 |
-
canvas.height = Math.round(image.naturalHeight * ds)
|
| 252 |
}
|
|
|
|
|
|
|
| 253 |
|
| 254 |
ctx.clearRect(0, 0, canvas.width, canvas.height)
|
| 255 |
ctx.drawImage(image, 0, 0, canvas.width, canvas.height)
|
| 256 |
|
| 257 |
const dense = detections.length > 300
|
| 258 |
const showLabels = detections.length <= 500
|
|
|
|
| 259 |
|
| 260 |
detections.forEach((d, idx) => {
|
| 261 |
const group = groups.find((g) => g.id === d.groupId) ?? groups[0]
|
| 262 |
const color = group?.color ?? '#ef4444'
|
| 263 |
const rgb = hexToRgb(color)
|
| 264 |
-
const
|
| 265 |
-
const
|
| 266 |
-
const
|
| 267 |
-
const
|
| 268 |
-
const
|
| 269 |
-
|
| 270 |
-
const radius = Math.max(dense ? 4 : 6, Math.min(pw, ph) / 2)
|
| 271 |
-
drawCircle(ctx, cx, cy, radius, rgb, 1, String(idx + 1), showLabels)
|
| 272 |
})
|
| 273 |
|
| 274 |
const area = areaRef.current
|
|
@@ -276,10 +288,10 @@ export function ResultCanvas({
|
|
| 276 |
ctx.save()
|
| 277 |
ctx.strokeStyle = 'rgba(255,255,255,0.9)'
|
| 278 |
ctx.fillStyle = 'rgba(255,255,255,0.08)'
|
| 279 |
-
ctx.lineWidth = 2
|
| 280 |
-
ctx.setLineDash([6, 4])
|
| 281 |
-
ctx.fillRect(area.x1
|
| 282 |
-
ctx.strokeRect(area.x1
|
| 283 |
ctx.restore()
|
| 284 |
}
|
| 285 |
}, [animating, image, detections, groups])
|
|
|
|
| 42 |
return { staggerMs: 2, durationMs: 100 }
|
| 43 |
}
|
| 44 |
|
| 45 |
+
/**
|
| 46 |
+
* Draw a single detection circle at full image resolution.
|
| 47 |
+
* @param displayPx CSS pixels per canvas pixel (used to scale strokes/fonts to look right)
|
| 48 |
+
*/
|
| 49 |
function drawCircle(
|
| 50 |
ctx: CanvasRenderingContext2D,
|
| 51 |
cx: number,
|
|
|
|
| 55 |
progress: number,
|
| 56 |
label: string,
|
| 57 |
showLabel: boolean,
|
| 58 |
+
displayPx: number,
|
| 59 |
) {
|
| 60 |
const [r, g, b] = rgb
|
| 61 |
const eased = easeOut(Math.min(1, progress))
|
| 62 |
+
// stroke at least 2 CSS px thick, to stay visible on any screen
|
| 63 |
+
const lw = Math.max(2 / displayPx, radius * 0.13)
|
| 64 |
|
| 65 |
ctx.save()
|
| 66 |
|
|
|
|
| 72 |
|
| 73 |
// stroke arc sweeps clockwise from 12 o'clock
|
| 74 |
ctx.beginPath()
|
| 75 |
+
const endAngle = progress >= 1 ? Math.PI * 2 : eased * Math.PI * 2
|
|
|
|
| 76 |
ctx.arc(cx, cy, radius, -Math.PI / 2, -Math.PI / 2 + endAngle)
|
| 77 |
ctx.strokeStyle = `rgba(${r},${g},${b},0.92)`
|
| 78 |
ctx.lineWidth = lw
|
| 79 |
ctx.lineCap = 'round'
|
| 80 |
ctx.stroke()
|
| 81 |
|
| 82 |
+
// number label — fades in alongside the fill
|
| 83 |
+
// font size expressed in natural-image pixels so it scales with the image
|
| 84 |
+
const minLabelRadius = 8 / displayPx // only label if circle is at least 8 CSS px radius
|
| 85 |
+
if (showLabel && radius >= minLabelRadius && eased > 0) {
|
| 86 |
+
const maxFs = 15 / displayPx
|
| 87 |
+
const minFs = 9 / displayPx
|
| 88 |
+
const fs = Math.max(minFs, Math.min(radius * 0.82, maxFs))
|
| 89 |
+
const alpha = Math.min(1, eased * 2)
|
| 90 |
ctx.font = `bold ${fs}px sans-serif`
|
| 91 |
ctx.textAlign = 'center'
|
| 92 |
ctx.textBaseline = 'middle'
|
| 93 |
+
// drop shadow in natural pixels
|
| 94 |
+
const shadowOff = Math.max(0.5, 0.6 / displayPx)
|
| 95 |
ctx.fillStyle = `rgba(0,0,0,${0.55 * alpha})`
|
| 96 |
+
ctx.fillText(label, cx + shadowOff, cy + shadowOff)
|
| 97 |
ctx.fillStyle = `rgba(255,255,255,${alpha})`
|
| 98 |
ctx.fillText(label, cx, cy)
|
| 99 |
}
|
|
|
|
| 161 |
const ctx = canvas.getContext('2d')
|
| 162 |
if (!ctx) return
|
| 163 |
|
| 164 |
+
// Draw at full natural resolution — CSS width:100% scales it down visually.
|
| 165 |
+
// This keeps the photo and circles crisp on all screen densities.
|
| 166 |
+
const displayW = canvas.parentElement?.clientWidth ?? image.naturalWidth
|
| 167 |
+
const displayPx = displayW / image.naturalWidth // CSS px per canvas px
|
| 168 |
+
canvas.width = image.naturalWidth
|
| 169 |
+
canvas.height = image.naturalHeight
|
| 170 |
|
| 171 |
onCanvasReady?.(canvas)
|
| 172 |
|
|
|
|
| 175 |
const totalMs = (sorted.length - 1) * staggerMs + durationMs
|
| 176 |
const dense = detections.length > 300
|
| 177 |
const showLabels = detections.length <= 500
|
| 178 |
+
// minimum circle radius expressed in CSS pixels → converted to natural px
|
| 179 |
+
const minR = (dense ? 5 : 7) / displayPx
|
| 180 |
|
| 181 |
let startTs: number | null = null
|
| 182 |
skipRef.current = false
|
|
|
|
| 200 |
const color = group?.color ?? '#ef4444'
|
| 201 |
const rgb = hexToRgb(color)
|
| 202 |
|
| 203 |
+
// coords already in natural image pixels (ds = 1)
|
| 204 |
+
const pw = d.xmax - d.xmin
|
| 205 |
+
const ph = d.ymax - d.ymin
|
| 206 |
+
const cx = d.xmin + pw / 2
|
| 207 |
+
const cy = d.ymin + ph / 2
|
| 208 |
+
const radius = Math.max(minR, Math.min(pw, ph) / 2)
|
|
|
|
| 209 |
|
| 210 |
+
drawCircle(ctx!, cx, cy, radius, rgb, progress, String(origIdx + 1), showLabels, displayPx)
|
| 211 |
visibleCount++
|
| 212 |
})
|
| 213 |
|
| 214 |
+
// live count update (DOM-direct for perf — avoids React re-renders)
|
| 215 |
if (countDisplayRef?.current) {
|
| 216 |
countDisplayRef.current.textContent = String(visibleCount)
|
| 217 |
}
|
|
|
|
| 226 |
const group = groups.find((g) => g.id === d.groupId) ?? groups[0]
|
| 227 |
const color = group?.color ?? '#ef4444'
|
| 228 |
const rgb = hexToRgb(color)
|
| 229 |
+
const pw = d.xmax - d.xmin
|
| 230 |
+
const ph = d.ymax - d.ymin
|
| 231 |
+
const cx = d.xmin + pw / 2
|
| 232 |
+
const cy = d.ymin + ph / 2
|
| 233 |
+
const radius = Math.max(minR, Math.min(pw, ph) / 2)
|
| 234 |
+
drawCircle(ctx!, cx, cy, radius, rgb, 1, String(origIdx + 1), showLabels, displayPx)
|
|
|
|
|
|
|
| 235 |
})
|
| 236 |
if (countDisplayRef?.current) {
|
| 237 |
countDisplayRef.current.textContent = String(detections.length)
|
|
|
|
| 256 |
const ctx = canvas.getContext('2d')
|
| 257 |
if (!ctx) return
|
| 258 |
|
| 259 |
+
// Ensure canvas is still at full natural resolution after animation
|
| 260 |
+
if (canvas.width !== image.naturalWidth) {
|
| 261 |
+
canvas.width = image.naturalWidth
|
| 262 |
+
canvas.height = image.naturalHeight
|
|
|
|
| 263 |
}
|
| 264 |
+
const displayW = canvas.parentElement?.clientWidth ?? image.naturalWidth
|
| 265 |
+
const displayPx = displayW / image.naturalWidth
|
| 266 |
|
| 267 |
ctx.clearRect(0, 0, canvas.width, canvas.height)
|
| 268 |
ctx.drawImage(image, 0, 0, canvas.width, canvas.height)
|
| 269 |
|
| 270 |
const dense = detections.length > 300
|
| 271 |
const showLabels = detections.length <= 500
|
| 272 |
+
const minR = (dense ? 5 : 7) / displayPx
|
| 273 |
|
| 274 |
detections.forEach((d, idx) => {
|
| 275 |
const group = groups.find((g) => g.id === d.groupId) ?? groups[0]
|
| 276 |
const color = group?.color ?? '#ef4444'
|
| 277 |
const rgb = hexToRgb(color)
|
| 278 |
+
const pw = d.xmax - d.xmin
|
| 279 |
+
const ph = d.ymax - d.ymin
|
| 280 |
+
const cx = d.xmin + pw / 2
|
| 281 |
+
const cy = d.ymin + ph / 2
|
| 282 |
+
const radius = Math.max(minR, Math.min(pw, ph) / 2)
|
| 283 |
+
drawCircle(ctx, cx, cy, radius, rgb, 1, String(idx + 1), showLabels, displayPx)
|
|
|
|
|
|
|
| 284 |
})
|
| 285 |
|
| 286 |
const area = areaRef.current
|
|
|
|
| 288 |
ctx.save()
|
| 289 |
ctx.strokeStyle = 'rgba(255,255,255,0.9)'
|
| 290 |
ctx.fillStyle = 'rgba(255,255,255,0.08)'
|
| 291 |
+
ctx.lineWidth = Math.max(2 / displayPx, 1)
|
| 292 |
+
ctx.setLineDash([6 / displayPx, 4 / displayPx])
|
| 293 |
+
ctx.fillRect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1)
|
| 294 |
+
ctx.strokeRect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1)
|
| 295 |
ctx.restore()
|
| 296 |
}
|
| 297 |
}, [animating, image, detections, groups])
|