@antv/g6 5.0.41 → 5.0.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/README.md +1 -1
  2. package/README.zh-CN.md +1 -1
  3. package/dist/g6.min.js +29 -29
  4. package/dist/lib/behaviors/zoom-canvas.d.ts +4 -4
  5. package/dist/lib/constants/events/common.d.ts +7 -1
  6. package/dist/lib/runtime/element.d.ts +1 -0
  7. package/dist/lib/runtime/graph.d.ts +2 -1
  8. package/dist/lib/runtime/layout.d.ts +3 -2
  9. package/dist/lib/utils/pinch.d.ts +116 -0
  10. package/dist/lib/utils/shortcut.d.ts +2 -0
  11. package/dist/lib/version.d.ts +1 -1
  12. package/esm/behaviors/drag-canvas.js +3 -1
  13. package/esm/behaviors/drag-canvas.js.map +1 -1
  14. package/esm/behaviors/zoom-canvas.d.ts +4 -4
  15. package/esm/behaviors/zoom-canvas.js +13 -6
  16. package/esm/behaviors/zoom-canvas.js.map +1 -1
  17. package/esm/constants/events/common.d.ts +7 -1
  18. package/esm/constants/events/common.js +6 -0
  19. package/esm/constants/events/common.js.map +1 -1
  20. package/esm/runtime/canvas.js +3 -0
  21. package/esm/runtime/canvas.js.map +1 -1
  22. package/esm/runtime/element.d.ts +1 -0
  23. package/esm/runtime/element.js +6 -2
  24. package/esm/runtime/element.js.map +1 -1
  25. package/esm/runtime/graph.d.ts +2 -1
  26. package/esm/runtime/graph.js +3 -2
  27. package/esm/runtime/graph.js.map +1 -1
  28. package/esm/runtime/layout.d.ts +3 -2
  29. package/esm/runtime/layout.js +4 -3
  30. package/esm/runtime/layout.js.map +1 -1
  31. package/esm/utils/pinch.d.ts +116 -0
  32. package/esm/utils/pinch.js +124 -0
  33. package/esm/utils/pinch.js.map +1 -0
  34. package/esm/utils/shortcut.d.ts +2 -0
  35. package/esm/utils/shortcut.js +10 -2
  36. package/esm/utils/shortcut.js.map +1 -1
  37. package/esm/version.d.ts +1 -1
  38. package/esm/version.js +1 -1
  39. package/lib/behaviors/drag-canvas.js +3 -1
  40. package/lib/behaviors/drag-canvas.js.map +1 -1
  41. package/lib/behaviors/zoom-canvas.d.ts +4 -4
  42. package/lib/behaviors/zoom-canvas.js +13 -6
  43. package/lib/behaviors/zoom-canvas.js.map +1 -1
  44. package/lib/constants/events/common.d.ts +7 -1
  45. package/lib/constants/events/common.js +6 -0
  46. package/lib/constants/events/common.js.map +1 -1
  47. package/lib/runtime/canvas.js +3 -0
  48. package/lib/runtime/canvas.js.map +1 -1
  49. package/lib/runtime/element.d.ts +1 -0
  50. package/lib/runtime/element.js +6 -2
  51. package/lib/runtime/element.js.map +1 -1
  52. package/lib/runtime/graph.d.ts +2 -1
  53. package/lib/runtime/graph.js +3 -2
  54. package/lib/runtime/graph.js.map +1 -1
  55. package/lib/runtime/layout.d.ts +3 -2
  56. package/lib/runtime/layout.js +4 -3
  57. package/lib/runtime/layout.js.map +1 -1
  58. package/lib/utils/pinch.d.ts +116 -0
  59. package/lib/utils/pinch.js +128 -0
  60. package/lib/utils/pinch.js.map +1 -0
  61. package/lib/utils/shortcut.d.ts +2 -0
  62. package/lib/utils/shortcut.js +10 -2
  63. package/lib/utils/shortcut.js.map +1 -1
  64. package/lib/version.d.ts +1 -1
  65. package/lib/version.js +1 -1
  66. package/package.json +2 -1
  67. package/src/behaviors/drag-canvas.ts +5 -4
  68. package/src/behaviors/zoom-canvas.ts +23 -9
  69. package/src/constants/events/common.ts +6 -0
  70. package/src/runtime/canvas.ts +4 -0
  71. package/src/runtime/element.ts +7 -2
  72. package/src/runtime/graph.ts +3 -2
  73. package/src/runtime/layout.ts +5 -4
  74. package/src/utils/pinch.ts +176 -0
  75. package/src/utils/shortcut.ts +11 -0
  76. package/src/version.ts +1 -1
