UNPKG

unit-path

Version:

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

2 lines (1 loc) 2.77 kB
"use strict";var D=(r,e,t)=>{if(!e.has(r))throw TypeError("Cannot "+t)};var E=(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)},a=(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 k{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.");a(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.");a(this,m,e)}setPath(e,...t){switch(e){case"LINE":a(this,p,R(this,b,O).call(this,t[0],t[1]));break;case"TWO_ORDER_BEZIER":a(this,p,R(this,M,_).call(this,t[0],t[1],t[2]));break;case"THREE_ORDER_BEZIER":a(this,p,R(this,T,B).call(this,t[0],t[1],t[2],t[3]));break;case"ARC":a(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(!E(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}=E(this,p).call(this,e);return E(this,f)>-1&&(t=Number(t.toFixed(E(this,f))),o=Number(o.toFixed(E(this,f)))),{x:t,y:o}}getPoints(e){if(e||(e=E(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:u,y:n}=o;return s=>({x:Math.pow(1-s,2)*h+2*s*(1-s)*w+Math.pow(s,2)*u,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:u}=t,{x:n,y:s}=o,{x:P,y:Z}=h;return c=>({x:i*Math.pow(1-c,3)+3*y*c*Math.pow(1-c,2)+3*n*Math.pow(c,2)*(1-c)+P*Math.pow(c,3),y:w*Math.pow(1-c,3)+3*u*c*Math.pow(1-c,2)+3*s*Math.pow(c,2)*(1-c)+Z*Math.pow(c,3)})},I=new WeakSet,N=function(e,t,o,h,i,w=!1){const u=2*Math.PI;if(w&&([h,i]=[i,h]),i-h>=u)i=h+u;else if(h!==i)if((h-i)%u===0)i=h;else for(h=h%u;i>h+u;)i-=u;const n=h>i?u-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}}};module.exports=k;