Skip to content

Commit

Permalink
Add Path.spawnCurve
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Feb 21, 2024
1 parent 9b60a1a commit 08b67e6
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,7 @@ export namespace Path {
const segLoc = toSegmentLocation(path, loc)
return Segment.orientation(segLoc.segment, segLoc.location)
}

/**
* Maps each segment in the path to a single or array of vertices and creates a new path concatinating those vertices. you can change the type of commands, and change the number of them in the path, but you cannot change the topology of the path. The segments that were originally continuous remains connected, and vice versa.
* @param path The path to map
Expand Down Expand Up @@ -1257,6 +1258,18 @@ export namespace Path {
}
}

export function spawnCurve<
V1 extends Vertex = Vertex,
V2 extends Vertex = Vertex,
>(
path: Path<V1>,
fn: (curve: Curve<V1>, curveIndex: number) => Curve<V2> | Curve<V2>[]
): Path<V2> {
return {
curves: path.curves.flatMap(fn),
}
}

/**
* Transforms the given path by the given matrix.
* @param path The path to transform
Expand Down Expand Up @@ -2699,11 +2712,18 @@ export namespace Path {
* Returns the path drawn by the pen so far.
*/
get(): Path {
return {
curves: [
...this.#curves.slice(0, -1),
...(this.current ? [this.current.curve] : []),
],
if (this.current) {
return {
curves: [
...this.#curves.slice(0, -1),
{
vertices: [...this.current.curve.vertices],
closed: this.current.curve.closed,
},
],
}
} else {
return {curves: [...this.#curves]}
}
}
}
Expand Down

0 comments on commit 08b67e6

Please sign in to comment.