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.

69 lines
1.9 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. // draw shadow if enabled
  20. this.enableShadow(ctx);
  21. ctx.stroke();
  22. this.disableShadow(ctx);
  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;