@@ -38,6 +38,7 @@ interface Rect {
3838export function Canvas ( { lenses, imageIndex, orientation, canvasRef } : CanvasProps ) {
3939 const imageRef = useRef < HTMLImageElement | null > ( null )
4040 const animFrameRef = useRef < number > ( 0 )
41+ const drawnRectsRef = useRef < Rect [ ] > ( [ ] )
4142 // Custom offsets from center (in canvas pixels) for each lens, keyed by index
4243 const [ offsets , setOffsets ] = useState < Record < number , { dx : number ; dy : number } > > ( { } )
4344 const dragRef = useRef < { index : number ; startX : number ; startY : number ; origDx : number ; origDy : number } | null > ( null )
@@ -150,6 +151,9 @@ export function Canvas({ lenses, imageIndex, orientation, canvasRef }: CanvasPro
150151 ctx . fillStyle = r . color
151152 ctx . fillText ( text , tx , ty )
152153 }
154+
155+ // Store rects with pill bounds for hit testing
156+ drawnRectsRef . current = rects
153157 } , [ canvasRef , computeRects ] )
154158
155159 // Load image
@@ -228,12 +232,10 @@ export function Canvas({ lenses, imageIndex, orientation, canvasRef }: CanvasPro
228232 } , [ canvasRef ] )
229233
230234 const startDrag = useCallback ( ( clientX : number , clientY : number ) : boolean => {
231- const canvas = canvasRef . current
232- if ( ! canvas ) return false
235+ if ( ! canvasRef . current ) return false
233236 const { cx, cy } = getCanvasCoords ( clientX , clientY )
234- const rects = computeRects ( canvas )
235237
236- const hit = [ ...rects ]
238+ const hit = [ ...drawnRectsRef . current ]
237239 . sort ( ( a , b ) => ( a . w * a . h ) - ( b . w * b . h ) )
238240 . find ( ( r ) => hitTestRect ( r , cx , cy ) )
239241
@@ -243,7 +245,7 @@ export function Canvas({ lenses, imageIndex, orientation, canvasRef }: CanvasPro
243245 return true
244246 }
245247 return false
246- } , [ canvasRef , computeRects , getCanvasCoords , offsets ] )
248+ } , [ canvasRef , getCanvasCoords , offsets ] )
247249
248250 const handleMouseDown = useCallback ( ( e : React . MouseEvent ) => {
249251 if ( startDrag ( e . clientX , e . clientY ) ) e . preventDefault ( )
@@ -290,11 +292,10 @@ export function Canvas({ lenses, imageIndex, orientation, canvasRef }: CanvasPro
290292 moveDrag ( e . clientX , e . clientY )
291293 } else {
292294 const { cx, cy } = getCanvasCoords ( e . clientX , e . clientY )
293- const rects = computeRects ( canvas )
294- const hover = rects . some ( ( r ) => hitTestRect ( r , cx , cy ) )
295+ const hover = drawnRectsRef . current . some ( ( r ) => hitTestRect ( r , cx , cy ) )
295296 canvas . style . cursor = hover ? 'grab' : 'default'
296297 }
297- } , [ canvasRef , computeRects , getCanvasCoords , moveDrag ] )
298+ } , [ canvasRef , getCanvasCoords , moveDrag ] )
298299
299300 const handleMouseUp = useCallback ( ( ) => {
300301 dragRef . current = null
0 commit comments