@antv/g6 5.0.15 → 5.0.16

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 (149) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/dist/lib/behaviors/drag-canvas.d.ts +13 -0
  3. package/dist/lib/behaviors/scroll-canvas.d.ts +7 -0
  4. package/dist/lib/behaviors/zoom-canvas.d.ts +7 -0
  5. package/dist/lib/elements/edges/base-edge.d.ts +2 -2
  6. package/dist/lib/elements/edges/polyline.d.ts +3 -20
  7. package/dist/lib/plugins/index.d.ts +2 -0
  8. package/dist/lib/plugins/snapline/index.d.ts +95 -0
  9. package/dist/lib/runtime/element.d.ts +1 -0
  10. package/dist/lib/runtime/viewport.d.ts +8 -0
  11. package/dist/lib/types/index.d.ts +1 -0
  12. package/dist/lib/types/router.d.ts +103 -0
  13. package/dist/lib/utils/bbox.d.ts +8 -0
  14. package/dist/lib/utils/router/orth.d.ts +8 -8
  15. package/dist/lib/utils/router/shortest-path.d.ts +57 -0
  16. package/dist/lib/utils/text.d.ts +1 -1
  17. package/dist/lib/utils/visibility.d.ts +2 -1
  18. package/dist/lib/version.d.ts +1 -1
  19. package/esm/behaviors/brush-select.js +11 -6
  20. package/esm/behaviors/brush-select.js.map +1 -1
  21. package/esm/behaviors/drag-canvas.d.ts +13 -0
  22. package/esm/behaviors/drag-canvas.js +10 -1
  23. package/esm/behaviors/drag-canvas.js.map +1 -1
  24. package/esm/behaviors/optimize-viewport-transform.js +1 -1
  25. package/esm/behaviors/optimize-viewport-transform.js.map +1 -1
  26. package/esm/behaviors/scroll-canvas.d.ts +7 -0
  27. package/esm/behaviors/scroll-canvas.js +3 -1
  28. package/esm/behaviors/scroll-canvas.js.map +1 -1
  29. package/esm/behaviors/zoom-canvas.d.ts +7 -0
  30. package/esm/behaviors/zoom-canvas.js +3 -1
  31. package/esm/behaviors/zoom-canvas.js.map +1 -1
  32. package/esm/elements/edges/base-edge.d.ts +2 -2
  33. package/esm/elements/edges/base-edge.js +10 -4
  34. package/esm/elements/edges/base-edge.js.map +1 -1
  35. package/esm/elements/edges/polyline.d.ts +3 -20
  36. package/esm/elements/edges/polyline.js +29 -34
  37. package/esm/elements/edges/polyline.js.map +1 -1
  38. package/esm/elements/shapes/base-shape.js +1 -1
  39. package/esm/elements/shapes/base-shape.js.map +1 -1
  40. package/esm/plugins/index.d.ts +2 -0
  41. package/esm/plugins/index.js +1 -0
  42. package/esm/plugins/index.js.map +1 -1
  43. package/esm/plugins/snapline/index.d.ts +95 -0
  44. package/esm/plugins/snapline/index.js +268 -0
  45. package/esm/plugins/snapline/index.js.map +1 -0
  46. package/esm/registry/build-in.js +3 -2
  47. package/esm/registry/build-in.js.map +1 -1
  48. package/esm/runtime/element.d.ts +1 -0
  49. package/esm/runtime/element.js +10 -4
  50. package/esm/runtime/element.js.map +1 -1
  51. package/esm/runtime/viewport.d.ts +8 -0
  52. package/esm/runtime/viewport.js +35 -2
  53. package/esm/runtime/viewport.js.map +1 -1
  54. package/esm/types/index.d.ts +1 -0
  55. package/esm/types/router.d.ts +103 -0
  56. package/esm/types/router.js +2 -0
  57. package/esm/types/router.js.map +1 -0
  58. package/esm/utils/bbox.d.ts +8 -0
  59. package/esm/utils/bbox.js +19 -0
  60. package/esm/utils/bbox.js.map +1 -1
  61. package/esm/utils/router/orth.d.ts +8 -8
  62. package/esm/utils/router/orth.js +13 -7
  63. package/esm/utils/router/orth.js.map +1 -1
  64. package/esm/utils/router/shortest-path.d.ts +57 -0
  65. package/esm/utils/router/shortest-path.js +387 -0
  66. package/esm/utils/router/shortest-path.js.map +1 -0
  67. package/esm/utils/text.d.ts +1 -1
  68. package/esm/utils/visibility.d.ts +2 -1
  69. package/esm/utils/visibility.js +3 -2
  70. package/esm/utils/visibility.js.map +1 -1
  71. package/esm/version.d.ts +1 -1
  72. package/esm/version.js +1 -1
  73. package/lib/behaviors/brush-select.js +11 -6
  74. package/lib/behaviors/brush-select.js.map +1 -1
  75. package/lib/behaviors/drag-canvas.d.ts +13 -0
  76. package/lib/behaviors/drag-canvas.js +10 -1
  77. package/lib/behaviors/drag-canvas.js.map +1 -1
  78. package/lib/behaviors/optimize-viewport-transform.js +1 -1
  79. package/lib/behaviors/optimize-viewport-transform.js.map +1 -1
  80. package/lib/behaviors/scroll-canvas.d.ts +7 -0
  81. package/lib/behaviors/scroll-canvas.js +3 -1
  82. package/lib/behaviors/scroll-canvas.js.map +1 -1
  83. package/lib/behaviors/zoom-canvas.d.ts +7 -0
  84. package/lib/behaviors/zoom-canvas.js +3 -1
  85. package/lib/behaviors/zoom-canvas.js.map +1 -1
  86. package/lib/elements/edges/base-edge.d.ts +2 -2
  87. package/lib/elements/edges/base-edge.js +9 -3
  88. package/lib/elements/edges/base-edge.js.map +1 -1
  89. package/lib/elements/edges/polyline.d.ts +3 -20
  90. package/lib/elements/edges/polyline.js +29 -34
  91. package/lib/elements/edges/polyline.js.map +1 -1
  92. package/lib/elements/shapes/base-shape.js +1 -1
  93. package/lib/elements/shapes/base-shape.js.map +1 -1
  94. package/lib/plugins/index.d.ts +2 -0
  95. package/lib/plugins/index.js +3 -1
  96. package/lib/plugins/index.js.map +1 -1
  97. package/lib/plugins/snapline/index.d.ts +95 -0
  98. package/lib/plugins/snapline/index.js +272 -0
  99. package/lib/plugins/snapline/index.js.map +1 -0
  100. package/lib/registry/build-in.js +2 -1
  101. package/lib/registry/build-in.js.map +1 -1
  102. package/lib/runtime/element.d.ts +1 -0
  103. package/lib/runtime/element.js +9 -3
  104. package/lib/runtime/element.js.map +1 -1
  105. package/lib/runtime/viewport.d.ts +8 -0
  106. package/lib/runtime/viewport.js +34 -1
  107. package/lib/runtime/viewport.js.map +1 -1
  108. package/lib/types/index.d.ts +1 -0
  109. package/lib/types/router.d.ts +103 -0
  110. package/lib/types/router.js +3 -0
  111. package/lib/types/router.js.map +1 -0
  112. package/lib/utils/bbox.d.ts +8 -0
  113. package/lib/utils/bbox.js +20 -0
  114. package/lib/utils/bbox.js.map +1 -1
  115. package/lib/utils/router/orth.d.ts +8 -8
  116. package/lib/utils/router/orth.js +13 -7
  117. package/lib/utils/router/orth.js.map +1 -1
  118. package/lib/utils/router/shortest-path.d.ts +57 -0
  119. package/lib/utils/router/shortest-path.js +394 -0
  120. package/lib/utils/router/shortest-path.js.map +1 -0
  121. package/lib/utils/text.d.ts +1 -1
  122. package/lib/utils/visibility.d.ts +2 -1
  123. package/lib/utils/visibility.js +3 -2
  124. package/lib/utils/visibility.js.map +1 -1
  125. package/lib/version.d.ts +1 -1
  126. package/lib/version.js +1 -1
  127. package/package.json +1 -1
  128. package/src/behaviors/brush-select.ts +12 -5
  129. package/src/behaviors/drag-canvas.ts +24 -1
  130. package/src/behaviors/optimize-viewport-transform.ts +1 -0
  131. package/src/behaviors/scroll-canvas.ts +9 -1
  132. package/src/behaviors/zoom-canvas.ts +9 -1
  133. package/src/elements/edges/base-edge.ts +19 -5
  134. package/src/elements/edges/polyline.ts +33 -68
  135. package/src/elements/shapes/base-shape.ts +1 -1
  136. package/src/plugins/grid-line.ts +1 -1
  137. package/src/plugins/index.ts +2 -0
  138. package/src/plugins/snapline/index.ts +383 -0
  139. package/src/registry/build-in.ts +3 -1
  140. package/src/runtime/element.ts +11 -4
  141. package/src/runtime/viewport.ts +42 -3
  142. package/src/types/index.ts +1 -0
  143. package/src/types/router.ts +107 -0
  144. package/src/utils/bbox.ts +24 -0
  145. package/src/utils/router/orth.ts +19 -10
  146. package/src/utils/router/shortest-path.ts +470 -0
  147. package/src/utils/text.ts +1 -1
  148. package/src/utils/visibility.ts +3 -1
  149. package/src/version.ts +1 -1
