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.

78 lines
2.3 KiB

  1. import CubicBezierEdgeBase from './util/CubicBezierEdgeBase'
  2. class CubicBezierEdge extends CubicBezierEdgeBase {
  3. constructor(options, body, labelModule) {
  4. super(options, body, labelModule);
  5. }
  6. /**
  7. * Draw a line between two nodes
  8. * @param {CanvasRenderingContext2D} ctx
  9. * @private
  10. */
  11. _line(ctx, values, viaNodes) {
  12. // get the coordinates of the support points.
  13. let via1 = viaNodes[0];
  14. let via2 = viaNodes[1];
  15. this._bezierCurve(ctx, values, via1, via2);
  16. }
  17. _getViaCoordinates() {
  18. let dx = this.from.x - this.to.x;
  19. let dy = this.from.y - this.to.y;
  20. let x1, y1, x2, y2;
  21. let roundness = this.options.smooth.roundness;
  22. // horizontal if x > y or if direction is forced or if direction is horizontal
  23. if ((Math.abs(dx) > Math.abs(dy) || this.options.smooth.forceDirection === true || this.options.smooth.forceDirection === 'horizontal') && this.options.smooth.forceDirection !== 'vertical') {
  24. y1 = this.from.y;
  25. y2 = this.to.y;
  26. x1 = this.from.x - roundness * dx;
  27. x2 = this.to.x + roundness * dx;
  28. }
  29. else {
  30. y1 = this.from.y - roundness * dy;
  31. y2 = this.to.y + roundness * dy;
  32. x1 = this.from.x;
  33. x2 = this.to.x;
  34. }
  35. return [{x: x1, y: y1},{x: x2, y: y2}];
  36. }
  37. getViaNode() {
  38. return this._getViaCoordinates();
  39. }
  40. _findBorderPosition(nearNode, ctx) {
  41. return this._findBorderPositionBezier(nearNode, ctx);
  42. }
  43. _getDistanceToEdge(x1, y1, x2, y2, x3, y3, [via1, via2] = this._getViaCoordinates()) { // x3,y3 is the point
  44. return this._getDistanceToBezierEdge(x1, y1, x2, y2, x3, y3, via1, via2);
  45. }
  46. /**
  47. * Combined function of pointOnLine and pointOnBezier. This gives the coordinates of a point on the line at a certain percentage of the way
  48. * @param percentage
  49. * @param via
  50. * @returns {{x: number, y: number}}
  51. * @private
  52. */
  53. getPoint(percentage, [via1, via2] = this._getViaCoordinates()) {
  54. let t = percentage;
  55. let vec = [];
  56. vec[0] = Math.pow(1 - t, 3);
  57. vec[1] = 3 * t * Math.pow(1 - t, 2);
  58. vec[2] = 3 * Math.pow(t,2) * (1 - t);
  59. vec[3] = Math.pow(t, 3);
  60. let x = vec[0] * this.fromPoint.x + vec[1] * via1.x + vec[2] * via2.x + vec[3] * this.toPoint.x;
  61. let y = vec[0] * this.fromPoint.y + vec[1] * via1.y + vec[2] * via2.y + vec[3] * this.toPoint.y;
  62. return {x: x, y: y};
  63. }
  64. }
  65. export default CubicBezierEdge;