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.

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