mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 09:36:08 +00:00
feat(annotation-canvas): optimize canvas resizing by using refs for strokes and currentStroke
This commit is contained in:
@@ -70,7 +70,15 @@ export const AnnotationCanvas = forwardRef<AnnotationCanvasHandle, AnnotationCan
|
|||||||
if (active) draw(active);
|
if (active) draw(active);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Resize canvas to match container
|
// Keep a stable ref to the latest strokes/currentStroke so the resize
|
||||||
|
// handler can redraw without being listed as a dependency (which would
|
||||||
|
// re-register the listener on every stroke change).
|
||||||
|
const strokesRef = useRef(strokes);
|
||||||
|
const currentStrokeRef = useRef(currentStroke);
|
||||||
|
useEffect(() => { strokesRef.current = strokes; }, [strokes]);
|
||||||
|
useEffect(() => { currentStrokeRef.current = currentStroke; }, [currentStroke]);
|
||||||
|
|
||||||
|
// Resize canvas to match container — listener registered once, never re-added.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const resizeCanvas = () => {
|
const resizeCanvas = () => {
|
||||||
const canvas = canvasRef.current;
|
const canvas = canvasRef.current;
|
||||||
@@ -82,13 +90,13 @@ export const AnnotationCanvas = forwardRef<AnnotationCanvasHandle, AnnotationCan
|
|||||||
canvas.height = rect.height;
|
canvas.height = rect.height;
|
||||||
|
|
||||||
const ctx = canvas.getContext('2d');
|
const ctx = canvas.getContext('2d');
|
||||||
if (ctx) renderStrokes(ctx, canvas, strokes, currentStroke);
|
if (ctx) renderStrokes(ctx, canvas, strokesRef.current, currentStrokeRef.current);
|
||||||
};
|
};
|
||||||
|
|
||||||
resizeCanvas();
|
resizeCanvas();
|
||||||
window.addEventListener('resize', resizeCanvas);
|
window.addEventListener('resize', resizeCanvas);
|
||||||
return () => window.removeEventListener('resize', resizeCanvas);
|
return () => window.removeEventListener('resize', resizeCanvas);
|
||||||
}, [strokes, currentStroke, renderStrokes]);
|
}, [renderStrokes]); // renderStrokes is stable (useCallback with no deps that change)
|
||||||
|
|
||||||
// Re-render on stroke changes
|
// Re-render on stroke changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user