@@ -1,10 +1,11 @@
1
- import { AABB } from '@antv/g';
1
+ import { AABB, ICamera } from '@antv/g';
2
2
  import { clamp, isNumber, pick } from '@antv/util';
3
3
  import { AnimationType, GraphEvent } from '../constants';
4
4
  import type { FitViewOptions, ID, Point, TransformOptions, Vector2, ViewportAnimationEffectTiming } from '../types';
5
5
  import { getAnimationOptions } from '../utils/animation';
6
- import { getBBoxSize, getCombinedBBox } from '../utils/bbox';
6
+ import { getBBoxSize, getCombinedBBox, isPointInBBox } from '../utils/bbox';
7
7
  import { AnimateEvent, ViewportEvent, emit } from '../utils/event';
8
+ import { isPoint } from '../utils/is';
8
9
  import { parsePadding } from '../utils/padding';
9
10
  import { add, divide, subtract } from '../utils/vector';
10
11
  import type { RuntimeContext } from './types';
@@ -30,7 +31,25 @@ export class ViewportController {
30
31
  }
31
32
 
32
33
  private get camera() {
33
- return this.context.canvas.getCamera();
34
+ const { canvas } = this.context;
35
+ return new Proxy(canvas.getCamera(), {
36
+ get: (target, prop: keyof ICamera) => {
37
+ const layers = Object.entries(canvas.getLayers()).filter(([name]) => !['main'].includes(name));
38
+ const cameras = layers.map(([, layer]) => layer.getCamera());
39
+
40
+ const value = target[prop];
41
+ if (typeof value === 'function') {
42
+ return (...args: any[]) => {
43
+ const result = (value as (...args: any[]) => any).apply(target, args);
44
+ cameras.forEach((camera) => {
45
+ (camera[prop] as (...args: any[]) => any).apply(camera, args);
46
+ });
47
+
48
+ return result;
49
+ };
50
+ }
51
+ },
52
+ });
34
53
  }
