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.

130 lines
3.6 KiB

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