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.

65 lines
1.7 KiB

  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. let angle = Math.atan2((node1.y - node2.y), (node1.x - node2.x));
  40. let dx = (node1.x - node2.x);
  41. let dy = (node1.y - node2.y);
  42. let edgeSegmentLength = Math.sqrt(dx * dx + dy * dy);
  43. let toBorderDist = nearNode.distanceToBorder(ctx, angle);
  44. let toBorderPoint = (edgeSegmentLength - toBorderDist) / edgeSegmentLength;
  45. let borderPos = {};
  46. borderPos.x = (1 - toBorderPoint) * node2.x + toBorderPoint * node1.x;
  47. borderPos.y = (1 - toBorderPoint) * node2.y + toBorderPoint * node1.y;
  48. return borderPos;
  49. }
  50. _getDistanceToEdge(x1, y1, x2, y2, x3, y3) { // x3,y3 is the point
  51. return this._getDistanceToLine(x1, y1, x2, y2, x3, y3);
  52. }
  53. }
  54. export default StraightEdge;