Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 37 additions & 29 deletions src/path.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
const pi = Math.PI,
tau = 2 * pi,
epsilon = 1e-6,
tauEpsilon = tau - epsilon;
tauEpsilon = tau - epsilon,
fractions = [];

function append(strings) {
this._ += strings[0];
for (let i = 1, n = strings.length; i < n; ++i) {
this._ += arguments[i] + strings[i];
}
function format(x) {
return x;
}

function appendRound(digits) {
function formatRound(digits) {
let d = Math.floor(digits);
if (!(d >= 0)) throw new Error(`invalid digits: ${digits}`);
if (d > 15) return append;
if (d > 15) return format;
const k = 10 ** d;
return function(strings) {
this._ += strings[0];
for (let i = 1, n = strings.length; i < n; ++i) {
this._ += Math.round(arguments[i] * k) / k + strings[i];
}
if (d > 4) return x => Math.round(x * k) / k;
const f = fractions[d] || (fractions[d] = Array.from({length: k}, (_, i) => i ? `.${String(i).padStart(d, "0")}`.replace(/0+$/, "") : ""));
return function(x) {
let n = Math.round(x * k), s = "";
if (!(Math.abs(n) < 2 ** 52)) return n / k;
if (n < 0) n = -n, s = "-";
const q = Math.floor(n / k);
return s + q + f[n - q * k];
};
}

Expand All @@ -44,29 +45,34 @@ export class Path {
this._x0 = this._y0 = // start of current subpath
this._x1 = this._y1 = null; // end of current subpath
this._ = "";
this._append = digits == null ? append : appendRound(digits);
this._format = digits == null ? format : formatRound(digits);
this._equal = digits == null ? equal : equalRound(digits);
}
moveTo(x, y) {
this._append`M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}`;
const f = this._format;
this._ += `M${f(this._x0 = this._x1 = +x)},${f(this._y0 = this._y1 = +y)}`;
}
closePath() {
if (this._x1 !== null) {
this._x1 = this._x0, this._y1 = this._y0;
this._append`Z`;
this._ += "Z";
}
}
lineTo(x, y) {
this._append`L${this._x1 = +x},${this._y1 = +y}`;
const f = this._format;
this._ += `L${f(this._x1 = +x)},${f(this._y1 = +y)}`;
}
quadraticCurveTo(x1, y1, x, y) {
this._append`Q${+x1},${+y1},${this._x1 = +x},${this._y1 = +y}`;
const f = this._format;
this._ += `Q${f(+x1)},${f(+y1)},${f(this._x1 = +x)},${f(this._y1 = +y)}`;
}
bezierCurveTo(x1, y1, x2, y2, x, y) {
this._append`C${+x1},${+y1},${+x2},${+y2},${this._x1 = +x},${this._y1 = +y}`;
const f = this._format;
this._ += `C${f(+x1)},${f(+y1)},${f(+x2)},${f(+y2)},${f(this._x1 = +x)},${f(this._y1 = +y)}`;
}
arcTo(x1, y1, x2, y2, r) {
x1 = +x1, y1 = +y1, x2 = +x2, y2 = +y2, r = +r;
const f = this._format;

// Is the radius negative? Error.
if (r < 0) throw new Error(`negative radius: ${r}`);
Expand All @@ -81,7 +87,7 @@ export class Path {

// Is this path empty? Move to (x1,y1).
if (this._x1 === null) {
this._append`M${this._x1 = x1},${this._y1 = y1}`;
this._ += `M${f(this._x1 = x1)},${f(this._y1 = y1)}`;
}

// Or, is (x1,y1) coincident with (x0,y0)? Do nothing.
Expand All @@ -91,7 +97,7 @@ export class Path {
// Equivalently, is (x1,y1) coincident with (x2,y2)?
// Or, is the radius zero? Line to (x1,y1).
else if (!(Math.abs(y01 * x21 - y21 * x01) > epsilon) || !r) {
this._append`L${this._x1 = x1},${this._y1 = y1}`;
this._ += `L${f(this._x1 = x1)},${f(this._y1 = y1)}`;
}

// Otherwise, draw an arc!
Expand All @@ -108,14 +114,15 @@ export class Path {

// If the start tangent is not coincident with (x0,y0), line to.
if (Math.abs(t01 - 1) > epsilon) {
this._append`L${x1 + t01 * x01},${y1 + t01 * y01}`;
this._ += `L${f(x1 + t01 * x01)},${f(y1 + t01 * y01)}`;
}

this._append`A${r},${r},0,0,${+(y01 * x20 > x01 * y20)},${this._x1 = x1 + t21 * x21},${this._y1 = y1 + t21 * y21}`;
this._ += `A${f(r)},${f(r)},0,0,${+(y01 * x20 > x01 * y20)},${f(this._x1 = x1 + t21 * x21)},${f(this._y1 = y1 + t21 * y21)}`;
}
}
arc(x, y, r, a0, a1, ccw) {
x = +x, y = +y, r = +r, ccw = !!ccw;
const f = this._format;

// Is the radius negative? Error.
if (r < 0) throw new Error(`negative radius: ${r}`);
Expand All @@ -129,12 +136,12 @@ export class Path {

// Is this path empty? Move to (x0,y0).
if (this._x1 === null) {
this._append`M${x0},${y0}`;
this._ += `M${f(x0)},${f(y0)}`;
}

// Or, is (x0,y0) not coincident with the previous point? Line to (x0,y0).
else if (Math.abs(this._x1 - x0) > epsilon || Math.abs(this._y1 - y0) > epsilon) {
this._append`L${x0},${y0}`;
this._ += `L${f(x0)},${f(y0)}`;
}

// Is this arc empty? We’re done.
Expand All @@ -145,7 +152,7 @@ export class Path {

// Is this a complete circle? Draw two arcs to complete the circle.
if (da > tauEpsilon) {
this._append`A${r},${r},0,1,${cw},${x - dx},${y - dy}A${r},${r},0,1,${cw},${this._x1 = x0},${this._y1 = y0}`;
this._ += `A${f(r)},${f(r)},0,1,${cw},${f(x - dx)},${f(y - dy)}A${f(r)},${f(r)},0,1,${cw},${f(this._x1 = x0)},${f(this._y1 = y0)}`;
}

// Is this arc non-empty? Draw an arc!
Expand All @@ -156,13 +163,14 @@ export class Path {
if (da >= pi && this._equal(x0, y0, x1, y1)) {
da /= 2;
let a00 = a0 + da;
this._append`A${r},${r},0,${+(da >= pi)},${cw},${x + r * Math.cos(a00)},${y + r * Math.sin(a00)}`;
this._ += `A${f(r)},${f(r)},0,${+(da >= pi)},${cw},${f(x + r * Math.cos(a00))},${f(y + r * Math.sin(a00))}`;
}
this._append`A${r},${r},0,${+(da >= pi)},${cw},${this._x1 = x1},${this._y1 = y1}`;
this._ += `A${f(r)},${f(r)},0,${+(da >= pi)},${cw},${f(this._x1 = x1)},${f(this._y1 = y1)}`;
}
}
rect(x, y, w, h) {
this._append`M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}h${w = +w}v${+h}h${-w}Z`;
const f = this._format;
this._ += `M${f(this._x0 = this._x1 = +x)},${f(this._y0 = this._y1 = +y)}h${f(w = +w)}v${f(+h)}h${f(-w)}Z`;
}
toString() {
return this._;
Expand Down