rounded.js 1.4 KB

12345678910111213141516171819202122232425262728
  1. import { Point, Path } from '@antv/x6-geometry';
  2. export const rounded = function (sourcePoint, targetPoint, routePoints, options = {}) {
  3. const path = new Path();
  4. path.appendSegment(Path.createSegment('M', sourcePoint));
  5. const f13 = 1 / 3;
  6. const f23 = 2 / 3;
  7. const radius = options.radius || 10;
  8. let prevDistance;
  9. let nextDistance;
  10. for (let i = 0, ii = routePoints.length; i < ii; i += 1) {
  11. const curr = Point.create(routePoints[i]);
  12. const prev = routePoints[i - 1] || sourcePoint;
  13. const next = routePoints[i + 1] || targetPoint;
  14. prevDistance = nextDistance || curr.distance(prev) / 2;
  15. nextDistance = curr.distance(next) / 2;
  16. const startMove = -Math.min(radius, prevDistance);
  17. const endMove = -Math.min(radius, nextDistance);
  18. const roundedStart = curr.clone().move(prev, startMove).round();
  19. const roundedEnd = curr.clone().move(next, endMove).round();
  20. const control1 = new Point(f13 * roundedStart.x + f23 * curr.x, f23 * curr.y + f13 * roundedStart.y);
  21. const control2 = new Point(f13 * roundedEnd.x + f23 * curr.x, f23 * curr.y + f13 * roundedEnd.y);
  22. path.appendSegment(Path.createSegment('L', roundedStart));
  23. path.appendSegment(Path.createSegment('C', control1, control2, roundedEnd));
  24. }
  25. path.appendSegment(Path.createSegment('L', targetPoint));
  26. return options.raw ? path : path.serialize();
  27. };
  28. //# sourceMappingURL=rounded.js.map