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