Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

fix: Don't break zoom when zooming in on UI (#2638)

Christopher Chedeau81f8039ec70c
diff --git a/src/components/App.tsx b/src/components/App.tsx
index 229ef9bf24..57bc08b021 100644
--- a/src/components/App.tsx
+++ b/src/components/App.tsx
@@ -1426,14 +1426,17 @@ class App extends React.Component<ExcalidrawProps, AppState> {
private onGestureChange = withBatchedUpdates((event: GestureEvent) => {
event.preventDefault();
- this.setState(({ zoom, offsetLeft, offsetTop }) => ({
- zoom: getNewZoom(
- getNormalizedZoom(gesture.initialScale! * event.scale),
- zoom,
- { left: offsetLeft, top: offsetTop },
- { x: cursorX, y: cursorY },
- ),
- }));
+ const initialScale = gesture.initialScale;
+ if (initialScale) {
+ this.setState(({ zoom, offsetLeft, offsetTop }) => ({
+ zoom: getNewZoom(
+ getNormalizedZoom(initialScale * event.scale),
+ zoom,
+ { left: offsetLeft, top: offsetTop },
+ { x: cursorX, y: cursorY },
+ ),
+ }));
+ }
});
private onGestureEnd = withBatchedUpdates((event: GestureEvent) => {
@@ -1745,20 +1748,26 @@ class App extends React.Component<ExcalidrawProps, AppState> {
});
}
- if (gesture.pointers.size === 2) {
+ const initialScale = gesture.initialScale;
+ if (
+ gesture.pointers.size === 2 &&
+ gesture.lastCenter &&
+ initialScale &&
+ gesture.initialDistance
+ ) {
const center = getCenter(gesture.pointers);
- const deltaX = center.x - gesture.lastCenter!.x;
- const deltaY = center.y - gesture.lastCenter!.y;
+ const deltaX = center.x - gesture.lastCenter.x;
+ const deltaY = center.y - gesture.lastCenter.y;
gesture.lastCenter = center;
const distance = getDistance(Array.from(gesture.pointers.values()));
- const scaleFactor = distance / gesture.initialDistance!;
+ const scaleFactor = distance / gesture.initialDistance;
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),
+ getNormalizedZoom(initialScale * scaleFactor),
zoom,
{ left: offsetLeft, top: offsetTop },
center,
diff --git a/src/packages/excalidraw/CHANGELOG.md b/src/packages/excalidraw/CHANGELOG.md
index e5aec1ff31..62ace97fa6 100644
--- a/src/packages/excalidraw/CHANGELOG.md
+++ b/src/packages/excalidraw/CHANGELOG.md
@@ -35,6 +35,7 @@ Please add the latest change on the top under the correct section.
- Fix element visibility and zoom on cursor when canvas offset isn't 0. [#2534](https://github.com/excalidraw/excalidraw/pull/2534)
- Fix Library Menu Layout [#2502](https://github.com/excalidraw/excalidraw/pull/2502)
- Support number with commas in charts [#2636](https://github.com/excalidraw/excalidraw/pull/2636)
+- Don't break zoom when zooming in on UI [#2638](https://github.com/excalidraw/excalidraw/pull/2638)
### Improvements