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.

102 lines
3.4 KiB

9 years ago
  1. import EdgeBase from './util/EdgeBase'
  2. /**
  3. * A Straight Edge.
  4. *
  5. * @extends EdgeBase
  6. */
  7. class StraightEdge extends EdgeBase {
  8. /**
  9. * @param {Object} options
  10. * @param {Object} body
  11. * @param {Label} labelModule
  12. */
  13. constructor(options, body, labelModule) {
  14. super(options, body, labelModule);
  15. }
  16. /**
  17. * Draw a line between two nodes
  18. * @param {CanvasRenderingContext2D} ctx
  19. * @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
  20. * @private
  21. */
  22. _line(ctx, values) {
  23. // draw a straight line
  24. ctx.beginPath();
  25. ctx.moveTo(this.fromPoint.x, this.fromPoint.y);
  26. ctx.lineTo(this.toPoint.x, this.toPoint.y);
  27. // draw shadow if enabled
  28. this.enableShadow(ctx, values);
  29. ctx.stroke();
  30. this.disableShadow(ctx, values);
  31. }
  32. /**
  33. *
  34. * @returns {undefined}
  35. */
  36. getViaNode() {
  37. return undefined;
  38. }
  39. /**
  40. * Combined function of pointOnLine and pointOnBezier. This gives the coordinates of a point on the line at a certain percentage of the way
  41. *
  42. * @param {number} percentage
  43. * @returns {{x: number, y: number}}
  44. * @private
  45. */
  46. getPoint(percentage) {
  47. return {
  48. x: (1 - percentage) * this.fromPoint.x + percentage * this.toPoint.x,
  49. y: (1 - percentage) * this.fromPoint.y + percentage * this.toPoint.y
  50. }
  51. }
  52. /**
  53. *
  54. * @param {Node} nearNode
  55. * @param {CanvasRenderingContext2D} ctx
  56. * @returns {{x: number, y: number}}
  57. * @private
  58. */
  59. _findBorderPosition(nearNode, ctx) {
  60. let node1 = this.to;
  61. let node2 = this.from;
  62. if (nearNode.id === this.from.id) {
  63. node1 = this.from;
  64. node2 = this.to;
  65. }
  66. let angle = Math.atan2((node1.y - node2.y), (node1.x - node2.x));
  67. let dx = (node1.x - node2.x);
  68. let dy = (node1.y - node2.y);
  69. let edgeSegmentLength = Math.sqrt(dx * dx + dy * dy);
  70. let toBorderDist = nearNode.distanceToBorder(ctx, angle);
  71. let toBorderPoint = (edgeSegmentLength - toBorderDist) / edgeSegmentLength;
  72. let borderPos = {};
  73. borderPos.x = (1 - toBorderPoint) * node2.x + toBorderPoint * node1.x;
  74. borderPos.y = (1 - toBorderPoint) * node2.y + toBorderPoint * node1.y;
  75. return borderPos;
  76. }
  77. /**
  78. *
  79. * @param {number} x1
  80. * @param {number} y1
  81. * @param {number} x2
  82. * @param {number} y2
  83. * @param {number} x3
  84. * @param {number} y3
  85. * @returns {number}
  86. * @private
  87. */
  88. _getDistanceToEdge(x1, y1, x2, y2, x3, y3) { // x3,y3 is the point
  89. return this._getDistanceToLine(x1, y1, x2, y2, x3, y3);
  90. }
  91. }
  92. export default StraightEdge;