UNPKG

4.26 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7const pi = Math.PI,
8 tau = 2 * pi,
9 epsilon = 1e-6,
10 tauEpsilon = tau - epsilon;
11
12function Path() {
13 this._x0 = this._y0 = // start of current subpath
14 this._x1 = this._y1 = null; // end of current subpath
15
16 this._ = "";
17}
18
19function path() {
20 return new Path();
21}
22
23Path.prototype = path.prototype = {
24 constructor: Path,
25 moveTo: function (x, y) {
26 this._ += "M" + (this._x0 = this._x1 = +x) + "," + (this._y0 = this._y1 = +y);
27 },
28 closePath: function () {
29 if (this._x1 !== null) {
30 this._x1 = this._x0, this._y1 = this._y0;
31 this._ += "Z";
32 }
33 },
34 lineTo: function (x, y) {
35 this._ += "L" + (this._x1 = +x) + "," + (this._y1 = +y);
36 },
37 quadraticCurveTo: function (x1, y1, x, y) {
38 this._ += "Q" + +x1 + "," + +y1 + "," + (this._x1 = +x) + "," + (this._y1 = +y);
39 },
40 bezierCurveTo: function (x1, y1, x2, y2, x, y) {
41 this._ += "C" + +x1 + "," + +y1 + "," + +x2 + "," + +y2 + "," + (this._x1 = +x) + "," + (this._y1 = +y);
42 },
43 arcTo: function (x1, y1, x2, y2, r) {
44 x1 = +x1, y1 = +y1, x2 = +x2, y2 = +y2, r = +r;
45 var x0 = this._x1,
46 y0 = this._y1,
47 x21 = x2 - x1,
48 y21 = y2 - y1,
49 x01 = x0 - x1,
50 y01 = y0 - y1,
51 l01_2 = x01 * x01 + y01 * y01; // Is the radius negative? Error.
52
53 if (r < 0) throw new Error("negative radius: " + r); // Is this path empty? Move to (x1,y1).
54
55 if (this._x1 === null) {
56 this._ += "M" + (this._x1 = x1) + "," + (this._y1 = y1);
57 } // Or, is (x1,y1) coincident with (x0,y0)? Do nothing.
58 else if (!(l01_2 > epsilon)) ; // Or, are (x0,y0), (x1,y1) and (x2,y2) collinear?
59 // Equivalently, is (x1,y1) coincident with (x2,y2)?
60 // Or, is the radius zero? Line to (x1,y1).
61 else if (!(Math.abs(y01 * x21 - y21 * x01) > epsilon) || !r) {
62 this._ += "L" + (this._x1 = x1) + "," + (this._y1 = y1);
63 } // Otherwise, draw an arc!
64 else {
65 var x20 = x2 - x0,
66 y20 = y2 - y0,
67 l21_2 = x21 * x21 + y21 * y21,
68 l20_2 = x20 * x20 + y20 * y20,
69 l21 = Math.sqrt(l21_2),
70 l01 = Math.sqrt(l01_2),
71 l = r * Math.tan((pi - Math.acos((l21_2 + l01_2 - l20_2) / (2 * l21 * l01))) / 2),
72 t01 = l / l01,
73 t21 = l / l21; // If the start tangent is not coincident with (x0,y0), line to.
74
75 if (Math.abs(t01 - 1) > epsilon) {
76 this._ += "L" + (x1 + t01 * x01) + "," + (y1 + t01 * y01);
77 }
78
79 this._ += "A" + r + "," + r + ",0,0," + +(y01 * x20 > x01 * y20) + "," + (this._x1 = x1 + t21 * x21) + "," + (this._y1 = y1 + t21 * y21);
80 }
81 },
82 arc: function (x, y, r, a0, a1, ccw) {
83 x = +x, y = +y, r = +r, ccw = !!ccw;
84 var dx = r * Math.cos(a0),
85 dy = r * Math.sin(a0),
86 x0 = x + dx,
87 y0 = y + dy,
88 cw = 1 ^ ccw,
89 da = ccw ? a0 - a1 : a1 - a0; // Is the radius negative? Error.
90
91 if (r < 0) throw new Error("negative radius: " + r); // Is this path empty? Move to (x0,y0).
92
93 if (this._x1 === null) {
94 this._ += "M" + x0 + "," + y0;
95 } // Or, is (x0,y0) not coincident with the previous point? Line to (x0,y0).
96 else if (Math.abs(this._x1 - x0) > epsilon || Math.abs(this._y1 - y0) > epsilon) {
97 this._ += "L" + x0 + "," + y0;
98 } // Is this arc empty? We’re done.
99
100
101 if (!r) return; // Does the angle go the wrong way? Flip the direction.
102
103 if (da < 0) da = da % tau + tau; // Is this a complete circle? Draw two arcs to complete the circle.
104
105 if (da > tauEpsilon) {
106 this._ += "A" + r + "," + r + ",0,1," + cw + "," + (x - dx) + "," + (y - dy) + "A" + r + "," + r + ",0,1," + cw + "," + (this._x1 = x0) + "," + (this._y1 = y0);
107 } // Is this arc non-empty? Draw an arc!
108 else if (da > epsilon) {
109 this._ += "A" + r + "," + r + ",0," + +(da >= pi) + "," + cw + "," + (this._x1 = x + r * Math.cos(a1)) + "," + (this._y1 = y + r * Math.sin(a1));
110 }
111 },
112 rect: function (x, y, w, h) {
113 this._ += "M" + (this._x0 = this._x1 = +x) + "," + (this._y0 = this._y1 = +y) + "h" + +w + "v" + +h + "h" + -w + "Z";
114 },
115 toString: function () {
116 return this._;
117 }
118};
119var _default = path;
120exports.default = _default;
\No newline at end of file