Socket
Socket
Sign inDemoInstall

perfect-freehand

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

perfect-freehand - npm Package Compare versions

Comparing version 0.3.4 to 0.3.5

4

CHANGELOG.md

@@ -0,1 +1,5 @@

## 0.3.5
- Improves caps.
## 0.3.4

@@ -2,0 +6,0 @@

92

dist/perfect-freehand.cjs.development.js

@@ -29,5 +29,2 @@ 'use strict';

}
function lerpAngles(a0, a1, t) {
return a0 + shortAngleDist(a0, a1) * t;
}
function getPointBetween(p0, p1, d) {

@@ -164,14 +161,11 @@ if (d === void 0) {

if (len === 0) {
return [];
} // If the point is only one point long, draw two caps at either end.
if (len === 0) return []; // If the point is only one point long, draw two caps at either end.
if (len === 1 || totalLength <= size / 4) {
var first = points[0],
last = points[len - 1],
angle = getAngle(first, last);
_last = points[len - 1],
angle = getAngle(first, _last);
if (thinning) {
r = getStrokeRadius(size, thinning, easing, last[2]);
r = getStrokeRadius(size, thinning, easing, _last[2]);
}

@@ -181,3 +175,3 @@

tl = projectPoint(first, angle + PI$1 + TAU - t * PI$1, r);
tr = projectPoint(last, angle + TAU - t * PI$1, r);
tr = projectPoint(_last, angle + TAU - t * PI$1, r);
leftPts.push(tl);

@@ -191,4 +185,4 @@ rightPts.push(tr);

for (var i = 1; i < len; i++) {
var prev = points[i - 1];
for (var i = 1; i < len - 1; i++) {
var next = points[i + 1];
var _points$i = points[i],

@@ -230,49 +224,45 @@ x = _points$i[0],

rightPts.push(tr);
}
} // 3.
// Handle sharp corners
// Find the delta between the current and next angle.
_angle = lerpAngles(pa, _angle, 0.75); // 3.
// Add points for the current point.
if (i === len - 1) {
// The last point in the line.
// Add points for an end cap.
for (var _t2 = 0, _step2 = 0.1; _t2 <= 1; _t2 += _step2) {
rightPts.push(projectPoint([x, y], _angle + TAU + _t2 * PI$1, r));
var absDelta = abs(getAngleDelta(next[3], _angle));
if (absDelta > SHARP) {
// A sharp corner.
// Project points (left and right) for a cap.
for (var _t2 = 0, _step2 = 0.25; _t2 <= 1; _t2 += _step2) {
tl = projectPoint([x, y], pa - TAU + _t2 * -PI$1, r);
tr = projectPoint([x, y], pa + TAU + _t2 * PI$1, r);
leftPts.push(tl);
rightPts.push(tr);
}
} else {
// Find the delta between the current and previous angle.
var delta = getAngleDelta(prev[3], _angle),
absDelta = abs(delta);
if (absDelta > SHARP && clen > r) {
// A sharp corner.
// Project points (left and right) for a cap.
var mid = getPointBetween(prev, [x, y]);
continue;
} // 4. Add regular point.
for (var _t3 = 0, _step3 = 0.25; _t3 <= 1; _t3 += _step3) {
tl = projectPoint(mid, pa - TAU + _t3 * -PI$1, r);
tr = projectPoint(mid, pa + TAU + _t3 * PI$1, r);
leftPts.push(tl);
rightPts.push(tr);
}
} else {
// A regular point.
// Add projected points left and right, if far enough away.
pl = projectPoint([x, y], _angle - TAU, r);
pr = projectPoint([x, y], _angle + TAU, r);
if (absDelta > DULL || getDistance(pl, tl) > minDist) {
leftPts.push(getPointBetween(tl, pl));
tl = pl;
}
pl = projectPoint([x, y], _angle - TAU, r);
pr = projectPoint([x, y], _angle + TAU, r);
if (absDelta > DULL || getDistance(pr, tr) > minDist) {
rightPts.push(getPointBetween(tr, pr));
tr = pr;
}
}
if (absDelta > DULL || getDistance(pl, tl) > minDist) {
leftPts.push(getPointBetween(tl, pl));
tl = pl;
}
pp = pressure;
pa = _angle;
if (absDelta > DULL || getDistance(pr, tr) > minDist) {
rightPts.push(getPointBetween(tr, pr));
tr = pr;
}
pp = pressure;
pa = _angle;
} // Add the end cap. This is tricky because some lines end with sharp angles.
var last = points[points.length - 1];
for (var _t3 = 0, _step3 = 0.1; _t3 <= 1; _t3 += _step3) {
rightPts.push(projectPoint(last, last[3] + TAU + _t3 * PI$1, r));
}

@@ -279,0 +269,0 @@

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r=Math.hypot,t=Math.cos,n=Math.max,e=Math.min,i=Math.sin,u=Math.atan2,o=2*Math.PI;function a(r,t,n){return r*(1-n)+t*n}function s(r,n,e){return[t(n)*e+r[0],i(n)*e+r[1]]}function f(r,t){var n=(t-r)%o;return 2*n%o-n}function v(r,t,n){return r+f(r,t)*n}function h(r,t,n){return void 0===n&&(n=.5),[r[0]+(t[0]-r[0])*n,r[1]+(t[1]-r[1])*n]}function c(r,t){return u(t[1]-r[1],t[0]-r[0])}function p(t,n){return r(n[1]-t[1],n[0]-t[0])}function d(r,t,i){return n(t,e(i,r))}var l=Math.abs,M=Math.min,g=Math.PI,m=g/2,x=m,P=x/2;function y(r,t,n,e){return void 0===e&&(e=.5),void 0===t?r/2:(e=d(n(e),0,1),(t<0?a(r,r+r*d(t,-.95,-.05),e):a(r-r*d(t,.05,.95),r,e))/2)}function b(r,t){void 0===t&&(t=.5);var n=function(r){return Array.isArray(r[0])?r.map((function(r){var t=r[2];return[r[0],r[1],void 0===t?.5:t]})):r.map((function(r){var t=r.pressure;return[r.x,r.y,void 0===t?.5:t]}))}(r);if(0===n.length)return[];n[0]=[n[0][0],n[0][1],n[0][2]||.5,0,0,0];for(var e=1,i=n[e],u=n[0];e<n.length;i=n[++e],u=n[e-1])i[0]=a(u[0],i[0],1-t),i[1]=a(u[1],i[1],1-t),i[3]=c(i,u),i[4]=p(i,u),i[5]=u[5]+i[4];return n}function k(r,t){void 0===t&&(t={});var n=t.size,e=void 0===n?8:n,i=t.thinning,u=void 0===i?.5:i,o=t.smoothing,a=t.simulatePressure,d=void 0===a||a,b=t.easing,k=void 0===b?function(r){return r}:b,A=r.length,I=e*(void 0===o?.5:o),O=[],S=[],_=r[0],j=r[0],z=_,q=j,w=j[3],B=0,C=e/2,D=!0;if(0===A)return[];if(1===A||r[A-1][5]<=e/4){var E=r[0],F=r[A-1],G=c(E,F);u&&(C=y(e,u,k,F[2]));for(var H=0;H<=1;H+=.1)z=s(E,G+g+m-H*g,C),q=s(F,G+m-H*g,C),O.push(z),S.push(q);return O.concat(S)}for(var J=1;J<A;J++){var K=r[J-1],L=r[J],N=L[0],Q=L[1],R=L[2],T=L[3],U=L[4],V=L[5];if(u){if(d){var W=M(1-U/e,1),X=M(U/e,1);R=M(1,B+X/2*(W-B))}C=y(e,u,k,R)}if(D){if(V<e/4)continue;D=!1;for(var Y=0;Y<=1;Y+=.1)z=s(r[0],T+m-Y*g,C),O.push(z);q=s(r[0],T+m,C),S.push(q)}if(T=v(w,T,.75),J===A-1)for(var Z=0;Z<=1;Z+=.1)S.push(s([N,Q],T+m+Z*g,C));else{var $=f(K[3],T),rr=l($);if(rr>x&&V>C)for(var tr=h(K,[N,Q]),nr=0;nr<=1;nr+=.25)z=s(tr,w-m+nr*-g,C),q=s(tr,w+m+nr*g,C),O.push(z),S.push(q);else _=s([N,Q],T-m,C),j=s([N,Q],T+m,C),(rr>P||p(_,z)>I)&&(O.push(h(z,_)),z=_),(rr>P||p(j,q)>I)&&(S.push(h(q,j)),q=j);B=R,w=T}}return O.concat(S.reverse())}exports.default=function(r,t){return void 0===t&&(t={}),k(b(r,t.streamline),t)},exports.getStrokeOutlinePoints=k,exports.getStrokePoints=b;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r=Math.hypot,t=Math.cos,n=Math.max,e=Math.min,i=Math.sin,o=Math.atan2,u=2*Math.PI;function a(r,t,n){return r*(1-n)+t*n}function s(r,n,e){return[t(n)*e+r[0],i(n)*e+r[1]]}function v(r,t,n){return void 0===n&&(n=.5),[r[0]+(t[0]-r[0])*n,r[1]+(t[1]-r[1])*n]}function f(r,t){return o(t[1]-r[1],t[0]-r[0])}function h(t,n){return r(n[1]-t[1],n[0]-t[0])}function c(r,t,i){return n(t,e(i,r))}var p=Math.abs,d=Math.min,l=Math.PI,M=l/2,g=M,m=g/2;function x(r,t,n,e){return void 0===e&&(e=.5),void 0===t?r/2:(e=c(n(e),0,1),(t<0?a(r,r+r*c(t,-.95,-.05),e):a(r-r*c(t,.05,.95),r,e))/2)}function P(r,t){void 0===t&&(t=.5);var n=function(r){return Array.isArray(r[0])?r.map((function(r){var t=r[2];return[r[0],r[1],void 0===t?.5:t]})):r.map((function(r){var t=r.pressure;return[r.x,r.y,void 0===t?.5:t]}))}(r);if(0===n.length)return[];n[0]=[n[0][0],n[0][1],n[0][2]||.5,0,0,0];for(var e=1,i=n[e],o=n[0];e<n.length;i=n[++e],o=n[e-1])i[0]=a(o[0],i[0],1-t),i[1]=a(o[1],i[1],1-t),i[3]=f(i,o),i[4]=h(i,o),i[5]=o[5]+i[4];return n}function y(r,t){void 0===t&&(t={});var n=t.size,e=void 0===n?8:n,i=t.thinning,o=void 0===i?.5:i,a=t.smoothing,c=t.simulatePressure,P=void 0===c||c,y=t.easing,b=void 0===y?function(r){return r}:y,k=r.length,A=e*(void 0===a?.5:a),I=[],O=[],S=r[0],_=r[0],j=S,z=_,q=_[3],w=0,B=e/2,C=!0;if(0===k)return[];if(1===k||r[k-1][5]<=e/4){var D=r[0],E=r[k-1],F=f(D,E);o&&(B=x(e,o,b,E[2]));for(var G=0;G<=1;G+=.1)j=s(D,F+l+M-G*l,B),z=s(E,F+M-G*l,B),I.push(j),O.push(z);return I.concat(O)}for(var H=1;H<k-1;H++){var J=r[H+1],K=r[H],L=K[0],N=K[1],Q=K[2],R=K[3],T=K[4],U=K[5];if(o){if(P){var V=d(1-T/e,1),W=d(T/e,1);Q=d(1,w+W/2*(V-w))}B=x(e,o,b,Q)}if(C){if(U<e/4)continue;C=!1;for(var X=0;X<=1;X+=.1)j=s(r[0],R+M-X*l,B),I.push(j);z=s(r[0],R+M,B),O.push(z)}var Y=p(function(r,t){var n=(t-r)%u;return 2*n%u-n}(J[3],R));if(Y>g)for(var Z=0;Z<=1;Z+=.25)j=s([L,N],q-M+Z*-l,B),z=s([L,N],q+M+Z*l,B),I.push(j),O.push(z);else S=s([L,N],R-M,B),_=s([L,N],R+M,B),(Y>m||h(S,j)>A)&&(I.push(v(j,S)),j=S),(Y>m||h(_,z)>A)&&(O.push(v(z,_)),z=_),w=Q,q=R}for(var $=r[r.length-1],rr=0;rr<=1;rr+=.1)O.push(s($,$[3]+M+rr*l,B));return I.concat(O.reverse())}exports.default=function(r,t){return void 0===t&&(t={}),y(P(r,t.streamline),t)},exports.getStrokeOutlinePoints=y,exports.getStrokePoints=P;
//# sourceMappingURL=perfect-freehand.cjs.production.min.js.map

@@ -25,5 +25,2 @@ var hypot = Math.hypot,

}
function lerpAngles(a0, a1, t) {
return a0 + shortAngleDist(a0, a1) * t;
}
function getPointBetween(p0, p1, d) {

@@ -160,14 +157,11 @@ if (d === void 0) {

if (len === 0) {
return [];
} // If the point is only one point long, draw two caps at either end.
if (len === 0) return []; // If the point is only one point long, draw two caps at either end.
if (len === 1 || totalLength <= size / 4) {
var first = points[0],
last = points[len - 1],
angle = getAngle(first, last);
_last = points[len - 1],
angle = getAngle(first, _last);
if (thinning) {
r = getStrokeRadius(size, thinning, easing, last[2]);
r = getStrokeRadius(size, thinning, easing, _last[2]);
}

@@ -177,3 +171,3 @@

tl = projectPoint(first, angle + PI$1 + TAU - t * PI$1, r);
tr = projectPoint(last, angle + TAU - t * PI$1, r);
tr = projectPoint(_last, angle + TAU - t * PI$1, r);
leftPts.push(tl);

@@ -187,4 +181,4 @@ rightPts.push(tr);

for (var i = 1; i < len; i++) {
var prev = points[i - 1];
for (var i = 1; i < len - 1; i++) {
var next = points[i + 1];
var _points$i = points[i],

@@ -226,49 +220,45 @@ x = _points$i[0],

rightPts.push(tr);
}
} // 3.
// Handle sharp corners
// Find the delta between the current and next angle.
_angle = lerpAngles(pa, _angle, 0.75); // 3.
// Add points for the current point.
if (i === len - 1) {
// The last point in the line.
// Add points for an end cap.
for (var _t2 = 0, _step2 = 0.1; _t2 <= 1; _t2 += _step2) {
rightPts.push(projectPoint([x, y], _angle + TAU + _t2 * PI$1, r));
var absDelta = abs(getAngleDelta(next[3], _angle));
if (absDelta > SHARP) {
// A sharp corner.
// Project points (left and right) for a cap.
for (var _t2 = 0, _step2 = 0.25; _t2 <= 1; _t2 += _step2) {
tl = projectPoint([x, y], pa - TAU + _t2 * -PI$1, r);
tr = projectPoint([x, y], pa + TAU + _t2 * PI$1, r);
leftPts.push(tl);
rightPts.push(tr);
}
} else {
// Find the delta between the current and previous angle.
var delta = getAngleDelta(prev[3], _angle),
absDelta = abs(delta);
if (absDelta > SHARP && clen > r) {
// A sharp corner.
// Project points (left and right) for a cap.
var mid = getPointBetween(prev, [x, y]);
continue;
} // 4. Add regular point.
for (var _t3 = 0, _step3 = 0.25; _t3 <= 1; _t3 += _step3) {
tl = projectPoint(mid, pa - TAU + _t3 * -PI$1, r);
tr = projectPoint(mid, pa + TAU + _t3 * PI$1, r);
leftPts.push(tl);
rightPts.push(tr);
}
} else {
// A regular point.
// Add projected points left and right, if far enough away.
pl = projectPoint([x, y], _angle - TAU, r);
pr = projectPoint([x, y], _angle + TAU, r);
if (absDelta > DULL || getDistance(pl, tl) > minDist) {
leftPts.push(getPointBetween(tl, pl));
tl = pl;
}
pl = projectPoint([x, y], _angle - TAU, r);
pr = projectPoint([x, y], _angle + TAU, r);
if (absDelta > DULL || getDistance(pr, tr) > minDist) {
rightPts.push(getPointBetween(tr, pr));
tr = pr;
}
}
if (absDelta > DULL || getDistance(pl, tl) > minDist) {
leftPts.push(getPointBetween(tl, pl));
tl = pl;
}
pp = pressure;
pa = _angle;
if (absDelta > DULL || getDistance(pr, tr) > minDist) {
rightPts.push(getPointBetween(tr, pr));
tr = pr;
}
pp = pressure;
pa = _angle;
} // Add the end cap. This is tricky because some lines end with sharp angles.
var last = points[points.length - 1];
for (var _t3 = 0, _step3 = 0.1; _t3 <= 1; _t3 += _step3) {
rightPts.push(projectPoint(last, last[3] + TAU + _t3 * PI$1, r));
}

@@ -275,0 +265,0 @@

{
"version": "0.3.4",
"version": "0.3.5",
"name": "perfect-freehand",

@@ -4,0 +4,0 @@ "author": {

@@ -9,3 +9,2 @@ import {

projectPoint,
lerpAngles,
lerp,

@@ -105,5 +104,3 @@ } from './utils'

// We can't do anything with an empty array.
if (len === 0) {
return []
}
if (len === 0) return []

@@ -131,4 +128,4 @@ // If the point is only one point long, draw two caps at either end.

// For a point with more than one point, create an outline shape.
for (let i = 1; i < len; i++) {
const prev = points[i - 1]
for (let i = 1; i < len - 1; i++) {
const next = points[i + 1]

@@ -139,2 +136,3 @@ let [x, y, pressure, angle, distance, clen] = points[i]

// Calculate the size of the current point.
if (thinning) {

@@ -154,2 +152,3 @@ if (simulatePressure) {

// Draw a cap once we've reached the minimum length.
if (short) {

@@ -172,51 +171,49 @@ if (clen < size / 4) continue

angle = lerpAngles(pa, angle, 0.75)
// 3.
// Handle sharp corners
// 3.
// Add points for the current point.
if (i === len - 1) {
// The last point in the line.
// Add points for an end cap.
for (let t = 0, step = 0.1; t <= 1; t += step) {
rightPts.push(projectPoint([x, y], angle + TAU + t * PI, r))
// Find the delta between the current and next angle.
const absDelta = abs(getAngleDelta(next[3], angle))
if (absDelta > SHARP) {
// A sharp corner.
// Project points (left and right) for a cap.
for (let t = 0, step = 0.25; t <= 1; t += step) {
tl = projectPoint([x, y], pa - TAU + t * -PI, r)
tr = projectPoint([x, y], pa + TAU + t * PI, r)
leftPts.push(tl)
rightPts.push(tr)
}
} else {
// Find the delta between the current and previous angle.
const delta = getAngleDelta(prev[3], angle),
absDelta = abs(delta)
if (absDelta > SHARP && clen > r) {
// A sharp corner.
// Project points (left and right) for a cap.
const mid = getPointBetween(prev, [x, y])
continue
}
for (let t = 0, step = 0.25; t <= 1; t += step) {
tl = projectPoint(mid, pa - TAU + t * -PI, r)
tr = projectPoint(mid, pa + TAU + t * PI, r)
// 4. Add regular point.
leftPts.push(tl)
rightPts.push(tr)
}
} else {
// A regular point.
// Add projected points left and right, if far enough away.
pl = projectPoint([x, y], angle - TAU, r)
pr = projectPoint([x, y], angle + TAU, r)
pl = projectPoint([x, y], angle - TAU, r)
pr = projectPoint([x, y], angle + TAU, r)
if (absDelta > DULL || getDistance(pl, tl) > minDist) {
leftPts.push(getPointBetween(tl, pl))
tl = pl
}
if (absDelta > DULL || getDistance(pl, tl) > minDist) {
leftPts.push(getPointBetween(tl, pl))
tl = pl
}
if (absDelta > DULL || getDistance(pr, tr) > minDist) {
rightPts.push(getPointBetween(tr, pr))
tr = pr
}
}
if (absDelta > DULL || getDistance(pr, tr) > minDist) {
rightPts.push(getPointBetween(tr, pr))
tr = pr
}
pp = pressure
pa = angle
}
pp = pressure
pa = angle
}
// Add the end cap. This is tricky because some lines end with sharp angles.
const last = points[points.length - 1]
for (let t = 0, step = 0.1; t <= 1; t += step) {
rightPts.push(projectPoint(last, last[3] + TAU + t * PI, r))
}
return leftPts.concat(rightPts.reverse())

@@ -223,0 +220,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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc