Khunndev Cursor commited on
Commit
c865058
·
1 Parent(s): ebe2f4e

Fit circle numbers to size with measureText and clip inside ring

Browse files
frontend/src/components/ResultCanvas.tsx CHANGED
@@ -72,6 +72,33 @@ function fitRadius(pw: number, ph: number, minR: number): number {
72
  return Math.min(Math.max(minR, target), halfBox)
73
  }
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  /**
76
  * Draw a single detection circle at full image resolution.
77
  * @param displayPx CSS pixels per canvas pixel (used to scale strokes/fonts to look right)
@@ -109,26 +136,37 @@ function drawCircle(
109
  ctx.lineCap = 'round'
110
  ctx.stroke()
111
 
112
- // number label — centred in every circle, fades in alongside the fill
113
- // font size expressed in natural-image pixels so it scales with the image
114
- const minLabelRadius = 4 / displayPx // label as soon as the circle is ~4 CSS px radius
115
  if (showLabel && radius >= minLabelRadius && eased > 0) {
116
- const maxFs = 16 / displayPx
117
- const minFs = 7 / displayPx
118
- // scale font with circle and shrink for longer numbers so it stays inside
119
- const fitFs = (radius * 1.5) / Math.max(1, label.length)
120
- const fs = Math.max(minFs, Math.min(fitFs, maxFs))
121
  const alpha = Math.min(1, eased * 2)
 
 
122
  ctx.font = `bold ${fs}px sans-serif`
123
  ctx.textAlign = 'center'
124
  ctx.textBaseline = 'middle'
125
- // crisp dark outline so the number reads on any pipe colour
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  ctx.lineJoin = 'round'
127
- ctx.lineWidth = Math.max(0.8 / displayPx, fs * 0.18)
128
- ctx.strokeStyle = `rgba(0,0,0,${0.6 * alpha})`
129
- ctx.strokeText(label, cx, cy)
130
  ctx.fillStyle = `rgba(255,255,255,${alpha})`
131
- ctx.fillText(label, cx, cy)
 
132
  }
133
 
134
  ctx.restore()
 
72
  return Math.min(Math.max(minR, target), halfBox)
73
  }
74
 
75
+ /**
76
+ * Font size so the label fits inside the circle without touching the stroke.
77
+ * Uses canvas measureText for real width/height instead of a digit-count guess.
78
+ */
79
+ function fitLabelFontSize(
80
+ ctx: CanvasRenderingContext2D,
81
+ label: string,
82
+ radius: number,
83
+ displayPx: number,
84
+ ): number {
85
+ const minFs = 6 / displayPx
86
+ // upper bound: text should not exceed ~70% of the inner diameter
87
+ let fs = Math.min(radius * 1.1, 18 / displayPx)
88
+ const maxW = radius * 2 * 0.72
89
+ const maxH = radius * 2 * 0.58
90
+
91
+ for (let i = 0; i < 12; i++) {
92
+ ctx.font = `bold ${fs}px sans-serif`
93
+ const m = ctx.measureText(label)
94
+ const w = m.width
95
+ const h = (m.actualBoundingBoxAscent ?? fs * 0.8) + (m.actualBoundingBoxDescent ?? fs * 0.2)
96
+ if (w <= maxW && h <= maxH) break
97
+ fs = Math.max(minFs, fs * 0.88)
98
+ }
99
+ return Math.max(minFs, fs)
100
+ }
101
+
102
  /**
103
  * Draw a single detection circle at full image resolution.
104
  * @param displayPx CSS pixels per canvas pixel (used to scale strokes/fonts to look right)
 
136
  ctx.lineCap = 'round'
137
  ctx.stroke()
138
 
139
+ // number label — centred, sized to fit inside the circle, clipped so it
140
+ // never spills past the ring even on dense layouts
141
+ const minLabelRadius = 5 / displayPx
142
  if (showLabel && radius >= minLabelRadius && eased > 0) {
 
 
 
 
 
143
  const alpha = Math.min(1, eased * 2)
144
+ const fs = fitLabelFontSize(ctx, label, radius, displayPx)
145
+
146
  ctx.font = `bold ${fs}px sans-serif`
147
  ctx.textAlign = 'center'
148
  ctx.textBaseline = 'middle'
149
+
150
+ // optical vertical centre (some fonts sit slightly below geometric middle)
151
+ const m = ctx.measureText(label)
152
+ const ascent = m.actualBoundingBoxAscent ?? fs * 0.78
153
+ const descent = m.actualBoundingBoxDescent ?? fs * 0.22
154
+ const ty = cy + (ascent - descent) * 0.08
155
+
156
+ // clip to inner circle so text never overlaps the stroke or neighbours
157
+ ctx.save()
158
+ ctx.beginPath()
159
+ ctx.arc(cx, cy, Math.max(1, radius - lw * 0.6), 0, Math.PI * 2)
160
+ ctx.clip()
161
+
162
+ const outline = Math.max(0.5 / displayPx, fs * 0.12)
163
  ctx.lineJoin = 'round'
164
+ ctx.lineWidth = outline
165
+ ctx.strokeStyle = `rgba(0,0,0,${0.55 * alpha})`
166
+ ctx.strokeText(label, cx, ty)
167
  ctx.fillStyle = `rgba(255,255,255,${alpha})`
168
+ ctx.fillText(label, cx, ty)
169
+ ctx.restore()
170
  }
171
 
172
  ctx.restore()