diff --git a/src/actions/actionCanvas.tsx b/src/actions/actionCanvas.tsx index 3f630404..d66bdd37 100644 --- a/src/actions/actionCanvas.tsx +++ b/src/actions/actionCanvas.tsx @@ -4,7 +4,7 @@ import { getDefaultAppState } from "../appState"; import { trash, zoomIn, zoomOut, resetZoom } from "../components/icons"; import { ToolButton } from "../components/ToolButton"; import { t } from "../i18n"; -import { getNormalizedZoom, calculateScrollCenter } from "../scene"; +import { getNormalizedZoom, normalizeScroll } from "../scene"; import { KEYS } from "../keys"; import { getShortcutKey } from "../utils"; import useIsMobile from "../is-mobile"; @@ -202,15 +202,22 @@ export const actionZoomToFit = register({ name: "zoomToFit", perform: (elements, appState) => { const nonDeletedElements = elements.filter((element) => !element.isDeleted); - const scrollCenter = calculateScrollCenter(nonDeletedElements); const commonBounds = getCommonBounds(nonDeletedElements); - const zoom = calculateZoom(commonBounds, appState.zoom, scrollCenter); + const [x1, y1, x2, y2] = commonBounds; + const centerX = (x1 + x2) / 2; + const centerY = (y1 + y2) / 2; + const scrollX = normalizeScroll(window.innerWidth / 2 - centerX); + const scrollY = normalizeScroll(window.innerHeight / 2 - centerY); + const zoom = calculateZoom(commonBounds, appState.zoom, { + scrollX, + scrollY, + }); return { appState: { ...appState, - scrollX: scrollCenter.scrollX, - scrollY: scrollCenter.scrollY, + scrollX, + scrollY, zoom, }, commitToHistory: false, diff --git a/src/components/App.tsx b/src/components/App.tsx index 56c69a38..3dab6d6d 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -847,6 +847,8 @@ class App extends React.Component { remoteElements.filter((element: { isDeleted: boolean }) => { return !element.isDeleted; }), + this.state, + this.canvas, ), }); } diff --git a/src/components/LayerUI.tsx b/src/components/LayerUI.tsx index b63edddf..75d8304e 100644 --- a/src/components/LayerUI.tsx +++ b/src/components/LayerUI.tsx @@ -256,7 +256,9 @@ const LayerUI = ({