1 | const pi = Math.PI, tau = 2 * pi, epsilon = 1e-6, tauEpsilon = tau - epsilon;
|
2 | function append(strings) {
|
3 | this._ += strings[0];
|
4 | for (let i = 1, n = strings.length; i < n; ++i) {
|
5 | this._ += arguments[i] + strings[i];
|
6 | }
|
7 | }
|
8 | function appendRound(digits) {
|
9 | let d = Math.floor(digits);
|
10 | if (!(d >= 0))
|
11 | throw new Error(`invalid digits: ${digits}`);
|
12 | if (d > 15)
|
13 | return append;
|
14 | const k = 10 ** d;
|
15 | return function(strings) {
|
16 | this._ += strings[0];
|
17 | for (let i = 1, n = strings.length; i < n; ++i) {
|
18 | this._ += Math.round(arguments[i] * k) / k + strings[i];
|
19 | }
|
20 | };
|
21 | }
|
22 | class Path {
|
23 | constructor(digits) {
|
24 | this._x0 = this._y0 =
|
25 | this._x1 = this._y1 = null;
|
26 | this._ = "";
|
27 | this._append = digits == null ? append : appendRound(digits);
|
28 | }
|
29 | moveTo(x, y) {
|
30 | this._append`M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}`;
|
31 | }
|
32 | closePath() {
|
33 | if (this._x1 !== null) {
|
34 | this._x1 = this._x0, this._y1 = this._y0;
|
35 | this._append`Z`;
|
36 | }
|
37 | }
|
38 | lineTo(x, y) {
|
39 | this._append`L${this._x1 = +x},${this._y1 = +y}`;
|
40 | }
|
41 | quadraticCurveTo(x1, y1, x, y) {
|
42 | this._append`Q${+x1},${+y1},${this._x1 = +x},${this._y1 = +y}`;
|
43 | }
|
44 | bezierCurveTo(x1, y1, x2, y2, x, y) {
|
45 | this._append`C${+x1},${+y1},${+x2},${+y2},${this._x1 = +x},${this._y1 = +y}`;
|
46 | }
|
47 | arcTo(x1, y1, x2, y2, r) {
|
48 | x1 = +x1, y1 = +y1, x2 = +x2, y2 = +y2, r = +r;
|
49 | if (r < 0)
|
50 | throw new Error(`negative radius: ${r}`);
|
51 | let x0 = this._x1, y0 = this._y1, x21 = x2 - x1, y21 = y2 - y1, x01 = x0 - x1, y01 = y0 - y1, l01_2 = x01 * x01 + y01 * y01;
|
52 | if (this._x1 === null) {
|
53 | this._append`M${this._x1 = x1},${this._y1 = y1}`;
|
54 | } else if (!(l01_2 > epsilon))
|
55 | ;
|
56 | else if (!(Math.abs(y01 * x21 - y21 * x01) > epsilon) || !r) {
|
57 | this._append`L${this._x1 = x1},${this._y1 = y1}`;
|
58 | } else {
|
59 | let x20 = x2 - x0, y20 = y2 - y0, l21_2 = x21 * x21 + y21 * y21, l20_2 = x20 * x20 + y20 * y20, l21 = Math.sqrt(l21_2), l01 = Math.sqrt(l01_2), l = r * Math.tan((pi - Math.acos((l21_2 + l01_2 - l20_2) / (2 * l21 * l01))) / 2), t01 = l / l01, t21 = l / l21;
|
60 | if (Math.abs(t01 - 1) > epsilon) {
|
61 | this._append`L${x1 + t01 * x01},${y1 + t01 * y01}`;
|
62 | }
|
63 | this._append`A${r},${r},0,0,${+(y01 * x20 > x01 * y20)},${this._x1 = x1 + t21 * x21},${this._y1 = y1 + t21 * y21}`;
|
64 | }
|
65 | }
|
66 | arc(x, y, r, a0, a1, ccw) {
|
67 | x = +x, y = +y, r = +r, ccw = !!ccw;
|
68 | if (r < 0)
|
69 | throw new Error(`negative radius: ${r}`);
|
70 | let dx = r * Math.cos(a0), dy = r * Math.sin(a0), x0 = x + dx, y0 = y + dy, cw = 1 ^ ccw, da = ccw ? a0 - a1 : a1 - a0;
|
71 | if (this._x1 === null) {
|
72 | this._append`M${x0},${y0}`;
|
73 | } else if (Math.abs(this._x1 - x0) > epsilon || Math.abs(this._y1 - y0) > epsilon) {
|
74 | this._append`L${x0},${y0}`;
|
75 | }
|
76 | if (!r)
|
77 | return;
|
78 | if (da < 0)
|
79 | da = da % tau + tau;
|
80 | if (da > tauEpsilon) {
|
81 | this._append`A${r},${r},0,1,${cw},${x - dx},${y - dy}A${r},${r},0,1,${cw},${this._x1 = x0},${this._y1 = y0}`;
|
82 | } else if (da > epsilon) {
|
83 | this._append`A${r},${r},0,${+(da >= pi)},${cw},${this._x1 = x + r * Math.cos(a1)},${this._y1 = y + r * Math.sin(a1)}`;
|
84 | }
|
85 | }
|
86 | rect(x, y, w, h) {
|
87 | this._append`M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}h${w = +w}v${+h}h${-w}Z`;
|
88 | }
|
89 | toString() {
|
90 | return this._;
|
91 | }
|
92 | }
|
93 | function constant(x) {
|
94 | return function constant2() {
|
95 | return x;
|
96 | };
|
97 | }
|
98 | function withPath(shape) {
|
99 | let digits = 3;
|
100 | shape.digits = function(_) {
|
101 | if (!arguments.length)
|
102 | return digits;
|
103 | if (_ == null) {
|
104 | digits = null;
|
105 | } else {
|
106 | const d = Math.floor(_);
|
107 | if (!(d >= 0))
|
108 | throw new RangeError(`invalid digits: ${_}`);
|
109 | digits = d;
|
110 | }
|
111 | return shape;
|
112 | };
|
113 | return () => new Path(digits);
|
114 | }
|
115 | export {
|
116 | constant as c,
|
117 | withPath as w
|
118 | };
|