From 4c7b1a22695c4b755890e18bc48f0543d2011dab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Forja?= Date: Mon, 14 Dec 2020 14:14:56 +0000 Subject: [PATCH] fix: Visibility and zooming when canvas offset is not zero (#2534) --- src/actions/actionCanvas.tsx | 20 +++++++++++++++----- src/components/App.tsx | 27 +++++++++++++++++---------- src/packages/excalidraw/CHANGELOG.MD | 7 +++++++ src/renderer/renderScene.ts | 11 +++++++++-- src/scene/zoom.ts | 7 +++++-- 5 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/actions/actionCanvas.tsx b/src/actions/actionCanvas.tsx index addc69d1..5435aab3 100644 --- a/src/actions/actionCanvas.tsx +++ b/src/actions/actionCanvas.tsx @@ -95,6 +95,7 @@ export const actionZoomIn = register({ const zoom = getNewZoom( getNormalizedZoom(appState.zoom.value + ZOOM_STEP), appState.zoom, + { left: appState.offsetLeft, top: appState.offsetTop }, { x: appState.width / 2, y: appState.height / 2 }, ); trackEvent(EVENT_ACTION, "zoom", "in", zoom.value * 100); @@ -128,6 +129,7 @@ export const actionZoomOut = register({ const zoom = getNewZoom( getNormalizedZoom(appState.zoom.value - ZOOM_STEP), appState.zoom, + { left: appState.offsetLeft, top: appState.offsetTop }, { x: appState.width / 2, y: appState.height / 2 }, ); @@ -163,10 +165,15 @@ export const actionResetZoom = register({ return { appState: { ...appState, - zoom: getNewZoom(1 as NormalizedZoomValue, appState.zoom, { - x: appState.width / 2, - y: appState.height / 2, - }), + zoom: getNewZoom( + 1 as NormalizedZoomValue, + appState.zoom, + { left: appState.offsetLeft, top: appState.offsetTop }, + { + x: appState.width / 2, + y: appState.height / 2, + }, + ), }, commitToHistory: false, }; @@ -223,7 +230,10 @@ const zoomToFitElements = ( width: appState.width, height: appState.height, }); - const newZoom = getNewZoom(zoomValue, appState.zoom); + const newZoom = getNewZoom(zoomValue, appState.zoom, { + left: appState.offsetLeft, + top: appState.offsetTop, + }); const action = zoomToSelection ? "selection" : "fit"; const [x1, y1, x2, y2] = commonBounds; diff --git a/src/components/App.tsx b/src/components/App.tsx index 9a96a4d0..43901a3b 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -1423,10 +1423,11 @@ class App extends React.Component { private onGestureChange = withBatchedUpdates((event: GestureEvent) => { event.preventDefault(); - this.setState(({ zoom }) => ({ + this.setState(({ zoom, offsetLeft, offsetTop }) => ({ zoom: getNewZoom( getNormalizedZoom(gesture.initialScale! * event.scale), zoom, + { left: offsetLeft, top: offsetTop }, { x: cursorX, y: cursorY }, ), })); @@ -1750,12 +1751,13 @@ class App extends React.Component { const distance = getDistance(Array.from(gesture.pointers.values())); const scaleFactor = distance / gesture.initialDistance!; - this.setState(({ zoom, scrollX, scrollY }) => ({ + this.setState(({ zoom, scrollX, scrollY, offsetLeft, offsetTop }) => ({ scrollX: normalizeScroll(scrollX + deltaX / zoom.value), scrollY: normalizeScroll(scrollY + deltaY / zoom.value), zoom: getNewZoom( getNormalizedZoom(gesture.initialScale! * scaleFactor), zoom, + { left: offsetLeft, top: offsetTop }, center, ), shouldCacheIgnoreZoom: true, @@ -2586,9 +2588,9 @@ class App extends React.Component { ); /* If arrow is pre-arrowheads, it will have undefined for both start and end arrowheads. - If so, we want it to be null for start and "arrow" for end. If the linear item is not - an arrow, we want it to be null for both. Otherwise, we want it to use the - values from appState. */ + If so, we want it to be null for start and "arrow" for end. If the linear item is not + an arrow, we want it to be null for both. Otherwise, we want it to use the + values from appState. */ const { currentItemStartArrowhead, currentItemEndArrowhead } = this.state; const [startArrowhead, endArrowhead] = @@ -3702,11 +3704,16 @@ class App extends React.Component { }, 1000); } - this.setState(({ zoom }) => ({ - zoom: getNewZoom(getNormalizedZoom(zoom.value - delta / 100), zoom, { - x: cursorX, - y: cursorY, - }), + this.setState(({ zoom, offsetLeft, offsetTop }) => ({ + zoom: getNewZoom( + getNormalizedZoom(zoom.value - delta / 100), + zoom, + { left: offsetLeft, top: offsetTop }, + { + x: cursorX, + y: cursorY, + }, + ), selectedElementIds: {}, previousSelectedElementIds: Object.keys(selectedElementIds).length !== 0 diff --git a/src/packages/excalidraw/CHANGELOG.MD b/src/packages/excalidraw/CHANGELOG.MD index 85d3d4b0..3ae1116f 100644 --- a/src/packages/excalidraw/CHANGELOG.MD +++ b/src/packages/excalidraw/CHANGELOG.MD @@ -1,4 +1,5 @@ # Changelog +