Skip to content

Commit

Permalink
feat: improve path builder
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Dec 16, 2024
1 parent 74d21e3 commit d0ca68d
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/component/utility/PathBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
export class PathBuilder {
private array: string[] = [];
private path = '';

private appendPath(segment: string) {
this.path += `${segment} `;
}

public moveTo(x: number, y: number) {
x = clamp(x);
y = clamp(y);
this.array.push(`M ${x} ${y}`);
this.appendPath(`M ${x} ${y}`);
}

public lineTo(x: number, y: number) {
x = clamp(x);
y = clamp(y);
this.array.push(`L ${x} ${y}`);
this.appendPath(`L ${x} ${y}`);
}

public closePath() {
if (this.array.length > 0) this.array.push('Z');
if (this.path.length > 0) this.appendPath('Z');
}

public toString() {
return this.array.join(' ');
return this.path.trim();
}

public concatPath(pathBuilder: PathBuilder) {
return this.array.concat(pathBuilder.array).join(' ');
return (this.path + pathBuilder.path).trim();
}
}

Expand Down

0 comments on commit d0ca68d

Please sign in to comment.