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. /**
  7. * Draw a line between two nodes
  8. * @param {CanvasRenderingContext2D} ctx
  9. * @private
  10. */
  11. _line(ctx, values) {
  12. // draw a straight line
  13. ctx.beginPath();
  14. ctx.moveTo(this.fromPoint.x, this.fromPoint.y);
  15. ctx.lineTo(this.toPoint.x, this.toPoint.y);
  16. // draw shadow if enabled
  17. this.enableShadow(ctx, values);
  18. ctx.stroke();
  19. this.disableShadow(ctx, values);
  20. }
  21. getViaNode() {
  22. return undefined;
  23. }
  24. /**
  25. * Combined function of pointOnLine and pointOnBezier. This gives the coordinates of a point on the line at a certain percentage of the way
  26. * @param percentage
  27. * @param via
  28. * @returns {{x: number, y: number}}
  29. * @private
  30. */
  31. getPoint(percentage) {
  32. return {
  33. x: (1 - percentage) * this.fromPoint.x + percentage * this.toPoint.x,
  34. y: (1 - percentage) * this.fromPoint.y + percentage * this.toPoint.y
  35. }
  36. }
  37. _findBorderPosition(nearNode, ctx) {
  38. let node1 = this.to;
  39. let node2 = this.from;
  40. if (nearNode.id === this.from.id) {
  41. node1 = this.from;
  42. node2 = this.to;
  43. }
  44. let angle = Math.atan2((node1.y - node2.y), (node1.x - node2.x));
  45. let dx = (node1.x - node2.x);
  46. let dy = (node1.y - node2.y);
  47. let edgeSegmentLength = Math.sqrt(dx * dx + dy * dy);
  48. let toBorderDist = nearNode.distanceToBorder(ctx, angle);
  49. let toBorderPoint = (edgeSegmentLength - toBorderDist) / edgeSegmentLength;
  50. let borderPos = {};
  51. borderPos.x = (1 - toBorderPoint) * node2.x + toBorderPoint * node1.x;
  52. borderPos.y = (1 - toBorderPoint) * node2.y + toBorderPoint * node1.y;
  53. return borderPos;
  54. }
  55. _getDistanceToEdge(x1, y1, x2, y2, x3, y3) { // x3,y3 is the point
  56. return this._getDistanceToLine(x1, y1, x2, y2, x3, y3);
  57. }
  58. }
  59. export default StraightEdge;