From 899742ef1503d1f0b71f4003df49eb3fa177d343 Mon Sep 17 00:00:00 2001 From: Baku Hashimoto Date: Thu, 28 Mar 2024 22:02:35 +0900 Subject: [PATCH] Add crossFirstPoint option to Path.trim --- src/Path.ts | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Path.ts b/src/Path.ts index 761bd30..eab77ec 100644 --- a/src/Path.ts +++ b/src/Path.ts @@ -1576,7 +1576,12 @@ export namespace Path { * @returns The trimmed path * @category Modifiers */ - export function trim(path: Path, from: PathLocation, to: PathLocation): Path { + export function trim( + path: Path, + from: PathLocation, + to: PathLocation, + crossFirstPoint = true + ): Path { let fromLoc = toTime(path, from) let toLoc = toTime(path, to) @@ -1596,7 +1601,30 @@ export namespace Path { // If the range is in the same curve const curve = path.curves[fromLoc.curveIndex] - return {curves: [Curve.trim(curve, fromCurveLoc, toCurveLoc)]} + if (crossFirstPoint && fromLoc.location.time > toLoc.location.time) { + const firstCurve = Curve.trim(curve, fromCurveLoc, 1) + const secondCurve = Curve.trim(curve, 0, toCurveLoc) + + if (curve.closed) { + return { + curves: [ + { + vertices: [ + ...firstCurve.vertices, + ...secondCurve.vertices.slice(1), + ], + closed: false, + }, + ], + } + } else { + return { + curves: [firstCurve, secondCurve], + } + } + } else { + return {curves: [Curve.trim(curve, fromCurveLoc, toCurveLoc)]} + } } else if (fromLoc.curveIndex !== toLoc.curveIndex) { const invert = fromLoc.curveIndex > toLoc.curveIndex