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.

123 lines
4.3 KiB

  1. import CubicBezierEdgeBase from './util/CubicBezierEdgeBase'
  2. /**
  3. * A Cubic Bezier Edge. Bezier curves are used to model smooth gradual
  4. * curves in paths between nodes.
  5. *
  6. * @class CubicBezierEdge
  7. * @extends CubicBezierEdgeBase
  8. */
  9. class CubicBezierEdge extends CubicBezierEdgeBase {
  10. /**
  11. * @param {Object} options
  12. * @param {Object} body
  13. * @param {Label} labelModule
  14. * @constructor CubicBezierEdge
  15. */
  16. constructor(options, body, labelModule) {
  17. super(options, body, labelModule);
  18. }
  19. /**
  20. * Draw a line between two nodes
  21. * @param {CanvasRenderingContext2D} ctx
  22. * @param {{toArrow: boolean, toArrowScale: (allOptions.edges.arrows.to.scaleFactor|{number}|allOptions.edges.arrows.middle.scaleFactor|allOptions.edges.arrows.from.scaleFactor|Array|number), toArrowType: *, middleArrow: boolean, middleArrowScale: (number|allOptions.edges.arrows.middle.scaleFactor|{number}|Array), middleArrowType: (allOptions.edges.arrows.middle.type|{string}|string|*), fromArrow: boolean, fromArrowScale: (allOptions.edges.arrows.to.scaleFactor|{number}|allOptions.edges.arrows.middle.scaleFactor|allOptions.edges.arrows.from.scaleFactor|Array|number), fromArrowType: *, arrowStrikethrough: (*|boolean|allOptions.edges.arrowStrikethrough|{boolean}), color: undefined, inheritsColor: (string|string|string|allOptions.edges.color.inherit|{string, boolean}|Array|*), opacity: *, hidden: *, length: *, shadow: *, shadowColor: *, shadowSize: *, shadowX: *, shadowY: *, dashes: (*|boolean|Array|allOptions.edges.dashes|{boolean, array}), width: *}} values
  23. * @param {Array<vis.Node>} viaNodes
  24. * @private
  25. */
  26. _line(ctx, values, viaNodes) {
  27. // get the coordinates of the support points.
  28. let via1 = viaNodes[0];
  29. let via2 = viaNodes[1];
  30. this._bezierCurve(ctx, values, via1, via2);
  31. }
  32. /**
  33. *
  34. * @returns {Array<{x: number, y: number}>}
  35. * @private
  36. */
  37. _getViaCoordinates() {
  38. let dx = this.from.x - this.to.x;
  39. let dy = this.from.y - this.to.y;
  40. let x1, y1, x2, y2;
  41. let roundness = this.options.smooth.roundness;
  42. // horizontal if x > y or if direction is forced or if direction is horizontal
  43. if ((Math.abs(dx) > Math.abs(dy) || this.options.smooth.forceDirection === true || this.options.smooth.forceDirection === 'horizontal') && this.options.smooth.forceDirection !== 'vertical') {
  44. y1 = this.from.y;
  45. y2 = this.to.y;
  46. x1 = this.from.x - roundness * dx;
  47. x2 = this.to.x + roundness * dx;
  48. }
  49. else {
  50. y1 = this.from.y - roundness * dy;
  51. y2 = this.to.y + roundness * dy;
  52. x1 = this.from.x;
  53. x2 = this.to.x;
  54. }
  55. return [{x: x1, y: y1},{x: x2, y: y2}];
  56. }
  57. /**
  58. *
  59. * @returns {Array<{x: number, y: number}>}
  60. */
  61. getViaNode() {
  62. return this._getViaCoordinates();
  63. }
  64. /**
  65. *
  66. * @param {Node} nearNode
  67. * @param {CanvasRenderingContext2D} ctx
  68. * @returns {{x: number, y: number, t: number}}
  69. * @private
  70. */
  71. _findBorderPosition(nearNode, ctx) {
  72. return this._findBorderPositionBezier(nearNode, ctx);
  73. }
  74. /**
  75. *
  76. * @param {number} x1
  77. * @param {number} y1
  78. * @param {number} x2
  79. * @param {number} y2
  80. * @param {number} x3
  81. * @param {number} y3
  82. * @param {Node} via1
  83. * @param {Node} via2
  84. * @returns {number}
  85. * @private
  86. */
  87. _getDistanceToEdge(x1, y1, x2, y2, x3, y3, [via1, via2] = this._getViaCoordinates()) { // x3,y3 is the point
  88. return this._getDistanceToBezierEdge(x1, y1, x2, y2, x3, y3, via1, via2);
  89. }
  90. /**
  91. * Combined function of pointOnLine and pointOnBezier. This gives the coordinates of a point on the line at a certain percentage of the way
  92. * @param {number} percentage
  93. * @param {{x: number, y: number}} [via1=this._getViaCoordinates()[0]]
  94. * @param {{x: number, y: number}} [via2=this._getViaCoordinates()[1]]
  95. * @returns {{x: number, y: number}}
  96. * @private
  97. */
  98. getPoint(percentage, [via1, via2] = this._getViaCoordinates()) {
  99. let t = percentage;
  100. let vec = [];
  101. vec[0] = Math.pow(1 - t, 3);
  102. vec[1] = 3 * t * Math.pow(1 - t, 2);
  103. vec[2] = 3 * Math.pow(t,2) * (1 - t);
  104. vec[3] = Math.pow(t, 3);
  105. let x = vec[0] * this.fromPoint.x + vec[1] * via1.x + vec[2] * via2.x + vec[3] * this.toPoint.x;
  106. let y = vec[0] * this.fromPoint.y + vec[1] * via1.y + vec[2] * via2.y + vec[3] * this.toPoint.y;
  107. return {x: x, y: y};
  108. }
  109. }
  110. export default CubicBezierEdge;