UNPKG

3.17 kBJavaScriptView Raw
1const c = Math.PI, x = 2 * c, u = 1e-6, m = x - u;
2function E(e) {
3 this._ += e[0];
4 for (let t = 1, h = e.length; t < h; ++t)
5 this._ += arguments[t] + e[t];
6}
7function A(e) {
8 let t = Math.floor(e);
9 if (!(t >= 0))
10 throw new Error(`invalid digits: ${e}`);
11 if (t > 15)
12 return E;
13 const h = 10 ** t;
14 return function(i) {
15 this._ += i[0];
16 for (let s = 1, n = i.length; s < n; ++s)
17 this._ += Math.round(arguments[s] * h) / h + i[s];
18 };
19}
20class L {
21 constructor(t) {
22 this._x0 = this._y0 = // start of current subpath
23 this._x1 = this._y1 = null, this._ = "", this._append = t == null ? E : A(t);
24 }
25 moveTo(t, h) {
26 this._append`M${this._x0 = this._x1 = +t},${this._y0 = this._y1 = +h}`;
27 }
28 closePath() {
29 this._x1 !== null && (this._x1 = this._x0, this._y1 = this._y0, this._append`Z`);
30 }
31 lineTo(t, h) {
32 this._append`L${this._x1 = +t},${this._y1 = +h}`;
33 }
34 quadraticCurveTo(t, h, i, s) {
35 this._append`Q${+t},${+h},${this._x1 = +i},${this._y1 = +s}`;
36 }
37 bezierCurveTo(t, h, i, s, n, $) {
38 this._append`C${+t},${+h},${+i},${+s},${this._x1 = +n},${this._y1 = +$}`;
39 }
40 arcTo(t, h, i, s, n) {
41 if (t = +t, h = +h, i = +i, s = +s, n = +n, n < 0)
42 throw new Error(`negative radius: ${n}`);
43 let $ = this._x1, r = this._y1, p = i - t, l = s - h, _ = $ - t, o = r - h, a = _ * _ + o * o;
44 if (this._x1 === null)
45 this._append`M${this._x1 = t},${this._y1 = h}`;
46 else if (a > u)
47 if (!(Math.abs(o * p - l * _) > u) || !n)
48 this._append`L${this._x1 = t},${this._y1 = h}`;
49 else {
50 let d = i - $, f = s - r, y = p * p + l * l, T = d * d + f * f, g = Math.sqrt(y), v = Math.sqrt(a), w = n * Math.tan((c - Math.acos((y + a - T) / (2 * g * v))) / 2), M = w / v, b = w / g;
51 Math.abs(M - 1) > u && this._append`L${t + M * _},${h + M * o}`, this._append`A${n},${n},0,0,${+(o * d > _ * f)},${this._x1 = t + b * p},${this._y1 = h + b * l}`;
52 }
53 }
54 arc(t, h, i, s, n, $) {
55 if (t = +t, h = +h, i = +i, $ = !!$, i < 0)
56 throw new Error(`negative radius: ${i}`);
57 let r = i * Math.cos(s), p = i * Math.sin(s), l = t + r, _ = h + p, o = 1 ^ $, a = $ ? s - n : n - s;
58 this._x1 === null ? this._append`M${l},${_}` : (Math.abs(this._x1 - l) > u || Math.abs(this._y1 - _) > u) && this._append`L${l},${_}`, i && (a < 0 && (a = a % x + x), a > m ? this._append`A${i},${i},0,1,${o},${t - r},${h - p}A${i},${i},0,1,${o},${this._x1 = l},${this._y1 = _}` : a > u && this._append`A${i},${i},0,${+(a >= c)},${o},${this._x1 = t + i * Math.cos(n)},${this._y1 = h + i * Math.sin(n)}`);
59 }
60 rect(t, h, i, s) {
61 this._append`M${this._x0 = this._x1 = +t},${this._y0 = this._y1 = +h}h${i = +i}v${+s}h${-i}Z`;
62 }
63 toString() {
64 return this._;
65 }
66}
67function P(e) {
68 return function() {
69 return e;
70 };
71}
72function q(e) {
73 let t = 3;
74 return e.digits = function(h) {
75 if (!arguments.length)
76 return t;
77 if (h == null)
78 t = null;
79 else {
80 const i = Math.floor(h);
81 if (!(i >= 0))
82 throw new RangeError(`invalid digits: ${h}`);
83 t = i;
84 }
85 return e;
86 }, () => new L(t);
87}
88export {
89 P as c,
90 q as w
91};