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.

66 lines
1.8 KiB

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