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.

70 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. /**
  7. * Draw a line between two nodes
  8. * @param {CanvasRenderingContext2D} ctx
  9. * @param {Array<Object>} values
  10. * @private
  11. */
  12. _line(ctx, values) {
  13. // draw a straight line
  14. ctx.beginPath();
  15. ctx.moveTo(this.fromPoint.x, this.fromPoint.y);
  16. ctx.lineTo(this.toPoint.x, this.toPoint.y);
  17. // draw shadow if enabled
  18. this.enableShadow(ctx, values);
  19. ctx.stroke();
  20. this.disableShadow(ctx, values);
  21. }
  22. getViaNode() {
  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. *
  28. * @param {number} percentage
  29. * @returns {{x: number, y: number}}
  30. * @private
  31. */
  32. getPoint(percentage) {
  33. return {
  34. x: (1 - percentage) * this.fromPoint.x + percentage * this.toPoint.x,
  35. y: (1 - percentage) * this.fromPoint.y + percentage * this.toPoint.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;