perfect-freehand
Advanced tools
Comparing version 0.4.7 to 0.4.8
@@ -0,1 +1,6 @@ | ||
## 0.4.8 | ||
- Fixes bug on start caps. | ||
- Adds simple test. | ||
## 0.4.6 | ||
@@ -2,0 +7,0 @@ |
@@ -5,53 +5,3 @@ 'use strict'; | ||
function lerp(y1, y2, mu) { | ||
return y1 * (1 - mu) + y2 * mu; | ||
} | ||
function clamp(n, a, b) { | ||
return Math.max(a, Math.min(b, n)); | ||
} | ||
/** | ||
* Convert an array of points to the correct format ([x, y, radius]) | ||
* @param points | ||
* @returns | ||
*/ | ||
function toPointsArray(points) { | ||
if (Array.isArray(points[0])) { | ||
return points.map(function (_ref) { | ||
var x = _ref[0], | ||
y = _ref[1], | ||
_ref$ = _ref[2], | ||
pressure = _ref$ === void 0 ? 0.5 : _ref$; | ||
return [x, y, pressure]; | ||
}); | ||
} else { | ||
return points.map(function (_ref2) { | ||
var x = _ref2.x, | ||
y = _ref2.y, | ||
_ref2$pressure = _ref2.pressure, | ||
pressure = _ref2$pressure === void 0 ? 0.5 : _ref2$pressure; | ||
return [x, y, pressure]; | ||
}); | ||
} | ||
} | ||
/** | ||
* Compute a radius based on the pressure. | ||
* @param size | ||
* @param thinning | ||
* @param easing | ||
* @param pressure | ||
* @returns | ||
*/ | ||
function getStrokeRadius(size, thinning, easing, pressure) { | ||
if (pressure === void 0) { | ||
pressure = 0.5; | ||
} | ||
if (!thinning) return size / 2; | ||
pressure = clamp(easing(pressure), 0, 1); | ||
return (thinning < 0 ? lerp(size, size + size * clamp(thinning, -0.95, -0.05), pressure) : lerp(size - size * clamp(thinning, 0.05, 0.95), size, pressure)) / 2; | ||
} | ||
/** | ||
* Negate a vector. | ||
@@ -190,4 +140,57 @@ * @param A | ||
return add(A, mul(vec(A, B), t)); | ||
} // isLeft: >0 for counterclockwise | ||
function isEqual(a, b) { | ||
return a[0] === b[0] && a[1] === b[1]; | ||
} | ||
function lerp(y1, y2, mu) { | ||
return y1 * (1 - mu) + y2 * mu; | ||
} | ||
function clamp(n, a, b) { | ||
return Math.max(a, Math.min(b, n)); | ||
} | ||
/** | ||
* Convert an array of points to the correct format ([x, y, radius]) | ||
* @param points | ||
* @returns | ||
*/ | ||
function toPointsArray(points) { | ||
if (Array.isArray(points[0])) { | ||
return points.map(function (_ref) { | ||
var x = _ref[0], | ||
y = _ref[1], | ||
_ref$ = _ref[2], | ||
pressure = _ref$ === void 0 ? 0.5 : _ref$; | ||
return [x, y, pressure]; | ||
}); | ||
} else { | ||
return points.map(function (_ref2) { | ||
var x = _ref2.x, | ||
y = _ref2.y, | ||
_ref2$pressure = _ref2.pressure, | ||
pressure = _ref2$pressure === void 0 ? 0.5 : _ref2$pressure; | ||
return [x, y, pressure]; | ||
}); | ||
} | ||
} | ||
/** | ||
* Compute a radius based on the pressure. | ||
* @param size | ||
* @param thinning | ||
* @param easing | ||
* @param pressure | ||
* @returns | ||
*/ | ||
function getStrokeRadius(size, thinning, easing, pressure) { | ||
if (pressure === void 0) { | ||
pressure = 0.5; | ||
} | ||
if (!thinning) return size / 2; | ||
pressure = clamp(easing(pressure), 0, 1); | ||
return (thinning < 0 ? lerp(size, size + size * clamp(thinning, -0.95, -0.05), pressure) : lerp(size - size * clamp(thinning, 0.05, 0.95), size, pressure)) / 2; | ||
} | ||
var min = Math.min, | ||
@@ -404,8 +407,5 @@ PI = Math.PI; | ||
var la = add(point, _offset); | ||
var ra = sub(point, _offset); | ||
for (var t = 0.2; t < 1; t += 0.2) { | ||
tr = rotAround(la, point, PI * -t); | ||
tl = rotAround(ra, point, PI * t); | ||
for (var t = 0; t < 1; t += 0.2) { | ||
tr = rotAround(add(point, _offset), point, PI * -t); | ||
tl = rotAround(sub(point, _offset), point, PI * t); | ||
rightPts.push(tr); | ||
@@ -507,12 +507,20 @@ leftPts.push(tl); | ||
tr = rightPts[1]; | ||
tl = leftPts[1]; | ||
var _start2 = sub(firstPoint.point, mul(uni(vec(tr, tl)), dist(tr, tl) / 2)); | ||
for (var _i3 = 1; _i3 < leftPts.length; _i3++) { | ||
if (!isEqual(tr, leftPts[_i3])) { | ||
tl = leftPts[_i3]; | ||
break; | ||
} | ||
} | ||
for (var _t2 = 0, _step = 0.2; _t2 <= 1; _t2 += _step) { | ||
startCap.push(rotAround(_start2, firstPoint.point, PI * _t2)); | ||
if (!isEqual(tr, tl)) { | ||
var _start2 = sub(firstPoint.point, mul(uni(vec(tr, tl)), dist(tr, tl) / 2)); | ||
for (var _t2 = 0, _step = 0.2; _t2 <= 1; _t2 += _step) { | ||
startCap.push(rotAround(_start2, firstPoint.point, PI * _t2)); | ||
} | ||
leftPts.shift(); | ||
rightPts.shift(); | ||
} | ||
leftPts.shift(); | ||
rightPts.shift(); | ||
} | ||
@@ -519,0 +527,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 s(n){return[n[1],-n[0]]}function a(n,r){return n[0]*r[0]+n[1]*r[1]}function v(n,r){return function(n){return n[0]*n[0]+n[1]*n[1]}(i(n,r))}function f(n){return function(n,r){return[n[0]/r,n[1]/r]}(n,function(n){return Math.hypot(n[0],n[1])}(n))}function c(n,r){return Math.hypot(n[1]-r[1],n[0]-r[0])}function p(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 d=Math.min,g=Math.PI;function l(n,r){var t=r.simulatePressure,i=r.streamline,u=void 0===i?.5:i,s=r.size,v=void 0===s?8:s;u/=2,void 0===t||t||(u/=2);var p=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),d=p.length;if(0===d)return[];1===d&&p.push(e(p[0],[1,0]));for(var g=[{point:[p[0][0],p[0][1]],pressure:p[0][2],vector:[0,0],distance:0,runningLength:0}],l=1,m=p[l],M=g[0];l<p.length;m=p[++l],M=g[l-1]){var L=h(M.point,m,1-u),x=m[2],y=f(o(L,M.point)),P=c(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.runningLength,O=z.vector,S=a(g[b-1].vector,g[b].vector);if(k-A>v/2||S<.8){for(var _=b;_<d;_++)g[_].vector=O;break}}return g}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,P=r.simulatePressure,k=void 0===P||P,b=r.easing,z=void 0===b?function(n){return n}:b,A=r.start,O=void 0===A?{}:A,S=r.end,_=void 0===S?{}:S,j=r.last,w=void 0!==j&&j,I=r.streamline,q=void 0===I?.5:I;q/=2;var B=O.taper,C=void 0===B?0:B,D=O.easing,E=void 0===D?function(n){return n*(2-n)}:D,F=_.taper,G=void 0===F?0:F,H=_.easing,J=void 0===H?function(n){return--n*n*n+1}:H,K=n.length;if(0===K)return[];for(var N=n[K-1].runningLength,Q=[],R=[],T=n.slice(0,5).reduce((function(n,r){return(n+r.pressure)/2}),n[0].pressure),U=t(m,L,z,n[K-1].pressure),V=n[0].vector,W=n[0].point,X=W,Y=W,Z=X,$=1;$<K-1;$++){var nn=n[$],rn=nn.point,tn=nn.pressure,en=nn.vector,on=nn.distance,un=nn.runningLength;if(L){if(k){var sn=d(1,1-on/m),an=d(1,on/m);tn=d(1,T+an/2*(sn-T))}U=t(m,L,z,tn)}else U=m/2;var vn=un<C?E(un/C):1,fn=N-un<G?J((N-un)/G):1;U*=Math.min(vn,fn);var cn=n[$+1].vector,pn=a(en,cn);if(pn<0){for(var hn=u(s(V),U),dn=e(rn,hn),gn=i(rn,hn),ln=.2;ln<1;ln+=.2)Z=p(dn,rn,g*-ln),Y=p(gn,rn,g*ln),R.push(Z),Q.push(Y);W=Y,X=Z}else{var mn=u(s(h(cn,en,pn)),U);Y=i(rn,mn),Z=e(rn,mn);var Mn=1===$||pn<.25,Ln=Math.pow((un>m?m:m/2)*y,2);(Mn||v(W,Y)>Ln)&&(Q.push(h(W,Y,q)),W=Y),(Mn||v(X,Z)>Ln)&&(R.push(h(X,Z,q)),X=Z),T=tn,V=en}}var xn=n[0],yn=n[K-1],Pn=R.length<2||Q.length<2;if(Pn&&(!C&&!G||w)){for(var kn=0,bn=0;bn<K;bn++){var zn=n[bn];if(zn.runningLength>m){kn=t(m,L,z,zn.pressure);break}}for(var An=i(xn.point,u(s(f(o(yn.point,xn.point))),kn||U)),On=[],Sn=0;Sn<=1;Sn+=.1)On.push(p(An,xn.point,2*g*Sn));return On}var _n=[];if(!(C||G&&Pn)){for(var jn=i(xn.point,u(f(o(Z=R[1],Y=Q[1])),c(Z,Y)/2)),wn=0;wn<=1;wn+=.2)_n.push(p(jn,xn.point,g*wn));Q.shift(),R.shift()}var In=[];if(G||C&&Pn)In.push(yn.point);else for(var qn=i(yn.point,u(s(yn.vector),U)),Bn=0;Bn<=1;Bn+=.1)In.push(p(qn,yn.point,3*g*Bn));return Q.concat(In,R.reverse(),_n)}exports.default=function(n,r){return void 0===r&&(r={}),m(l(n,r),r)},exports.getStrokeOutlinePoints=m,exports.getStrokePoints=l; | ||
"use strict";function r(r,n){return[r[0]+n[0],r[1]+n[1]]}function n(r,n){return[r[0]-n[0],r[1]-n[1]]}function t(r,n){return[n[0]-r[0],n[1]-r[1]]}function e(r,n){return[r[0]*n,r[1]*n]}function i(r){return[r[1],-r[0]]}function o(r,n){return r[0]*n[0]+r[1]*n[1]}function u(r,t){return function(r){return r[0]*r[0]+r[1]*r[1]}(n(r,t))}function a(r){return function(r,n){return[r[0]/n,r[1]/n]}(r,function(r){return Math.hypot(r[0],r[1])}(r))}function s(r,n){return Math.hypot(r[1]-n[1],r[0]-n[0])}function v(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 f(n,i,o){return r(n,e(t(n,i),o))}function c(r,n){return r[0]===n[0]&&r[1]===n[1]}function p(r,n,t){return r*(1-t)+n*t}function h(r,n,t){return Math.max(n,Math.min(t,r))}function d(r,n,t,e){return void 0===e&&(e=.5),n?(e=h(t(e),0,1),(n<0?p(r,r+r*h(n,-.95,-.05),e):p(r-r*h(n,.05,.95),r,e))/2):r/2}Object.defineProperty(exports,"__esModule",{value:!0});var g=Math.min,l=Math.PI;function m(n,e){var i=e.simulatePressure,u=e.streamline,v=void 0===u?.5:u,c=e.size,p=void 0===c?8:c;v/=2,void 0===i||i||(v/=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]}))}(n),d=h.length;if(0===d)return[];1===d&&h.push(r(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=f(M.point,m,1-v),x=m[2],y=a(t(L,M.point)),P=s(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.runningLength,O=z.vector,S=o(g[b-1].vector,g[b].vector);if(k-A>p/2||S<.8){for(var _=b;_<d;_++)g[_].vector=O;break}}return g}function M(p,h){void 0===h&&(h={});var m=h.size,M=void 0===m?8:m,L=h.thinning,x=void 0===L?.5:L,y=h.smoothing,P=void 0===y?.5:y,k=h.simulatePressure,b=void 0===k||k,z=h.easing,A=void 0===z?function(r){return r}:z,O=h.start,S=void 0===O?{}:O,_=h.end,j=void 0===_?{}:_,w=h.last,I=void 0!==w&&w,q=h.streamline,B=void 0===q?.5:q;B/=2;var C=S.taper,D=void 0===C?0:C,E=S.easing,F=void 0===E?function(r){return r*(2-r)}:E,G=j.taper,H=void 0===G?0:G,J=j.easing,K=void 0===J?function(r){return--r*r*r+1}:J,N=p.length;if(0===N)return[];for(var Q=p[N-1].runningLength,R=[],T=[],U=p.slice(0,5).reduce((function(r,n){return(r+n.pressure)/2}),p[0].pressure),V=d(M,x,A,p[N-1].pressure),W=p[0].vector,X=p[0].point,Y=X,Z=X,$=Y,rr=1;rr<N-1;rr++){var nr=p[rr],tr=nr.point,er=nr.pressure,ir=nr.vector,or=nr.distance,ur=nr.runningLength;if(x){if(b){var ar=g(1,1-or/M),sr=g(1,or/M);er=g(1,U+sr/2*(ar-U))}V=d(M,x,A,er)}else V=M/2;var vr=ur<D?F(ur/D):1,fr=Q-ur<H?K((Q-ur)/H):1;V*=Math.min(vr,fr);var cr=p[rr+1].vector,pr=o(ir,cr);if(pr<0){for(var hr=e(i(W),V),dr=0;dr<1;dr+=.2)$=v(r(tr,hr),tr,l*-dr),Z=v(n(tr,hr),tr,l*dr),T.push($),R.push(Z);X=Z,Y=$}else{var gr=e(i(f(cr,ir,pr)),V);Z=n(tr,gr),$=r(tr,gr);var lr=1===rr||pr<.25,mr=Math.pow((ur>M?M:M/2)*P,2);(lr||u(X,Z)>mr)&&(R.push(f(X,Z,B)),X=Z),(lr||u(Y,$)>mr)&&(T.push(f(Y,$,B)),Y=$),U=er,W=ir}}var Mr=p[0],Lr=p[N-1],xr=T.length<2||R.length<2;if(xr&&(!D&&!H||I)){for(var yr=0,Pr=0;Pr<N;Pr++){var kr=p[Pr];if(kr.runningLength>M){yr=d(M,x,A,kr.pressure);break}}for(var br=n(Mr.point,e(i(a(t(Lr.point,Mr.point))),yr||V)),zr=[],Ar=0;Ar<=1;Ar+=.1)zr.push(v(br,Mr.point,2*l*Ar));return zr}var Or=[];if(!(D||H&&xr)){$=T[1];for(var Sr=1;Sr<R.length;Sr++)if(!c($,R[Sr])){Z=R[Sr];break}if(!c($,Z)){for(var _r=n(Mr.point,e(a(t($,Z)),s($,Z)/2)),jr=0;jr<=1;jr+=.2)Or.push(v(_r,Mr.point,l*jr));R.shift(),T.shift()}}var wr=[];if(H||D&&xr)wr.push(Lr.point);else for(var Ir=n(Lr.point,e(i(Lr.vector),V)),qr=0;qr<=1;qr+=.1)wr.push(v(Ir,Lr.point,3*l*qr));return R.concat(wr,T.reverse(),Or)}exports.default=function(r,n){return void 0===n&&(n={}),M(m(r,n),n)},exports.getStrokeOutlinePoints=M,exports.getStrokePoints=m; | ||
//# sourceMappingURL=perfect-freehand.cjs.production.min.js.map |
@@ -1,52 +0,2 @@ | ||
function lerp(y1, y2, mu) { | ||
return y1 * (1 - mu) + y2 * mu; | ||
} | ||
function clamp(n, a, b) { | ||
return Math.max(a, Math.min(b, n)); | ||
} | ||
/** | ||
* Convert an array of points to the correct format ([x, y, radius]) | ||
* @param points | ||
* @returns | ||
*/ | ||
function toPointsArray(points) { | ||
if (Array.isArray(points[0])) { | ||
return points.map(function (_ref) { | ||
var x = _ref[0], | ||
y = _ref[1], | ||
_ref$ = _ref[2], | ||
pressure = _ref$ === void 0 ? 0.5 : _ref$; | ||
return [x, y, pressure]; | ||
}); | ||
} else { | ||
return points.map(function (_ref2) { | ||
var x = _ref2.x, | ||
y = _ref2.y, | ||
_ref2$pressure = _ref2.pressure, | ||
pressure = _ref2$pressure === void 0 ? 0.5 : _ref2$pressure; | ||
return [x, y, pressure]; | ||
}); | ||
} | ||
} | ||
/** | ||
* Compute a radius based on the pressure. | ||
* @param size | ||
* @param thinning | ||
* @param easing | ||
* @param pressure | ||
* @returns | ||
*/ | ||
function getStrokeRadius(size, thinning, easing, pressure) { | ||
if (pressure === void 0) { | ||
pressure = 0.5; | ||
} | ||
if (!thinning) return size / 2; | ||
pressure = clamp(easing(pressure), 0, 1); | ||
return (thinning < 0 ? lerp(size, size + size * clamp(thinning, -0.95, -0.05), pressure) : lerp(size - size * clamp(thinning, 0.05, 0.95), size, pressure)) / 2; | ||
} | ||
/** | ||
* Negate a vector. | ||
@@ -185,4 +135,57 @@ * @param A | ||
return add(A, mul(vec(A, B), t)); | ||
} // isLeft: >0 for counterclockwise | ||
function isEqual(a, b) { | ||
return a[0] === b[0] && a[1] === b[1]; | ||
} | ||
function lerp(y1, y2, mu) { | ||
return y1 * (1 - mu) + y2 * mu; | ||
} | ||
function clamp(n, a, b) { | ||
return Math.max(a, Math.min(b, n)); | ||
} | ||
/** | ||
* Convert an array of points to the correct format ([x, y, radius]) | ||
* @param points | ||
* @returns | ||
*/ | ||
function toPointsArray(points) { | ||
if (Array.isArray(points[0])) { | ||
return points.map(function (_ref) { | ||
var x = _ref[0], | ||
y = _ref[1], | ||
_ref$ = _ref[2], | ||
pressure = _ref$ === void 0 ? 0.5 : _ref$; | ||
return [x, y, pressure]; | ||
}); | ||
} else { | ||
return points.map(function (_ref2) { | ||
var x = _ref2.x, | ||
y = _ref2.y, | ||
_ref2$pressure = _ref2.pressure, | ||
pressure = _ref2$pressure === void 0 ? 0.5 : _ref2$pressure; | ||
return [x, y, pressure]; | ||
}); | ||
} | ||
} | ||
/** | ||
* Compute a radius based on the pressure. | ||
* @param size | ||
* @param thinning | ||
* @param easing | ||
* @param pressure | ||
* @returns | ||
*/ | ||
function getStrokeRadius(size, thinning, easing, pressure) { | ||
if (pressure === void 0) { | ||
pressure = 0.5; | ||
} | ||
if (!thinning) return size / 2; | ||
pressure = clamp(easing(pressure), 0, 1); | ||
return (thinning < 0 ? lerp(size, size + size * clamp(thinning, -0.95, -0.05), pressure) : lerp(size - size * clamp(thinning, 0.05, 0.95), size, pressure)) / 2; | ||
} | ||
var min = Math.min, | ||
@@ -399,8 +402,5 @@ PI = Math.PI; | ||
var la = add(point, _offset); | ||
var ra = sub(point, _offset); | ||
for (var t = 0.2; t < 1; t += 0.2) { | ||
tr = rotAround(la, point, PI * -t); | ||
tl = rotAround(ra, point, PI * t); | ||
for (var t = 0; t < 1; t += 0.2) { | ||
tr = rotAround(add(point, _offset), point, PI * -t); | ||
tl = rotAround(sub(point, _offset), point, PI * t); | ||
rightPts.push(tr); | ||
@@ -502,12 +502,20 @@ leftPts.push(tl); | ||
tr = rightPts[1]; | ||
tl = leftPts[1]; | ||
var _start2 = sub(firstPoint.point, mul(uni(vec(tr, tl)), dist(tr, tl) / 2)); | ||
for (var _i3 = 1; _i3 < leftPts.length; _i3++) { | ||
if (!isEqual(tr, leftPts[_i3])) { | ||
tl = leftPts[_i3]; | ||
break; | ||
} | ||
} | ||
for (var _t2 = 0, _step = 0.2; _t2 <= 1; _t2 += _step) { | ||
startCap.push(rotAround(_start2, firstPoint.point, PI * _t2)); | ||
if (!isEqual(tr, tl)) { | ||
var _start2 = sub(firstPoint.point, mul(uni(vec(tr, tl)), dist(tr, tl) / 2)); | ||
for (var _t2 = 0, _step = 0.2; _t2 <= 1; _t2 += _step) { | ||
startCap.push(rotAround(_start2, firstPoint.point, PI * _t2)); | ||
} | ||
leftPts.shift(); | ||
rightPts.shift(); | ||
} | ||
leftPts.shift(); | ||
rightPts.shift(); | ||
} | ||
@@ -514,0 +522,0 @@ /* |
@@ -22,1 +22,2 @@ export declare function lerp(y1: number, y2: number, mu: number): number; | ||
export declare function getStrokeRadius(size: number, thinning: number, easing: (t: number) => number, pressure?: number): number; | ||
export declare function withoutDuplicates(pts: number[][]): number[][]; |
@@ -94,1 +94,4 @@ /** | ||
export declare function lrp(A: number[], B: number[], t: number): number[]; | ||
export declare function isLeft(p1: number[], pc: number[], p2: number[]): number; | ||
export declare function clockwise(p1: number[], pc: number[], p2: number[]): boolean; | ||
export declare function isEqual(a: number[], b: number[]): boolean; |
{ | ||
"version": "0.4.7", | ||
"version": "0.4.8", | ||
"name": "perfect-freehand", | ||
@@ -4,0 +4,0 @@ "author": { |
@@ -7,3 +7,3 @@ # ![Screenshot](screenshot.svg 'Perfect Freehand') | ||
💰 Want to use this library in your commercial product? [Contact me here](steveruizok+perfectfreehand@gmail.com). | ||
💰 Want to use this library in your commercial product? [Contact me here](mailto:steveruizok+perfectfreehand@gmail.com). | ||
@@ -239,2 +239,3 @@ ## Table of Contents | ||
```js | ||
import { strokePoints } from 'perfect-freehand' | ||
const strokePoints = getStrokePoints(rawInputPoints) | ||
@@ -245,3 +246,3 @@ ``` | ||
#### `getStrokeOutlinePoints` | ||
#### `getOutlinePoints` | ||
@@ -251,2 +252,3 @@ Accepts an array of points (formatted as `[x, y, pressure, angle, distance, length]`, i.e. the output of `getStrokePoints`) and returns an array of points (`[x, y]`) defining the outline of a pressure-sensitive stroke. | ||
```js | ||
import { getOutlinePoints } from 'perfect-freehand' | ||
const outlinePoints = getOutlinePoints(strokePoints) | ||
@@ -253,0 +255,0 @@ ``` |
@@ -229,8 +229,6 @@ import { toPointsArray, getStrokeRadius } from './utils' | ||
const offset = vec.mul(vec.per(prevVector), radius) | ||
const la = vec.add(point, offset) | ||
const ra = vec.sub(point, offset) | ||
for (let t = 0.2; t < 1; t += 0.2) { | ||
tr = vec.rotAround(la, point, PI * -t) | ||
tl = vec.rotAround(ra, point, PI * t) | ||
for (let t = 0; t < 1; t += 0.2) { | ||
tr = vec.rotAround(vec.add(point, offset), point, PI * -t) | ||
tl = vec.rotAround(vec.sub(point, offset), point, PI * t) | ||
@@ -246,3 +244,2 @@ rightPts.push(tr) | ||
} | ||
/* | ||
@@ -347,15 +344,23 @@ Add regular points | ||
tr = rightPts[1] | ||
tl = leftPts[1] | ||
const start = vec.sub( | ||
firstPoint.point, | ||
vec.mul(vec.uni(vec.vec(tr, tl)), vec.dist(tr, tl) / 2) | ||
) | ||
for (let i = 1; i < leftPts.length; i++) { | ||
if (!vec.isEqual(tr, leftPts[i])) { | ||
tl = leftPts[i] | ||
break | ||
} | ||
} | ||
for (let t = 0, step = 0.2; t <= 1; t += step) { | ||
startCap.push(vec.rotAround(start, firstPoint.point, PI * t)) | ||
if (!vec.isEqual(tr, tl)) { | ||
const start = vec.sub( | ||
firstPoint.point, | ||
vec.mul(vec.uni(vec.vec(tr, tl)), vec.dist(tr, tl) / 2) | ||
) | ||
for (let t = 0, step = 0.2; t <= 1; t += step) { | ||
startCap.push(vec.rotAround(start, firstPoint.point, PI * t)) | ||
} | ||
leftPts.shift() | ||
rightPts.shift() | ||
} | ||
leftPts.shift() | ||
rightPts.shift() | ||
} | ||
@@ -362,0 +367,0 @@ |
@@ -0,1 +1,3 @@ | ||
import { isEqual, vec } from './vec' | ||
export function lerp(y1: number, y2: number, mu: number) { | ||
@@ -55,1 +57,15 @@ return y1 * (1 - mu) + y2 * mu | ||
} | ||
export function withoutDuplicates(pts: number[][]) { | ||
const unique: number[][] = [] | ||
let prev: number[] | undefined = undefined | ||
for (let pt of pts) { | ||
if (prev && isEqual(prev, pt)) continue | ||
unique.push(pt) | ||
prev = pt | ||
} | ||
return pts | ||
} |
@@ -151,1 +151,16 @@ /** | ||
} | ||
// isLeft: >0 for counterclockwise | ||
// =0 for none (degenerate) | ||
// <0 for clockwise | ||
export function isLeft(p1: number[], pc: number[], p2: number[]) { | ||
return (pc[0] - p1[0]) * (p2[1] - p1[1]) - (p2[0] - p1[0]) * (pc[1] - p1[1]) | ||
} | ||
export function clockwise(p1: number[], pc: number[], p2: number[]) { | ||
return isLeft(p1, pc, p2) > 0 | ||
} | ||
export function isEqual(a: number[], b: number[]) { | ||
return a[0] === b[0] && a[1] === b[1] | ||
} |
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
161979
1763
264