From 796191dbc5dd712c7106a02cb4e4b4489e3a9ad8 Mon Sep 17 00:00:00 2001 From: Andrea Cappuccio Date: Sat, 21 Nov 2026 23:16:50 +0100 Subject: [PATCH] faster rounded path strings --- src/path.js | 66 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/src/path.js b/src/path.js index 960efde..4fd5574 100644 --- a/src/path.js +++ b/src/path.js @@ -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]; }; } @@ -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}`); @@ -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. @@ -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! @@ -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}`); @@ -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. @@ -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! @@ -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._;