refactor(view): extract canvas viewport hooks
This commit is contained in:
20
view/canvas/useCanvasResize.ts
Normal file
20
view/canvas/useCanvasResize.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { useEffect, type RefObject } from "react";
|
||||
import type { Dispatch } from "@commands/dispatcher";
|
||||
|
||||
export function useCanvasResize(canvasRef: RefObject<HTMLCanvasElement | null>, dispatch: Dispatch) {
|
||||
useEffect(() => {
|
||||
const canvas = canvasRef.current;
|
||||
if (!canvas) return;
|
||||
|
||||
const resizeObserver = new ResizeObserver(([entry]) => {
|
||||
if (!entry) return;
|
||||
|
||||
const width = Math.floor(entry.contentRect.width);
|
||||
const height = Math.floor(entry.contentRect.height);
|
||||
dispatch("viewport.setSize", { w: width, h: height });
|
||||
});
|
||||
|
||||
resizeObserver.observe(canvas);
|
||||
return () => resizeObserver.disconnect();
|
||||
}, [canvasRef, dispatch]);
|
||||
}
|
||||
Reference in New Issue
Block a user