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