smooth.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { Curve, Path } from '@antv/x6-geometry';
  2. export const smooth = function (sourcePoint, targetPoint, routePoints, options = {}) {
  3. let path;
  4. let direction = options.direction;
  5. if (routePoints && routePoints.length !== 0) {
  6. const points = [sourcePoint, ...routePoints, targetPoint];
  7. const curves = Curve.throughPoints(points);
  8. path = new Path(curves);
  9. }
  10. else {
  11. // If we have no route, use a default cubic bezier curve, cubic bezier
  12. // requires two control points, the control points have `x` midway
  13. // between source and target. This produces an S-like curve.
  14. path = new Path();
  15. path.appendSegment(Path.createSegment('M', sourcePoint));
  16. if (!direction) {
  17. direction =
  18. Math.abs(sourcePoint.x - targetPoint.x) >=
  19. Math.abs(sourcePoint.y - targetPoint.y)
  20. ? 'H'
  21. : 'V';
  22. }
  23. if (direction === 'H') {
  24. const controlPointX = (sourcePoint.x + targetPoint.x) / 2;
  25. path.appendSegment(Path.createSegment('C', controlPointX, sourcePoint.y, controlPointX, targetPoint.y, targetPoint.x, targetPoint.y));
  26. }
  27. else {
  28. const controlPointY = (sourcePoint.y + targetPoint.y) / 2;
  29. path.appendSegment(Path.createSegment('C', sourcePoint.x, controlPointY, targetPoint.x, controlPointY, targetPoint.x, targetPoint.y));
  30. }
  31. }
  32. return options.raw ? path : path.serialize();
  33. };
  34. //# sourceMappingURL=smooth.js.map