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.

79 lines
2.1 KiB

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