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.

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