UNPKG

4.54 kBJavaScriptView Raw
1const pi = Math.PI,
2 tau = 2 * pi,
3 epsilon = 1e-6,
4 tauEpsilon = tau - epsilon;
5
6function append(strings) {
7 this._ += strings[0];
8 for (let i = 1, n = strings.length; i < n; ++i) {
9 this._ += arguments[i] + strings[i];
10 }
11}
12
13function appendRound(digits) {
14 let d = Math.floor(digits);
15 if (!(d >= 0)) throw new Error(`invalid digits: ${digits}`);
16 if (d > 15) return append;
17 const k = 10 ** d;
18 return function(strings) {
19 this._ += strings[0];
20 for (let i = 1, n = strings.length; i < n; ++i) {
21 this._ += Math.round(arguments[i] * k) / k + strings[i];
22 }
23 };
24}
25
26export class Path {
27 constructor(digits) {
28 this._x0 = this._y0 = // start of current subpath
29 this._x1 = this._y1 = null; // end of current subpath
30 this._ = "";
31 this._append = digits == null ? append : appendRound(digits);
32 }
33 moveTo(x, y) {
34 this._append`M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}`;
35 }
36 closePath() {
37 if (this._x1 !== null) {
38 this._x1 = this._x0, this._y1 = this._y0;
39 this._append`Z`;
40 }
41 }
42 lineTo(x, y) {
43 this._append`L${this._x1 = +x},${this._y1 = +y}`;
44 }
45 quadraticCurveTo(x1, y1, x, y) {
46 this._append`Q${+x1},${+y1},${this._x1 = +x},${this._y1 = +y}`;
47 }
48 bezierCurveTo(x1, y1, x2, y2, x, y) {
49 this._append`C${+x1},${+y1},${+x2},${+y2},${this._x1 = +x},${this._y1 = +y}`;
50 }
51 arcTo(x1, y1, x2, y2, r) {
52 x1 = +x1, y1 = +y1, x2 = +x2, y2 = +y2, r = +r;
53
54 // Is the radius negative? Error.
55 if (r < 0) throw new Error(`negative radius: ${r}`);
56
57 let x0 = this._x1,
58 y0 = this._y1,
59 x21 = x2 - x1,
60 y21 = y2 - y1,
61 x01 = x0 - x1,
62 y01 = y0 - y1,
63 l01_2 = x01 * x01 + y01 * y01;
64
65 // Is this path empty? Move to (x1,y1).
66 if (this._x1 === null) {
67 this._append`M${this._x1 = x1},${this._y1 = y1}`;
68 }
69
70 // Or, is (x1,y1) coincident with (x0,y0)? Do nothing.
71 else if (!(l01_2 > epsilon));
72
73 // Or, are (x0,y0), (x1,y1) and (x2,y2) collinear?
74 // Equivalently, is (x1,y1) coincident with (x2,y2)?
75 // Or, is the radius zero? Line to (x1,y1).
76 else if (!(Math.abs(y01 * x21 - y21 * x01) > epsilon) || !r) {
77 this._append`L${this._x1 = x1},${this._y1 = y1}`;
78 }
79
80 // Otherwise, draw an arc!
81 else {
82 let x20 = x2 - x0,
83 y20 = y2 - y0,
84 l21_2 = x21 * x21 + y21 * y21,
85 l20_2 = x20 * x20 + y20 * y20,
86 l21 = Math.sqrt(l21_2),
87 l01 = Math.sqrt(l01_2),
88 l = r * Math.tan((pi - Math.acos((l21_2 + l01_2 - l20_2) / (2 * l21 * l01))) / 2),
89 t01 = l / l01,
90 t21 = l / l21;
91
92 // If the start tangent is not coincident with (x0,y0), line to.
93 if (Math.abs(t01 - 1) > epsilon) {
94 this._append`L${x1 + t01 * x01},${y1 + t01 * y01}`;
95 }
96
97 this._append`A${r},${r},0,0,${+(y01 * x20 > x01 * y20)},${this._x1 = x1 + t21 * x21},${this._y1 = y1 + t21 * y21}`;
98 }
99 }
100 arc(x, y, r, a0, a1, ccw) {
101 x = +x, y = +y, r = +r, ccw = !!ccw;
102
103 // Is the radius negative? Error.
104 if (r < 0) throw new Error(`negative radius: ${r}`);
105
106 let dx = r * Math.cos(a0),
107 dy = r * Math.sin(a0),
108 x0 = x + dx,
109 y0 = y + dy,
110 cw = 1 ^ ccw,
111 da = ccw ? a0 - a1 : a1 - a0;
112
113 // Is this path empty? Move to (x0,y0).
114 if (this._x1 === null) {
115 this._append`M${x0},${y0}`;
116 }
117
118 // Or, is (x0,y0) not coincident with the previous point? Line to (x0,y0).
119 else if (Math.abs(this._x1 - x0) > epsilon || Math.abs(this._y1 - y0) > epsilon) {
120 this._append`L${x0},${y0}`;
121 }
122
123 // Is this arc empty? We’re done.
124 if (!r) return;
125
126 // Does the angle go the wrong way? Flip the direction.
127 if (da < 0) da = da % tau + tau;
128
129 // Is this a complete circle? Draw two arcs to complete the circle.
130 if (da > tauEpsilon) {
131 this._append`A${r},${r},0,1,${cw},${x - dx},${y - dy}A${r},${r},0,1,${cw},${this._x1 = x0},${this._y1 = y0}`;
132 }
133
134 // Is this arc non-empty? Draw an arc!
135 else if (da > epsilon) {
136 this._append`A${r},${r},0,${+(da >= pi)},${cw},${this._x1 = x + r * Math.cos(a1)},${this._y1 = y + r * Math.sin(a1)}`;
137 }
138 }
139 rect(x, y, w, h) {
140 this._append`M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}h${w = +w}v${+h}h${-w}Z`;
141 }
142 toString() {
143 return this._;
144 }
145}
146
147export function path() {
148 return new Path;
149}
150
151// Allow instanceof d3.path
152path.prototype = Path.prototype;
153
154export function pathRound(digits = 3) {
155 return new Path(+digits);
156}