Skip to content

Commit

Permalink
feat(geom): add attrib support to PathBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 22, 2019
1 parent 8c1df49 commit a017b10
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions packages/geom/src/ctors/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,41 +43,47 @@ export const normalizedPath =
);

export const roundedRect =
(pos: Vec, size: Vec, r: number | Vec) => {
(pos: Vec, size: Vec, r: number | Vec, attribs?: Attribs) => {
r = isNumber(r) ? [r, r] : r;
const b = new PathBuilder();
const [w, h] = maddN2([], size, r, -2);
b.moveTo([pos[0] + r[0], pos[1]]);
b.hlineTo(w, true);
b.arcTo(r, r, 0, false, true, true);
b.vlineTo(h, true);
b.arcTo([-r[0], r[1]], r, 0, false, true, true);
b.hlineTo(-w, true);
b.arcTo([-r[0], -r[1]], r, 0, false, true, true);
b.vlineTo(-h, true);
b.arcTo([r[0], -r[1]], r, 0, false, true, true);
return b.curr;
return new PathBuilder(attribs)
.moveTo([pos[0] + r[0], pos[1]])
.hlineTo(w, true)
.arcTo(r, r, 0, false, true, true)
.vlineTo(h, true)
.arcTo([-r[0], r[1]], r, 0, false, true, true)
.hlineTo(-w, true)
.arcTo([-r[0], -r[1]], r, 0, false, true, true)
.vlineTo(-h, true)
.arcTo([r[0], -r[1]], r, 0, false, true, true)
.current();
};

export class PathBuilder {

paths: Path[];
curr: Path;
attribs: Attribs;
protected curr: Path;
protected currP: Vec;
protected bezierP: Vec;
protected startP: Vec;

constructor() {
constructor(attribs?: Attribs) {
this.paths = [];
this.attribs = attribs;
this.newPath();
}

*[Symbol.iterator]() {
yield* this.paths;
}

current() {
return this.curr;
}

newPath() {
this.curr = new Path();
this.curr = new Path([], this.attribs);
this.paths.push(this.curr);
this.currP = zeroes(2);
this.bezierP = zeroes(2);
Expand Down Expand Up @@ -242,7 +248,7 @@ export class PathBuilder {
}

export const pathBuilder =
() => new PathBuilder();
(attribs?: Attribs) => new PathBuilder(attribs);

const CMD_RE = /[achlmqstvz]/i;

Expand Down

0 comments on commit a017b10

Please sign in to comment.