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.

70 lines
1.8 KiB

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