UNPKG

unit-path

Version:

Get points from paths that like straight lines, quadratic Bessel curves, cubic Bessel curves, circles, arcs

114 lines (113 loc) 3.89 kB
var D = (r, e, t) => { if (!e.has(r)) throw TypeError("Cannot " + t); }; var a = (r, e, t) => (D(r, e, "read from private field"), t ? t.call(r) : e.get(r)), x = (r, e, t) => { if (e.has(r)) throw TypeError("Cannot add the same private member more than once"); e instanceof WeakSet ? e.add(r) : e.set(r, t); }, E = (r, e, t, o) => (D(r, e, "write to private field"), o ? o.call(r, t) : e.set(r, t), t); var R = (r, e, t) => (D(r, e, "access private method"), t); var p, f, m, b, O, M, _, T, B, I, N; class v { constructor(e = {}) { x(this, b); x(this, M); x(this, T); x(this, I); x(this, p, void 0); x(this, f, -1); x(this, m, 50); const { decimalPlaces: t = -1, defaultQuantity: o = 50 } = e; this.setDecimalPlaces(t), this.setDefaultQuantity(o); } setDecimalPlaces(e = -1) { if (typeof e != "number" || e % 1 !== 0 || e < -1) throw new TypeError("The argument 'decimalPlaces' must be a positive integer or -1."); E(this, f, e); } setDefaultQuantity(e) { if (typeof e != "number" || e % 1 !== 0 || e <= 0) throw new TypeError("The argument 'defaultQuantity' must be a positive integer."); E(this, m, e); } setPath(e, ...t) { switch (e) { case "LINE": E(this, p, R(this, b, O).call(this, t[0], t[1])); break; case "TWO_ORDER_BEZIER": E(this, p, R(this, M, _).call(this, t[0], t[1], t[2])); break; case "THREE_ORDER_BEZIER": E(this, p, R(this, T, B).call(this, t[0], t[1], t[2], t[3])); break; case "ARC": E(this, p, R(this, I, N).call(this, t[0], t[1], t[2], t[3], t[4], t[5])); break; default: throw new TypeError(`The argument 'type' expact 'LINE', 'TWO_ORDER_BEZIER', 'THREE_ORDER_BEZIER', or 'ARC', but got '${e}'`); } return this; } getPoint(e = 0) { if (!a(this, p)) throw new Error("Please set path first."); if (e > 1 || e < 0) throw new RangeError("The argument 't' must be between 0 and 1."); let { x: t, y: o } = a(this, p).call(this, e); return a(this, f) > -1 && (t = Number(t.toFixed(a(this, f))), o = Number(o.toFixed(a(this, f)))), { x: t, y: o }; } getPoints(e) { if (e || (e = a(this, m)), typeof e != "number" || e % 1 !== 0 || e <= 0) throw new TypeError("The argument 'quantity' must be a positive integer."); const t = []; for (let o = 0; o < e; o++) t.push(this.getPoint(o / (e - 1))); return t; } } p = new WeakMap(), f = new WeakMap(), m = new WeakMap(), b = new WeakSet(), O = function(e, t) { const { x: o, y: h } = e, { x: i, y: w } = t; return (y) => ({ x: o + (i - o) * y, y: h + (w - h) * y }); }, M = new WeakSet(), _ = function(e, t, o) { const { x: h, y: i } = e, { x: w, y } = t, { x: c, y: n } = o; return (s) => ({ x: Math.pow(1 - s, 2) * h + 2 * s * (1 - s) * w + Math.pow(s, 2) * c, y: Math.pow(1 - s, 2) * i + 2 * s * (1 - s) * y + Math.pow(s, 2) * n }); }, T = new WeakSet(), B = function(e, t, o, h) { const { x: i, y: w } = e, { x: y, y: c } = t, { x: n, y: s } = o, { x: P, y: Z } = h; return (u) => ({ x: i * Math.pow(1 - u, 3) + 3 * y * u * Math.pow(1 - u, 2) + 3 * n * Math.pow(u, 2) * (1 - u) + P * Math.pow(u, 3), y: w * Math.pow(1 - u, 3) + 3 * c * u * Math.pow(1 - u, 2) + 3 * s * Math.pow(u, 2) * (1 - u) + Z * Math.pow(u, 3) }); }, I = new WeakSet(), N = function(e, t, o, h, i, w = !1) { const c = 2 * Math.PI; if (w && ([h, i] = [i, h]), i - h >= c) i = h + c; else if (h !== i) if ((h - i) % c === 0) i = h; else for (h = h % c; i > h + c; ) i -= c; const n = h > i ? c - h + i : i - h; return (s) => { w && (s = 1 - s); const P = n * s + h; return { x: e + Math.cos(P) * o, y: t + Math.sin(P) * o }; }; }; export { v as default };