35
54
 
36
55
  private landmarkCounter = 0;
@@ -255,6 +274,26 @@ export class ViewportController {
255
274
  return bboxInViewport;
256
275
  }
257
276
 
277
+ /**
278
+ * <zh/> 判断点或包围盒是否在视口中
279
+ *
280
+ * <en/> Determine whether the point or bounding box is in the viewport
281
+ * @param target - <zh/> 点或包围盒 | <en/> Point or bounding box
282
+ * @returns - <zh/> 是否在视口中 | <en/> Whether it is in the viewport
283
+ */
284
+ public isInViewport(target: Point | AABB) {
285
+ const { graph } = this.context;
286
+ const size = this.getCanvasSize();
287
+
288
+ const [x1, y1] = graph.getCanvasByViewport([0, 0]);
289
+ const [x2, y2] = graph.getCanvasByViewport(size);
290
+
291
+ const viewportBBox = new AABB();
292
+ viewportBBox.setMinMax([x1, y1, 0], [x2, y2, 0]);
293
+
294
+ return isPoint(target) ? isPointInBBox(target, viewportBBox) : viewportBBox.intersects(target);
295
+ }
296
+
258
297
  public cancelAnimation() {
259
298
  // @ts-expect-error landmarks is private
260
299
  if (this.camera.landmarks?.length) {
@@ -16,6 +16,7 @@ export type * from './padding';
16
16
  export type * from './placement';
17
17
  export type * from './point';
18
18
  export type * from './prefix';
19
+ export type * from './router';
19
20
  export type * from './size';
20
21
  export type * from './state';
21
22
  export type * from './style';
@@ -0,0 +1,107 @@
1
+ import type { Padding } from './padding';
2
+ import type { CardinalPlacement } from './placement';
3
+ import { Point } from './point';
4
+
5
+ export type Direction = CardinalPlacement;
6
+
7
+ export type PolylineRouter = false | OrthRouter | ShortestPathRouter;
8
+
9
+ export interface OrthRouter extends OrthRouterOptions {
10
+ /**
11
+ * <zh/> 正交路由,通过在路径上添加额外的控制点,使得边的每一段都保持水平或垂直
12
+ *
13
+ * <en/> Orthogonal routing that adds additional control points on the path to ensure each segment of the edge horizontal or vertical
14
+ * @remarks
15
+ * <zh/> 采用基于节点的相对位置和专家经验得出的寻径算法来模糊计算控制点,非最优解但计算速度快。该路由支持 `controlPoints` 来作为额外的控制点,但不支持自动避障。
16
+ *
17
+ * <en/> It uses a pathfinding algorithm based on the relative position of the nodes and expert experience to calculate the control points, which is not the optimal solution but is fast to calculate. This routing supports `controlPoints` as additional control points, but does not support automatic obstacle avoidance.
18
+ */
19
+ type: 'orth';
20
+ }
21
+
22
+ export interface ShortestPathRouter extends ShortestPathRouterOptions {
23
+ /**
24
+ * <zh/> 最短路径路由,是正交路由 `'orth'` 的智能版本。该路由由水平或垂直的正交线段组成。采用 A* 算法计算最短路径,并支持自动避开路径上的其他节点(障碍)
25
+ *
26
+ * <en/> The shortest path routing is an intelligent version of the orthogonal routing `'orth'`. The routing consists of horizontal or vertical orthogonal line segments. It uses the A* algorithm to calculate the shortest path and supports automatic avoidance of other nodes (obstacles) on the path.
27
+ */
28
+ type: 'shortest-path';
29
+ }
30
+
31
+ export type RouterOptions = OrthRouterOptions | ShortestPathRouterOptions;
32
+
33
+ export interface OrthRouterOptions {
34
+ /**
35
+ * <zh/> 指定节点连接点与转角的最小距离
36
+ *
37
+ * <en/> The minimum distance between the node connection point and the corner
38
+ */
39
+ padding?: Padding;
40
+ }
41
+
42
+ export interface ShortestPathRouterOptions {
43
+ /**
44
+ * <zh/> 节点锚点与转角的最小距离
45
+ *
46
+ * <en/> The minimum distance between the node anchor point and the corner
47
+ */
48
+ offset?: Padding;
49
+ /**
50
+ * <zh/> grid 格子大小
51
+ *
52
+ * <en/> grid size
53
+ */
54
+ gridSize?: number;
55
+ /**
56
+ * <zh/> 支持的最大旋转角度(弧度)
57
+ *
58
+ * <en/> Maximum allowable rotation angle (radian)
59
+ */
60
+ maxAllowedDirectionChange?: number;
61
+ /**
62
+ * <zh/> 节点的可能起始方向
63
+ *
64
+ * <en/> Possible starting directions from a node
65
+ */
66
+ startDirections?: Direction[];
67
+ /**
68
+ * <zh/> 节点的可能结束方向
69
+ *
70
+ * <en/> Possible ending directions from a node
71
+ */
72
+ endDirections?: Direction[];
73
+ /**
74
+ * <zh/> 指定可移动的方向
75
+ *
76
+ * <en/> Allowed edge directions
77
+ */
78
+ directionMap?: {
79
+ [key in Direction]: { stepX: number; stepY: number };
80
+ };
81
+ /**
82
+ * <zh/> 表示在路径搜索过程中某些路径的额外代价。key 为弧度值,value 为代价
83
+ *
84
+ * <en/> Penalties for direction changes. Key is the radian value, value is the penalty
85
+ */
86
+ penalties?: {
87
+ [key: string]: number;
88
+ };
89
+ /**
90
+ * <zh/> 指定计算两点之间距离的函数
91
+ *
92
+ * <en/> Function to calculate the distance between two points
93
+ */
94
+ distFunc?: (p1: Point, p2: Point) => number;
95
+ /**
96
+ * <zh/> 最大迭代次数
97
+ *
98
+ * <en/> Maximum loops
99
+ */
100
+ maximumLoops?: number;
101
+ /**
102
+ * <zh/> 是否开启避障
103
+ *
104
+ * <en/> Whether to enable obstacle avoidance while computing the path
105
+ */
106
+ enableObstacleAvoidance?: boolean;
107
+ }
package/src/utils/bbox.ts CHANGED
@@ -247,3 +247,27 @@ export function getIncircleRadius(bbox: AABB, direction: TriangleDirection): num
247
247
  // 三角形的内切圆半径
248
248
  return (h ** 2 - (Math.sqrt((w / 2) ** 2 + h ** 2) - w / 2) ** 2) / (2 * h);
249
249
  }
250
+
251
+ /**
252
+ * <zh/> 获取包围盒的四条边,顺序依次为上、右、下、左
253
+ *
254
+ * <en/> Get the four segments of the bounding box, in order from top, right, bottom, left
255
+ * @param bbox - <zh/> 包围盒 | <en/> Bounding box
256
+ * @returns <zh/> 包围盒的四条边 | <en/> The four segments of the bounding box
257
+ */
258
+ export function getBBoxSegments(bbox: AABB): [Point, Point][] {
259
+ const {
260
+ min: [minX, minY],
261
+ max: [maxX, maxY],
262
+ } = bbox;
263
+ const topLeftCorner: Point = [minX, maxY];
264
+ const topRightCorner: Point = [maxX, maxY];
265
+ const bottomRightCorner: Point = [maxX, minY];
266
+ const bottomLeftCorner: Point = [minX, minY];
267
+
268
+ const top = [topLeftCorner, topRightCorner];
269
+ const right = [topRightCorner, bottomRightCorner];
270
+ const bottom = [bottomRightCorner, bottomLeftCorner];
271
+ const left = [bottomLeftCorner, topLeftCorner];
272
+ return [top, right, bottom, left] as [Point, Point][];
273
+ }
@@ -1,6 +1,6 @@
1
1
  import type { AABB } from '@antv/g';
2
2
  import { difference, isEqual } from '@antv/util';
3
- import type { Padding, Point } from '../../types';
3
+ import type { Node, OrthRouterOptions, Point } from '../../types';
4
4
  import {
5
5
  getBBoxHeight,
6
6
  getBBoxWidth,
@@ -21,26 +21,35 @@ type Route = {
21
21
  direction: Direction;
22
22
  };
23
23
 
24
+ const defaultOptions: OrthRouterOptions = {
25
+ padding: 10,
26
+ };
27
+
24
28
  /**
25
- * <zh/> 获取两点之间的直角线段路径
29
+ * <zh/> 获取两点之间的正交线段路径
26
30
  *
27
- * <en/> Get the orthogonal line segment path between two points
31
+ * <en/> Get orthogonal line segments between two points
28
32
  * @param sourcePoint - <zh/> 起始点 | <en/> start point
29
33
  * @param targetPoint - <zh/> 终止点 | <en/> end point
30
- * @param sourceBBox - <zh/> 起始节点的包围盒 | <en/> bounding box of the start node
31
- * @param targetBBox - <zh/> 终止节点的包围盒 | <en/> bounding box of the end node
34
+ * @param sourceNode - <zh/> 起始节点 | <en/> source node
35
+ * @param targetNode - <zh/> 终止节点 | <en/> target node
32
36
  * @param controlPoints - <zh/> 控制点 | <en/> control points
33
- * @param padding - <zh/> 内边距 | <en/> padding
34
- * @returns <zh/> 直角线段路径 | <en/> orthogonal line segment path
37
+ * @param options - <zh/> 配置项 | <en/> options
38
+ * @returns <zh/> 路径点集 | <en/> vertices
35
39
  */
36
40
  export function orth(
37
41
  sourcePoint: Point,
38
42
  targetPoint: Point,
39
- sourceBBox: AABB,
40
- targetBBox: AABB,
43
+ sourceNode: Node,
44
+ targetNode: Node,
41
45
  controlPoints: Point[],
42
- padding: Padding,
46
+ options: OrthRouterOptions,
43
47
  ) {
48
+ const { padding } = Object.assign(defaultOptions, options);
49
+
50
+ const sourceBBox = getNodeBBox(sourceNode, padding);
51
+ const targetBBox = getNodeBBox(targetNode, padding);
52
+
44
53
  const points: Point[] = [sourcePoint, ...controlPoints, targetPoint];
45
54
 
46
55
  // direction of previous route segment