vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
4.4 KiB

  1. import BezierEdgeBase from './util/BezierEdgeBase'
  2. class BezierEdgeDynamic extends BezierEdgeBase {
  3. constructor(id, options, body, labelModule) {
  4. //this.via = undefined; // Here for completeness but not allowed to defined before super() is invoked.
  5. super(id, options, body, labelModule); // --> this calls the setOptions below
  6. this._boundFunction = () => {this.positionBezierNode();};
  7. this.body.emitter.on("_repositionBezierNodes", this._boundFunction);
  8. }
  9. setOptions(options) {
  10. // check if the physics has changed.
  11. let physicsChange = false;
  12. if (this.options.physics !== options.physics) {
  13. physicsChange = true;
  14. }
  15. // set the options and the to and from nodes
  16. this.options = options;
  17. this.from = this.body.nodes[this.options.from];
  18. this.to = this.body.nodes[this.options.to];
  19. // setup the support node and connect
  20. this.setupSupportNode();
  21. this.connect();
  22. // when we change the physics state of the edge, we reposition the support node.
  23. if (physicsChange === true) {
  24. this.via.setOptions({physics: this.options.physics})
  25. this.positionBezierNode();
  26. }
  27. }
  28. connect() {
  29. this.from = this.body.nodes[this.options.from];
  30. this.to = this.body.nodes[this.options.to];
  31. if (this.from === undefined || this.to === undefined || this.options.physics === false) {
  32. this.via.setOptions({physics:false})
  33. }
  34. else {
  35. // fix weird behaviour where a self referencing node has physics enabled
  36. if (this.from.id === this.to.id) {
  37. this.via.setOptions({physics: false})
  38. }
  39. else {
  40. this.via.setOptions({physics: true})
  41. }
  42. }
  43. }
  44. /**
  45. * remove the support nodes
  46. * @returns {boolean}
  47. */
  48. cleanup() {
  49. this.body.emitter.off("_repositionBezierNodes", this._boundFunction);
  50. if (this.via !== undefined) {
  51. delete this.body.nodes[this.via.id];
  52. this.via = undefined;
  53. return true;
  54. }
  55. return false;
  56. }
  57. /**
  58. * Bezier curves require an anchor point to calculate the smooth flow. These points are nodes. These nodes are invisible but
  59. * are used for the force calculation.
  60. *
  61. * The changed data is not called, if needed, it is returned by the main edge constructor.
  62. * @private
  63. */
  64. setupSupportNode() {
  65. if (this.via === undefined) {
  66. var nodeId = "edgeId:" + this.id;
  67. var properties = {
  68. shape: 'circle',
  69. physics: true,
  70. hidden: true
  71. };
  72. var node = this.body.functions.createNode(nodeId, properties);
  73. this.body.nodes[nodeId] = node;
  74. this.via = node;
  75. this.via.parentEdgeId = this.id;
  76. this.positionBezierNode();
  77. }
  78. }
  79. positionBezierNode() {
  80. if (this.via !== undefined && this.from !== undefined && this.to !== undefined) {
  81. this.via.x = 0.5 * (this.from.x + this.to.x);
  82. this.via.y = 0.5 * (this.from.y + this.to.y);
  83. }
  84. else if (this.via !== undefined) {
  85. this.via.x = 0;
  86. this.via.y = 0;
  87. }
  88. }
  89. /**
  90. * Draw a line between two nodes
  91. * @param {CanvasRenderingContext2D} ctx
  92. * @private
  93. */
  94. _line(ctx, viaNode) {
  95. // draw a straight line
  96. ctx.beginPath();
  97. ctx.moveTo(this.fromPoint.x, this.fromPoint.y);
  98. // fallback to normal straight edges
  99. if (viaNode.x === undefined) {
  100. ctx.lineTo(this.toPoint.x, this.toPoint.y);
  101. }
  102. else {
  103. ctx.quadraticCurveTo(viaNode.x, viaNode.y, this.toPoint.x, this.toPoint.y);
  104. }
  105. // draw shadow if enabled
  106. this.enableShadow(ctx);
  107. ctx.stroke();
  108. this.disableShadow(ctx);
  109. }
  110. getViaNode() {
  111. return this.via;
  112. }
  113. /**
  114. * Combined function of pointOnLine and pointOnBezier. This gives the coordinates of a point on the line at a certain percentage of the way
  115. * @param percentage
  116. * @param viaNode
  117. * @returns {{x: number, y: number}}
  118. * @private
  119. */
  120. getPoint(percentage, viaNode = this.via) {
  121. let t = percentage;
  122. let x = Math.pow(1 - t, 2) * this.fromPoint.x + (2 * t * (1 - t)) * viaNode.x + Math.pow(t, 2) * this.toPoint.x;
  123. let y = Math.pow(1 - t, 2) * this.fromPoint.y + (2 * t * (1 - t)) * viaNode.y + Math.pow(t, 2) * this.toPoint.y;
  124. return {x: x, y: y};
  125. }
  126. _findBorderPosition(nearNode, ctx) {
  127. return this._findBorderPositionBezier(nearNode, ctx, this.via);
  128. }
  129. _getDistanceToEdge(x1, y1, x2, y2, x3, y3) { // x3,y3 is the point
  130. return this._getDistanceToBezierEdge(x1, y1, x2, y2, x3, y3, this.via);
  131. }
  132. }
  133. export default BezierEdgeDynamic;