dragPoint.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. export default function(G6) {
  2. G6.registerBehavior('dragPoint', {
  3. getDefaultCfg() {
  4. return {
  5. updateEdge: true,
  6. delegate: true,
  7. delegateStyle: {},
  8. dragEdge: false,
  9. };
  10. },
  11. getEvents() {
  12. return {
  13. 'controlPoint:dragstart': 'onDragStart',
  14. 'controlPoint:drag': 'onDrag',
  15. 'controlPoint:dragend': 'onDragEnd',
  16. };
  17. },
  18. onDragStart(e) {
  19. const node = e.target.getParent().getParent().get('item');
  20. const anchorIndex = e.item.get('index');
  21. this.target = e.item;
  22. this.origin = {
  23. x: e.x,
  24. y: e.y,
  25. sourceNode: node,
  26. sourceAnchor: anchorIndex,
  27. };
  28. this.graph.set('edgeDragging', true);
  29. },
  30. onDrag(e) {
  31. if (!this.origin) {
  32. return;
  33. }
  34. const node = e.target.getParent().getParent().get('item');
  35. const anchorIndex = e.item.get('index');
  36. const model = node.getModel();
  37. const currentWidth = model.size[0];
  38. const currentHeight = model.size[1];
  39. const addWidth = e.x - this.origin.x;
  40. const addHeight = e.y - this.origin.y;
  41. let width = currentWidth;
  42. let height = currentHeight;
  43. // 0,0 // 两个都是负的变大
  44. // 1,0 // 一正 一负 变大
  45. // 1,1 // 两个都是负变大
  46. // 0,1 // 一负 一正 变大
  47. const pointIndex = this.origin.sourceAnchor;
  48. if (pointIndex === 0) {
  49. width = currentWidth - addWidth;
  50. height = currentHeight - addHeight;
  51. } else if (pointIndex === 1) {
  52. height = currentHeight - addHeight;
  53. } else if (pointIndex === 2) {
  54. width = currentWidth + addWidth;
  55. height = currentHeight - addHeight;
  56. } else if (pointIndex === 3) {
  57. width = currentWidth + addWidth;
  58. } else if (pointIndex === 4) {
  59. width = currentWidth + addWidth;
  60. height = currentHeight + addHeight;
  61. } else if (pointIndex === 5) {
  62. height = currentHeight + addHeight;
  63. } else if (pointIndex === 6) {
  64. width = currentWidth - addWidth;
  65. height = currentHeight + addHeight;
  66. } else if (pointIndex === 7) {
  67. width = currentWidth - addWidth;
  68. }
  69. const group = node.getContainer();
  70. // 移动过程中的控制点全部隐藏。
  71. group.controlPointShapes.forEach(a => a.hide());
  72. this.graph.updateItem(node, { size: [width, height], });
  73. this.origin = {
  74. x: e.x,
  75. y: e.y,
  76. sourceNode: node,
  77. sourceAnchor: anchorIndex,
  78. };
  79. },
  80. onDragEnd(e) {
  81. if (!this.origin) {
  82. return;
  83. }
  84. const node = e.target.getParent().getParent().get('item');
  85. this.target = null;
  86. this.origin = null;
  87. const group = node.getContainer();
  88. group.clearControlPoints(group);
  89. group.showControlPoints(group);
  90. this.graph.set('edgeDragging', false);
  91. },
  92. });
  93. }