loop.js 1.0 KB

1234567891011121314151617181920212223
  1. import { Path, Point } from '@antv/x6-geometry';
  2. export const loop = function (sourcePoint, targetPoint, routePoints, options = {}) {
  3. const fix = routePoints.length === 3 ? 0 : 1;
  4. const p1 = Point.create(routePoints[0 + fix]);
  5. const p2 = Point.create(routePoints[2 + fix]);
  6. const center = Point.create(routePoints[1 + fix]);
  7. if (!Point.equals(sourcePoint, targetPoint)) {
  8. const middle = new Point((sourcePoint.x + targetPoint.x) / 2, (sourcePoint.y + targetPoint.y) / 2);
  9. const angle = middle.angleBetween(Point.create(sourcePoint).rotate(90, middle), center);
  10. if (angle > 1) {
  11. p1.rotate(180 - angle, middle);
  12. p2.rotate(180 - angle, middle);
  13. center.rotate(180 - angle, middle);
  14. }
  15. }
  16. const pathData = `
  17. M ${sourcePoint.x} ${sourcePoint.y}
  18. Q ${p1.x} ${p1.y} ${center.x} ${center.y}
  19. Q ${p2.x} ${p2.y} ${targetPoint.x} ${targetPoint.y}
  20. `;
  21. return options.raw ? Path.parse(pathData) : pathData;
  22. };
  23. //# sourceMappingURL=loop.js.map