smooth.js 1.7 KB

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