UNPKG

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