锁定节点

G6 3.1.4 版本中新增了 lock()、unlock() 和 hasLocked() 三个 API,方便用户锁定某个节点。默认情况下,当锁定某个节点后,拖动节点时锁定的节点不会有任何反应,但拖动画布和缩放画布时,仍然会对锁定的节点有影响,如果不想让锁定的节点收到拖动画布和缩放画布的影响,可以通过自定义 Behavior 的方式来实现。

拖动画布时候不处理锁定的节点

G6 内置的 drag-canvas 不区分节点是否锁定,全部一视同仁。绝大数情况下,这种行为是完全没问题的,但某些业务可能会要求锁定的节点,拖动画布时也不能移动,对于这种情况,可以通过重新定义拖动画布的 Behavior 来实现。

1. import G6 from '@antv/g6';
2. const Util = G6.Util;
3. const abs = Math.abs;
4. const DRAG_OFFSET = 10;
5. const body = document.body;
6. const ALLOW_EVENTS = [16, 17, 18];

8. G6.registerBehavior('drag-canvas-exclude-lockedNode', {
9. getDefaultCfg() {
10. return {
11. direction: 'both',
12. };
13. },
14. getEvents() {
15. return {
16. 'canvas:mousedown': 'onMouseDown',
17. 'canvas:mousemove': 'onMouseMove',
18. 'canvas:mouseup': 'onMouseUp',
19. 'canvas:click': 'onMouseUp',
20. 'canvas:mouseleave': 'onOutOfRange',
21. keyup: 'onKeyUp',
22. keydown: 'onKeyDown',
23. };
24. },
25. updateViewport(e) {
26. const origin = this.origin;
27. const clientX = +e.clientX;
28. const clientY = +e.clientY;
29. if (isNaN(clientX) || isNaN(clientY)) {
30. return;
31. }
32. let dx = clientX - origin.x;
33. let dy = clientY - origin.y;
34. if (this.get('direction') === 'x') {
35. dy = 0;
36. } else if (this.get('direction') === 'y') {
37. dx = 0;
38. }
39. this.origin = {
40. x: clientX,
41. y: clientY,
42. };
43. // 和内置 drag-canvas 不同的地方是在这里
44. const lockedNodes = this.graph.findAll('node', node => !node.hasLocked());
45. lockedNodes.forEach(node => {
46. node.get('group').translate(dx, dy);
47. });
48. this.graph.paint();
49. },
50. onMouseDown(e) {
51. if (this.keydown) {
52. return;
53. }

55. this.origin = { x: e.clientX, y: e.clientY };
56. this.dragging = false;
57. },
58. onMouseMove(e) {
59. if (this.keydown) {
60. return;
61. }

63. e = Util.cloneEvent(e);
64. const graph = this.graph;
65. if (!this.origin) {
66. return;
67. }
68. if (this.origin && !this.dragging) {
69. if (
70. abs(this.origin.x - e.clientX) + abs(this.origin.y - e.clientY) <
71. DRAG_OFFSET
72. ) {
73. return;
74. }
75. if (this.shouldBegin.call(this, e)) {
76. e.type = 'dragstart';
77. graph.emit('canvas:dragstart', e);
78. this.dragging = true;
79. }
80. }
81. if (this.dragging) {
82. e.type = 'drag';
83. graph.emit('canvas:drag', e);
84. }
85. if (this.shouldUpdate.call(this, e)) {
86. this.updateViewport(e);
87. }
88. },
89. onMouseUp(e) {
90. if (this.keydown) {
91. return;
92. }

94. if (!this.dragging) {
95. this.origin = null;
96. return;
97. }
98. e = Util.cloneEvent(e);
99. const graph = this.graph;
100. if (this.shouldEnd.call(this, e)) {
101. this.updateViewport(e);
102. }
103. e.type = 'dragend';
104. graph.emit('canvas:dragend', e);
105. this.endDrag();
106. },
107. endDrag() {
108. if (this.dragging) {
109. this.origin = null;
110. this.dragging = false;
111. // 终止时需要判断此时是否在监听画布外的 mouseup 事件,若有则解绑
112. const fn = this.fn;
113. if (fn) {
114. body.removeEventListener('mouseup', fn, false);
115. this.fn = null;
116. }
117. }
118. },
119. // 若在拖拽时,鼠标移出画布区域,此时放开鼠标无法终止 drag 行为。在画布外监听 mouseup 事件,放开则终止
120. onOutOfRange(e) {
121. if (this.dragging) {
122. const self = this;
123. const canvasElement = self.graph.get('canvas').get('el');
124. const fn = ev => {
125. if (ev.target !== canvasElement) {
126. self.onMouseUp(e);
127. }
128. };
129. this.fn = fn;
130. body.addEventListener('mouseup', fn, false);
131. }
132. },
133. onKeyDown(e) {
134. const code = e.keyCode || e.which;
135. if (!code) {
136. return;
137. }
138. if (ALLOW_EVENTS.indexOf(code) > -1) {
139. this.keydown = true;
140. } else {
141. this.keydown = false;
142. }
143. },
144. onKeyUp() {
145. this.keydown = false;
146. },
147. });

缩放画布时不处理锁定的节点

默认情况下,G6 内置的 zoom-canvas 在缩放画布时候也会对锁定的节点缩放,如果缩放过程中不需要操作锁定的节点,则可以通过下面的方式来实现。

1. const DELTA = 0.05;

3. G6.registerBehavior('zoom-canvas-exclude-lockedNode', {
4. getDefaultCfg() {
5. return {
6. sensitivity: 2,
7. minZoom: 0.1,
8. maxZoom: 10,
9. };
10. },
11. getEvents() {
12. return {
13. wheel: 'onWheel',
14. };
15. },
16. onWheel(e) {
17. e.preventDefault();
18. if (!this.shouldUpdate.call(this, e)) {
19. return;
20. }
21. const graph = this.graph;
22. const canvas = graph.get('canvas');
23. const point = canvas.getPointByClient(e.clientX, e.clientY);
24. const pixelRatio = canvas.get('pixelRatio');
25. const sensitivity = this.get('sensitivity');
26. let ratio = graph.getZoom();
27. // 兼容IE、Firefox及Chrome
28. if (e.wheelDelta < 0) {
29. ratio = 1 - DELTA * sensitivity;
30. } else {
31. ratio = 1 + DELTA * sensitivity;
32. }
33. const zoom = ratio * graph.getZoom();
34. if (zoom > this.get('maxZoom') || zoom < this.get('minZoom')) {
35. return;
36. }
37. graph.zoom(ratio, { x: point.x / pixelRatio, y: point.y / pixelRatio });
38. const lockedNodes = this.graph.findAll('node', node => !node.hasLocked());
39. lockedNodes.forEach(node => {
40. const matrix = Util.clone(node.get('group').getMatrix());
41. const center = node.getModel();
42. Util.mat3.translate(matrix, matrix, [-center.x, -center.y]);
43. Util.mat3.scale(matrix, matrix, [ratio, ratio]);
44. Util.mat3.translate(matrix, matrix, [center.x, center.y]);
45. node.get('group').setMatrix(matrix);
46. });
47. graph.paint();
48. graph.emit('wheelzoom', e);
49. },
50. });