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.

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