perfect-freehand
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -1,3 +0,7 @@ | ||
# 0.1.5 | ||
# 0.2.1 | ||
- Fixes actual pressure sensitivity. | ||
# 0.2.0 | ||
- Breaks up algorithm into smaller functions. Because `getPath` returns an SVG path data, you can use `getPath` only with the Path2D element (for HTML Canvas) or SVG paths. These new functions will allow you to create paths in other rendering technologies. | ||
@@ -4,0 +8,0 @@ |
@@ -169,2 +169,4 @@ 'use strict'; | ||
var _options2 = options, | ||
_options2$minSize = _options2.minSize, | ||
minSize = _options2$minSize === void 0 ? 2.5 : _options2$minSize, | ||
_options2$maxSize = _options2.maxSize, | ||
@@ -179,5 +181,5 @@ maxSize = _options2$maxSize === void 0 ? 8 : _options2$maxSize; | ||
var size = maxSize, | ||
p0 = points[0], | ||
var p0 = points[0], | ||
p1 = points[len - 1], | ||
size = p0[2] === p1[2] ? maxSize : minSize + (maxSize - minSize) * p1[2], | ||
a = p0 === p1 ? Math.random() * (PI * 2) : atan2(p1[1] - p0[1], p1[0] - p0[0]), | ||
@@ -238,7 +240,10 @@ m = getPointBetween(p0[0], p0[1], p1[0], p1[1], 0.5); | ||
if (pressure) { | ||
// Accellerate the reported pressure. | ||
var rp = min(1 - distance / maxSize, 1); | ||
var sp = min(distance / maxSize, 1); | ||
ip = min(1, pp + (rp - pp) * (sp / 2)); // Compute the size based on the pressure. | ||
if (ip === points[i - 1][2]) { | ||
// Accellerate the reported pressure. | ||
var rp = min(1 - distance / maxSize, 1); | ||
var sp = min(distance / maxSize, 1); | ||
ip = min(1, pp + (rp - pp) * (sp / 2)); | ||
} // Compute the size based on the pressure. | ||
size = clamp(minSize + ip * (maxSize - minSize), minSize, maxSize); | ||
@@ -245,0 +250,0 @@ } else { |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r,t=(r=require("polygon-clipping"))&&"object"==typeof r&&"default"in r?r.default:r;function e(r,t){(null==t||t>r.length)&&(t=r.length);for(var e=0,n=new Array(t);e<t;e++)n[e]=r[e];return n}function n(r,t){var n;if("undefined"==typeof Symbol||null==r[Symbol.iterator]){if(Array.isArray(r)||(n=function(r,t){if(r){if("string"==typeof r)return e(r,void 0);var n=Object.prototype.toString.call(r).slice(8,-1);return"Object"===n&&r.constructor&&(n=r.constructor.name),"Map"===n||"Set"===n?Array.from(r):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?e(r,void 0):void 0}}(r))||t&&r&&"number"==typeof r.length){n&&(r=n);var o=0;return function(){return o>=r.length?{done:!0}:{done:!1,value:r[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(n=r[Symbol.iterator]()).next.bind(n)}var o=Math.hypot,i=Math.cos,a=Math.max,u=Math.min,v=Math.sin,l=Math.atan2,s=Math.PI,f=s/2;function h(r,t,e,n){return[i(e)*n+r,v(e)*n+t]}function p(r,t,e,n,o){return void 0===o&&(o=.5),[r+(e-r)*o,t+(n-t)*o]}function d(r,t){void 0===t&&(t={});var e,n,i,a=t.streamline,u=void 0===a?.5:a,v=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),s=0,f=.01,h=v.length,p=[].concat(v[0],[0,0,0]),d=[p];if(0===h)return[];for(var c=1;c<h;c++){var y=v[c],m=y[2],g=p[0],b=p[1];i=l((n=b+(y[1]-b)*(1-u))-b,(e=g+(y[0]-g)*(1-u))-g),f=o(n-b,e-g),d.push(p=[e,n,m,i,f,s+=f])}return d.length>1&&(d[0][2]=d[1][2]),d}function c(r,t){void 0===t&&(t={});var e=t.maxSize,n=r.length;if(0===n)return[];var o=void 0===e?8:e,i=r[0],a=r[n-1],u=i===a?Math.random()*(2*s):l(a[1]-i[1],a[0]-i[0]),v=p(i[0],i[1],a[0],a[1],.5);return[h(v[0],v[1],u+f,o),h(i[0],i[1],u+s,o),h(v[0],v[1],u-f,o),h(a[0],a[1],u,o),h(v[0],v[1],u+f,o)]}function y(r,t){void 0===t&&(t={});var e=t.pressure,n=void 0===e||e,o=t.minSize,i=void 0===o?2.5:o,v=t.maxSize,l=void 0===v?8:v,s=t.smooth,d=void 0===s?8:s,c=r.length,y=r[0],m=r[0],g=y,b=m,M=y,S=y,x=0,A=.5,j=m,P=[];if(0===c)return[];for(var O=1;O<c;O++){var z=r[O],I=z[0],k=z[1],w=z[2],Q=z[3],_=z[4];if(n){var q=u(1-_/l,1),C=u(_/l,1);w=u(1,A+C/2*(q-A)),x=a(i,u(l,i+w*(l-i)))}else x=l;y=h(I,k,Q-f,x),m=h(I,k,Q+f,x),0===O?(g=y,b=m):(Math.hypot(y[0]-g[0],y[1]-g[1])>d&&(P.push(M),M=p(g[0],g[1],y[0],y[1],.5),g=y),Math.hypot(m[0]-b[0],m[1]-b[1])>d&&(P.unshift(S),S=p(b[0],b[1],m[0],m[1],.5),b=m)),A=w,j=[I,k]}return P.push(j),P.unshift(j),P}function m(r){return t.union([r])}exports.clipPath=m,exports.default=function(r,t){if(void 0===t&&(t={}),0===r.length)return"";var e=t.clip,o=void 0===e||e,i=t.maxSize,a=void 0===i?8:i,u=d(r,t),v=u[u.length-1][5]<2*a?c(u,t):y(u,t),l=[];if(o)for(var s,f=n(m(v));!(s=f()).done;)for(var h,g=n(s.value);!(h=g()).done;){var b=h.value,M=b[0],S=b[1];b.push(M),l.push("M "+M[0]+" "+M[1]);for(var x=1;x<b.length;x++){var A=p(M[0],M[1],S[0],S[1],.5);l.push(" Q "+M[0]+","+M[1]+" "+A[0]+","+A[1]),M=S,S=b[x+1]}}else{var j=v[0],P=v[1];v.push(j),l.push("M "+j[0]+" "+j[1]);for(var O=1;O<v.length;O++){var z=p(j[0],j[1],P[0],P[1],.5);l.push("Q "+j[0]+","+j[1]+" "+z[0]+","+z[1]),j=P,P=v[O+1]}}return l.join(" ")},exports.getShortStrokeOutlinePoints=c,exports.getStrokeOutlinePoints=y,exports.getStrokePoints=d; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r,t=(r=require("polygon-clipping"))&&"object"==typeof r&&"default"in r?r.default:r;function e(r,t){(null==t||t>r.length)&&(t=r.length);for(var e=0,n=new Array(t);e<t;e++)n[e]=r[e];return n}function n(r,t){var n;if("undefined"==typeof Symbol||null==r[Symbol.iterator]){if(Array.isArray(r)||(n=function(r,t){if(r){if("string"==typeof r)return e(r,void 0);var n=Object.prototype.toString.call(r).slice(8,-1);return"Object"===n&&r.constructor&&(n=r.constructor.name),"Map"===n||"Set"===n?Array.from(r):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?e(r,void 0):void 0}}(r))||t&&r&&"number"==typeof r.length){n&&(r=n);var o=0;return function(){return o>=r.length?{done:!0}:{done:!1,value:r[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(n=r[Symbol.iterator]()).next.bind(n)}var o=Math.hypot,i=Math.cos,a=Math.max,u=Math.min,v=Math.sin,f=Math.atan2,l=Math.PI,s=l/2;function h(r,t,e,n){return[i(e)*n+r,v(e)*n+t]}function p(r,t,e,n,o){return void 0===o&&(o=.5),[r+(e-r)*o,t+(n-t)*o]}function d(r,t){void 0===t&&(t={});var e,n,i,a=t.streamline,u=void 0===a?.5:a,v=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),l=0,s=.01,h=v.length,p=[].concat(v[0],[0,0,0]),d=[p];if(0===h)return[];for(var c=1;c<h;c++){var m=v[c],y=m[2],g=p[0],S=p[1];i=f((n=S+(m[1]-S)*(1-u))-S,(e=g+(m[0]-g)*(1-u))-g),s=o(n-S,e-g),d.push(p=[e,n,y,i,s,l+=s])}return d.length>1&&(d[0][2]=d[1][2]),d}function c(r,t){void 0===t&&(t={});var e=t.minSize,n=void 0===e?2.5:e,o=t.maxSize,i=void 0===o?8:o,a=r.length;if(0===a)return[];var u=r[0],v=r[a-1],d=u[2]===v[2]?i:n+(i-n)*v[2],c=u===v?Math.random()*(2*l):f(v[1]-u[1],v[0]-u[0]),m=p(u[0],u[1],v[0],v[1],.5);return[h(m[0],m[1],c+s,d),h(u[0],u[1],c+l,d),h(m[0],m[1],c-s,d),h(v[0],v[1],c,d),h(m[0],m[1],c+s,d)]}function m(r,t){void 0===t&&(t={});var e=t.pressure,n=void 0===e||e,o=t.minSize,i=void 0===o?2.5:o,v=t.maxSize,f=void 0===v?8:v,l=t.smooth,d=void 0===l?8:l,c=r.length,m=r[0],y=r[0],g=m,S=y,b=m,M=m,x=0,A=.5,j=y,P=[];if(0===c)return[];for(var z=1;z<c;z++){var O=r[z],I=O[0],k=O[1],w=O[2],Q=O[3],_=O[4];if(n){if(w===r[z-1][2]){var q=u(1-_/f,1),C=u(_/f,1);w=u(1,A+C/2*(q-A))}x=a(i,u(f,i+w*(f-i)))}else x=f;m=h(I,k,Q-s,x),y=h(I,k,Q+s,x),0===z?(g=m,S=y):(Math.hypot(m[0]-g[0],m[1]-g[1])>d&&(P.push(b),b=p(g[0],g[1],m[0],m[1],.5),g=m),Math.hypot(y[0]-S[0],y[1]-S[1])>d&&(P.unshift(M),M=p(S[0],S[1],y[0],y[1],.5),S=y)),A=w,j=[I,k]}return P.push(j),P.unshift(j),P}function y(r){return t.union([r])}exports.clipPath=y,exports.default=function(r,t){if(void 0===t&&(t={}),0===r.length)return"";var e=t.clip,o=void 0===e||e,i=t.maxSize,a=void 0===i?8:i,u=d(r,t),v=u[u.length-1][5]<2*a?c(u,t):m(u,t),f=[];if(o)for(var l,s=n(y(v));!(l=s()).done;)for(var h,g=n(l.value);!(h=g()).done;){var S=h.value,b=S[0],M=S[1];S.push(b),f.push("M "+b[0]+" "+b[1]);for(var x=1;x<S.length;x++){var A=p(b[0],b[1],M[0],M[1],.5);f.push(" Q "+b[0]+","+b[1]+" "+A[0]+","+A[1]),b=M,M=S[x+1]}}else{var j=v[0],P=v[1];v.push(j),f.push("M "+j[0]+" "+j[1]);for(var z=1;z<v.length;z++){var O=p(j[0],j[1],P[0],P[1],.5);f.push("Q "+j[0]+","+j[1]+" "+O[0]+","+O[1]),j=P,P=v[z+1]}}return f.join(" ")},exports.getShortStrokeOutlinePoints=c,exports.getStrokeOutlinePoints=m,exports.getStrokePoints=d; | ||
//# sourceMappingURL=perfect-freehand.cjs.production.min.js.map |
@@ -163,2 +163,4 @@ import polygonClipping from 'polygon-clipping'; | ||
var _options2 = options, | ||
_options2$minSize = _options2.minSize, | ||
minSize = _options2$minSize === void 0 ? 2.5 : _options2$minSize, | ||
_options2$maxSize = _options2.maxSize, | ||
@@ -173,5 +175,5 @@ maxSize = _options2$maxSize === void 0 ? 8 : _options2$maxSize; | ||
var size = maxSize, | ||
p0 = points[0], | ||
var p0 = points[0], | ||
p1 = points[len - 1], | ||
size = p0[2] === p1[2] ? maxSize : minSize + (maxSize - minSize) * p1[2], | ||
a = p0 === p1 ? Math.random() * (PI * 2) : atan2(p1[1] - p0[1], p1[0] - p0[0]), | ||
@@ -232,7 +234,10 @@ m = getPointBetween(p0[0], p0[1], p1[0], p1[1], 0.5); | ||
if (pressure) { | ||
// Accellerate the reported pressure. | ||
var rp = min(1 - distance / maxSize, 1); | ||
var sp = min(distance / maxSize, 1); | ||
ip = min(1, pp + (rp - pp) * (sp / 2)); // Compute the size based on the pressure. | ||
if (ip === points[i - 1][2]) { | ||
// Accellerate the reported pressure. | ||
var rp = min(1 - distance / maxSize, 1); | ||
var sp = min(distance / maxSize, 1); | ||
ip = min(1, pp + (rp - pp) * (sp / 2)); | ||
} // Compute the size based on the pressure. | ||
size = clamp(minSize + ip * (maxSize - minSize), minSize, maxSize); | ||
@@ -239,0 +244,0 @@ } else { |
{ | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
@@ -74,8 +74,8 @@ # Perfect Freehand | ||
getPath(myPoints, { | ||
type: 'pen', | ||
minSize: 2.5, | ||
maxSize: 8, | ||
simulatePressure: true, | ||
pressure: true, | ||
streamline: 0.5, | ||
smooth: 0.5, | ||
clip: true, | ||
}) | ||
@@ -91,7 +91,5 @@ ``` | ||
export default function Example() { | ||
const [currentType, setCurrentType] = React.useState('mouse') | ||
const [currentMark, setCurrentMark] = React.useState([]) | ||
function handlePointerDown(e) { | ||
setCurrentType(e.pointerType) | ||
setCurrentMark([[e.pageX, e.pageY, e.pressure]]) | ||
@@ -101,3 +99,3 @@ } | ||
function handlePointerMove(e) { | ||
if (e.buttons === 1 && e.pointerType === currentType) { | ||
if (e.buttons === 1) { | ||
setCurrentMark([...currentMark, [e.pageX, e.pageY, e.pressure]]) | ||
@@ -113,2 +111,3 @@ } | ||
onPointerMove={handlePointerMove} | ||
style={{ touchAction: 'none' }} | ||
> | ||
@@ -115,0 +114,0 @@ <path d={getPath(currentMark)} /> |
@@ -130,3 +130,3 @@ import polygonClipping from 'polygon-clipping' | ||
) { | ||
const { maxSize = 8 } = options | ||
const { minSize = 2.5, maxSize = 8 } = options | ||
@@ -141,5 +141,5 @@ const len = points.length | ||
// Draw a kind of shitty shape around the start and end points. | ||
const size = maxSize, | ||
p0 = points[0], | ||
const p0 = points[0], | ||
p1 = points[len - 1], | ||
size = p0[2] === p1[2] ? maxSize : minSize + (maxSize - minSize) * p1[2], | ||
a = | ||
@@ -198,6 +198,8 @@ p0 === p1 | ||
if (pressure) { | ||
// Accellerate the reported pressure. | ||
let rp = min(1 - distance / maxSize, 1) | ||
const sp = min(distance / maxSize, 1) | ||
ip = min(1, pp + (rp - pp) * (sp / 2)) | ||
if (ip === points[i - 1][2]) { | ||
// Accellerate the reported pressure. | ||
let rp = min(1 - distance / maxSize, 1) | ||
const sp = min(distance / maxSize, 1) | ||
ip = min(1, pp + (rp - pp) * (sp / 2)) | ||
} | ||
@@ -204,0 +206,0 @@ // Compute the size based on the pressure. |
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
91400
951
137