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.

71 lines
1.8 KiB

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