const epsilon = 1e-6; export default class Path { constructor() { this._x0 = this._y0 = // start of current subpath this._x1 = this._y1 = null; // end of current subpath this._ = ""; } moveTo(x, y) { this._ += `M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}`; } closePath() { if (this._x1 !== null) { this._x1 = this._x0, this._y1 = this._y0; this._ += "Z"; } } lineTo(x, y) { this._ += `L${this._x1 = +x},${this._y1 = +y}`; } arc(x, y, r) { x = +x, y = +y, r = +r; const x0 = x + r; const y0 = y; if (r < 0) throw new Error("negative radius"); if (this._x1 === null) this._ += `M${x0},${y0}`; else if (Math.abs(this._x1 - x0) > epsilon || Math.abs(this._y1 - y0) > epsilon) this._ += "L" + x0 + "," + y0; if (!r) return; this._ += `A${r},${r},0,1,1,${x - r},${y}A${r},${r},0,1,1,${this._x1 = x0},${this._y1 = y0}`; } rect(x, y, w, h) { this._ += `M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}h${+w}v${+h}h${-w}Z`; } value() { return this._ || null; } }