@@ -0,0 +1,176 @@
1
+ import EventEmitter from '@antv/event-emitter';
2
+ import { CommonEvent } from '../constants';
3
+ import { IPointerEvent } from '../types';
4
+
5
+ /**
6
+ * <zh/> 表示指针位置的点坐标
7
+ *
8
+ * <en/> Represents the coordinates of a pointer position
9
+ */
10
+ export interface PointerPoint {
11
+ x: number;
12
+ y: number;
13
+ pointerId: number;
14
+ }
15
+
16
+ /**
17
+ * <zh/> 捏合事件参数
18
+ *
19
+ * <en/> Pinch event parameters
20
+ * @remarks
21
+ * <zh/> 包含与捏合手势相关的参数,当前支持缩放比例,未来可扩展中心点坐标、旋转角度等参数
22
+ *
23
+ * <en/> Contains parameters related to pinch gestures, currently supports scale factor,
24
+ * can be extended with center coordinates, rotation angle etc. in the future
25
+ */
26
+ export interface PinchEventOptions {
27
+ /**
28
+ * <zh/> 缩放比例因子,>1 表示放大,<1 表示缩小
29
+ *
30
+ * <en/> Scaling factor, >1 indicates zoom in, <1 indicates zoom out
31
+ */
32
+ scale: number;
33
+ }
34
+
35
+ /**
36
+ * <zh/> 捏合手势回调函数类型
37
+ *
38
+ * <en/> Pinch gesture callback function type
39
+ * @param event - <zh/> 原始指针事件对象 | <en/> Original pointer event object
40
+ * @param options - <zh/> 捏合事件参数对象 | <en/> Pinch event parameters object
41
+ */
42
+ export type PinchCallback = (event: IPointerEvent, options: PinchEventOptions) => void;
43
+
44
+ /**
45
+ * <zh/> 捏合手势处理器
46
+ *
47
+ * <en/> Pinch gesture handler
48
+ * @remarks
49
+ * <zh/> 处理双指触摸事件,计算缩放比例并触发回调。通过跟踪两个触摸点的位置变化,计算两点间距离变化率来确定缩放比例。
50
+ *
51
+ * <en/> Handles two-finger touch events, calculates zoom ratio and triggers callbacks. Tracks position changes of two touch points to determine zoom ratio based on distance variation.
52
+ */
53
+ export class PinchHandler {
54
+ /**
55
+ * <zh/> 当前跟踪的触摸点集合
56
+ *
57
+ * <en/> Currently tracked touch points collection
58
+ */
59
+ private pointerByTouch: PointerPoint[] = [];
60
+
61
+ /**
62
+ * <zh/> 初始两点间距离
63
+ *
64
+ * <en/> Initial distance between two points
65
+ */
66
+ private initialDistance: number | null = null;
67
+
68
+ private emitter: EventEmitter;
69
+
70
+ constructor(
71
+ emitter: EventEmitter,
72
+ private callback: PinchCallback,
73
+ ) {
74
+ this.emitter = emitter;
75
+ this.onPointerDown = this.onPointerDown.bind(this);
76
+ this.onPointerMove = this.onPointerMove.bind(this);
77
+ this.onPointerUp = this.onPointerUp.bind(this);
78
+ this.bindEvents();
79
+ }
80
+
81
+ private bindEvents() {
82
+ const { emitter } = this;
83
+ emitter.on(CommonEvent.POINTER_DOWN, this.onPointerDown);
84
+ emitter.on(CommonEvent.POINTER_MOVE, this.onPointerMove);
85
+ emitter.on(CommonEvent.POINTER_UP, this.onPointerUp);
86
+ }
87
+
88
+ /**
89
+ * <zh/> 更新指定指针的位置
90
+ *
91
+ * <en/> Update position of specified pointer
92
+ * @param pointerId - <zh/> 指针唯一标识符 | <en/> Pointer unique identifier<sup>1</sup>
93
+ * @param x - <zh/> 新的X坐标 | <en/> New X coordinate
94
+ * @param y - <zh/> 新的Y坐标 | <en/> New Y coordinate
95
+ */
96
+ private updatePointerPosition(pointerId: number, x: number, y: number) {
97
+ const index = this.pointerByTouch.findIndex((p) => p.pointerId === pointerId);
98
+ if (index >= 0) {
99
+ this.pointerByTouch[index] = { x, y, pointerId };
100
+ }
101
+ }
102
+
103
+ /**
104
+ * <zh/> 处理指针按下事件
105
+ *
106
+ * <en/> Handle pointer down event
107
+ * @param event - <zh/> 指针事件对象 | <en/> Pointer event object
108
+ * @remarks
109
+ * <zh/> 当检测到两个触摸点时记录初始距离
110
+ *
111
+ * <en/> Record initial distance when detecting two touch points
112
+ */
113
+ onPointerDown(event: IPointerEvent) {
114
+ const { x, y } = event.client;
115
+ if (x === undefined || y === undefined) return;
116
+ this.pointerByTouch.push({ x, y, pointerId: event.pointerId });
117
+
118
+ if (event.pointerType === 'touch' && this.pointerByTouch.length === 2) {
119
+ const dx = this.pointerByTouch[0].x - this.pointerByTouch[1].x;
120
+ const dy = this.pointerByTouch[0].y - this.pointerByTouch[1].y;
121
+ this.initialDistance = Math.sqrt(dx * dx + dy * dy);
122
+ }
123
+ }
124
+
125
+ /**
126
+ * <zh/> 处理指针移动事件
127
+ *
128
+ * <en/> Handle pointer move event
129
+ * @param event - <zh/> 指针事件对象 | <en/> Pointer event object
130
+ * @remarks
131
+ * <zh/> 当存在两个有效触摸点时计算缩放比例
132
+ *
133
+ * <en/> Calculate zoom ratio when two valid touch points exist
134
+ */
135
+ onPointerMove(event: IPointerEvent) {
136
+ if (this.pointerByTouch.length !== 2 || this.initialDistance === null) return;
137
+ const { x, y } = event.client;
138
+ if (x === undefined || y === undefined) return;
139
+ this.updatePointerPosition(event.pointerId, x, y);
140
+ const dx = this.pointerByTouch[0].x - this.pointerByTouch[1].x;
141
+ const dy = this.pointerByTouch[0].y - this.pointerByTouch[1].y;
142
+ const currentDistance = Math.sqrt(dx * dx + dy * dy);
143
+ const ratio = currentDistance / this.initialDistance;
144
+
145
+ this.callback(event, { scale: (ratio - 1) * 5 });
146
+ }
147
+
148
+ /**
149
+ * <zh/> 处理指针抬起事件
150
+ *
151
+ * <en/> Handle pointer up event
152
+ * @remarks
153
+ * <zh/> 重置触摸状态和初始距离
154
+ *
155
+ * <en/> Reset touch state and initial distance
156
+ */
157
+ onPointerUp() {
158
+ this.initialDistance = null;
159
+ this.pointerByTouch = [];
160
+ }
161
+
162
+ /**
163
+ * <zh/> 销毁捏合手势相关监听
164
+ *
165
+ * <en/> Destroy pinch gesture listeners
166
+ * @remarks
167
+ * <zh/> 移除指针按下、移动、抬起事件的监听
168
+ *
169
+ * <en/> Remove listeners for pointer down, move, and up events
170
+ */
171
+ destroy() {
172
+ this.emitter.off(CommonEvent.POINTER_DOWN, this.onPointerDown);
173
+ this.emitter.off(CommonEvent.POINTER_MOVE, this.onPointerMove);
174
+ this.emitter.off(CommonEvent.POINTER_UP, this.onPointerUp);
175
+ }
176
+ }
@@ -2,6 +2,8 @@ import EventEmitter from '@antv/event-emitter';
2
2
  import type { FederatedMouseEvent } from '@antv/g';
