diff --git a/apps/star-chart/web/src/components/canvas/Canvas.ts b/apps/star-chart/web/src/components/canvas/Canvas.ts index c79503d1..83ade680 100644 --- a/apps/star-chart/web/src/components/canvas/Canvas.ts +++ b/apps/star-chart/web/src/components/canvas/Canvas.ts @@ -4,7 +4,7 @@ import { clampVector, snap } from './math.js'; import { ObjectBounds } from './ObjectBounds.js'; import { Selections } from './Selections.js'; import { RectLimits, Vector2 } from './types.js'; -import { Viewport, ViewportConfig } from './Viewport.js'; +import { Viewport, ViewportConfig, ViewportEventOrigin } from './Viewport.js'; import { proxy } from 'valtio'; export interface CanvasOptions { @@ -222,5 +222,16 @@ export class Canvas extends EventSubscriber { } }; + zoomToFit = ( + options: { origin?: ViewportEventOrigin; margin?: number } = {}, + ) => { + const bounds = this.bounds.getCurrentContainer(); + if (bounds) { + this.viewport.fitOnScreen(bounds, options); + } else { + this.viewport.doMove(this.center, 1, options); + } + }; + dispose = () => {}; } diff --git a/apps/star-chart/web/src/components/project/ProjectCanvas.tsx b/apps/star-chart/web/src/components/project/ProjectCanvas.tsx index 1979d608..ff6e87ae 100644 --- a/apps/star-chart/web/src/components/project/ProjectCanvas.tsx +++ b/apps/star-chart/web/src/components/project/ProjectCanvas.tsx @@ -136,10 +136,7 @@ function useZoomToFit(tasks: Task[]) { // effects that ran on the same render. mainly // the canvas viewport setup stuff... requestAnimationFrame(() => { - const bounds = canvas.bounds.getCurrentContainer(); - if (!bounds) return; - - canvas.viewport.fitOnScreen(bounds, { origin: 'control', margin: 10 }); + canvas.zoomToFit({ origin: 'control', margin: 10 }); }); setHasZoomed(true); }, [tasks, canvas, hasZoomed]);