@antv/g6 5.0.17 → 5.0.18

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 (108) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/dist/lib/behaviors/click-select.d.ts +0 -1
  3. package/dist/lib/constants/events/canvas.d.ts +1 -1
  4. package/dist/lib/elements/effect.d.ts +19 -2
  5. package/dist/lib/elements/nodes/html.d.ts +14 -0
  6. package/dist/lib/exports.d.ts +3 -2
  7. package/dist/lib/plugins/edge-filter-lens/index.d.ts +25 -9
  8. package/dist/lib/plugins/fisheye/index.d.ts +167 -0
  9. package/dist/lib/plugins/index.d.ts +2 -0
  10. package/dist/lib/plugins/tooltip.d.ts +1 -0
  11. package/dist/lib/runtime/element.d.ts +3 -1
  12. package/dist/lib/types/element.d.ts +7 -0
  13. package/dist/lib/utils/dom.d.ts +8 -5
  14. package/dist/lib/version.d.ts +1 -1
  15. package/esm/behaviors/click-select.d.ts +0 -1
  16. package/esm/behaviors/click-select.js.map +1 -1
  17. package/esm/constants/events/canvas.d.ts +1 -1
  18. package/esm/constants/events/canvas.js +1 -1
  19. package/esm/constants/events/canvas.js.map +1 -1
  20. package/esm/elements/effect.d.ts +19 -2
  21. package/esm/elements/effect.js +19 -2
  22. package/esm/elements/effect.js.map +1 -1
  23. package/esm/elements/nodes/html.d.ts +14 -0
  24. package/esm/elements/nodes/html.js +10 -4
  25. package/esm/elements/nodes/html.js.map +1 -1
  26. package/esm/exports.d.ts +3 -2
  27. package/esm/exports.js +2 -1
  28. package/esm/exports.js.map +1 -1
  29. package/esm/plugins/contextmenu/index.js +1 -1
  30. package/esm/plugins/contextmenu/index.js.map +1 -1
  31. package/esm/plugins/edge-filter-lens/index.d.ts +25 -9
  32. package/esm/plugins/edge-filter-lens/index.js +19 -14
  33. package/esm/plugins/edge-filter-lens/index.js.map +1 -1
  34. package/esm/plugins/fisheye/index.d.ts +167 -0
  35. package/esm/plugins/fisheye/index.js +288 -0
  36. package/esm/plugins/fisheye/index.js.map +1 -0
  37. package/esm/plugins/index.d.ts +2 -0
  38. package/esm/plugins/index.js +1 -0
  39. package/esm/plugins/index.js.map +1 -1
  40. package/esm/plugins/tooltip.d.ts +1 -0
  41. package/esm/plugins/tooltip.js +16 -17
  42. package/esm/plugins/tooltip.js.map +1 -1
  43. package/esm/registry/build-in.js +2 -1
  44. package/esm/registry/build-in.js.map +1 -1
  45. package/esm/runtime/element.d.ts +3 -1
  46. package/esm/runtime/element.js +8 -2
  47. package/esm/runtime/element.js.map +1 -1
  48. package/esm/types/element.d.ts +7 -0
  49. package/esm/utils/dom.d.ts +8 -5
  50. package/esm/utils/dom.js +24 -15
  51. package/esm/utils/dom.js.map +1 -1
  52. package/esm/version.d.ts +1 -1
  53. package/esm/version.js +1 -1
  54. package/lib/behaviors/click-select.d.ts +0 -1
  55. package/lib/behaviors/click-select.js.map +1 -1
  56. package/lib/constants/events/canvas.d.ts +1 -1
  57. package/lib/constants/events/canvas.js +1 -1
  58. package/lib/constants/events/canvas.js.map +1 -1
  59. package/lib/elements/effect.d.ts +19 -2
  60. package/lib/elements/effect.js +19 -2
  61. package/lib/elements/effect.js.map +1 -1
  62. package/lib/elements/nodes/html.d.ts +14 -0
  63. package/lib/elements/nodes/html.js +10 -4
  64. package/lib/elements/nodes/html.js.map +1 -1
  65. package/lib/exports.d.ts +3 -2
  66. package/lib/exports.js +5 -2
  67. package/lib/exports.js.map +1 -1
  68. package/lib/plugins/contextmenu/index.js +1 -1
  69. package/lib/plugins/contextmenu/index.js.map +1 -1
  70. package/lib/plugins/edge-filter-lens/index.d.ts +25 -9
  71. package/lib/plugins/edge-filter-lens/index.js +18 -13
  72. package/lib/plugins/edge-filter-lens/index.js.map +1 -1
  73. package/lib/plugins/fisheye/index.d.ts +167 -0
  74. package/lib/plugins/fisheye/index.js +292 -0
  75. package/lib/plugins/fisheye/index.js.map +1 -0
  76. package/lib/plugins/index.d.ts +2 -0
  77. package/lib/plugins/index.js +3 -1
  78. package/lib/plugins/index.js.map +1 -1
  79. package/lib/plugins/tooltip.d.ts +1 -0
  80. package/lib/plugins/tooltip.js +16 -17
  81. package/lib/plugins/tooltip.js.map +1 -1
  82. package/lib/registry/build-in.js +1 -0
  83. package/lib/registry/build-in.js.map +1 -1
  84. package/lib/runtime/element.d.ts +3 -1
  85. package/lib/runtime/element.js +8 -2
  86. package/lib/runtime/element.js.map +1 -1
  87. package/lib/types/element.d.ts +7 -0
  88. package/lib/utils/dom.d.ts +8 -5
  89. package/lib/utils/dom.js +24 -15
  90. package/lib/utils/dom.js.map +1 -1
  91. package/lib/version.d.ts +1 -1
  92. package/lib/version.js +1 -1
  93. package/package.json +1 -1
  94. package/src/behaviors/click-select.ts +0 -1
  95. package/src/constants/events/canvas.ts +1 -1
  96. package/src/elements/effect.ts +19 -2
  97. package/src/elements/nodes/html.ts +28 -4
  98. package/src/exports.ts +3 -0
  99. package/src/plugins/contextmenu/index.ts +1 -1
  100. package/src/plugins/edge-filter-lens/index.ts +36 -23
  101. package/src/plugins/fisheye/index.ts +481 -0
  102. package/src/plugins/index.ts +2 -0
  103. package/src/plugins/tooltip.ts +18 -17
  104. package/src/registry/build-in.ts +2 -0
  105. package/src/runtime/element.ts +11 -3
  106. package/src/types/element.ts +7 -0
  107. package/src/utils/dom.ts +24 -15
  108. package/src/version.ts +1 -1