3
3
  import { isEqual, isString } from '@antv/util';
4
4
  import { CommonEvent } from '../constants';
5
+ import type { PinchCallback } from './pinch';
6
+ import { PinchHandler } from './pinch';
5
7
 
6
8
  export interface ShortcutOptions {}
7
9
 
@@ -13,6 +15,7 @@ const lowerCaseKeys = (keys: ShortcutKey) => keys.map((key) => (isString(key) ?
13
15
 
14
16
  export class Shortcut {
15
17
  private map: Map<ShortcutKey, Handler> = new Map();
18
+ private pinchHandler: PinchHandler | undefined;
16
19
 
17
20
  private emitter: EventEmitter;
18
21
 
@@ -25,6 +28,9 @@ export class Shortcut {
25
28
 
26
29
  public bind(key: ShortcutKey, handler: Handler) {
27
30
  if (key.length === 0) return;
31
+ if (key.includes(CommonEvent.PINCH) && !this.pinchHandler) {
32
+ this.pinchHandler = new PinchHandler(this.emitter, this.handlePinch.bind(this));
33
+ }
28
34
  this.map.set(key, handler);
29
35
  }
30
36
 
@@ -107,6 +113,10 @@ export class Shortcut {
107
113
  this.triggerExtendKey(CommonEvent.DRAG, event);
108
114
  };
109
115
 
116
+ private handlePinch: PinchCallback = (event, options) => {
117
+ this.triggerExtendKey(CommonEvent.PINCH, { ...event, ...options });
118
+ };
119
+
110
120
  private onFocus = () => {
111
121
  this.recordKey.clear();
112
122
  };
@@ -117,6 +127,7 @@ export class Shortcut {
117
127
  this.emitter.off(CommonEvent.KEY_UP, this.onKeyUp);
118
128
  this.emitter.off(CommonEvent.WHEEL, this.onWheel);
119
129
  this.emitter.off(CommonEvent.DRAG, this.onDrag);
130
+ this.pinchHandler?.destroy();
120
131
  globalThis.removeEventListener?.('blur', this.onFocus);
121
132
  }
122
133
  }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '5.0.41';
1
+ export const version = '5.0.43';