perfect-freehand
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -1,1 +0,1 @@ | ||
var at=Object.defineProperty;var ht=t=>at(t,"__esModule",{value:!0});var dt=(t,e)=>{ht(t);for(var n in e)at(t,n,{get:e[n],enumerable:!0})};dt(exports,{default:()=>Pt,getStroke:()=>rt,getStrokeOutlinePoints:()=>et,getStrokePoints:()=>nt});function Y(t,e,n,E=g=>g){return t*E(.5-e*(.5-n))}function v(t,e){return[t[0]+e[0],t[1]+e[1]]}function x(t,e){return[t[0]-e[0],t[1]-e[1]]}function z(t,e){return[t[0]*e,t[1]*e]}function xt(t,e){return[t[0]/e,t[1]/e]}function N(t){return[t[1],-t[0]]}function lt(t,e){return t[0]*e[0]+t[1]*e[1]}function C(t,e){return t[0]===e[0]&&t[1]===e[1]}function kt(t){return Math.hypot(t[0],t[1])}function St(t){return t[0]*t[0]+t[1]*t[1]}function tt(t,e){return St(x(t,e))}function T(t){return xt(t,kt(t))}function K(t,e){return Math.hypot(t[1]-e[1],t[0]-e[0])}function ft(t,e){return z(v(t,e),.5)}function _(t,e,n){let E=Math.sin(n),g=Math.cos(n),i=t[0]-e[0],k=t[1]-e[1],l=i*g-k*E,j=i*E+k*g;return[l+e[0],j+e[1]]}function R(t,e,n){return v(t,z(x(e,t),n))}function b(t,e,n){return v(t,z(e,n))}var bt=.3,{min:F,PI:J}=Math;function et(t,e={}){let{size:n=16,smoothing:E=.5,thinning:g=.5,simulatePressure:i=!0,easing:k=r=>r,start:l={},end:j={},last:q=!1}=e,{streamline:S=.5}=e,{cap:M=!0,taper:P=0,easing:Q=r=>r*(2-r)}=l,{cap:Z=!0,taper:D=0,easing:gt=r=>--r*r*r+1}=j;if(S/=2,t.length===0)return[];let ot=t[t.length-1].runningLength,f=[],y=[],$=t.slice(0,10).reduce((r,d)=>{let c=d.pressure;if(i){let s=F(1,d.distance/n),a=F(1,1-s);c=F(1,r+(a-r)*(s*bt))}return(r+c)/2},t[0].pressure),O=Y(n,g,t[t.length-1].pressure,k),st,ut=t[0].vector,V=t[0].point,G=V,p=V,m=G,U=!0;for(let r=0;r<t.length-1;r++){let{pressure:d}=t[r],{point:c,vector:s,distance:a,runningLength:o}=t[r];if(r>0&&U&&o<n/2)continue;if(U&&(U=!1),g){if(i){let I=F(1,a/n),w=F(1,1-I);d=F(1,$+(w-$)*(I*bt))}O=Y(n,g,d,k)}else O=n/2;st===void 0&&(st=O);let u=o<P?Q(o/P):1,H=ot-o<D?gt((ot-o)/D):1;O=Math.max(.01,O*Math.min(u,H));let L=t[r+1].vector,A=lt(s,L);if(A<0){let I=z(N(ut),O);for(let w=0;w<1;w+=.2)m=_(v(c,I),c,J*-w),p=_(x(c,I),c,J*w),y.push(m),f.push(p);V=p,G=m;continue}let pt=z(N(R(L,s,A)),O);p=x(c,pt),m=v(c,pt);let mt=r<2||A<.25,ct=Math.pow(Math.max((o>n?n:n/2)*E,1),2);(mt||tt(V,p)>ct)&&(f.push(R(V,p,S)),V=p),(mt||tt(G,m)>ct)&&(y.push(R(G,m,S)),G=m),$=d,ut=s}let h=t[0],it=t[t.length-1],B=U||y.length<2||f.length<2;if(B&&(!(P||D)||q)){let r=0;for(let s=0;s<t.length;s++){let{pressure:a,runningLength:o}=t[s];if(o>n){r=Y(n,g,a,k);break}}let d=b(h.point,N(T(x(h.point,it.point))),-(r||O)),c=[];for(let s=0,a=.1;s<=1;s+=a)c.push(_(d,h.point,J*2*s));return c}let W=[],X=[];if(f.length>1&&y.length>1){m=y[1];for(let o=1;o<f.length;o++)if(!C(m,f[o])){p=f[o];break}if(M||P)if(!P&&!(D&&B)){if(!C(m,p)){let o=b(h.point,T(x(p,m)),-K(m,p)/2);for(let u=0,H=.1;u<=1;u+=H){let L=_(o,h.point,J*u);if(K(L,p)<1)break;W.push(L)}f.shift(),y.shift()}}else W.push(h.point,v(h.point,[.1,.1]));else if(!C(m,p)){let o=T(x(p,m)),u=K(m,p)/2;W.concat(b(h.point,o,u*.95),b(h.point,o,u),b(h.point,o,-u),b(h.point,o,-u*.95)),f.shift(),y.shift()}let r=f[f.length-1],d=y[y.length-1],c=ft(r,d),s=it.point,a=N(T(x(s,c)));if(Z||D)if(!D&&!(P&&B)){let o=b(s,a,O);for(let u=0,H=.1;u<=1;u+=H){let L=_(o,s,J*3*u);if(K(L,d)<1)break;X.push(L)}}else X.push(s);else{let o=R(c,s,.95),u=O*.95;X.concat(b(o,a,u),b(s,a,u),b(s,a,-u),b(o,a,-u))}}return f.concat(X,y.reverse(),W)}function nt(t,e={}){let{streamline:n=.5}=e,{simulatePressure:E=!0,last:g=!1}=e;if(t.length===0)return[];n=n/(E?3:2);let i=Array.isArray(t[0])?t:t.map(({x:S,y:M,pressure:P=.5})=>[S,M,P]);i.length===1&&i.push([...v(i[0],[1,1]),i[0][2]||.5]);let k=[],l={point:[i[0][0],i[0][1]],pressure:i[0][2]||.5,vector:[0,0],distance:0,runningLength:0};k.push(l);let j=i.length,q;for(let S=0;S<j;S++){q=i[S];let M=g&&S===j-1?q:R(l.point,q,1-n);if(C(l.point,M))continue;let P=T(x(l.point,M)),Q=K(M,l.point),Z=l.runningLength+Q;l={point:M,pressure:q[2]||.5,vector:P,distance:Q,runningLength:Z},k.push(l)}return k}function rt(t,e={}){return et(nt(t,e),e)}var Pt=rt; | ||
var mt=Object.defineProperty;var dt=t=>mt(t,"__esModule",{value:!0});var xt=(t,e)=>{dt(t);for(var n in e)mt(t,n,{get:e[n],enumerable:!0})};xt(exports,{default:()=>yt,getStroke:()=>nt,getStrokeOutlinePoints:()=>tt,getStrokePoints:()=>et});function X(t,e,n,O=k=>k){return t*O(.5-e*(.5-n))}function v(t,e){return[t[0]+e[0],t[1]+e[1]]}function d(t,e){return[t[0]-e[0],t[1]-e[1]]}function z(t,e){return[t[0]*e,t[1]*e]}function kt(t,e){return[t[0]/e,t[1]/e]}function I(t){return[t[1],-t[0]]}function at(t,e){return t[0]*e[0]+t[1]*e[1]}function j(t,e){return t[0]===e[0]&&t[1]===e[1]}function St(t){return Math.hypot(t[0],t[1])}function Pt(t){return t[0]*t[0]+t[1]*t[1]}function A(t,e){return Pt(d(t,e))}function R(t){return kt(t,St(t))}function q(t,e){return Math.hypot(t[1]-e[1],t[0]-e[0])}function lt(t,e){return z(v(t,e),.5)}function C(t,e,n){let O=Math.sin(n),k=Math.cos(n),u=t[0]-e[0],a=t[1]-e[1],b=u*k-a*O,g=u*O+a*k;return[b+e[0],g+e[1]]}function D(t,e,n){return v(t,z(d(e,t),n))}function x(t,e,n){return v(t,z(e,n))}var ft=.3,{min:_,PI:N}=Math;function tt(t,e={}){var ut;let{size:n=16,smoothing:O=.5,thinning:k=.5,simulatePressure:u=!0,easing:a=o=>o,start:b={},end:g={},last:E=!1}=e,{streamline:T=.5}=e,{cap:J=!0,taper:M=0,easing:bt=o=>o*(2-o)}=b,{cap:gt=!0,taper:K=0,easing:ht=o=>--o*o*o+1}=g;if(T/=2,t.length===0)return[];let rt=t[t.length-1].runningLength,h=[],P=[],Y=t.slice(0,10).reduce((o,S)=>{let l=S.pressure;if(u){let i=_(1,S.distance/n),p=_(1,1-i);l=_(1,o+(p-o)*(i*ft))}return(o+l)/2},t[0].pressure),y=X(n,k,t[t.length-1].pressure,a),ot,st=t[0].vector,V=t[0].point,F=V,c=V,m=F,Q=!0;for(let o=0;o<t.length-1;o++){let{pressure:S}=t[o],{point:l,vector:i,distance:p,runningLength:r}=t[o];if(o>0&&Q&&r<n/2)continue;if(Q&&(Q=!1),k){if(u){let H=_(1,p/n),w=_(1,1-H);S=_(1,Y+(w-Y)*(H*ft))}y=X(n,k,S,a)}else y=n/2;ot===void 0&&(ot=y);let s=r<M?bt(r/M):1,G=rt-r<K?ht((rt-r)/K):1;y=Math.max(.01,y*Math.min(s,G));let L=((ut=t[o+1])==null?void 0:ut.vector)||i,B=at(i,L);if(B<0){let H=z(I(st),y);for(let w=0;w<1;w+=.2)m=C(v(l,H),l,N*-w),c=C(d(l,H),l,N*w),P.push(m),h.push(c);V=c,F=m;continue}let it=z(I(D(L,i,B)),y);c=d(l,it),m=v(l,it);let pt=o<2||B<.25,ct=Math.pow(Math.max((r>n?n:n/2)*O,1),2);(pt||A(V,c)>ct)&&(h.push(D(V,c,T)),V=c),(pt||A(F,m)>ct)&&(P.push(D(F,m,T)),F=m),Y=S,st=i}let f=t[0],Z=t[t.length-1],$=Q||P.length<2||h.length<2;if($&&(!(M||K)||E)){let o=0,S=j(f.point,Z.point)?v(f.point,[1,1]):Z.point;for(let p=0;p<t.length;p++){let{pressure:r,runningLength:s}=t[p];if(s>n){o=X(n,k,r,a);break}}let l=x(f.point,I(R(d(f.point,S))),-(o||y)),i=[];for(let p=0,r=.1;p<=1;p+=r)i.push(C(l,f.point,N*2*p));return i}let U=[],W=[];if(h.length>1&&P.length>1){m=P[1];for(let r=1;r<h.length;r++)if(!j(m,h[r])){c=h[r];break}if(J||M)if(!M&&!(K&&$)){if(!j(m,c)){let r=x(f.point,R(d(c,m)),-q(m,c)/2);for(let s=0,G=.1;s<=1;s+=G){let L=C(r,f.point,N*s);if(q(L,c)<1)break;U.push(L)}h.shift(),P.shift()}}else U.push(f.point,v(f.point,[.1,.1]));else if(!j(m,c)){let r=R(d(c,m)),s=q(m,c)/2;U.concat(x(f.point,r,s*.95),x(f.point,r,s),x(f.point,r,-s),x(f.point,r,-s*.95)),h.shift(),P.shift()}let o=h[h.length-1],S=P[P.length-1],l=lt(o,S),i=Z.point,p=I(R(d(i,l)));if(gt||K)if(!K&&!(M&&$)){let r=x(i,p,y);for(let s=0,G=.1;s<=1;s+=G){let L=C(r,i,N*3*s);if(q(L,S)<1)break;W.push(L)}}else W.push(i);else{let r=D(l,i,.95),s=y*.95;W.concat(x(r,p,s),x(i,p,s),x(i,p,-s),x(r,p,-s))}}return h.concat(W,P.reverse(),U)}function et(t,e={}){let{streamline:n=.5}=e,{simulatePressure:O=!0,last:k=!1}=e;if(t.length===0)return[];n=n/(O?3:2);let u=Array.isArray(t[0])?t:t.map(({x:g,y:E,pressure:T=.5})=>[g,E,T]);u.length===1&&u.push([...v(u[0],[1,1]),u[0][2]||.5]);let a=[],b={point:[u[0][0],u[0][1]],pressure:u[0][2]||.25,vector:[1,1],distance:0,runningLength:0};a.push(b);for(let g=2;g<u.length;g++){let E=k&&g===u.length-1?u[g]:D(b.point,u[g],1-n);if(j(b.point,E))continue;let T=R(d(b.point,E)),J=q(E,b.point),M=b.runningLength+J;b={point:E,pressure:u[g][2]||.5,vector:T,distance:J,runningLength:M},a.push(b)}return a.length>1&&(a[0].vector=R(d(a[0].point,a[1].point))),a}function nt(t,e={}){return tt(et(t,e),e)}var yt=nt; |
@@ -1,1 +0,1 @@ | ||
function Y(t,e,o,E=g=>g){return t*E(.5-e*(.5-o))}function v(t,e){return[t[0]+e[0],t[1]+e[1]]}function x(t,e){return[t[0]-e[0],t[1]-e[1]]}function z(t,e){return[t[0]*e,t[1]*e]}function gt(t,e){return[t[0]/e,t[1]/e]}function N(t){return[t[1],-t[0]]}function pt(t,e){return t[0]*e[0]+t[1]*e[1]}function C(t,e){return t[0]===e[0]&&t[1]===e[1]}function ht(t){return Math.hypot(t[0],t[1])}function dt(t){return t[0]*t[0]+t[1]*t[1]}function tt(t,e){return dt(x(t,e))}function T(t){return gt(t,ht(t))}function K(t,e){return Math.hypot(t[1]-e[1],t[0]-e[0])}function mt(t,e){return z(v(t,e),.5)}function _(t,e,o){let E=Math.sin(o),g=Math.cos(o),i=t[0]-e[0],k=t[1]-e[1],l=i*g-k*E,j=i*E+k*g;return[l+e[0],j+e[1]]}function R(t,e,o){return v(t,z(x(e,t),o))}function b(t,e,o){return v(t,z(e,o))}var ct=.3,{min:F,PI:J}=Math;function at(t,e={}){let{size:o=16,smoothing:E=.5,thinning:g=.5,simulatePressure:i=!0,easing:k=n=>n,start:l={},end:j={},last:q=!1}=e,{streamline:S=.5}=e,{cap:M=!0,taper:P=0,easing:Q=n=>n*(2-n)}=l,{cap:Z=!0,taper:D=0,easing:bt=n=>--n*n*n+1}=j;if(S/=2,t.length===0)return[];let et=t[t.length-1].runningLength,f=[],y=[],$=t.slice(0,10).reduce((n,d)=>{let c=d.pressure;if(i){let s=F(1,d.distance/o),a=F(1,1-s);c=F(1,n+(a-n)*(s*ct))}return(n+c)/2},t[0].pressure),O=Y(o,g,t[t.length-1].pressure,k),nt,rt=t[0].vector,V=t[0].point,G=V,p=V,m=G,U=!0;for(let n=0;n<t.length-1;n++){let{pressure:d}=t[n],{point:c,vector:s,distance:a,runningLength:r}=t[n];if(n>0&&U&&r<o/2)continue;if(U&&(U=!1),g){if(i){let I=F(1,a/o),w=F(1,1-I);d=F(1,$+(w-$)*(I*ct))}O=Y(o,g,d,k)}else O=o/2;nt===void 0&&(nt=O);let u=r<P?Q(r/P):1,H=et-r<D?bt((et-r)/D):1;O=Math.max(.01,O*Math.min(u,H));let L=t[n+1].vector,A=pt(s,L);if(A<0){let I=z(N(rt),O);for(let w=0;w<1;w+=.2)m=_(v(c,I),c,J*-w),p=_(x(c,I),c,J*w),y.push(m),f.push(p);V=p,G=m;continue}let st=z(N(R(L,s,A)),O);p=x(c,st),m=v(c,st);let ut=n<2||A<.25,it=Math.pow(Math.max((r>o?o:o/2)*E,1),2);(ut||tt(V,p)>it)&&(f.push(R(V,p,S)),V=p),(ut||tt(G,m)>it)&&(y.push(R(G,m,S)),G=m),$=d,rt=s}let h=t[0],ot=t[t.length-1],B=U||y.length<2||f.length<2;if(B&&(!(P||D)||q)){let n=0;for(let s=0;s<t.length;s++){let{pressure:a,runningLength:r}=t[s];if(r>o){n=Y(o,g,a,k);break}}let d=b(h.point,N(T(x(h.point,ot.point))),-(n||O)),c=[];for(let s=0,a=.1;s<=1;s+=a)c.push(_(d,h.point,J*2*s));return c}let W=[],X=[];if(f.length>1&&y.length>1){m=y[1];for(let r=1;r<f.length;r++)if(!C(m,f[r])){p=f[r];break}if(M||P)if(!P&&!(D&&B)){if(!C(m,p)){let r=b(h.point,T(x(p,m)),-K(m,p)/2);for(let u=0,H=.1;u<=1;u+=H){let L=_(r,h.point,J*u);if(K(L,p)<1)break;W.push(L)}f.shift(),y.shift()}}else W.push(h.point,v(h.point,[.1,.1]));else if(!C(m,p)){let r=T(x(p,m)),u=K(m,p)/2;W.concat(b(h.point,r,u*.95),b(h.point,r,u),b(h.point,r,-u),b(h.point,r,-u*.95)),f.shift(),y.shift()}let n=f[f.length-1],d=y[y.length-1],c=mt(n,d),s=ot.point,a=N(T(x(s,c)));if(Z||D)if(!D&&!(P&&B)){let r=b(s,a,O);for(let u=0,H=.1;u<=1;u+=H){let L=_(r,s,J*3*u);if(K(L,d)<1)break;X.push(L)}}else X.push(s);else{let r=R(c,s,.95),u=O*.95;X.concat(b(r,a,u),b(s,a,u),b(s,a,-u),b(r,a,-u))}}return f.concat(X,y.reverse(),W)}function lt(t,e={}){let{streamline:o=.5}=e,{simulatePressure:E=!0,last:g=!1}=e;if(t.length===0)return[];o=o/(E?3:2);let i=Array.isArray(t[0])?t:t.map(({x:S,y:M,pressure:P=.5})=>[S,M,P]);i.length===1&&i.push([...v(i[0],[1,1]),i[0][2]||.5]);let k=[],l={point:[i[0][0],i[0][1]],pressure:i[0][2]||.5,vector:[0,0],distance:0,runningLength:0};k.push(l);let j=i.length,q;for(let S=0;S<j;S++){q=i[S];let M=g&&S===j-1?q:R(l.point,q,1-o);if(C(l.point,M))continue;let P=T(x(l.point,M)),Q=K(M,l.point),Z=l.runningLength+Q;l={point:M,pressure:q[2]||.5,vector:P,distance:Q,runningLength:Z},k.push(l)}return k}function ft(t,e={}){return at(lt(t,e),e)}var Rt=ft;export{Rt as default,ft as getStroke,at as getStrokeOutlinePoints,lt as getStrokePoints}; | ||
function X(t,e,o,O=k=>k){return t*O(.5-e*(.5-o))}function v(t,e){return[t[0]+e[0],t[1]+e[1]]}function d(t,e){return[t[0]-e[0],t[1]-e[1]]}function z(t,e){return[t[0]*e,t[1]*e]}function ht(t,e){return[t[0]/e,t[1]/e]}function I(t){return[t[1],-t[0]]}function it(t,e){return t[0]*e[0]+t[1]*e[1]}function j(t,e){return t[0]===e[0]&&t[1]===e[1]}function dt(t){return Math.hypot(t[0],t[1])}function xt(t){return t[0]*t[0]+t[1]*t[1]}function A(t,e){return xt(d(t,e))}function R(t){return ht(t,dt(t))}function q(t,e){return Math.hypot(t[1]-e[1],t[0]-e[0])}function pt(t,e){return z(v(t,e),.5)}function C(t,e,o){let O=Math.sin(o),k=Math.cos(o),u=t[0]-e[0],a=t[1]-e[1],b=u*k-a*O,g=u*O+a*k;return[b+e[0],g+e[1]]}function D(t,e,o){return v(t,z(d(e,t),o))}function x(t,e,o){return v(t,z(e,o))}var ct=.3,{min:_,PI:N}=Math;function mt(t,e={}){var rt;let{size:o=16,smoothing:O=.5,thinning:k=.5,simulatePressure:u=!0,easing:a=r=>r,start:b={},end:g={},last:E=!1}=e,{streamline:T=.5}=e,{cap:J=!0,taper:M=0,easing:ft=r=>r*(2-r)}=b,{cap:bt=!0,taper:K=0,easing:gt=r=>--r*r*r+1}=g;if(T/=2,t.length===0)return[];let tt=t[t.length-1].runningLength,h=[],P=[],Y=t.slice(0,10).reduce((r,S)=>{let l=S.pressure;if(u){let i=_(1,S.distance/o),p=_(1,1-i);l=_(1,r+(p-r)*(i*ct))}return(r+l)/2},t[0].pressure),y=X(o,k,t[t.length-1].pressure,a),et,nt=t[0].vector,V=t[0].point,F=V,c=V,m=F,Q=!0;for(let r=0;r<t.length-1;r++){let{pressure:S}=t[r],{point:l,vector:i,distance:p,runningLength:n}=t[r];if(r>0&&Q&&n<o/2)continue;if(Q&&(Q=!1),k){if(u){let H=_(1,p/o),w=_(1,1-H);S=_(1,Y+(w-Y)*(H*ct))}y=X(o,k,S,a)}else y=o/2;et===void 0&&(et=y);let s=n<M?ft(n/M):1,G=tt-n<K?gt((tt-n)/K):1;y=Math.max(.01,y*Math.min(s,G));let L=((rt=t[r+1])==null?void 0:rt.vector)||i,B=it(i,L);if(B<0){let H=z(I(nt),y);for(let w=0;w<1;w+=.2)m=C(v(l,H),l,N*-w),c=C(d(l,H),l,N*w),P.push(m),h.push(c);V=c,F=m;continue}let ot=z(I(D(L,i,B)),y);c=d(l,ot),m=v(l,ot);let st=r<2||B<.25,ut=Math.pow(Math.max((n>o?o:o/2)*O,1),2);(st||A(V,c)>ut)&&(h.push(D(V,c,T)),V=c),(st||A(F,m)>ut)&&(P.push(D(F,m,T)),F=m),Y=S,nt=i}let f=t[0],Z=t[t.length-1],$=Q||P.length<2||h.length<2;if($&&(!(M||K)||E)){let r=0,S=j(f.point,Z.point)?v(f.point,[1,1]):Z.point;for(let p=0;p<t.length;p++){let{pressure:n,runningLength:s}=t[p];if(s>o){r=X(o,k,n,a);break}}let l=x(f.point,I(R(d(f.point,S))),-(r||y)),i=[];for(let p=0,n=.1;p<=1;p+=n)i.push(C(l,f.point,N*2*p));return i}let U=[],W=[];if(h.length>1&&P.length>1){m=P[1];for(let n=1;n<h.length;n++)if(!j(m,h[n])){c=h[n];break}if(J||M)if(!M&&!(K&&$)){if(!j(m,c)){let n=x(f.point,R(d(c,m)),-q(m,c)/2);for(let s=0,G=.1;s<=1;s+=G){let L=C(n,f.point,N*s);if(q(L,c)<1)break;U.push(L)}h.shift(),P.shift()}}else U.push(f.point,v(f.point,[.1,.1]));else if(!j(m,c)){let n=R(d(c,m)),s=q(m,c)/2;U.concat(x(f.point,n,s*.95),x(f.point,n,s),x(f.point,n,-s),x(f.point,n,-s*.95)),h.shift(),P.shift()}let r=h[h.length-1],S=P[P.length-1],l=pt(r,S),i=Z.point,p=I(R(d(i,l)));if(bt||K)if(!K&&!(M&&$)){let n=x(i,p,y);for(let s=0,G=.1;s<=1;s+=G){let L=C(n,i,N*3*s);if(q(L,S)<1)break;W.push(L)}}else W.push(i);else{let n=D(l,i,.95),s=y*.95;W.concat(x(n,p,s),x(i,p,s),x(i,p,-s),x(n,p,-s))}}return h.concat(W,P.reverse(),U)}function at(t,e={}){let{streamline:o=.5}=e,{simulatePressure:O=!0,last:k=!1}=e;if(t.length===0)return[];o=o/(O?3:2);let u=Array.isArray(t[0])?t:t.map(({x:g,y:E,pressure:T=.5})=>[g,E,T]);u.length===1&&u.push([...v(u[0],[1,1]),u[0][2]||.5]);let a=[],b={point:[u[0][0],u[0][1]],pressure:u[0][2]||.25,vector:[1,1],distance:0,runningLength:0};a.push(b);for(let g=2;g<u.length;g++){let E=k&&g===u.length-1?u[g]:D(b.point,u[g],1-o);if(j(b.point,E))continue;let T=R(d(b.point,E)),J=q(E,b.point),M=b.runningLength+J;b={point:E,pressure:u[g][2]||.5,vector:T,distance:J,runningLength:M},a.push(b)}return a.length>1&&(a[0].vector=R(d(a[0].point,a[1].point))),a}function lt(t,e={}){return mt(at(t,e),e)}var qt=lt;export{qt as default,lt as getStroke,mt as getStrokeOutlinePoints,at as getStrokePoints}; |
@@ -16,8 +16,7 @@ import type { StrokeOptions } from './types'; | ||
*/ | ||
export declare function getStroke<T extends number[]>(points: T[], options?: StrokeOptions): number[][]; | ||
export declare function getStroke<K extends { | ||
export declare function getStroke(points: (number[] | { | ||
x: number; | ||
y: number; | ||
pressure?: number; | ||
}>(points: K[], options?: StrokeOptions): number[][]; | ||
})[], options?: StrokeOptions): number[][]; | ||
//# sourceMappingURL=getStroke.d.ts.map |
{ | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"name": "perfect-freehand", | ||
@@ -60,3 +60,3 @@ "private": false, | ||
}, | ||
"gitHead": "1f9b57500bf9750f0fe04d8ab97460c2b65d96da" | ||
"gitHead": "629d7676258936730d13ecb767d00f79a9cd0e68" | ||
} |
@@ -19,14 +19,8 @@ import type { StrokeOptions } from './types' | ||
*/ | ||
export function getStroke<T extends number[]>( | ||
points: T[], | ||
options?: StrokeOptions | ||
): number[][] | ||
export function getStroke< | ||
K extends { x: number; y: number; pressure?: number } | ||
>(points: K[], options?: StrokeOptions): number[][] | ||
export function getStroke< | ||
T extends number[], | ||
K extends { x: number; y: number; pressure?: number } | ||
>(points: (T | K)[], options: StrokeOptions = {} as StrokeOptions): number[][] { | ||
export function getStroke( | ||
points: (number[] | { x: number; y: number; pressure?: number })[], | ||
options: StrokeOptions = {} as StrokeOptions | ||
): number[][] { | ||
return getStrokeOutlinePoints(getStrokePoints(points, options), options) | ||
} |
@@ -195,3 +195,3 @@ import { getStrokeRadius } from './getStrokeRadius' | ||
const nextVector = points[i + 1].vector | ||
const nextVector = points[i + 1]?.vector || vector | ||
@@ -280,2 +280,6 @@ const nextDpr = dpr(vector, nextVector) | ||
const lastPt = isEqual(firstPoint.point, lastPoint.point) | ||
? add(firstPoint.point, [1, 1]) | ||
: lastPoint.point | ||
for (let i = 0; i < points.length; i++) { | ||
@@ -291,3 +295,3 @@ const { pressure, runningLength } = points[i] | ||
firstPoint.point, | ||
per(uni(sub(firstPoint.point, lastPoint.point))), | ||
per(uni(sub(firstPoint.point, lastPt))), | ||
-(ir || radius) | ||
@@ -294,0 +298,0 @@ ) |
@@ -38,4 +38,4 @@ import { add, dist, isEqual, lrp, sub, uni } from './vec' | ||
point: [pts[0][0], pts[0][1]], | ||
pressure: pts[0][2] || 0.5, | ||
vector: [0, 0], | ||
pressure: pts[0][2] || 0.25, | ||
vector: [1, 1], | ||
distance: 0, | ||
@@ -47,10 +47,6 @@ runningLength: 0, | ||
// Iterate through all of the points. | ||
const len = pts.length | ||
// Iterate through all of the points, skipping the second. | ||
// This fixes a "ball" shape at the beginning of the line. | ||
let curr: number[] | ||
for (let i = 0; i < len; i++) { | ||
curr = pts[i] | ||
for (let i = 2; i < pts.length; i++) { | ||
// If we're at the last point, then add the actual input point. | ||
@@ -63,3 +59,5 @@ // Otherwise, using the streamline option, interpolate a new point | ||
const point = | ||
isComplete && i === len - 1 ? curr : lrp(prev.point, curr, 1 - streamline) | ||
isComplete && i === pts.length - 1 | ||
? pts[i] | ||
: lrp(prev.point, pts[i], 1 - streamline) | ||
@@ -81,3 +79,3 @@ // If the new point is the same as the previous point, skip ahead. | ||
point, | ||
pressure: curr[2] || 0.5, | ||
pressure: pts[i][2] || 0.5, | ||
vector, | ||
@@ -91,3 +89,9 @@ distance, | ||
if (strokePoints.length > 1) { | ||
strokePoints[0].vector = uni( | ||
sub(strokePoints[0].point, strokePoints[1].point) | ||
) | ||
} | ||
return strokePoints | ||
} |
import { getStroke } from '../getStroke' | ||
import inputs from './inputs.json' | ||
const { onePoint, twoPoints, manyPoints, withDuplicates } = inputs | ||
const { onePoint, twoPoints, twoEqualPoints, manyPoints, withDuplicates } = | ||
inputs | ||
describe('getStroke', () => { | ||
it('runs on a line with no points', () => { | ||
for (const [key, value] of Object.entries(inputs)) { | ||
it(`runs ${key} without generating NaN values`, () => { | ||
expect( | ||
getStroke(value).find((t) => JSON.stringify(t).includes('null')) | ||
).toBeUndefined() | ||
}) | ||
} | ||
it('gets stroke from a line with no points', () => { | ||
expect(getStroke([])).toMatchSnapshot('get-stroke-no-points') | ||
}) | ||
it('runs on a line with a single point', () => { | ||
it('gets stroke from a line with a single point', () => { | ||
expect(getStroke(onePoint)).toMatchSnapshot('get-stroke-one-point') | ||
}) | ||
it('runs on a line with two points', () => { | ||
expect(getStroke(twoPoints)).toMatchSnapshot('get-stroke-one-point') | ||
it('gets stroke from a line with two points', () => { | ||
expect(getStroke(twoPoints)).toMatchSnapshot('get-stroke-two-points') | ||
}) | ||
it('runs on a line with a many points', () => { | ||
it('gets stroke from a line with two equal points', () => { | ||
expect(getStroke(twoEqualPoints)).toMatchSnapshot( | ||
'get-stroke-two-equal-points' | ||
) | ||
}) | ||
it('gets stroke from a line with a many points', () => { | ||
expect(getStroke(manyPoints)).toMatchSnapshot('get-stroke-many-points') | ||
}) | ||
it('runs on a line with withDuplicates', () => { | ||
expect(getStroke(withDuplicates)).toMatchSnapshot('get-stroke-one-point') | ||
it('gets stroke from a line with with duplicates', () => { | ||
expect(getStroke(withDuplicates)).toMatchSnapshot( | ||
'get-stroke-with-duplicates' | ||
) | ||
}) | ||
@@ -26,0 +43,0 @@ |
@@ -5,6 +5,17 @@ import { getStrokePoints } from '../getStrokePoints' | ||
const { onePoint, twoPoints, manyPoints, withDuplicates } = inputs | ||
const { onePoint, twoPoints, twoEqualPoints, manyPoints, withDuplicates } = | ||
inputs | ||
describe('getStrokeOutlinePoints', () => { | ||
it('runs on a line with a single point', () => { | ||
for (const [key, value] of Object.entries(inputs)) { | ||
it(`runs ${key} without generating NaN values`, () => { | ||
expect( | ||
getStrokeOutlinePoints(getStrokePoints(value)).find((t) => | ||
JSON.stringify(t).includes('null') | ||
) | ||
).toBeUndefined() | ||
}) | ||
} | ||
it('gets stroke outline points with a single point', () => { | ||
expect(getStrokeOutlinePoints(getStrokePoints(onePoint))).toMatchSnapshot( | ||
@@ -15,9 +26,21 @@ 'get-stroke-outline-points-one-point' | ||
it('runs on a line with two points', () => { | ||
it('gets stroke outline points with two points', () => { | ||
expect(getStrokeOutlinePoints(getStrokePoints(twoPoints))).toMatchSnapshot( | ||
'get-stroke-outline-points-one-point' | ||
'get-stroke-outline-points-two-points' | ||
) | ||
}) | ||
it('runs on a line with a many points', () => { | ||
it('gets stroke outline points with two equal points', () => { | ||
expect( | ||
getStrokeOutlinePoints(getStrokePoints(twoEqualPoints)).find((t) => | ||
JSON.stringify(t).includes('null') | ||
) | ||
).toBeUndefined() | ||
expect( | ||
getStrokeOutlinePoints(getStrokePoints(twoEqualPoints)) | ||
).toMatchSnapshot('get-stroke-outline-points-two-equal-points') | ||
}) | ||
it('gets stroke outline points on a line with a many points', () => { | ||
expect(getStrokeOutlinePoints(getStrokePoints(manyPoints))).toMatchSnapshot( | ||
@@ -28,7 +51,7 @@ 'get-stroke-outline-points-many-points' | ||
it('runs on a line with withDuplicates', () => { | ||
it('gets stroke outline points with duplicates', () => { | ||
expect( | ||
getStrokeOutlinePoints(getStrokePoints(withDuplicates)) | ||
).toMatchSnapshot('get-stroke-outline-points-one-point') | ||
).toMatchSnapshot('get-stroke-outline-points-duplicates') | ||
}) | ||
}) |
@@ -18,2 +18,12 @@ { | ||
], | ||
"twoEqualPoints": [ | ||
[ | ||
1, | ||
1 | ||
], | ||
[ | ||
1, | ||
1 | ||
] | ||
], | ||
"numberPairs": [ | ||
@@ -20,0 +30,0 @@ [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
304289
40
2838