package/src/exports.ts CHANGED
@@ -28,6 +28,7 @@ export {
28
28
  } from './constants';
29
29
  export { BaseCombo, CircleCombo, RectCombo } from './elements/combos';
30
30
  export { BaseEdge, Cubic, CubicHorizontal, CubicVertical, Line, Polyline, Quadratic } from './elements/edges';
31
+ export { effect } from './elements/effect';
31
32
  export {
32
33
  BaseNode,
33
34
  Circle,
@@ -68,6 +69,7 @@ export {
68
69
  CameraSetting,
69
70
  Contextmenu,
70
71
  EdgeFilterLens,
72
+ Fisheye,
71
73
  Fullscreen,
72
74
  GridLine,
73
75
  History,
@@ -170,6 +172,7 @@ export type {
170
172
  CameraSettingOptions,
171
173
  ContextmenuOptions,
172
174
  EdgeFilterLensOptions,
175
+ FisheyeOptions,
173
176
  FullscreenOptions,
174
177
  GridLineOptions,
175
178
  HistoryOptions,
@@ -100,7 +100,7 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
100
100
  }
101
101
 
102
102
  private initElement() {
103
- this.$element = createPluginContainer('contextmenu', false);
103
+ this.$element = createPluginContainer('contextmenu', false, { zIndex: '99' });
104
104
  const { className } = this.options;
105
105
  if (className) this.$element.classList.add(className);
106
106
 
@@ -16,12 +16,17 @@ import type {
16
16
  PointObject,
17
17
  } from '../../types';
18
18
  import { idOf } from '../../utils/id';
19
- import { parsePoint } from '../../utils/point';
19
+ import { parsePoint, toPointObject } from '../../utils/point';
20
20
  import { positionOf } from '../../utils/position';
21
21
  import { distance } from '../../utils/vector';
22
22
  import type { BasePluginOptions } from '../base-plugin';
23
23
  import { BasePlugin } from '../base-plugin';
24
24
 
25
+ /**
26
+ * <zh/> 边过滤镜插件配置项
27
+ *
28
+ * <en/> Edge filter lens plugin options
29
+ */
25
30
  export interface EdgeFilterLensOptions extends BasePluginOptions {
26
31
  /**
27
32
  * <zh/> 移动透镜的方式
@@ -44,26 +49,28 @@ export interface EdgeFilterLensOptions extends BasePluginOptions {
44
49
  */
45
50
  r?: number;
46
51
  /**
47
- * <zh/> 透镜的最大半径。只有在开启 `scaleRByWheel` 时生效
52
+ * <zh/> 透镜的最大半径。只有在 `scaleRBy` 为 `wheel` 时生效
48
53
  *
49
- * <en/> The maximum radius of the lens. Only valid when `scaleRByWheel` is enabled
54
+ * <en/> The maximum radius of the lens. Only valid when `scaleRBy` is `wheel`
50
55
  * @defaultValue canvas 宽高最小值的一半
51
56
  */
52
57
  maxR?: number;
53
58
  /**
54
- * <zh/> 透镜的最小半径。只有在开启 `scaleRByWheel` 时生效
59
+ * <zh/> 透镜的最小半径。只有在 `scaleRBy` 为 `wheel` 时生效
55
60
  *
56
- * <en/> The minimum radius of the lens. Only valid when `scaleRByWheel` is enabled
61
+ * <en/> The minimum radius of the lens. Only valid when `scaleRBy` is `wheel`
57
62
  * @defaultValue 0
58
63
  */
59
64
  minR?: number;
60
65
  /**
61
- * <zh/> 是否通过滚轮缩放透镜的半径
66
+ * <zh/> 缩放透镜半径的方式
67
+ * - `'wheel'`:通过滚轮缩放透镜的半径
62
68
  *
63
- * <en/> Whether to scale the radius of the lens by wheel
64
- * @defaultValue true
69
+ * <en/> The way to scale the radius of the lens
70
+ * - `'wheel'`: scale the radius of the lens by the wheel
71
+ * @defaultValue `'wheel'`
65
72
  */
66
- scaleRByWheel?: boolean;
73
+ scaleRBy?: 'wheel';
67
74
  /**
68
75
  * <zh/> 边显示的条件
69
76
  * - `'both'`:只有起始节点和目标节点都在透镜中时,边才会显示
@@ -71,7 +78,7 @@ export interface EdgeFilterLensOptions extends BasePluginOptions {
71
78
  * - `'target'`:只有目标节点在透镜中时,边才会显示
72
79
  * - `'either'`:只要起始节点或目标节点有一个在透镜中时,边就会显示
73
80
  *
74
- * <zh/> The condition for displaying the edge
81
+ * <en/> The condition for displaying the edge
75
82
  * - `'both'`: The edge is displayed only when both the source node and the target node are in the lens
76
83
  * - `'source'`: The edge is displayed only when the source node is in the lens
77
84
  * - `'target'`: The edge is displayed only when the target node is in the lens
@@ -125,6 +132,15 @@ const defaultLensStyle: BaseStyleProps = {
125
132
 
126
133
  const DELTA = 0.05;
127
134
 
135
+ /**
136
+ * <zh/> 边过滤镜插件
137
+ *
138
+ * <en/> Edge filter lens plugin
139
+ * @remarks
140
+ * <zh/> 边过滤镜可以将关注的边保留在过滤镜范围内,其他边将在该范围内不显示。
141
+ *
142
+ * <en/> EdgeFilterLens can keep the focused edges within the lens range, while other edges will not be displayed within that range.
143
+ */
128
144
  export class EdgeFilterLens extends BasePlugin<EdgeFilterLensOptions> {
129
145
  static defaultOptions: Partial<EdgeFilterLensOptions> = {
130
146
  trigger: 'pointermove',
@@ -134,7 +150,7 @@ export class EdgeFilterLens extends BasePlugin<EdgeFilterLensOptions> {
134
150
  style: { lineWidth: 2 },
135
151
  nodeStyle: { label: false },
136
152
  edgeStyle: { label: true },
137
- scaleRByWheel: true,
153
+ scaleRBy: 'wheel',
138
154
  preventDefault: true,
139
155
  };
140
156
 
@@ -166,17 +182,16 @@ export class EdgeFilterLens extends BasePlugin<EdgeFilterLensOptions> {
166
182
  };
167
183
 
168
184
  private renderLens = (origin: Point) => {
169
- const [x, y] = origin;
170
- const positionStyle = { size: this.r * 2, x, y };
185
+ const style = Object.assign({}, defaultLensStyle, this.options.style);
171
186
 
172
187
  if (!this.isLensOn) {
173
- const style = Object.assign({}, defaultLensStyle, this.options.style, positionStyle);
174
188
  this.lens = new Circle({ style });
175
- } else {
176
- this.lens.update(positionStyle);
189
+ this.canvas.appendChild(this.lens);
177
190
  }
178
191
 
179
- this.canvas.appendChild(this.lens);
192
+ Object.assign(style, toPointObject(origin), { size: this.r * 2 });
193
+
194
+ this.lens.update(style);
180
195
  };
181
196
 
182
197
  private getFilterData = (): Required<GraphData> => {
@@ -232,8 +247,6 @@ export class EdgeFilterLens extends BasePlugin<EdgeFilterLensOptions> {
232
247
 
233
248
  const ids = new Set<ID>();
234
249
 
235
- const { nodeStyle, edgeStyle } = this.options;
236
-
237
250
  const iterate = (datum: ElementDatum) => {
238
251
  const id = idOf(datum);
239
252
  ids.add(id);
@@ -328,7 +341,7 @@ export class EdgeFilterLens extends BasePlugin<EdgeFilterLensOptions> {
328
341
 
329
342
  private bindEvents() {
330
343
  const { graph } = this.context;
331
- const { trigger, scaleRByWheel } = this.options;
344
+ const { trigger, scaleRBy } = this.options;
332
345
 
333
346
  const canvas = graph.getCanvas().getLayer();
334
347
 
@@ -344,14 +357,14 @@ export class EdgeFilterLens extends BasePlugin<EdgeFilterLensOptions> {
344
357
  canvas.addEventListener(CommonEvent.DRAG_END, this.onDragEnd);
345
358
  }
346
359
 
347
- if (scaleRByWheel) {
360
+ if (scaleRBy === 'wheel') {
348
361
  this.graphDom?.addEventListener(CommonEvent.WHEEL, this.scaleRByWheel, { passive: false });
349
362
  }
350
363
  }
351
364
 
352
365
  private unbindEvents() {
353
366
  const { graph } = this.context;
354
- const { trigger, scaleRByWheel } = this.options;
367
+ const { trigger, scaleRBy } = this.options;
355
368
  const canvas = graph.getCanvas().getLayer();
356
369
 
357
370
  if (['click', 'drag'].includes(trigger)) {
@@ -366,7 +379,7 @@ export class EdgeFilterLens extends BasePlugin<EdgeFilterLensOptions> {
366
379
  canvas.removeEventListener(CommonEvent.DRAG_END, this.onDragEnd);
367
380
  }
368
381
 
369
- if (scaleRByWheel) {
382
+ if (scaleRBy === 'wheel') {
370
383
  this.graphDom?.removeEventListener(CommonEvent.WHEEL, this.scaleRByWheel);
371
384
  }
372
385
  }
@@ -0,0 +1,481 @@
1
+ import { pick } from '@antv/util';
2
+ import { CommonEvent } from '../../constants';
3
+ import type { CircleStyleProps } from '../../elements';
4
+ import { Circle } from '../../elements';
5
+ import type { RuntimeContext } from '../../runtime/types';
6
+ import type { NodeData } from '../../spec';
7
+ import type { NodeStyle } from '../../spec/element/node';
8
+ import type { ID, IDragEvent, IPointerEvent, Node, Point, PointObject } from '../../types';
9
+ import { arrayDiff } from '../../utils/diff';
10
+ import { idOf } from '../../utils/id';
11
+ import { parsePoint, toPointObject } from '../../utils/point';
12
+ import { positionOf } from '../../utils/position';
13
+ import { distance } from '../../utils/vector';
14
+ import type { BasePluginOptions } from '../base-plugin';
15
+ import { BasePlugin } from '../base-plugin';
16
+
17
+ /**
18
+ * <zh/> 鱼眼放大镜插件配置项
19
+ *
20
+ * <en/> Fisheye Plugin Options
21
+ */
22
+ export interface FisheyeOptions extends BasePluginOptions {
23
+ /**
24
+ * <zh/> 移动鱼眼放大镜的方式
25
+ * - `'pointermove'`:始终跟随鼠标移动
26
+ * - `'click'`:鼠标点击时移动
27
+ * - `'drag'`:拖拽移动
28
+ *
29
+ * <en/> The way to move the fisheye lens
30
+ * - `'pointermove'`: always follow the mouse movement
31
+ * - `'click'`: move when the mouse is clicked
32
+ * - `'drag'`: move by dragging
33
+ * @defaultValue `'pointermove'`
34
+ */
35
+ trigger?: 'pointermove' | 'drag' | 'click';
36
+ /**
37
+ * <zh/> 鱼眼放大镜半径
38
+ *
39
+ * <en/> The radius of the fisheye lens
40
+ * @remarks
41
+ * <img src="https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*unAvQqAb_NMAAAAAAAAAAAAADmJ7AQ/original" width="200" />
42
+ * @defaultValue 120
43
+ */
44
+ r?: number;
45
+ /**
46
+ * <zh/> 鱼眼放大镜可调整的最大半径,配合 `scaleRBy` 使用
47
+ *
48
+ * <en/> The maximum radius that the fisheye lens can be adjusted, used with `scaleRBy`
49
+ * @defaultValue 画布宽高的最小值的一半
50
+ */
51
+ maxR?: number;
52
+ /**
53
+ * <zh/> 鱼眼放大镜可调整的最小半径,配合 `scaleRBy` 使用
54
+ *
55
+ * <en/> The minimum radius that the fisheye lens can be adjusted, used with `scaleRBy`
56
+ * @defaultValue 0
57
+ */
58
+ minR?: number;
59
+ /**
60
+ * <zh/> 调整鱼眼放大镜范围半径的方式
61
+ * - `'wheel'`:滚轮调整
62
+ * - `'drag'`:拖拽调整
63
+ *
64
+ * <en/> The way to adjust the range radius of the fisheye lens
65
+ * - `'wheel'`: adjust by wheel
66
+ * - `'drag'`: adjust by drag
67
+ * @remarks
68
+ * <zh/> 如果 `trigger`、`scaleRBy` 和 `scaleDBy` 同时设置为 `'drag'`,优先级顺序为 `trigger` > `scaleRBy` > `scaleDBy`,只会为优先级最高的配置项绑定拖拽事件。同理,如果 `scaleRBy` 和 `scaleDBy` 同时设置为 `'wheel'`,只会为 `scaleRBy` 绑定滚轮事件
69
+ *
70
+ * <en/> If `trigger`, `scaleRBy`, and `scaleDBy` are set to `'drag'` at the same time, the priority order is `trigger` > `scaleRBy` > `scaleDBy`, and only the configuration item with the highest priority will be bound to the drag event. Similarly, if `scaleRBy` and `scaleDBy` are set to `'wheel'` at the same time, only `scaleRBy` will be bound to the wheel event
71
+ */
72
+ scaleRBy?: 'wheel' | 'drag';
73
+ /**
74
+ * <zh/> 畸变因子
75
+ *
76
+ * <en/> Distortion factor
77
+ * @remarks
78
+ * <img src="https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*4ITFR7GOl8UAAAAAAAAAAAAADmJ7AQ/original" width="200" />
79
+ * @defaultValue 1.5
80
+ */
81
+ d?: number;
82
+ /**
83
+ * <zh/> 鱼眼放大镜可调整的最大畸变因子,配合 `scaleDBy` 使用
84
+ *
85
+ * <en/> The maximum distortion factor that the fisheye lens can be adjusted, used with `scaleDBy`
86
+ * @defaultValue 5
87
+ */
88
+ maxD?: number;
89
+ /**
90
+ * <zh/> 鱼眼放大镜可调整的最小畸变因子,配合 `scaleDBy` 使用
91
+ *
92
+ * <en/> The minimum distortion factor that the fisheye lens can be adjusted, used with `scaleDBy`
93
+ * @defaultValue 0
94
+ */
95
+ minD?: number;
96
+ /**
97
+ * <zh/> 调整鱼眼放大镜畸变因子的方式
98
+ * - `'wheel'`:滚轮调整
99
+ * - `'drag'`:拖拽调整
100
+ *
101
+ * <en/> The way to adjust the distortion factor of the fisheye lens
102
+ * - `'wheel'`: adjust by wheel
103
+ * - `'drag'`: adjust by drag
104
+ */
105
+ scaleDBy?: 'wheel' | 'drag';
106
+ /**
107
+ * <zh/> 是否在鱼眼放大镜中显示畸变因子数值
108
+ *
109
+ * <en/> Whether to display the value of the distortion factor in the fisheye lens
110
+ * @defaultValue true
111
+ */
112
+ showDPercent?: boolean;
113
+ /**
114
+ * <zh/> 鱼眼放大镜样式
115
+ *
116
+ * <en/> Fisheye Lens Style
117
+ */
118
+ style?: Partial<CircleStyleProps>;
119
+ /**
120
+ * <zh/> 在鱼眼放大镜中的节点样式
121
+ *
122
+ * <en/> Node style in the fisheye lens
123
+ */
124
+ nodeStyle?: NodeStyle | ((datum: NodeData) => NodeStyle);
125
+ /**
126
+ * <zh/> 是否阻止默认事件
127
+ *
128
+ * <en/> Whether to prevent the default event
129
+ * @defaultValue true
130
+ */
131
+ preventDefault?: boolean;
132
+ }
133
+
134
+ const defaultLensStyle: Exclude<CircleStyleProps, 'r'> = {
135
+ fill: '#ccc',
136
+ fillOpacity: 0.1,
137
+ lineWidth: 2,
138
+ stroke: '#000',
139
+ strokeOpacity: 0.8,
140
+ labelFontSize: 12,
141
+ };
142
+
143
+ const R_DELTA = 0.05;
144
+ const D_DELTA = 0.1;
145
+
146
+ /**
147
+ * <zh/> 鱼眼放大镜
148
+ *
149
+ * <en/> Fisheye Distortion
150
+ * @remarks
151
+ * <zh/> Fisheye 鱼眼放大镜是为 focus+context 的探索场景设计的,它能够保证在放大关注区域的同时,保证上下文以及上下文与关注中心的关系不丢失。
152
+ *
153
+ * <en/> Fisheye is designed for focus+context exploration, it keeps the context and the relationships between context and the focus while magnifying the focus area.
154
+ */
155
+ export class Fisheye extends BasePlugin<FisheyeOptions> {
156
+ static defaultOptions: Partial<FisheyeOptions> = {
157
+ trigger: 'pointermove',
158
+ r: 120,
159
+ d: 1.5,
160
+ maxD: 5,
161
+ minD: 0,
162
+ showDPercent: true,
163
+ style: {},
164
+ nodeStyle: { label: true },
165
+ preventDefault: true,
166
+ };
167
+
168
+ constructor(context: RuntimeContext, options: FisheyeOptions) {
169
+ super(context, Object.assign({}, Fisheye.defaultOptions, options));
170
+ this.bindEvents();
171
+ }
172
+
173
+ private lens!: Circle;
174
+
175
+ private r = this.options.r;
176
+ private d = this.options.d;
177
+
178
+ private get canvas() {
179
+ return this.context.canvas.getLayer('transient');
180
+ }
181
+
182
+ private get isLensOn() {
183
+ return this.lens && !this.lens.destroyed;
184
+ }
185
+
186
+ protected onCreateFisheye = (event: IPointerEvent) => {
187
+ if (this.options.trigger === 'drag' && this.isLensOn) return;
188
+
189
+ const origin = parsePoint(event.canvas as PointObject);
190
+ this.onMagnify(origin);
191
+ };
192
+
193
+ protected onMagnify = (origin: Point) => {
194
+ if (origin.some(isNaN)) return;
195
+
196
+ this.renderLens(origin);
197
+ this.renderFocusElements();
198
+ };
199
+
200
+ private renderLens = (origin: Point) => {
201
+ const style = Object.assign({}, defaultLensStyle, this.options.style);
202
+
203
+ if (!this.isLensOn) {
204
+ this.lens = new Circle({ style });
205
+ this.canvas.appendChild(this.lens);
206
+ }
207
+
208
+ Object.assign(style, toPointObject(origin), {
209
+ size: this.r * 2,
210
+ label: this.options.showDPercent,
211
+ labelText: this.getDPercent(),
212
+ });
213
+
214
+ this.lens.update(style);
215
+ };
216
+
217
+ private getDPercent = () => {
218
+ const { minD, maxD } = this.options as Required<FisheyeOptions>;
219
+ const percent = Math.round(((this.d - minD) / (maxD - minD)) * 100);
220
+ return `${percent}%`;
221
+ };
222
+
223
+ private prevMagnifiedStyleMap = new Map<ID, NodeStyle>();
224
+ private prevOriginStyleMap = new Map<ID, NodeStyle>();
225
+
226
+ private renderFocusElements = () => {
227
+ if (!this.isLensOn) return;
228
+
229
+ const { graph } = this.context;
230
+ const origin = this.lens.getCenter();
231
+
232
+ const molecularParam = (this.d + 1) * this.r;
233
+
234
+ const magnifiedStyleMap = new Map<ID, NodeStyle>();
235
+ const originStyleMap = new Map<ID, NodeStyle>();
236
+
237
+ const nodeData = graph.getNodeData();
238
+ nodeData.forEach((datum) => {
239
+ const position = positionOf(datum);
240
+ const distanceToOrigin = distance(position, origin);
241
+ if (distanceToOrigin > this.r) return;
242
+
243
+ const magnifiedDistance = (molecularParam * distanceToOrigin) / (this.d * distanceToOrigin + this.r);
244
+ const [nodeX, nodeY] = position;
245
+ const [originX, originY] = origin;
246
+ const cos = (nodeX - originX) / distanceToOrigin;
247
+ const sin = (nodeY - originY) / distanceToOrigin;
248
+
249
+ const newPoint: Point = [originX + magnifiedDistance * cos, originY + magnifiedDistance * sin];
250
+ const nodeId = idOf(datum);
251
+
252
+ const style = this.getNodeStyle(datum);
253
+ const originStyle = pick(graph.getElementRenderStyle(nodeId), Object.keys(style));
254
+
255
+ magnifiedStyleMap.set(nodeId, { ...toPointObject(newPoint), ...style });
256
+ originStyleMap.set(nodeId, { ...toPointObject(position), ...originStyle });
257
+ });
258
+
259
+ this.updateStyle(magnifiedStyleMap, originStyleMap);
260
+ };
261
+
262
+ private getNodeStyle = (datum: NodeData) => {
263
+ const { nodeStyle } = this.options;
264
+ return typeof nodeStyle === 'function' ? nodeStyle(datum) : nodeStyle;
265
+ };
266
+
267
+ private updateStyle = (magnifiedStyleMap: Map<ID, NodeStyle>, originStyleMap: Map<ID, NodeStyle>) => {
268
+ const { graph, element } = this.context;
269
+
270
+ const { enter, exit, keep } = arrayDiff<ID>(
271
+ Array.from(this.prevMagnifiedStyleMap.keys()),
272
+ Array.from(magnifiedStyleMap.keys()),
273
+ (d) => d,
274
+ );
275
+
276
+ const relatedEdges = new Set<ID>();
277
+
278
+ const update = (nodeId: ID, style: NodeStyle) => {
279
+ const node = element!.getElement(nodeId) as Node;
280
+ node.update(style);
281
+
282
+ graph.getRelatedEdgesData(nodeId).forEach((datum) => {
283
+ relatedEdges.add(idOf(datum));
284
+ });
285
+ };
286
+
287
+ [...enter, ...keep].forEach((nodeId) => {
288
+ update(nodeId, magnifiedStyleMap.get(nodeId)!);
289
+ });
290
+
291
+ exit.forEach((nodeId) => {
292
+ update(nodeId, this.prevOriginStyleMap.get(nodeId)!);
293
+ this.prevOriginStyleMap.delete(nodeId);
294
+ });
295
+
296
+ relatedEdges.forEach((edgeId) => {
297
+ const edge = element!.getElement(edgeId);
298
+ edge?.update({});
299
+ });
300
+
301
+ this.prevMagnifiedStyleMap = magnifiedStyleMap;
302
+ originStyleMap.forEach((style, nodeId) => {
303
+ if (!this.prevOriginStyleMap.has(nodeId)) {
304
+ this.prevOriginStyleMap.set(nodeId, style);
305
+ }
306
+ });
307
+ };
308
+
309
+ private isWheelValid = (event: WheelEvent) => {
310
+ if (this.options.preventDefault) event.preventDefault();
311
+
312
+ if (!this.isLensOn) return false;
313
+
314
+ const { clientX, clientY } = event;
315
+ const scaleOrigin = this.context.graph.getCanvasByClient([clientX, clientY]);
316
+ const origin = this.lens.getCenter();
317
+ if (distance(scaleOrigin, origin) > this.r) return false;
318
+
319
+ return true;
320
+ };
321
+
322
+ private scaleR = (positive: boolean) => {
323
+ const { maxR, minR } = this.options;
324
+ const ratio = positive ? 1 / (1 - R_DELTA) : 1 - R_DELTA;
325
+ const canvasR = Math.min(...this.context.canvas.getSize()) / 2;
326
+ this.r = Math.max(minR || 0, Math.min(maxR || canvasR, this.r * ratio));
327
+ };
328
+
329
+ private scaleD = (positive: boolean) => {
330
+ const { maxD, minD } = this.options as Required<FisheyeOptions>;
331
+ const newD = positive ? this.d + D_DELTA : this.d - D_DELTA;
332
+ this.d = Math.max(minD, Math.min(maxD, newD));
333
+ };
334
+
335
+ private scaleRByWheel = (event: WheelEvent) => {
336
+ if (!this.isWheelValid(event)) return;
337
+
338
+ const { deltaX, deltaY } = event;
339
+ this.scaleR(deltaX + deltaY > 0);
340
+
341
+ const origin = this.lens.getCenter();
342
+ this.onMagnify(origin);
343
+ };
344
+
345
+ private scaleDByWheel = (event: WheelEvent) => {
346
+ if (!this.isWheelValid(event)) return;
347
+
348
+ const { deltaX, deltaY } = event;
349
+ this.scaleD(deltaX + deltaY > 0);
350
+
351
+ const origin = this.lens.getCenter();
352
+ this.onMagnify(origin);
353
+ };
354
+
355
+ private isDragValid = (event: IDragEvent) => {
356
+ if (this.options.preventDefault) event.preventDefault();
357
+
358
+ if (!this.isLensOn) return false;
359
+
360
+ const dragOrigin = parsePoint(event.canvas as PointObject);
361
+ const origin = this.lens.getCenter();
362
+ if (distance(dragOrigin, origin) > this.r) return false;
363
+
364
+ return true;
365
+ };
366
+
367
+ private isLensDragging = false;
368
+
369
+ private onDragStart = (event: IDragEvent) => {
370
+ if (!this.isDragValid(event)) return;
371
+
372
+ this.isLensDragging = true;
373
+ };
374
+
375
+ private onDrag = (event: IDragEvent) => {
376
+ if (!this.isLensDragging) return;
377
+
378
+ const dragOrigin = parsePoint(event.canvas as PointObject);
379
+ this.onMagnify(dragOrigin);
380
+ };
381
+
382
+ private onDragEnd = () => {
383
+ this.isLensDragging = false;
384
+ };
385
+
386
+ private scaleRByDrag = (event: IDragEvent) => {
387
+ if (!this.isLensDragging) return;
388
+
389
+ const { dx, dy } = event;
390
+ this.scaleR(dx - dy > 0);
391
+
392
+ const origin = this.lens.getCenter();
393
+ this.onMagnify(origin);
394
+ };
395
+
396
+ private scaleDByDrag = (event: IDragEvent) => {
397
+ if (!this.isLensDragging) return;
398
+
399
+ const { dx, dy } = event;
400
+ this.scaleD(dx - dy > 0);
401
+
402
+ const origin = this.lens.getCenter();
403
+ this.onMagnify(origin);
404
+ };
405
+
406
+ get graphDom() {
407
+ return this.context.graph.getCanvas().getContextService().getDomElement();
408
+ }
409
+
410
+ private bindEvents() {
411
+ const { graph } = this.context;
412
+ const { trigger, scaleRBy, scaleDBy } = this.options;
413
+
414
+ const canvas = graph.getCanvas().getLayer();
415
+
416
+ if (['click', 'drag'].includes(trigger)) {
417
+ canvas.addEventListener(CommonEvent.CLICK, this.onCreateFisheye);
418
+ }
419
+
420
+ if (trigger === 'pointermove') {
421
+ canvas.addEventListener(CommonEvent.POINTER_MOVE, this.onCreateFisheye);
422
+ }
423
+
424
+ if (trigger === 'drag' || scaleRBy === 'drag' || scaleDBy === 'drag') {
425
+ canvas.addEventListener(CommonEvent.DRAG_START, this.onDragStart);
426
+ canvas.addEventListener(CommonEvent.DRAG_END, this.onDragEnd);
427
+
428
+ const dragFunc = trigger === 'drag' ? this.onDrag : scaleRBy === 'drag' ? this.scaleRByDrag : this.scaleDByDrag;
429
+ canvas.addEventListener(CommonEvent.DRAG, dragFunc);
430
+ }
431
+
432
+ if (scaleRBy === 'wheel' || scaleDBy === 'wheel') {
433
+ const wheelFunc = scaleRBy === 'wheel' ? this.scaleRByWheel : this.scaleDByWheel;
434
+ this.graphDom?.addEventListener(CommonEvent.WHEEL, wheelFunc, { passive: false });
435
+ }
436
+ }
437
+
438
+ private unbindEvents() {
439
+ const { graph } = this.context;
440
+ const { trigger, scaleRBy, scaleDBy } = this.options;
441
+
442
+ const canvas = graph.getCanvas().getLayer();
443
+
444
+ if (['click', 'drag'].includes(trigger)) {
445
+ canvas.removeEventListener(CommonEvent.CLICK, this.onCreateFisheye);
446
+ }
447
+
448
+ if (trigger === 'pointermove') {
449
+ canvas.removeEventListener(CommonEvent.POINTER_MOVE, this.onCreateFisheye);
450
+ }
451
+
452
+ if (trigger === 'drag' || scaleRBy === 'drag' || scaleDBy === 'drag') {
453
+ canvas.removeEventListener(CommonEvent.DRAG_START, this.onDragStart);
454
+ canvas.removeEventListener(CommonEvent.DRAG_END, this.onDragEnd);
455
+
456
+ const dragFunc = trigger === 'drag' ? this.onDrag : scaleRBy === 'drag' ? this.scaleRByDrag : this.scaleDByDrag;
457
+ canvas.removeEventListener(CommonEvent.DRAG, dragFunc);
458
+ }
459
+
460
+ if (scaleRBy === 'wheel' || scaleDBy === 'wheel') {
461
+ const wheelFunc = scaleRBy === 'wheel' ? this.scaleRByWheel : this.scaleDByWheel;
462
+ this.graphDom?.removeEventListener(CommonEvent.WHEEL, wheelFunc);
463
+ }
464
+ }
465
+
466
+ public update(options: Partial<FisheyeOptions>) {
467
+ this.unbindEvents();
468
+ super.update(options);
469
+ this.bindEvents();
470
+ }
471
+
472
+ public destroy() {
473
+ this.unbindEvents();
474
+ if (this.isLensOn) {
475
+ this.lens?.destroy();
476
+ }
477
+ this.prevMagnifiedStyleMap.clear();
478
+ this.prevOriginStyleMap.clear();
479
+ super.destroy();
480
+ }
481
+ }
@@ -4,6 +4,7 @@ export { BubbleSets } from './bubble-sets';
4
4
  export { CameraSetting } from './camera-setting';
5
5
  export { Contextmenu } from './contextmenu';
6
6
  export { EdgeFilterLens } from './edge-filter-lens';
7
+ export { Fisheye } from './fisheye';
7
8
  export { Fullscreen } from './fullscreen';
8
9
  export { GridLine } from './grid-line';
9
10
  export { History } from './history';
@@ -22,6 +23,7 @@ export type { BubbleSetsOptions } from './bubble-sets';
22
23
  export type { CameraSettingOptions } from './camera-setting';
23
24
  export type { ContextmenuOptions } from './contextmenu';
24
25
  export type { EdgeFilterLensOptions } from './edge-filter-lens';
26
+ export type { FisheyeOptions } from './fisheye';
25
27
  export type { FullscreenOptions } from './fullscreen';
26
28
  export type { GridLineOptions } from './grid-line';
27
29
  export type { HistoryOptions } from './history';