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. /**
  7. * Draw a line between two nodes
  8. * @param {CanvasRenderingContext2D} ctx
  9. * @private
  10. */
  11. _line(ctx) {
  12. // draw a straight line
  13. ctx.beginPath();
  14. ctx.moveTo(this.from.x, this.from.y);
  15. ctx.lineTo(this.to.x, this.to.y);
  16. // draw shadow if enabled
  17. this.enableShadow(ctx);
  18. ctx.stroke();
  19. this.disableShadow(ctx);
  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;