Skip to content

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

fix: improve withPinchZoom Maintain pointerRecords accurately, as previous schemes would cause pointerRecords to be confused(using touch events)

pubuzhixing8f39b1c81becf
diff --git a/apps/web/src/app/app.tsx b/apps/web/src/app/app.tsx
index 19f9e69..2a6cd31 100644
--- a/apps/web/src/app/app.tsx
+++ b/apps/web/src/app/app.tsx
@@ -55,12 +55,12 @@ export function App() {
afterInit={(board) => {
console.log('board initialized');
- console.log(
- `add __drawnix__web__debug_log to window, so you can call add log anywhere, like: window.__drawnix__web__console('some thing')`
- );
- (window as any)['__drawnix__web__console'] = (value: string) => {
- addDebugLog(board, value);
- };
+ // console.log(
+ // `add __drawnix__web__debug_log to window, so you can call add log anywhere, like: window.__drawnix__web__console('some thing')`
+ // );
+ // (window as any)['__drawnix__web__console'] = (value: string) => {
+ // addDebugLog(board, value);
+ // };
}}
></Drawnix>
);
diff --git a/apps/web/src/styles.scss b/apps/web/src/styles.scss
index 0a77b10..d96e32d 100644
--- a/apps/web/src/styles.scss
+++ b/apps/web/src/styles.scss
@@ -20,7 +20,7 @@ body {
transform: translateY(-50%);
left: 0;
height: 200px;
- width: 200px;
+ width: 100px;
overflow: auto;
background-color: black;
color: white;
diff --git a/package-lock.json b/package-lock.json
index 02699a7..cafd9b4 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -12,12 +12,12 @@
"@floating-ui/react": "^0.26.24",
"@plait-board/markdown-to-drawnix": "^0.0.7",
"@plait-board/mermaid-to-drawnix": "^0.0.7",
- "@plait/common": "^0.89.1",
- "@plait/core": "^0.89.1",
- "@plait/draw": "^0.89.1",
- "@plait/layouts": "^0.89.1",
- "@plait/mind": "^0.89.1",
- "@plait/text-plugins": "^0.89.1",
+ "@plait/common": "^0.89.2",
+ "@plait/core": "^0.89.2",
+ "@plait/draw": "^0.89.2",
+ "@plait/layouts": "^0.89.2",
+ "@plait/mind": "^0.89.2",
+ "@plait/text-plugins": "^0.89.2",
"ahooks": "^3.8.0",
"browser-fs-access": "^0.35.0",
"classnames": "^2.5.1",
@@ -4849,17 +4849,17 @@
}
},
"node_modules/@plait/common": {
- "version": "0.89.1",
- "resolved": "https://registry.npmjs.org/@plait/common/-/common-0.89.1.tgz",
- "integrity": "sha512-Z9FwLTxf+BGhGhRNzvSjfFIRcJl4sIt1r7F7RjgVAbxewBRPtZndmwPBm7A5G3KPnxzWblf9nTMsVG5IDdA7Kg==",
+ "version": "0.89.2",
+ "resolved": "https://registry.npmjs.org/@plait/common/-/common-0.89.2.tgz",
+ "integrity": "sha512-ySwXJq+0KCTei5lk6VoRxofehZ9yL5bwkTAQ/ALXgJ7+HsvaSA0orFA5gnxi1Hbbw/iyssFnWygX6/CW3A+h/A==",
"dependencies": {
"tslib": "^2.3.0"
}
},
"node_modules/@plait/core": {
- "version": "0.89.1",
- "resolved": "https://registry.npmjs.org/@plait/core/-/core-0.89.1.tgz",
- "integrity": "sha512-dLO4qiPkmMEKzOyVYkgVPJsB3R4+kBDSeKNzZqdZGlIbw0dPm7Fdn3JnAc3VFM92mjw0nQDRisDwVuWVkw1eCA==",
+ "version": "0.89.2",
+ "resolved": "https://registry.npmjs.org/@plait/core/-/core-0.89.2.tgz",
+ "integrity": "sha512-6qsQZy6Y9w7akTVNXNm+0PXgwoPmABNwqF73Z2lDuKfgaFGcEDpMsbtKZWT7nhYuBVD7wnv3Qvf2alwaweqJ8Q==",
"dependencies": {
"points-on-curve": "^1.0.0",
"roughjs": "^4.5.2",
@@ -4871,33 +4871,33 @@
}
},
"node_modules/@plait/draw": {
- "version": "0.89.1",
- "resolved": "https://registry.npmjs.org/@plait/draw/-/draw-0.89.1.tgz",
- "integrity": "sha512-4jd1FFaEuFcg6/tTP2VpJBCEn2jbVasBliy8/hvGq8N7qHs9WipaGtT0Ud6NXX48sn76tqZSvOjEsEmnCBQG5A==",
+ "version": "0.89.2",
+ "resolved": "https://registry.npmjs.org/@plait/draw/-/draw-0.89.2.tgz",
+ "integrity": "sha512-Hba0IBUgdKxekrkyHtzqsqIE2x2qSYygRbamimhG90PF8k9L33j4I6KmCiEIJ3RwcZNdhqN5kHxz55OmniEc+g==",
"dependencies": {
"tslib": "^2.3.0"
}
},
"node_modules/@plait/layouts": {
- "version": "0.89.1",
- "resolved": "https://registry.npmjs.org/@plait/layouts/-/layouts-0.89.1.tgz",
- "integrity": "sha512-kmPFv3uhkTSK35IuigtnA/aoX4+TNMgxptjVbQkq5W3vdLeMncU7RrmjkZK9FqLHmjHMzUhzVWqTGZTjiEwiOg==",
+ "version": "0.89.2",
+ "resolved": "https://registry.npmjs.org/@plait/layouts/-/layouts-0.89.2.tgz",
+ "integrity": "sha512-QxgkNzMXuz1LcTFW+IaGFnNJClHqZCnpDhgZ1VVWzTcLEykNq+W+r0yGrIcYmc1LFxboqiWKErpyCv0i1R0Njg==",
"dependencies": {
"tslib": "^2.3.0"
}
},
"node_modules/@plait/mind": {
- "version": "0.89.1",
- "resolved": "https://registry.npmjs.org/@plait/mind/-/mind-0.89.1.tgz",
- "integrity": "sha512-UUzrI768w29KlvjvXizqSfiwqFqoxOMPlmmalpv1B2rON2ha/kxH4HdIJd/yhaAfN9fVYyEjvhbSsqlylqsrQg==",
+ "version": "0.89.2",
+ "resolved": "https://registry.npmjs.org/@plait/mind/-/mind-0.89.2.tgz",
+ "integrity": "sha512-XhzoFFz4WIZOGttYbuDMH5/vg6A8xgGpvIoSdg8e4HDXlCVF6GtDHIPxeCfmQrWrOyyv3HtvdHqmifA1HZLEYQ==",
"dependencies": {
"tslib": "^2.3.0"
}
},
"node_modules/@plait/text-plugins": {
- "version": "0.89.1",
- "resolved": "https://registry.npmjs.org/@plait/text-plugins/-/text-plugins-0.89.1.tgz",
- "integrity": "sha512-O9CvQT3hTWn4KDBZMvIuI4Ca+efu4pAcYh08w5FzgttBabB1PsiJTQzX7m3YMWfV82o8c7+8Yc/Pp/uxZ12QmQ==",
+ "version": "0.89.2",
+ "resolved": "https://registry.npmjs.org/@plait/text-plugins/-/text-plugins-0.89.2.tgz",
+ "integrity": "sha512-F020zCFbkOClz5AeC9ntlsOJ7W5SJLieZi3hpaYIJX1V6GuY7gRr49FNHkrQDUhdf5V2rX9JtZn290sWTtjmAg==",
"dependencies": {
"tslib": "^2.3.0"
}
diff --git a/package.json b/package.json
index 88316e0..f4ebd27 100644
--- a/package.json
+++ b/package.json
@@ -16,12 +16,12 @@
"@floating-ui/react": "^0.26.24",
"@plait-board/markdown-to-drawnix": "^0.0.7",
"@plait-board/mermaid-to-drawnix": "^0.0.7",
- "@plait/common": "^0.89.1",
- "@plait/core": "^0.89.1",
- "@plait/draw": "^0.89.1",
- "@plait/layouts": "^0.89.1",
- "@plait/mind": "^0.89.1",
- "@plait/text-plugins": "^0.89.1",
+ "@plait/common": "^0.89.2",
+ "@plait/core": "^0.89.2",
+ "@plait/draw": "^0.89.2",
+ "@plait/layouts": "^0.89.2",
+ "@plait/mind": "^0.89.2",
+ "@plait/text-plugins": "^0.89.2",
"ahooks": "^3.8.0",
"browser-fs-access": "^0.35.0",
"classnames": "^2.5.1",
diff --git a/packages/react-board/src/plugins/with-pinch-zoom-plugin.ts b/packages/react-board/src/plugins/with-pinch-zoom-plugin.ts
index 803fe31..eb45a03 100644
--- a/packages/react-board/src/plugins/with-pinch-zoom-plugin.ts
+++ b/packages/react-board/src/plugins/with-pinch-zoom-plugin.ts
@@ -17,175 +17,141 @@ interface PointerRecord {
}
export const withPinchZoom = (board: PlaitBoard) => {
- const {
- pointerDown,
- pointerMove,
- pointerUp,
- globalPointerUp,
- touchStart,
- touchMove,
- } = board;
+ const { touchStart, touchMove, touchEnd } = board;
- const pointerRecords: PointerRecord[] = [];
+ let pointerRecords: PointerRecord[] = [];
let initializeZoom = 0;
let isPinching = false;
board.touchStart = (event: TouchEvent) => {
- if (event.touches.length >= 2) {
- event.preventDefault();
- return;
- }
- touchStart(event);
- };
-
- board.pointerDown = (event: PointerEvent) => {
- const point: Point = [event.clientX, event.clientY];
- (window as any)['__drawnix__web__console'](`pointerId${event.pointerId}`);
-
- if (pointerRecords.length < 2) {
+ pointerRecords = Array.from(event.touches).map((touch) => ({
+ pointerId: touch.identifier,
+ lastPoint: [touch.clientX, touch.clientY],
+ currentPoint: [touch.clientX, touch.clientY],
+ hasMoved: false,
+ }));
+ if (pointerRecords.length >= 2) {
initializeZoom = board.viewport.zoom;
- pointerRecords.push({
- pointerId: event.pointerId,
- lastPoint: point,
- currentPoint: point,
- hasMoved: false,
- });
}
-
- pointerDown(event);
+ touchStart(event);
};
- board.pointerMove = (event: PointerEvent) => {
- const point: Point = [event.clientX, event.clientY];
-
- if (pointerRecords.length >= 2) {
+ board.touchMove = (event: TouchEvent) => {
+ Array.from(event.changedTouches).forEach((touch) => {
const record = pointerRecords.find(
- (r) => r.pointerId === event.pointerId
+ (record) => record.pointerId === touch.identifier
);
if (record) {
- record.currentPoint = point;
+ record.lastPoint = record.currentPoint;
+ record.currentPoint = [touch.clientX, touch.clientY];
record.hasMoved = true;
-
- // 检查是否两个触控点都移动过
- if (
- pointerRecords.length === 2 &&
- pointerRecords.every((r) => r.hasMoved)
- ) {
- const [p1, p2] = pointerRecords;
- const pinchCenter = getPointBetween(
- ...p1.lastPoint,
- ...p2.lastPoint
- ) as Point;
- const newPinchCenter = getPointBetween(
- ...p1.currentPoint,
- ...p2.currentPoint
- ) as Point;
- const dx = newPinchCenter[0] - pinchCenter[0];
- const dy = newPinchCenter[1] - pinchCenter[1];
-
- // hand moving
- const boardContainerRect =
- PlaitBoard.getBoardContainer(board).getBoundingClientRect();
- const halfOfWidth = boardContainerRect.width / 2;
- const halfOfHeight = boardContainerRect.height / 2;
- const zoom = board.viewport.zoom;
- const origination = getViewportOrigination(board);
- let centerX = origination![0] + halfOfWidth / zoom - dx / zoom;
- let centerY = origination![1] + halfOfHeight / zoom - dy / zoom;
- let newOrigination = [
- centerX - boardContainerRect.width / 2 / zoom,
- centerY - boardContainerRect.height / 2 / zoom,
- ] as Point;
-
- let newZoom = zoom;
- const lastDistance = distanceBetweenPointAndPoint(
- ...p1.lastPoint,
- ...p2.lastPoint
- );
- const currentDistance = distanceBetweenPointAndPoint(
- ...p1.currentPoint,
- ...p2.currentPoint
- );
- // zoom 处理
- const scale = currentDistance / lastDistance;
-
- const v1 = [
- p1.currentPoint[0] - p1.lastPoint[0],
- p1.currentPoint[1] - p1.lastPoint[1],
- ] as Point;
- const v2 = [
- p2.currentPoint[0] - p2.lastPoint[0],
- p2.currentPoint[1] - p2.lastPoint[1],
- ] as Point;
-
- const dotProduct = v1[0] * v2[0] + v1[1] * v2[1];
- const v1Magnitude = Math.sqrt(v1[0] * v1[0] + v1[1] * v1[1]);
- const v2Magnitude = Math.sqrt(v2[0] * v2[0] + v2[1] * v2[1]);
-
- const cosTheta = dotProduct / (v1Magnitude * v2Magnitude || 1);
-
- // 控制缩放
- // 基于余弦相似度(Cosine Similarity)
- // 余弦值 > 0.8:判定为平移手势(向量基本同向)
- // 余弦值 < -0.7:判定为缩放手势(向量基本反向)
- // 其他情况:未知手势
- if (
- cosTheta < -0.7 ||
- (cosTheta <= 0.8 && isPinching && scale >= 0.01)
- ) {
- isPinching = true;
- } else {
- isPinching = false;
- }
- if (isPinching) {
- newZoom = Math.min(
- Math.max(board.viewport.zoom * scale, MIN_ZOOM),
- MAX_ZOOM
- );
- const nativeElement = PlaitBoard.getBoardContainer(board);
- const nativeElementRect = nativeElement.getBoundingClientRect();
- const zoomCenterWidth = newPinchCenter[0] - nativeElementRect.x;
- const zoomCenterHeight = newPinchCenter[1] - nativeElementRect.y;
- centerX = newOrigination[0] + zoomCenterWidth / zoom;
- centerY = newOrigination[1] + zoomCenterHeight / zoom;
- newOrigination = [
- centerX - zoomCenterWidth / newZoom,
- centerY - zoomCenterHeight / newZoom,
- ] as Point;
- }
- BoardTransforms.updateViewport(board, newOrigination, newZoom);
- pointerRecords.forEach((r) => {
- r.lastPoint = r.currentPoint;
- r.hasMoved = false;
- });
- }
}
- return;
+ });
+ if (pointerRecords.length === 2) {
+ event.preventDefault();
}
-
- pointerMove(event);
- };
-
- board.pointerUp = (event: PointerEvent) => {
- const index = pointerRecords.findIndex(
- (r) => r.pointerId === event.pointerId
- );
- if (index !== -1) {
- pointerRecords.splice(index, 1);
+ if (
+ pointerRecords.length === 2 &&
+ pointerRecords.every((record) => record.hasMoved)
+ ) {
+ const [p1, p2] = pointerRecords;
+ const pinchCenter = getPointBetween(
+ ...p1.lastPoint,
+ ...p2.lastPoint
+ ) as Point;
+ const newPinchCenter = getPointBetween(
+ ...p1.currentPoint,
+ ...p2.currentPoint
+ ) as Point;
+ const dx = newPinchCenter[0] - pinchCenter[0];
+ const dy = newPinchCenter[1] - pinchCenter[1];
+
+ // hand moving
+ const boardContainerRect =
+ PlaitBoard.getBoardContainer(board).getBoundingClientRect();
+ const halfOfWidth = boardContainerRect.width / 2;
+ const halfOfHeight = boardContainerRect.height / 2;
+ const zoom = board.viewport.zoom;
+ const origination = getViewportOrigination(board);
+ let centerX = origination![0] + halfOfWidth / zoom - dx / zoom;
+ let centerY = origination![1] + halfOfHeight / zoom - dy / zoom;
+ let newOrigination = [
+ centerX - boardContainerRect.width / 2 / zoom,
+ centerY - boardContainerRect.height / 2 / zoom,
+ ] as Point;
+
+ let newZoom = zoom;
+ const lastDistance = distanceBetweenPointAndPoint(
+ ...p1.lastPoint,
+ ...p2.lastPoint
+ );
+ const currentDistance = distanceBetweenPointAndPoint(
+ ...p1.currentPoint,
+ ...p2.currentPoint
+ );
+ // zoom 处理
+ const scale = currentDistance / lastDistance;
+
+ const v1 = [
+ p1.currentPoint[0] - p1.lastPoint[0],
+ p1.currentPoint[1] - p1.lastPoint[1],
+ ] as Point;
+ const v2 = [
+ p2.currentPoint[0] - p2.lastPoint[0],
+ p2.currentPoint[1] - p2.lastPoint[1],
+ ] as Point;
+
+ const dotProduct = v1[0] * v2[0] + v1[1] * v2[1];
+ const v1Magnitude = Math.sqrt(v1[0] * v1[0] + v1[1] * v1[1]);
+ const v2Magnitude = Math.sqrt(v2[0] * v2[0] + v2[1] * v2[1]);
+
+ const cosTheta = dotProduct / (v1Magnitude * v2Magnitude || 1);
+
+ // 控制缩放
+ // 基于余弦相似度(Cosine Similarity)
+ // 余弦值 > 0.8:判定为平移手势(向量基本同向)
+ // 余弦值 < -0.7:判定为缩放手势(向量基本反向)
+ // 其他情况:未知手势
+ if (cosTheta < -0.7 || (cosTheta <= 0.8 && isPinching && scale >= 0.01)) {
+ isPinching = true;
+ } else {
+ isPinching = false;
+ }
+ if (isPinching) {
+ newZoom = Math.min(
+ Math.max(board.viewport.zoom * scale, MIN_ZOOM),
+ MAX_ZOOM
+ );
+ const nativeElement = PlaitBoard.getBoardContainer(board);
+ const nativeElementRect = nativeElement.getBoundingClientRect();
+ const zoomCenterWidth = newPinchCenter[0] - nativeElementRect.x;
+ const zoomCenterHeight = newPinchCenter[1] - nativeElementRect.y;
+ centerX = newOrigination[0] + zoomCenterWidth / zoom;
+ centerY = newOrigination[1] + zoomCenterHeight / zoom;
+ newOrigination = [
+ centerX - zoomCenterWidth / newZoom,
+ centerY - zoomCenterHeight / newZoom,
+ ] as Point;
+ }
+ BoardTransforms.updateViewport(board, newOrigination, newZoom);
+ pointerRecords[0].lastPoint = p1.currentPoint;
+ pointerRecords[1].lastPoint = p2.currentPoint;
+ pointerRecords[0].hasMoved = false;
+ pointerRecords[1].hasMoved = false;
+ return;
}
-
- pointerUp(event);
+ touchMove(event);
};
- board.globalPointerUp = (event: PointerEvent) => {
+ board.touchEnd = (event: TouchEvent) => {
const index = pointerRecords.findIndex(
- (r) => r.pointerId === event.pointerId
+ (r) => r.pointerId === event.changedTouches[0].identifier
);
if (index !== -1) {
pointerRecords.splice(index, 1);
}
- globalPointerUp(event);
+ touchEnd(event);
};
return board;