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.

90 lines
2.7 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) {
  12. // get the coordinates of the support points.
  13. let [via1,via2] = this._getViaCoordinates();
  14. let returnValue = [via1,via2];
  15. // start drawing the line.
  16. ctx.beginPath();
  17. ctx.moveTo(this.from.x, this.from.y);
  18. // fallback to normal straight edges
  19. if (via1.x === undefined) {
  20. ctx.lineTo(this.to.x, this.to.y);
  21. returnValue = undefined;
  22. }
  23. else {
  24. ctx.bezierCurveTo(via1.x, via1.y, via2.x, via2.y, this.to.x, this.to.y);
  25. }
  26. // draw shadow if enabled
  27. this.enableShadow(ctx);
  28. ctx.stroke();
  29. this.disableShadow(ctx);
  30. return returnValue;
  31. }
  32. _getViaCoordinates() {
  33. let dx = this.from.x - this.to.x;
  34. let dy = this.from.y - this.to.y;
  35. let x1, y1, x2, y2;
  36. let roundness = this.options.smooth.roundness;;
  37. // horizontal if x > y or if direction is forced or if direction is horizontal
  38. if ((Math.abs(dx) > Math.abs(dy) || this.options.smooth.forceDirection === true || this.options.smooth.forceDirection === 'horizontal') && this.options.smooth.forceDirection !== 'vertical') {
  39. y1 = this.from.y;
  40. y2 = this.to.y;
  41. x1 = this.from.x - roundness * dx;
  42. x2 = this.to.x + roundness * dx;
  43. }
  44. else {
  45. y1 = this.from.y - roundness * dy;
  46. y2 = this.to.y + roundness * dy;
  47. x1 = this.from.x;
  48. x2 = this.to.x;
  49. }
  50. return [{x: x1, y: y1},{x: x2, y: y2}];
  51. }
  52. _findBorderPosition(nearNode, ctx) {
  53. return this._findBorderPositionBezier(nearNode, ctx);
  54. }
  55. _getDistanceToEdge(x1, y1, x2, y2, x3, y3, [via1, via2] = this._getViaCoordinates()) { // x3,y3 is the point
  56. return this._getDistanceToBezierEdge(x1, y1, x2, y2, x3, y3, via1, via2);
  57. }
  58. /**
  59. * Combined function of pointOnLine and pointOnBezier. This gives the coordinates of a point on the line at a certain percentage of the way
  60. * @param percentage
  61. * @param via
  62. * @returns {{x: number, y: number}}
  63. * @private
  64. */
  65. getPoint(percentage, [via1, via2] = this._getViaCoordinates()) {
  66. let t = percentage;
  67. let vec = [];
  68. vec[0] = Math.pow(1 - t, 3);
  69. vec[1] = 3 * t * Math.pow(1 - t, 2);
  70. vec[2] = 3 * Math.pow(t,2) * (1 - t);
  71. vec[3] = Math.pow(t, 3);
  72. let x = vec[0] * this.from.x + vec[1] * via1.x + vec[2] * via2.x + vec[3] * this.to.x;
  73. let y = vec[0] * this.from.y + vec[1] * via1.y + vec[2] * via2.y + vec[3] * this.to.y;
  74. return {x: x, y: y};
  75. }
  76. }
  77. export default CubicBezierEdge;