Skip to content

开源白板工具(SaaS),一体化白板,包含思维导图、流程图、自由画等。All in one open-source whiteboard tool with mind, flowchart, freehand and etc.

feat(pinch-zoom): support pinch zoom for touch device (#60)

pubuzhixing8b657a570d5cd
diff --git a/packages/react-board/src/hooks/use-board-event.ts b/packages/react-board/src/hooks/use-board-event.ts
index 65aebaa..d102ded 100644
--- a/packages/react-board/src/hooks/use-board-event.ts
+++ b/packages/react-board/src/hooks/use-board-event.ts
@@ -60,7 +60,11 @@ const useBoardEvent = (
-sign *
// reduced amplification for small deltas (small movements on a trackpad)
Math.min(1, absDelta / 20);
- BoardTransforms.updateZoom(board, newZoom, false);
+ BoardTransforms.updateZoom(
+ board,
+ newZoom,
+ PlaitBoard.getMovingPointInBoard(board)
+ );
}
},
{ target: viewportContainerRef, passive: false }
diff --git a/packages/react-board/src/hooks/with-pinch-zoom-plugin.ts b/packages/react-board/src/hooks/with-pinch-zoom-plugin.ts
new file mode 100644
index 0000000..4637e89
--- /dev/null
+++ b/packages/react-board/src/hooks/with-pinch-zoom-plugin.ts
@@ -0,0 +1,104 @@
+import {
+ BoardTransforms,
+ distanceBetweenPointAndPoint,
+ MAX_ZOOM,
+ MIN_ZOOM,
+ PlaitBoard,
+ Point,
+ throttleRAF,
+ toHostPoint,
+ toViewBoxPoint,
+} from '@plait/core';
+
+export const withPinchZoom = (board: PlaitBoard) => {
+ const { pointerDown, pointerMove, pointerUp, globalPointerUp } = board;
+ let isPinching = false;
+ let initialDistance = 0;
+ let initialZoom = 1;
+ let lastZoom = 1;
+ let touchPoints: {
+ [key: number]: [number, number];
+ } = {};
+ let pinchCenter: [number, number] | null = null;
+
+ const getMidPoint = (
+ p1: [number, number],
+ p2: [number, number]
+ ): [number, number] => {
+ return [(p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2];
+ };
+
+ board.pointerDown = (event: PointerEvent) => {
+ const point = [event.x, event.y] as Point;
+ touchPoints[event.pointerId] = point;
+ const touchPointsCount = Object.keys(touchPoints).length;
+ if (touchPointsCount === 2) {
+ isPinching = true;
+ const points = Object.values(touchPoints);
+ initialDistance = distanceBetweenPointAndPoint(
+ points[0][0],
+ points[0][1],
+ points[1][0],
+ points[1][1]
+ );
+ initialZoom = board.viewport.zoom;
+ lastZoom = initialZoom;
+ pinchCenter = getMidPoint(
+ toViewBoxPoint(board, toHostPoint(board, points[0][0], points[0][1])),
+ toViewBoxPoint(board, toHostPoint(board, points[1][0], points[1][1]))
+ );
+ return;
+ }
+ pointerDown(event);
+ };
+
+ board.pointerMove = (event: PointerEvent) => {
+ if (isPinching) {
+ const point = [event.x, event.y] as Point;
+ touchPoints[event.pointerId] = point;
+ const points = Object.values(touchPoints);
+ if (points.length === 2) {
+ const currentDistance = distanceBetweenPointAndPoint(
+ points[0][0],
+ points[0][1],
+ points[1][0],
+ points[1][1]
+ );
+ const scale = currentDistance / initialDistance;
+ const nextZoom = Math.min(
+ Math.max(initialZoom * scale, MIN_ZOOM),
+ MAX_ZOOM
+ );
+ throttleRAF(board, 'pinch-zoom', () => {
+ pinchCenter &&
+ BoardTransforms.updateZoom(board, nextZoom, pinchCenter);
+ });
+ }
+ return;
+ }
+
+ pointerMove(event);
+ };
+
+ board.pointerUp = (event: PointerEvent) => {
+ delete touchPoints[event.pointerId];
+ if (Object.keys(touchPoints).length < 2) {
+ isPinching = false;
+ initialDistance = 0;
+ initialZoom = 1;
+ lastZoom = 1;
+ }
+ pointerUp(event);
+ };
+
+ board.globalPointerUp = (event: PointerEvent) => {
+ touchPoints = {};
+ isPinching = false;
+ initialDistance = 0;
+ initialZoom = 1;
+ lastZoom = 1;
+ globalPointerUp(event);
+ };
+
+ return board;
+};
diff --git a/packages/react-board/src/wrapper.tsx b/packages/react-board/src/wrapper.tsx
index c231082..a7fd12d 100644
--- a/packages/react-board/src/wrapper.tsx
+++ b/packages/react-board/src/wrapper.tsx
@@ -28,6 +28,7 @@ import { withReact } from './plugins/with-react';
import { withImage, withText } from '@plait/common';
import { BoardContext, BoardContextValue } from './hooks/use-board';
import React from 'react';
+import { withPinchZoom } from './hooks/with-pinch-zoom-plugin';
export type WrapperProps = {
value: PlaitElement[];
@@ -128,12 +129,16 @@ const initializeBoard = (
withHotkey(
withHandPointer(
withHistory(
- withSelection(
- withMoving(
- withBoard(
- withViewport(
- withOptions(
- withReact(withImage(withText(createBoard(value, options))))
+ withPinchZoom(
+ withSelection(
+ withMoving(
+ withBoard(
+ withViewport(
+ withOptions(
+ withReact(
+ withImage(withText(createBoard(value, options)))
+ )
+ )
)
)
)