perfect-freehand
Advanced tools
Comparing version 0.4.5 to 0.4.6
@@ -13,3 +13,3 @@ import { StrokeOptions, StrokePoint } from './types'; | ||
pressure?: number; | ||
}>(points: (T | K)[], streamline?: number, size?: number): StrokePoint[]; | ||
}>(points: (T | K)[], options: StrokeOptions): StrokePoint[]; | ||
/** | ||
@@ -16,0 +16,0 @@ * ## getStrokeOutlinePoints |
@@ -148,11 +148,2 @@ 'use strict'; | ||
/** | ||
* Mean between two vectors or mid vector between two vectors | ||
* @param A | ||
* @param B | ||
*/ | ||
function med(A, B) { | ||
return mul(add(A, B), 0.5); | ||
} | ||
/** | ||
* Rotate a vector around another vector by r (radians) | ||
@@ -194,9 +185,13 @@ * @param A vector | ||
function getStrokePoints(points, streamline, size) { | ||
if (streamline === void 0) { | ||
streamline = 0.5; | ||
} | ||
function getStrokePoints(points, options) { | ||
var _options$simulatePres = options.simulatePressure, | ||
simulatePressure = _options$simulatePres === void 0 ? true : _options$simulatePres, | ||
_options$streamline = options.streamline, | ||
streamline = _options$streamline === void 0 ? 0.5 : _options$streamline, | ||
_options$size = options.size, | ||
size = _options$size === void 0 ? 8 : _options$size; | ||
streamline /= 2; | ||
if (size === void 0) { | ||
size = 8; | ||
if (!simulatePressure) { | ||
streamline /= 2; | ||
} | ||
@@ -231,24 +226,2 @@ | ||
/* | ||
Align vectors at the start of the line | ||
Find the first stroke point past the size and then set all preceding points' | ||
vectors to match this point's vector. This aligns the start cap and reduces | ||
noise at the start of the line. | ||
*/ | ||
for (var _i = 0; _i < len; _i++) { | ||
var _strokePoints$_i = strokePoints[_i], | ||
_runningLength = _strokePoints$_i.runningLength, | ||
_vector = _strokePoints$_i.vector; | ||
if (_runningLength > size || _i === len - 1) { | ||
for (var j = 0; j < _i; j++) { | ||
strokePoints[j].vector = _vector; | ||
} | ||
break; | ||
} | ||
} | ||
/* | ||
Align vectors at the end of the line | ||
@@ -265,10 +238,10 @@ Starting from the last point, work back until we've traveled more than | ||
for (var _i2 = len - 1; _i2 > 1; _i2--) { | ||
var _strokePoints$_i2 = strokePoints[_i2], | ||
_runningLength2 = _strokePoints$_i2.runningLength, | ||
_vector2 = _strokePoints$_i2.vector; | ||
for (var _i = len - 2; _i > 1; _i--) { | ||
var _strokePoints$_i = strokePoints[_i], | ||
_runningLength = _strokePoints$_i.runningLength, | ||
_vector = _strokePoints$_i.vector; | ||
if (totalLength - _runningLength2 > size / 2) { | ||
for (var _j = _i2; _j < len; _j++) { | ||
strokePoints[_j].vector = _vector2; | ||
if (totalLength - _runningLength > size / 2 || dpr(strokePoints[_i - 1].vector, strokePoints[_i].vector) < 0.8) { | ||
for (var j = _i; j < len; j++) { | ||
strokePoints[j].vector = _vector; | ||
} | ||
@@ -303,4 +276,4 @@ | ||
var _options = options, | ||
_options$size = _options.size, | ||
size = _options$size === void 0 ? 8 : _options$size, | ||
_options$size2 = _options.size, | ||
size = _options$size2 === void 0 ? 8 : _options$size2, | ||
_options$thinning = _options.thinning, | ||
@@ -310,4 +283,4 @@ thinning = _options$thinning === void 0 ? 0.5 : _options$thinning, | ||
smoothing = _options$smoothing === void 0 ? 0.5 : _options$smoothing, | ||
_options$simulatePres = _options.simulatePressure, | ||
simulatePressure = _options$simulatePres === void 0 ? true : _options$simulatePres, | ||
_options$simulatePres2 = _options.simulatePressure, | ||
simulatePressure = _options$simulatePres2 === void 0 ? true : _options$simulatePres2, | ||
_options$easing = _options.easing, | ||
@@ -323,2 +296,6 @@ easing = _options$easing === void 0 ? function (t) { | ||
isComplete = _options$last === void 0 ? false : _options$last; | ||
var _options2 = options, | ||
_options2$streamline = _options2.streamline, | ||
streamline = _options2$streamline === void 0 ? 0.5 : _options2$streamline; | ||
streamline /= 2; | ||
var _start$taper = start.taper, | ||
@@ -441,14 +418,12 @@ taperStart = _start$taper === void 0 ? 0 : _start$taper, | ||
tr = add(point, offset); | ||
var tlu = uni(vec(tr, pr)); | ||
var tru = uni(vec(tl, pl)); | ||
var alwaysAdd = i === 1 || dpr$1 < 0.25; | ||
var minDistance = (runningLength > size ? size : size / 2) * smoothing; | ||
if (alwaysAdd || dist(pl, tl) > minDistance && dpr(tlu, vector) > 0) { | ||
leftPts.push(med(pl, tl)); | ||
if (alwaysAdd || dist(pl, tl) > minDistance) { | ||
leftPts.push(lrp(pl, tl, streamline)); | ||
pl = tl; | ||
} | ||
if (alwaysAdd || dist(pr, tr) > minDistance && dpr(tru, vector) > 0) { | ||
rightPts.push(med(pr, tr)); | ||
if (alwaysAdd || dist(pr, tr) > minDistance) { | ||
rightPts.push(lrp(pr, tr, streamline)); | ||
pr = tr; | ||
@@ -485,8 +460,8 @@ } // Set variables for next iteration | ||
for (var _i3 = 0; _i3 < len; _i3++) { | ||
var _points$_i = points[_i3], | ||
for (var _i2 = 0; _i2 < len; _i2++) { | ||
var _points$_i = points[_i2], | ||
_pressure = _points$_i.pressure, | ||
_runningLength3 = _points$_i.runningLength; | ||
_runningLength2 = _points$_i.runningLength; | ||
if (_runningLength3 > size) { | ||
if (_runningLength2 > size) { | ||
ir = getStrokeRadius(size, thinning, easing, _pressure); | ||
@@ -512,3 +487,3 @@ break; | ||
the distance between the second left and right point for the cap's radius. | ||
Finallym remove the first left and right points. :psyduck: | ||
Finally remove the first left and right points. :psyduck: | ||
*/ | ||
@@ -550,5 +525,2 @@ | ||
} | ||
leftPts.pop(); | ||
rightPts.pop(); | ||
} else { | ||
@@ -586,3 +558,3 @@ endCap.push(lastPoint.point); | ||
return getStrokeOutlinePoints(getStrokePoints(points, options.streamline), options); | ||
return getStrokeOutlinePoints(getStrokePoints(points, options), options); | ||
} | ||
@@ -589,0 +561,0 @@ |
@@ -1,2 +0,2 @@ | ||
"use strict";function n(n,r,t){return n*(1-t)+r*t}function r(n,r,t){return Math.max(r,Math.min(t,n))}function t(t,e,i,o){return void 0===o&&(o=.5),e?(o=r(i(o),0,1),(e<0?n(t,t+t*r(e,-.95,-.05),o):n(t-t*r(e,.05,.95),t,o))/2):t/2}function e(n,r){return[n[0]+r[0],n[1]+r[1]]}function i(n,r){return[n[0]-r[0],n[1]-r[1]]}function o(n,r){return[r[0]-n[0],r[1]-n[1]]}function u(n,r){return[n[0]*r,n[1]*r]}function a(n){return[n[1],-n[0]]}function v(n,r){return n[0]*r[0]+n[1]*r[1]}function s(n){return function(n,r){return[n[0]/r,n[1]/r]}(n,function(n){return Math.hypot(n[0],n[1])}(n))}function f(n,r){return Math.hypot(n[1]-r[1],n[0]-r[0])}function p(n,r){return u(e(n,r),.5)}function c(n,r,t){var e=Math.sin(t),i=Math.cos(t),o=n[0]-r[0],u=n[1]-r[1];return[o*i-u*e+r[0],o*e+u*i+r[1]]}function h(n,r,t){return e(n,u(o(n,r),t))}Object.defineProperty(exports,"__esModule",{value:!0});var g=Math.min,d=Math.PI;function l(n,r,t){void 0===r&&(r=.5),void 0===t&&(t=8);var i=function(n){return Array.isArray(n[0])?n.map((function(n){var r=n[2];return[n[0],n[1],void 0===r?.5:r]})):n.map((function(n){var r=n.pressure;return[n.x,n.y,void 0===r?.5:r]}))}(n),u=i.length;if(0===u)return[];1===u&&i.push(e(i[0],[1,0]));for(var a=[{point:[i[0][0],i[0][1]],pressure:i[0][2],vector:[0,0],distance:0,runningLength:0}],v=1,p=i[v],c=a[0];v<i.length;p=i[++v],c=a[v-1]){var g=h(c.point,p,1-r),d=p[2],l=s(o(g,c.point)),M=f(g,c.point);a.push({point:g,pressure:d,vector:l,distance:M,runningLength:c.runningLength+M})}for(var m=0;m<u;m++){var L=a[m],x=L.vector;if(L.runningLength>t||m===u-1){for(var y=0;y<m;y++)a[y].vector=x;break}}for(var k=a[u-1].runningLength,P=u-1;P>1;P--){var b=a[P],A=b.vector;if(k-b.runningLength>t/2){for(var O=P;O<u;O++)a[O].vector=A;break}}return a}function M(n,r){void 0===r&&(r={});var l=r.size,M=void 0===l?8:l,m=r.thinning,L=void 0===m?.5:m,x=r.smoothing,y=void 0===x?.5:x,k=r.simulatePressure,P=void 0===k||k,b=r.easing,A=void 0===b?function(n){return n}:b,O=r.start,S=void 0===O?{}:O,_=r.end,j=void 0===_?{}:_,z=r.last,I=void 0!==z&&z,q=S.taper,w=void 0===q?0:q,B=S.easing,C=void 0===B?function(n){return n*(2-n)}:B,D=j.taper,E=void 0===D?0:D,F=j.easing,G=void 0===F?function(n){return--n*n*n+1}:F,H=n.length;if(0===H)return[];for(var J=n[H-1].runningLength,K=[],N=[],Q=n.slice(0,5).reduce((function(n,r){return(n+r.pressure)/2}),n[0].pressure),R=t(M,L,A,n[H-1].pressure),T=n[0].vector,U=n[0].point,V=U,W=U,X=V,Y=1;Y<H-1;Y++){var Z=n[Y],$=Z.point,nn=Z.pressure,rn=Z.vector,tn=Z.distance,en=Z.runningLength;if(L){if(P){var on=g(1,1-tn/M),un=g(1,tn/M);nn=g(1,Q+un/2*(on-Q))}R=t(M,L,A,nn)}else R=M/2;var an=en<w?C(en/w):1,vn=J-en<E?G((J-en)/E):1;R*=Math.min(an,vn);var sn=n[Y+1].vector,fn=v(rn,sn);if(fn<0){for(var pn=u(a(T),R),cn=e($,pn),hn=i($,pn),gn=.2;gn<1;gn+=.2)X=c(cn,$,d*-gn),W=c(hn,$,d*gn),N.push(X),K.push(W);U=W,V=X}else{var dn=u(a(h(sn,rn,fn)),R);W=i($,dn);var ln=s(o(X=e($,dn),V)),Mn=s(o(W,U)),mn=1===Y||fn<.25,Ln=(en>M?M:M/2)*y;(mn||f(U,W)>Ln&&v(ln,rn)>0)&&(K.push(p(U,W)),U=W),(mn||f(V,X)>Ln&&v(Mn,rn)>0)&&(N.push(p(V,X)),V=X),Q=nn,T=rn}}var xn=n[0],yn=n[H-1],kn=N.length<2||K.length<2;if(kn&&(!w&&!E||I)){for(var Pn=0,bn=0;bn<H;bn++){var An=n[bn];if(An.runningLength>M){Pn=t(M,L,A,An.pressure);break}}for(var On=i(xn.point,u(a(s(o(yn.point,xn.point))),Pn||R)),Sn=[],_n=0;_n<=1;_n+=.1)Sn.push(c(On,xn.point,2*d*_n));return Sn}var jn=[];if(!(w||E&&kn)){for(var zn=i(xn.point,u(s(o(X=N[1],W=K[1])),f(X,W)/2)),In=0;In<=1;In+=.2)jn.push(c(zn,xn.point,d*In));K.shift(),N.shift()}var qn=[];if(E||w&&kn)qn.push(yn.point);else{for(var wn=i(yn.point,u(a(yn.vector),R)),Bn=0;Bn<=1;Bn+=.1)qn.push(c(wn,yn.point,3*d*Bn));K.pop(),N.pop()}return K.concat(qn,N.reverse(),jn)}exports.default=function(n,r){return void 0===r&&(r={}),M(l(n,r.streamline),r)},exports.getStrokeOutlinePoints=M,exports.getStrokePoints=l; | ||
"use strict";function r(r,n,t){return r*(1-t)+n*t}function n(r,n,t){return Math.max(n,Math.min(t,r))}function t(t,e,i,o){return void 0===o&&(o=.5),e?(o=n(i(o),0,1),(e<0?r(t,t+t*n(e,-.95,-.05),o):r(t-t*n(e,.05,.95),t,o))/2):t/2}function e(r,n){return[r[0]+n[0],r[1]+n[1]]}function i(r,n){return[r[0]-n[0],r[1]-n[1]]}function o(r,n){return[n[0]-r[0],n[1]-r[1]]}function u(r,n){return[r[0]*n,r[1]*n]}function s(r){return[r[1],-r[0]]}function a(r,n){return r[0]*n[0]+r[1]*n[1]}function v(r){return function(r,n){return[r[0]/n,r[1]/n]}(r,function(r){return Math.hypot(r[0],r[1])}(r))}function f(r,n){return Math.hypot(r[1]-n[1],r[0]-n[0])}function c(r,n,t){var e=Math.sin(t),i=Math.cos(t),o=r[0]-n[0],u=r[1]-n[1];return[o*i-u*e+n[0],o*e+u*i+n[1]]}function p(r,n,t){return e(r,u(o(r,n),t))}Object.defineProperty(exports,"__esModule",{value:!0});var h=Math.min,d=Math.PI;function g(r,n){var t=n.simulatePressure,i=n.streamline,u=void 0===i?.5:i,s=n.size,c=void 0===s?8:s;u/=2,void 0===t||t||(u/=2);var h=function(r){return Array.isArray(r[0])?r.map((function(r){var n=r[2];return[r[0],r[1],void 0===n?.5:n]})):r.map((function(r){var n=r.pressure;return[r.x,r.y,void 0===n?.5:n]}))}(r),d=h.length;if(0===d)return[];1===d&&h.push(e(h[0],[1,0]));for(var g=[{point:[h[0][0],h[0][1]],pressure:h[0][2],vector:[0,0],distance:0,runningLength:0}],l=1,m=h[l],M=g[0];l<h.length;m=h[++l],M=g[l-1]){var L=p(M.point,m,1-u),x=m[2],y=v(o(L,M.point)),P=f(L,M.point);g.push({point:L,pressure:x,vector:y,distance:P,runningLength:M.runningLength+P})}for(var k=g[d-1].runningLength,b=d-2;b>1;b--){var z=g[b],A=z.vector;if(k-z.runningLength>c/2||a(g[b-1].vector,g[b].vector)<.8){for(var O=b;O<d;O++)g[O].vector=A;break}}return g}function l(r,n){void 0===n&&(n={});var g=n.size,l=void 0===g?8:g,m=n.thinning,M=void 0===m?.5:m,L=n.smoothing,x=void 0===L?.5:L,y=n.simulatePressure,P=void 0===y||y,k=n.easing,b=void 0===k?function(r){return r}:k,z=n.start,A=void 0===z?{}:z,O=n.end,S=void 0===O?{}:O,_=n.last,j=void 0!==_&&_,I=n.streamline,q=void 0===I?.5:I;q/=2;var w=A.taper,B=void 0===w?0:w,C=A.easing,D=void 0===C?function(r){return r*(2-r)}:C,E=S.taper,F=void 0===E?0:E,G=S.easing,H=void 0===G?function(r){return--r*r*r+1}:G,J=r.length;if(0===J)return[];for(var K=r[J-1].runningLength,N=[],Q=[],R=r.slice(0,5).reduce((function(r,n){return(r+n.pressure)/2}),r[0].pressure),T=t(l,M,b,r[J-1].pressure),U=r[0].vector,V=r[0].point,W=V,X=V,Y=W,Z=1;Z<J-1;Z++){var $=r[Z],rr=$.point,nr=$.pressure,tr=$.vector,er=$.distance,ir=$.runningLength;if(M){if(P){var or=h(1,1-er/l),ur=h(1,er/l);nr=h(1,R+ur/2*(or-R))}T=t(l,M,b,nr)}else T=l/2;var sr=ir<B?D(ir/B):1,ar=K-ir<F?H((K-ir)/F):1;T*=Math.min(sr,ar);var vr=r[Z+1].vector,fr=a(tr,vr);if(fr<0){for(var cr=u(s(U),T),pr=e(rr,cr),hr=i(rr,cr),dr=.2;dr<1;dr+=.2)Y=c(pr,rr,d*-dr),X=c(hr,rr,d*dr),Q.push(Y),N.push(X);V=X,W=Y}else{var gr=u(s(p(vr,tr,fr)),T);X=i(rr,gr),Y=e(rr,gr);var lr=1===Z||fr<.25,mr=(ir>l?l:l/2)*x;(lr||f(V,X)>mr)&&(N.push(p(V,X,q)),V=X),(lr||f(W,Y)>mr)&&(Q.push(p(W,Y,q)),W=Y),R=nr,U=tr}}var Mr=r[0],Lr=r[J-1],xr=Q.length<2||N.length<2;if(xr&&(!B&&!F||j)){for(var yr=0,Pr=0;Pr<J;Pr++){var kr=r[Pr];if(kr.runningLength>l){yr=t(l,M,b,kr.pressure);break}}for(var br=i(Mr.point,u(s(v(o(Lr.point,Mr.point))),yr||T)),zr=[],Ar=0;Ar<=1;Ar+=.1)zr.push(c(br,Mr.point,2*d*Ar));return zr}var Or=[];if(!(B||F&&xr)){for(var Sr=i(Mr.point,u(v(o(Y=Q[1],X=N[1])),f(Y,X)/2)),_r=0;_r<=1;_r+=.2)Or.push(c(Sr,Mr.point,d*_r));N.shift(),Q.shift()}var jr=[];if(F||B&&xr)jr.push(Lr.point);else for(var Ir=i(Lr.point,u(s(Lr.vector),T)),qr=0;qr<=1;qr+=.1)jr.push(c(Ir,Lr.point,3*d*qr));return N.concat(jr,Q.reverse(),Or)}exports.default=function(r,n){return void 0===n&&(n={}),l(g(r,n),n)},exports.getStrokeOutlinePoints=l,exports.getStrokePoints=g; | ||
//# sourceMappingURL=perfect-freehand.cjs.production.min.js.map |
@@ -144,11 +144,2 @@ function lerp(y1, y2, mu) { | ||
/** | ||
* Mean between two vectors or mid vector between two vectors | ||
* @param A | ||
* @param B | ||
*/ | ||
function med(A, B) { | ||
return mul(add(A, B), 0.5); | ||
} | ||
/** | ||
* Rotate a vector around another vector by r (radians) | ||
@@ -190,9 +181,13 @@ * @param A vector | ||
function getStrokePoints(points, streamline, size) { | ||
if (streamline === void 0) { | ||
streamline = 0.5; | ||
} | ||
function getStrokePoints(points, options) { | ||
var _options$simulatePres = options.simulatePressure, | ||
simulatePressure = _options$simulatePres === void 0 ? true : _options$simulatePres, | ||
_options$streamline = options.streamline, | ||
streamline = _options$streamline === void 0 ? 0.5 : _options$streamline, | ||
_options$size = options.size, | ||
size = _options$size === void 0 ? 8 : _options$size; | ||
streamline /= 2; | ||
if (size === void 0) { | ||
size = 8; | ||
if (!simulatePressure) { | ||
streamline /= 2; | ||
} | ||
@@ -227,24 +222,2 @@ | ||
/* | ||
Align vectors at the start of the line | ||
Find the first stroke point past the size and then set all preceding points' | ||
vectors to match this point's vector. This aligns the start cap and reduces | ||
noise at the start of the line. | ||
*/ | ||
for (var _i = 0; _i < len; _i++) { | ||
var _strokePoints$_i = strokePoints[_i], | ||
_runningLength = _strokePoints$_i.runningLength, | ||
_vector = _strokePoints$_i.vector; | ||
if (_runningLength > size || _i === len - 1) { | ||
for (var j = 0; j < _i; j++) { | ||
strokePoints[j].vector = _vector; | ||
} | ||
break; | ||
} | ||
} | ||
/* | ||
Align vectors at the end of the line | ||
@@ -261,10 +234,10 @@ Starting from the last point, work back until we've traveled more than | ||
for (var _i2 = len - 1; _i2 > 1; _i2--) { | ||
var _strokePoints$_i2 = strokePoints[_i2], | ||
_runningLength2 = _strokePoints$_i2.runningLength, | ||
_vector2 = _strokePoints$_i2.vector; | ||
for (var _i = len - 2; _i > 1; _i--) { | ||
var _strokePoints$_i = strokePoints[_i], | ||
_runningLength = _strokePoints$_i.runningLength, | ||
_vector = _strokePoints$_i.vector; | ||
if (totalLength - _runningLength2 > size / 2) { | ||
for (var _j = _i2; _j < len; _j++) { | ||
strokePoints[_j].vector = _vector2; | ||
if (totalLength - _runningLength > size / 2 || dpr(strokePoints[_i - 1].vector, strokePoints[_i].vector) < 0.8) { | ||
for (var j = _i; j < len; j++) { | ||
strokePoints[j].vector = _vector; | ||
} | ||
@@ -299,4 +272,4 @@ | ||
var _options = options, | ||
_options$size = _options.size, | ||
size = _options$size === void 0 ? 8 : _options$size, | ||
_options$size2 = _options.size, | ||
size = _options$size2 === void 0 ? 8 : _options$size2, | ||
_options$thinning = _options.thinning, | ||
@@ -306,4 +279,4 @@ thinning = _options$thinning === void 0 ? 0.5 : _options$thinning, | ||
smoothing = _options$smoothing === void 0 ? 0.5 : _options$smoothing, | ||
_options$simulatePres = _options.simulatePressure, | ||
simulatePressure = _options$simulatePres === void 0 ? true : _options$simulatePres, | ||
_options$simulatePres2 = _options.simulatePressure, | ||
simulatePressure = _options$simulatePres2 === void 0 ? true : _options$simulatePres2, | ||
_options$easing = _options.easing, | ||
@@ -319,2 +292,6 @@ easing = _options$easing === void 0 ? function (t) { | ||
isComplete = _options$last === void 0 ? false : _options$last; | ||
var _options2 = options, | ||
_options2$streamline = _options2.streamline, | ||
streamline = _options2$streamline === void 0 ? 0.5 : _options2$streamline; | ||
streamline /= 2; | ||
var _start$taper = start.taper, | ||
@@ -437,14 +414,12 @@ taperStart = _start$taper === void 0 ? 0 : _start$taper, | ||
tr = add(point, offset); | ||
var tlu = uni(vec(tr, pr)); | ||
var tru = uni(vec(tl, pl)); | ||
var alwaysAdd = i === 1 || dpr$1 < 0.25; | ||
var minDistance = (runningLength > size ? size : size / 2) * smoothing; | ||
if (alwaysAdd || dist(pl, tl) > minDistance && dpr(tlu, vector) > 0) { | ||
leftPts.push(med(pl, tl)); | ||
if (alwaysAdd || dist(pl, tl) > minDistance) { | ||
leftPts.push(lrp(pl, tl, streamline)); | ||
pl = tl; | ||
} | ||
if (alwaysAdd || dist(pr, tr) > minDistance && dpr(tru, vector) > 0) { | ||
rightPts.push(med(pr, tr)); | ||
if (alwaysAdd || dist(pr, tr) > minDistance) { | ||
rightPts.push(lrp(pr, tr, streamline)); | ||
pr = tr; | ||
@@ -481,8 +456,8 @@ } // Set variables for next iteration | ||
for (var _i3 = 0; _i3 < len; _i3++) { | ||
var _points$_i = points[_i3], | ||
for (var _i2 = 0; _i2 < len; _i2++) { | ||
var _points$_i = points[_i2], | ||
_pressure = _points$_i.pressure, | ||
_runningLength3 = _points$_i.runningLength; | ||
_runningLength2 = _points$_i.runningLength; | ||
if (_runningLength3 > size) { | ||
if (_runningLength2 > size) { | ||
ir = getStrokeRadius(size, thinning, easing, _pressure); | ||
@@ -508,3 +483,3 @@ break; | ||
the distance between the second left and right point for the cap's radius. | ||
Finallym remove the first left and right points. :psyduck: | ||
Finally remove the first left and right points. :psyduck: | ||
*/ | ||
@@ -546,5 +521,2 @@ | ||
} | ||
leftPts.pop(); | ||
rightPts.pop(); | ||
} else { | ||
@@ -582,3 +554,3 @@ endCap.push(lastPoint.point); | ||
return getStrokeOutlinePoints(getStrokePoints(points, options.streamline), options); | ||
return getStrokeOutlinePoints(getStrokePoints(points, options), options); | ||
} | ||
@@ -585,0 +557,0 @@ |
{ | ||
"version": "0.4.5", | ||
"version": "0.4.6", | ||
"name": "perfect-freehand", | ||
@@ -4,0 +4,0 @@ "author": { |
@@ -17,3 +17,11 @@ import { toPointsArray, getStrokeRadius } from './utils' | ||
K extends { x: number; y: number; pressure?: number } | ||
>(points: (T | K)[], streamline = 0.5, size = 8): StrokePoint[] { | ||
>(points: (T | K)[], options: StrokeOptions): StrokePoint[] { | ||
let { simulatePressure = true, streamline = 0.5, size = 8 } = options | ||
streamline /= 2 | ||
if (!simulatePressure) { | ||
streamline /= 2 | ||
} | ||
const pts = toPointsArray(points) | ||
@@ -56,19 +64,2 @@ const len = pts.length | ||
/* | ||
Align vectors at the start of the line | ||
Find the first stroke point past the size and then set all preceding points' | ||
vectors to match this point's vector. This aligns the start cap and reduces | ||
noise at the start of the line. | ||
*/ | ||
for (let i = 0; i < len; i++) { | ||
const { runningLength, vector } = strokePoints[i] | ||
if (runningLength > size || i === len - 1) { | ||
for (let j = 0; j < i; j++) { | ||
strokePoints[j].vector = vector | ||
} | ||
break | ||
} | ||
} | ||
/* | ||
@@ -83,7 +74,11 @@ Align vectors at the end of the line | ||
*/ | ||
const totalLength = strokePoints[len - 1].runningLength | ||
for (let i = len - 1; i > 1; i--) { | ||
for (let i = len - 2; i > 1; i--) { | ||
const { runningLength, vector } = strokePoints[i] | ||
if (totalLength - runningLength > size / 2) { | ||
if ( | ||
totalLength - runningLength > size / 2 || | ||
vec.dpr(strokePoints[i - 1].vector, strokePoints[i].vector) < 0.8 | ||
) { | ||
for (let j = i; j < len; j++) { | ||
@@ -128,2 +123,6 @@ strokePoints[j].vector = vector | ||
let { streamline = 0.5 } = options | ||
streamline /= 2 | ||
const { | ||
@@ -267,21 +266,12 @@ taper: taperStart = 0, | ||
const tlu = vec.uni(vec.vec(tr, pr)) | ||
const tru = vec.uni(vec.vec(tl, pl)) | ||
const alwaysAdd = i === 1 || dpr < 0.25 | ||
const minDistance = (runningLength > size ? size : size / 2) * smoothing | ||
if ( | ||
alwaysAdd || | ||
(vec.dist(pl, tl) > minDistance && vec.dpr(tlu, vector) > 0) | ||
) { | ||
leftPts.push(vec.med(pl, tl)) | ||
if (alwaysAdd || vec.dist(pl, tl) > minDistance) { | ||
leftPts.push(vec.lrp(pl, tl, streamline)) | ||
pl = tl | ||
} | ||
if ( | ||
alwaysAdd || | ||
(vec.dist(pr, tr) > minDistance && vec.dpr(tru, vector) > 0) | ||
) { | ||
rightPts.push(vec.med(pr, tr)) | ||
if (alwaysAdd || vec.dist(pr, tr) > minDistance) { | ||
rightPts.push(vec.lrp(pr, tr, streamline)) | ||
pr = tr | ||
@@ -351,3 +341,3 @@ } | ||
the distance between the second left and right point for the cap's radius. | ||
Finallym remove the first left and right points. :psyduck: | ||
Finally remove the first left and right points. :psyduck: | ||
*/ | ||
@@ -395,5 +385,2 @@ | ||
} | ||
leftPts.pop() | ||
rightPts.pop() | ||
} else { | ||
@@ -430,8 +417,5 @@ endCap.push(lastPoint.point) | ||
>(points: (T | K)[], options: StrokeOptions = {} as StrokeOptions): number[][] { | ||
return getStrokeOutlinePoints( | ||
getStrokePoints(points, options.streamline), | ||
options | ||
) | ||
return getStrokeOutlinePoints(getStrokePoints(points, options), options) | ||
} | ||
export { StrokeOptions } |
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
153044
1655