UNPKG

3.28 kBJavaScriptView Raw
1const pi = Math.PI, tau = 2 * pi, epsilon = 1e-6, tauEpsilon = tau - epsilon;
2function Path() {
3 this._x0 = this._y0 = // start of current subpath
4 this._x1 = this._y1 = null;
5 this._ = "";
6}
7function path() {
8 return new Path();
9}
10Path.prototype = path.prototype = {
11 constructor: Path,
12 moveTo: function(x, y) {
13 this._ += "M" + (this._x0 = this._x1 = +x) + "," + (this._y0 = this._y1 = +y);
14 },
15 closePath: function() {
16 if (this._x1 !== null) {
17 this._x1 = this._x0, this._y1 = this._y0;
18 this._ += "Z";
19 }
20 },
21 lineTo: function(x, y) {
22 this._ += "L" + (this._x1 = +x) + "," + (this._y1 = +y);
23 },
24 quadraticCurveTo: function(x1, y1, x, y) {
25 this._ += "Q" + +x1 + "," + +y1 + "," + (this._x1 = +x) + "," + (this._y1 = +y);
26 },
27 bezierCurveTo: function(x1, y1, x2, y2, x, y) {
28 this._ += "C" + +x1 + "," + +y1 + "," + +x2 + "," + +y2 + "," + (this._x1 = +x) + "," + (this._y1 = +y);
29 },
30 arcTo: function(x1, y1, x2, y2, r) {
31 x1 = +x1, y1 = +y1, x2 = +x2, y2 = +y2, r = +r;
32 var x0 = this._x1, y0 = this._y1, x21 = x2 - x1, y21 = y2 - y1, x01 = x0 - x1, y01 = y0 - y1, l01_2 = x01 * x01 + y01 * y01;
33 if (r < 0)
34 throw new Error("negative radius: " + r);
35 if (this._x1 === null) {
36 this._ += "M" + (this._x1 = x1) + "," + (this._y1 = y1);
37 } else if (!(l01_2 > epsilon))
38 ;
39 else if (!(Math.abs(y01 * x21 - y21 * x01) > epsilon) || !r) {
40 this._ += "L" + (this._x1 = x1) + "," + (this._y1 = y1);
41 } else {
42 var 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;
43 if (Math.abs(t01 - 1) > epsilon) {
44 this._ += "L" + (x1 + t01 * x01) + "," + (y1 + t01 * y01);
45 }
46 this._ += "A" + r + "," + r + ",0,0," + +(y01 * x20 > x01 * y20) + "," + (this._x1 = x1 + t21 * x21) + "," + (this._y1 = y1 + t21 * y21);
47 }
48 },
49 arc: function(x, y, r, a0, a1, ccw) {
50 x = +x, y = +y, r = +r, ccw = !!ccw;
51 var 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;
52 if (r < 0)
53 throw new Error("negative radius: " + r);
54 if (this._x1 === null) {
55 this._ += "M" + x0 + "," + y0;
56 } else if (Math.abs(this._x1 - x0) > epsilon || Math.abs(this._y1 - y0) > epsilon) {
57 this._ += "L" + x0 + "," + y0;
58 }
59 if (!r)
60 return;
61 if (da < 0)
62 da = da % tau + tau;
63 if (da > tauEpsilon) {
64 this._ += "A" + r + "," + r + ",0,1," + cw + "," + (x - dx) + "," + (y - dy) + "A" + r + "," + r + ",0,1," + cw + "," + (this._x1 = x0) + "," + (this._y1 = y0);
65 } else if (da > epsilon) {
66 this._ += "A" + r + "," + r + ",0," + +(da >= pi) + "," + cw + "," + (this._x1 = x + r * Math.cos(a1)) + "," + (this._y1 = y + r * Math.sin(a1));
67 }
68 },
69 rect: function(x, y, w, h) {
70 this._ += "M" + (this._x0 = this._x1 = +x) + "," + (this._y0 = this._y1 = +y) + "h" + +w + "v" + +h + "h" + -w + "Z";
71 },
72 toString: function() {
73 return this._;
74 }
75};
76function constant(x) {
77 return function constant2() {
78 return x;
79 };
80}
81export {
82 constant as c,
83 path as p
84};