fabric-warpvas
Version:
This JavaScript library helps you efficiently build image warping tools on Fabric.js
3 lines (2 loc) • 29 kB
JavaScript
import{B as e,T as n,S as r,d as i,I as o}from"./base.class-_EIOxPF8.js";import{i as s,r as c,f as a,c as u,a as l}from"./register-limit-move-event-_gHDOT2-.js";"function"==typeof SuppressedError&&SuppressedError;const h=(t,e,n)=>{if(t.x===e.x)return{x:t.x,y:n.y};if(t.y===e.y)return{x:n.x,y:t.y};const r=(e.y-t.y)/(e.x-t.x),i=-1/r,o=(-r*t.x+t.y+i*n.x-n.y)/(i-r);return{x:o,y:r*(o-t.x)+t.y}},x=(t,e)=>Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2)),f=(t,e,n)=>{const r=Object.assign({},t),i={x:e.x-t.x,y:e.y-t.y};if(0===i.x&&0===i.y)return r;if(0===i.x)r.y+=n*Math.sign(i.y);else if(0===i.y)r.x+=n*Math.sign(i.x);else{const t=Math.sqrt(Math.pow(i.x,2)+Math.pow(i.y,2));r.x+=i.x*(n/t),r.y+=i.y*(n/t)}return r};var y,p,d=Object.freeze({__proto__:null,calcBoundingBox:(t,e,n)=>{const r=Math.min(t.x,e.x,n.x),i=Math.max(t.x,e.x,n.x),o=Math.min(t.y,e.y,n.y);return{width:i-r,height:Math.max(t.y,e.y,n.y)-o}},calcCoordDistance:x,calcExpandCoord:(t,e,n,r=1)=>{const i=h(e,n,t),o=x(t,i),s=f(t,i,o+r);return{x:s.x-i.x+e.x,y:s.y-i.y+e.y}},calcIntersection:(t,e,n,r)=>{const i=(t,e)=>Math.abs(t-e)<Math.pow(.1,12);if(i(t.x,e.x)&&i(n.x,r.x))return null;if(i(t.y,e.y)&&i(n.y,r.y))return null;const o=1e4*(e.y-t.y)/Math.round(1e4*(e.x-t.x)),s=1e4*(r.y-n.y)/Math.round(1e4*(r.x-n.x)),c=t.y-o*t.x,a=n.y-s*n.x;if(Math.abs(o)===1/0)return{x:t.x,y:s*t.x+a};if(Math.abs(s)===1/0)return{x:n.x,y:o*n.x+c};const u=(a-c)/(o-s);return{x:u,y:o*u+c}},calcMatrix:(t,e)=>{const[n,r,i]=t,[o,s,c]=e,a=.1/Math.abs([...t,...e].reduce(((t,e)=>Math.min(t,e.x,e.y)),-1)),u=(t,e,n)=>{const[r,i,o,s]=t.map(Number),[c,a,u,l]=e.map(Number),[h,x,f,y]=n.map(Number),p=o-i*u/a,d=u-a*f/x,m=l-a*y/x,g=c-a*h/x,v=(p/d*m-(s-i*l/a))/(p/d*g-(r-i*c/a)),b=(m-g*v)/d;return{x:v,y:(s-r*v-o*b)/i,z:b}},l=[o.x+a,o.y+a,1,n.x+a],h=[s.x+a,s.y+a,1,r.x+a],x=[c.x+a,c.y+a,1,i.x+a],f=u(l,h,x);l[3]=n.y+a,h[3]=r.y+a,x[3]=i.y+a;const y=u(l,h,x),p=f.x,d=f.y,m=f.z;return[p,y.x,d,y.y,m,y.z]},calcPerpendicularIntersection:h,calcRelativeCoord:f,isTriangleContainsPoint:(t,e,n,r,i=.1)=>{const o=(t,e,n)=>Math.abs(t.x*(e.y-n.y)+e.x*(n.y-t.y)+n.x*(t.y-e.y))/2,s=o(e,n,r);return o(e,n,t)+o(n,r,t)+o(r,e,t)<=s+i}});!function(t){t.TOP_LEFT="tl",t.TOP_RIGHT="tr",t.BOTTOM_LEFT="bl",t.BOTTOM_RIGHT="br"}(y||(y={})),function(t){t.TOP="top",t.BOTTOM="bottom",t.LEFT="left",t.RIGHT="right"}(p||(p={}));const{abs:m,cos:g,sin:v,acos:b,atan2:z,sqrt:_,pow:w}=Math;function T(t){return t<0?-w(-t,1/3):w(t,1/3)}const C=Math.PI,O=2*C,M=C/2,E=Number.MAX_SAFE_INTEGER||9007199254740991,I=Number.MIN_SAFE_INTEGER||-9007199254740991,k={x:0,y:0,z:0},j={Tvalues:[-.06405689286260563,.06405689286260563,-.1911188674736163,.1911188674736163,-.3150426796961634,.3150426796961634,-.4337935076260451,.4337935076260451,-.5454214713888396,.5454214713888396,-.6480936519369755,.6480936519369755,-.7401241915785544,.7401241915785544,-.820001985973903,.820001985973903,-.8864155270044011,.8864155270044011,-.9382745520027328,.9382745520027328,-.9747285559713095,.9747285559713095,-.9951872199970213,.9951872199970213],Cvalues:[.12793819534675216,.12793819534675216,.1258374563468283,.1258374563468283,.12167047292780339,.12167047292780339,.1155056680537256,.1155056680537256,.10744427011596563,.10744427011596563,.09761865210411388,.09761865210411388,.08619016153195327,.08619016153195327,.0733464814110803,.0733464814110803,.05929858491543678,.05929858491543678,.04427743881741981,.04427743881741981,.028531388628933663,.028531388628933663,.0123412297999872,.0123412297999872],arcfn:function(t,e){const n=e(t);let r=n.x*n.x+n.y*n.y;return void 0!==n.z&&(r+=n.z*n.z),_(r)},compute:function(t,e,n){if(0===t)return e[0].t=0,e[0];const r=e.length-1;if(1===t)return e[r].t=1,e[r];const i=1-t;let o=e;if(0===r)return e[0].t=t,e[0];if(1===r){const e={x:i*o[0].x+t*o[1].x,y:i*o[0].y+t*o[1].y,t:t};return n&&(e.z=i*o[0].z+t*o[1].z),e}if(r<4){let e,s,c,a=i*i,u=t*t,l=0;2===r?(o=[o[0],o[1],o[2],k],e=a,s=i*t*2,c=u):3===r&&(e=a*i,s=a*t*3,c=i*u*3,l=t*u);const h={x:e*o[0].x+s*o[1].x+c*o[2].x+l*o[3].x,y:e*o[0].y+s*o[1].y+c*o[2].y+l*o[3].y,t:t};return n&&(h.z=e*o[0].z+s*o[1].z+c*o[2].z+l*o[3].z),h}const s=JSON.parse(JSON.stringify(e));for(;s.length>1;){for(let e=0;e<s.length-1;e++)s[e]={x:s[e].x+(s[e+1].x-s[e].x)*t,y:s[e].y+(s[e+1].y-s[e].y)*t},void 0!==s[e].z&&(s[e].z=s[e].z+(s[e+1].z-s[e].z)*t);s.splice(s.length-1,1)}return s[0].t=t,s[0]},computeWithRatios:function(t,e,n,r){const i=1-t,o=n,s=e;let c,a=o[0],u=o[1],l=o[2],h=o[3];return a*=i,u*=t,2===s.length?(c=a+u,{x:(a*s[0].x+u*s[1].x)/c,y:(a*s[0].y+u*s[1].y)/c,z:!!r&&(a*s[0].z+u*s[1].z)/c,t:t}):(a*=i,u*=2*i,l*=t*t,3===s.length?(c=a+u+l,{x:(a*s[0].x+u*s[1].x+l*s[2].x)/c,y:(a*s[0].y+u*s[1].y+l*s[2].y)/c,z:!!r&&(a*s[0].z+u*s[1].z+l*s[2].z)/c,t:t}):(a*=i,u*=1.5*i,l*=3*i,h*=t*t*t,4===s.length?(c=a+u+l+h,{x:(a*s[0].x+u*s[1].x+l*s[2].x+h*s[3].x)/c,y:(a*s[0].y+u*s[1].y+l*s[2].y+h*s[3].y)/c,z:!!r&&(a*s[0].z+u*s[1].z+l*s[2].z+h*s[3].z)/c,t:t}):void 0))},derive:function(t,e){const n=[];for(let r=t,i=r.length,o=i-1;i>1;i--,o--){const t=[];for(let n,i=0;i<o;i++)n={x:o*(r[i+1].x-r[i].x),y:o*(r[i+1].y-r[i].y)},e&&(n.z=o*(r[i+1].z-r[i].z)),t.push(n);n.push(t),r=t}return n},between:function(t,e,n){return e<=t&&t<=n||j.approximately(t,e)||j.approximately(t,n)},approximately:function(t,e,n){return m(t-e)<=(n||1e-6)},length:function(t){const e=j.Tvalues.length;let n=0;for(let r,i=0;i<e;i++)r=.5*j.Tvalues[i]+.5,n+=j.Cvalues[i]*j.arcfn(r,t);return.5*n},map:function(t,e,n,r,i){return r+(t-e)/(n-e)*(i-r)},lerp:function(t,e,n){const r={x:e.x+t*(n.x-e.x),y:e.y+t*(n.y-e.y)};return void 0!==e.z&&void 0!==n.z&&(r.z=e.z+t*(n.z-e.z)),r},pointToString:function(t){let e=t.x+"/"+t.y;return void 0!==t.z&&(e+="/"+t.z),e},pointsToString:function(t){return"["+t.map(j.pointToString).join(", ")+"]"},copy:function(t){return JSON.parse(JSON.stringify(t))},angle:function(t,e,n){const r=e.x-t.x,i=e.y-t.y,o=n.x-t.x,s=n.y-t.y;return z(r*s-i*o,r*o+i*s)},round:function(t,e){const n=""+t,r=n.indexOf(".");return parseFloat(n.substring(0,r+1+e))},dist:function(t,e){const n=t.x-e.x,r=t.y-e.y;return _(n*n+r*r)},closest:function(t,e){let n,r,i=w(2,63);return t.forEach((function(t,o){r=j.dist(e,t),r<i&&(i=r,n=o)})),{mdist:i,mpos:n}},abcratio:function(t,e){if(2!==e&&3!==e)return!1;if(void 0===t)t=.5;else if(0===t||1===t)return t;const n=w(t,e)+w(1-t,e);return m((n-1)/n)},projectionratio:function(t,e){if(2!==e&&3!==e)return!1;if(void 0===t)t=.5;else if(0===t||1===t)return t;const n=w(1-t,e);return n/(w(t,e)+n)},lli8:function(t,e,n,r,i,o,s,c){const a=(t-n)*(o-c)-(e-r)*(i-s);return 0!=a&&{x:((t*r-e*n)*(i-s)-(t-n)*(i*c-o*s))/a,y:((t*r-e*n)*(o-c)-(e-r)*(i*c-o*s))/a}},lli4:function(t,e,n,r){const i=t.x,o=t.y,s=e.x,c=e.y,a=n.x,u=n.y,l=r.x,h=r.y;return j.lli8(i,o,s,c,a,u,l,h)},lli:function(t,e){return j.lli4(t,t.c,e,e.c)},makeline:function(t,e){return new H(t.x,t.y,(t.x+e.x)/2,(t.y+e.y)/2,e.x,e.y)},findbbox:function(t){let e=E,n=E,r=I,i=I;return t.forEach((function(t){const o=t.bbox();e>o.x.min&&(e=o.x.min),n>o.y.min&&(n=o.y.min),r<o.x.max&&(r=o.x.max),i<o.y.max&&(i=o.y.max)})),{x:{min:e,mid:(e+r)/2,max:r,size:r-e},y:{min:n,mid:(n+i)/2,max:i,size:i-n}}},shapeintersections:function(t,e,n,r,i){if(!j.bboxoverlap(e,r))return[];const o=[],s=[t.startcap,t.forward,t.back,t.endcap],c=[n.startcap,n.forward,n.back,n.endcap];return s.forEach((function(e){e.virtual||c.forEach((function(r){if(r.virtual)return;const s=e.intersects(r,i);s.length>0&&(s.c1=e,s.c2=r,s.s1=t,s.s2=n,o.push(s))}))})),o},makeshape:function(t,e,n){const r=e.points.length,i=t.points.length,o=j.makeline(e.points[r-1],t.points[0]),s=j.makeline(t.points[i-1],e.points[0]),c={startcap:o,forward:t,back:e,endcap:s,bbox:j.findbbox([o,t,e,s]),intersections:function(t){return j.shapeintersections(c,c.bbox,t,t.bbox,n)}};return c},getminmax:function(t,e,n){if(!n)return{min:0,max:0};let r,i,o=E,s=I;-1===n.indexOf(0)&&(n=[0].concat(n)),-1===n.indexOf(1)&&n.push(1);for(let c=0,a=n.length;c<a;c++)r=n[c],i=t.get(r),i[e]<o&&(o=i[e]),i[e]>s&&(s=i[e]);return{min:o,mid:(o+s)/2,max:s,size:s-o}},align:function(t,e){const n=e.p1.x,r=e.p1.y,i=-z(e.p2.y-r,e.p2.x-n);return t.map((function(t){return{x:(t.x-n)*g(i)-(t.y-r)*v(i),y:(t.x-n)*v(i)+(t.y-r)*g(i)}}))},roots:function(t,e){e=e||{p1:{x:0,y:0},p2:{x:1,y:0}};const n=t.length-1,r=j.align(t,e),i=function(t){return 0<=t&&t<=1};if(2===n){const t=r[0].y,e=r[1].y,n=r[2].y,o=t-2*e+n;if(0!==o){const r=-_(e*e-t*n),s=-t+e;return[-(r+s)/o,-(-r+s)/o].filter(i)}return e!==n&&0===o?[(2*e-n)/(2*e-2*n)].filter(i):[]}const o=r[0].y,s=r[1].y,c=r[2].y;let a=3*s-o-3*c+r[3].y,u=3*o-6*s+3*c,l=-3*o+3*s,h=o;if(j.approximately(a,0)){if(j.approximately(u,0))return j.approximately(l,0)?[]:[-h/l].filter(i);const t=_(l*l-4*u*h),e=2*u;return[(t-l)/e,(-l-t)/e].filter(i)}u/=a,l/=a,h/=a;const x=(3*l-u*u)/3,f=x/3,y=(2*u*u*u-9*u*l+27*h)/27,p=y/2,d=p*p+f*f*f;let m,v,z,w,C;if(d<0){const t=-x/3,e=_(t*t*t),n=-y/(2*e),r=b(n<-1?-1:n>1?1:n),o=2*T(e);return z=o*g(r/3)-u/3,w=o*g((r+O)/3)-u/3,C=o*g((r+2*O)/3)-u/3,[z,w,C].filter(i)}if(0===d)return m=p<0?T(-p):-T(p),z=2*m-u/3,w=-m-u/3,[z,w].filter(i);{const t=_(d);return m=T(-p+t),v=T(p+t),[m-v-u/3].filter(i)}},droots:function(t){if(3===t.length){const e=t[0],n=t[1],r=t[2],i=e-2*n+r;if(0!==i){const t=-_(n*n-e*r),o=-e+n;return[-(t+o)/i,-(-t+o)/i]}return n!==r&&0===i?[(2*n-r)/(2*(n-r))]:[]}if(2===t.length){const e=t[0],n=t[1];return e!==n?[e/(e-n)]:[]}return[]},curvature:function(t,e,n,r,i){let o,s,c,a,u=0,l=0;const h=j.compute(t,e),x=j.compute(t,n),f=h.x*h.x+h.y*h.y;if(r?(o=_(w(h.y*x.z-x.y*h.z,2)+w(h.z*x.x-x.z*h.x,2)+w(h.x*x.y-x.x*h.y,2)),s=w(f+h.z*h.z,1.5)):(o=h.x*x.y-h.y*x.x,s=w(f,1.5)),0===o||0===s)return{k:0,r:0};if(u=o/s,l=s/o,!i){const i=j.curvature(t-.001,e,n,r,!0).k,o=j.curvature(t+.001,e,n,r,!0).k;a=(o-u+(u-i))/2,c=(m(o-u)+m(u-i))/2}return{k:u,r:l,dk:a,adk:c}},inflections:function(t){if(t.length<4)return[];const e=j.align(t,{p1:t[0],p2:t.slice(-1)[0]}),n=e[2].x*e[1].y,r=e[3].x*e[1].y,i=e[1].x*e[2].y,o=18*(-3*n+2*r+3*i-e[3].x*e[2].y),s=18*(3*n-r-3*i),c=18*(i-n);if(j.approximately(o,0)){if(!j.approximately(s,0)){let t=-c/s;if(0<=t&&t<=1)return[t]}return[]}const a=2*o;if(j.approximately(a,0))return[];const u=s*s-4*o*c;if(u<0)return[];const l=Math.sqrt(u);return[(l-s)/a,-(s+l)/a].filter((function(t){return 0<=t&&t<=1}))},bboxoverlap:function(t,e){const n=["x","y"],r=n.length;for(let i,o,s,c,a=0;a<r;a++)if(i=n[a],o=t[i].mid,s=e[i].mid,c=(t[i].size+e[i].size)/2,m(o-s)>=c)return!1;return!0},expandbox:function(t,e){e.x.min<t.x.min&&(t.x.min=e.x.min),e.y.min<t.y.min&&(t.y.min=e.y.min),e.z&&e.z.min<t.z.min&&(t.z.min=e.z.min),e.x.max>t.x.max&&(t.x.max=e.x.max),e.y.max>t.y.max&&(t.y.max=e.y.max),e.z&&e.z.max>t.z.max&&(t.z.max=e.z.max),t.x.mid=(t.x.min+t.x.max)/2,t.y.mid=(t.y.min+t.y.max)/2,t.z&&(t.z.mid=(t.z.min+t.z.max)/2),t.x.size=t.x.max-t.x.min,t.y.size=t.y.max-t.y.min,t.z&&(t.z.size=t.z.max-t.z.min)},pairiteration:function(t,e,n){const r=t.bbox(),i=e.bbox(),o=1e5,s=n||.5;if(r.x.size+r.y.size<s&&i.x.size+i.y.size<s)return[(o*(t._t1+t._t2)/2|0)/o+"/"+(o*(e._t1+e._t2)/2|0)/o];let c=t.split(.5),a=e.split(.5),u=[{left:c.left,right:a.left},{left:c.left,right:a.right},{left:c.right,right:a.right},{left:c.right,right:a.left}];u=u.filter((function(t){return j.bboxoverlap(t.left.bbox(),t.right.bbox())}));let l=[];return 0===u.length||(u.forEach((function(t){l=l.concat(j.pairiteration(t.left,t.right,s))})),l=l.filter((function(t,e){return l.indexOf(t)===e}))),l},getccenter:function(t,e,n){const r=e.x-t.x,i=e.y-t.y,o=n.x-e.x,s=n.y-e.y,c=r*g(M)-i*v(M),a=r*v(M)+i*g(M),u=o*g(M)-s*v(M),l=o*v(M)+s*g(M),h=(t.x+e.x)/2,x=(t.y+e.y)/2,f=(e.x+n.x)/2,y=(e.y+n.y)/2,p=h+c,d=x+a,m=f+u,b=y+l,_=j.lli8(h,x,p,d,f,y,m,b),w=j.dist(_,t);let T,C=z(t.y-_.y,t.x-_.x),E=z(e.y-_.y,e.x-_.x),I=z(n.y-_.y,n.x-_.x);return C<I?((C>E||E>I)&&(C+=O),C>I&&(T=I,I=C,C=T)):I<E&&E<C?(T=I,I=C,C=T):I+=O,_.s=C,_.e=I,_.r=w,_},numberSort:function(t,e){return t-e}};class B{constructor(t){this.curves=[],this._3d=!1,t&&(this.curves=t,this._3d=this.curves[0]._3d)}valueOf(){return this.toString()}toString(){return"["+this.curves.map((function(t){return j.pointsToString(t.points)})).join(", ")+"]"}addCurve(t){this.curves.push(t),this._3d=this._3d||t._3d}length(){return this.curves.map((function(t){return t.length()})).reduce((function(t,e){return t+e}))}curve(t){return this.curves[t]}bbox(){const t=this.curves;for(var e=t[0].bbox(),n=1;n<t.length;n++)j.expandbox(e,t[n].bbox());return e}offset(t){const e=[];return this.curves.forEach((function(n){e.push(...n.offset(t))})),new B(e)}}const{abs:P,min:S,max:D,cos:R,sin:F,acos:N,sqrt:A}=Math,G=Math.PI;class H{constructor(t){let e=t&&t.forEach?t:Array.from(arguments).slice(),n=!1;if("object"==typeof e[0]){n=e.length;const t=[];e.forEach((function(e){["x","y","z"].forEach((function(n){void 0!==e[n]&&t.push(e[n])}))})),e=t}let r=!1;const i=e.length;if(n){if(n>4){if(1!==arguments.length)throw new Error("Only new Bezier(point[]) is accepted for 4th and higher order curves");r=!0}}else if(6!==i&&8!==i&&9!==i&&12!==i&&1!==arguments.length)throw new Error("Only new Bezier(point[]) is accepted for 4th and higher order curves");const o=this._3d=!r&&(9===i||12===i)||t&&t[0]&&void 0!==t[0].z,s=this.points=[];for(let t=0,n=o?3:2;t<i;t+=n){var c={x:e[t],y:e[t+1]};o&&(c.z=e[t+2]),s.push(c)}const a=this.order=s.length-1,u=this.dims=["x","y"];o&&u.push("z"),this.dimlen=u.length;const l=j.align(s,{p1:s[0],p2:s[a]}),h=j.dist(s[0],s[a]);this._linear=l.reduce(((t,e)=>t+P(e.y)),0)<h/50,this._lut=[],this._t1=0,this._t2=1,this.update()}static quadraticFromPoints(t,e,n,r){if(void 0===r&&(r=.5),0===r)return new H(e,e,n);if(1===r)return new H(t,e,e);const i=H.getABC(2,t,e,n,r);return new H(t,i.A,n)}static cubicFromPoints(t,e,n,r,i){void 0===r&&(r=.5);const o=H.getABC(3,t,e,n,r);void 0===i&&(i=j.dist(e,o.C));const s=i*(1-r)/r,c=j.dist(t,n),a=(n.x-t.x)/c,u=(n.y-t.y)/c,l=i*a,h=i*u,x=s*a,f=s*u,y=e.x-l,p=e.y-h,d=e.x+x,m=e.y+f,g=o.A,v=g.x+(y-g.x)/(1-r),b=g.y+(p-g.y)/(1-r),z=g.x+(d-g.x)/r,_=g.y+(m-g.y)/r,w={x:t.x+(v-t.x)/r,y:t.y+(b-t.y)/r},T={x:n.x+(z-n.x)/(1-r),y:n.y+(_-n.y)/(1-r)};return new H(t,w,T,n)}static getUtils(){return j}getUtils(){return H.getUtils()}static get PolyBezier(){return B}valueOf(){return this.toString()}toString(){return j.pointsToString(this.points)}toSVG(){if(this._3d)return!1;const t=this.points,e=["M",t[0].x,t[0].y,2===this.order?"Q":"C"];for(let n=1,r=t.length;n<r;n++)e.push(t[n].x),e.push(t[n].y);return e.join(" ")}setRatios(t){if(t.length!==this.points.length)throw new Error("incorrect number of ratio values");this.ratios=t,this._lut=[]}verify(){const t=this.coordDigest();t!==this._print&&(this._print=t,this.update())}coordDigest(){return this.points.map((function(t,e){return""+e+t.x+t.y+(t.z?t.z:0)})).join("")}update(){this._lut=[],this.dpoints=j.derive(this.points,this._3d),this.computedirection()}computedirection(){const t=this.points,e=j.angle(t[0],t[this.order],t[1]);this.clockwise=e>0}length(){return j.length(this.derivative.bind(this))}static getABC(t=2,e,n,r,i=.5){const o=j.projectionratio(i,t),s=1-o,c={x:o*e.x+s*r.x,y:o*e.y+s*r.y},a=j.abcratio(i,t);return{A:{x:n.x+(n.x-c.x)/a,y:n.y+(n.y-c.y)/a},B:n,C:c,S:e,E:r}}getABC(t,e){e=e||this.get(t);let n=this.points[0],r=this.points[this.order];return H.getABC(this.order,n,e,r,t)}getLUT(t){if(this.verify(),t=t||100,this._lut.length===t+1)return this._lut;this._lut=[],t++,this._lut=[];for(let e,n,r=0;r<t;r++)n=r/(t-1),e=this.compute(n),e.t=n,this._lut.push(e);return this._lut}on(e,n){n=n||5;const r=this.getLUT(),i=[];for(let t,o=0,s=0;o<r.length;o++)t=r[o],j.dist(t,e)<n&&(i.push(t),s+=o/r.length);return!!i.length&&(t/=i.length)}project(t){const e=this.getLUT(),n=e.length-1,r=j.closest(e,t),i=r.mpos,o=(i-1)/n,s=(i+1)/n,c=.1/n;let a,u=r.mdist,l=o,h=l;u+=1;for(let e;l<s+c;l+=c)a=this.compute(l),e=j.dist(t,a),e<u&&(u=e,h=l);return h=h<0?0:h>1?1:h,a=this.compute(h),a.t=h,a.d=u,a}get(t){return this.compute(t)}point(t){return this.points[t]}compute(t){return this.ratios?j.computeWithRatios(t,this.points,this.ratios,this._3d):j.compute(t,this.points,this._3d,this.ratios)}raise(){const t=this.points,e=[t[0]],n=t.length;for(let r,i,o=1;o<n;o++)r=t[o],i=t[o-1],e[o]={x:(n-o)/n*r.x+o/n*i.x,y:(n-o)/n*r.y+o/n*i.y};return e[n]=t[n-1],new H(e)}derivative(t){return j.compute(t,this.dpoints[0],this._3d)}dderivative(t){return j.compute(t,this.dpoints[1],this._3d)}align(){let t=this.points;return new H(j.align(t,{p1:t[0],p2:t[t.length-1]}))}curvature(t){return j.curvature(t,this.dpoints[0],this.dpoints[1],this._3d)}inflections(){return j.inflections(this.points)}normal(t){return this._3d?this.__normal3(t):this.__normal2(t)}__normal2(t){const e=this.derivative(t),n=A(e.x*e.x+e.y*e.y);return{t:t,x:-e.y/n,y:e.x/n}}__normal3(t){const e=this.derivative(t),n=this.derivative(t+.01),r=A(e.x*e.x+e.y*e.y+e.z*e.z),i=A(n.x*n.x+n.y*n.y+n.z*n.z);e.x/=r,e.y/=r,e.z/=r,n.x/=i,n.y/=i,n.z/=i;const o={x:n.y*e.z-n.z*e.y,y:n.z*e.x-n.x*e.z,z:n.x*e.y-n.y*e.x},s=A(o.x*o.x+o.y*o.y+o.z*o.z);o.x/=s,o.y/=s,o.z/=s;const c=[o.x*o.x,o.x*o.y-o.z,o.x*o.z+o.y,o.x*o.y+o.z,o.y*o.y,o.y*o.z-o.x,o.x*o.z-o.y,o.y*o.z+o.x,o.z*o.z];return{t:t,x:c[0]*e.x+c[1]*e.y+c[2]*e.z,y:c[3]*e.x+c[4]*e.y+c[5]*e.z,z:c[6]*e.x+c[7]*e.y+c[8]*e.z}}hull(t){let e=this.points,n=[],r=[],i=0;for(r[i++]=e[0],r[i++]=e[1],r[i++]=e[2],3===this.order&&(r[i++]=e[3]);e.length>1;){n=[];for(let o,s=0,c=e.length-1;s<c;s++)o=j.lerp(t,e[s],e[s+1]),r[i++]=o,n.push(o);e=n}return r}split(t,e){if(0===t&&e)return this.split(e).left;if(1===e)return this.split(t).right;const n=this.hull(t),r={left:2===this.order?new H([n[0],n[3],n[5]]):new H([n[0],n[4],n[7],n[9]]),right:2===this.order?new H([n[5],n[4],n[2]]):new H([n[9],n[8],n[6],n[3]]),span:n};return r.left._t1=j.map(0,0,1,this._t1,this._t2),r.left._t2=j.map(t,0,1,this._t1,this._t2),r.right._t1=j.map(t,0,1,this._t1,this._t2),r.right._t2=j.map(1,0,1,this._t1,this._t2),e?(e=j.map(e,t,1,0,1),r.right.split(e).left):r}extrema(){const t={};let e=[];return this.dims.forEach(function(n){let r=function(t){return t[n]},i=this.dpoints[0].map(r);t[n]=j.droots(i),3===this.order&&(i=this.dpoints[1].map(r),t[n]=t[n].concat(j.droots(i))),t[n]=t[n].filter((function(t){return t>=0&&t<=1})),e=e.concat(t[n].sort(j.numberSort))}.bind(this)),t.values=e.sort(j.numberSort).filter((function(t,n){return e.indexOf(t)===n})),t}bbox(){const t=this.extrema(),e={};return this.dims.forEach(function(n){e[n]=j.getminmax(this,n,t[n])}.bind(this)),e}overlaps(t){const e=this.bbox(),n=t.bbox();return j.bboxoverlap(e,n)}offset(t,e){if(void 0!==e){const n=this.get(t),r=this.normal(t),i={c:n,n:r,x:n.x+r.x*e,y:n.y+r.y*e};return this._3d&&(i.z=n.z+r.z*e),i}if(this._linear){const e=this.normal(0),n=this.points.map((function(n){const r={x:n.x+t*e.x,y:n.y+t*e.y};return n.z&&e.z&&(r.z=n.z+t*e.z),r}));return[new H(n)]}return this.reduce().map((function(e){return e._linear?e.offset(t)[0]:e.scale(t)}))}simple(){if(3===this.order){const t=j.angle(this.points[0],this.points[3],this.points[1]),e=j.angle(this.points[0],this.points[3],this.points[2]);if(t>0&&e<0||t<0&&e>0)return!1}const t=this.normal(0),e=this.normal(1);let n=t.x*e.x+t.y*e.y;return this._3d&&(n+=t.z*e.z),P(N(n))<G/3}reduce(){let t,e,n=0,r=0,i=.01,o=[],s=[],c=this.extrema().values;for(-1===c.indexOf(0)&&(c=[0].concat(c)),-1===c.indexOf(1)&&c.push(1),n=c[0],t=1;t<c.length;t++)r=c[t],e=this.split(n,r),e._t1=n,e._t2=r,o.push(e),n=r;return o.forEach((function(t){for(n=0,r=0;r<=1;)for(r=n+i;r<=1.01;r+=i)if(e=t.split(n,r),!e.simple()){if(r-=i,P(n-r)<i)return[];e=t.split(n,r),e._t1=j.map(n,0,1,t._t1,t._t2),e._t2=j.map(r,0,1,t._t1,t._t2),s.push(e),n=r;break}n<1&&(e=t.split(n,1),e._t1=j.map(n,0,1,t._t1,t._t2),e._t2=t._t2,s.push(e))})),s}translate(t,e,n){n="number"==typeof n?n:e;const r=this.order;let i=this.points.map(((t,i)=>(1-i/r)*e+i/r*n));return new H(this.points.map(((e,n)=>({x:e.x+t.x*i[n],y:e.y+t.y*i[n]}))))}scale(t){const e=this.order;let n=!1;if("function"==typeof t&&(n=t),n&&2===e)return this.raise().scale(n);const r=this.clockwise,i=this.points;if(this._linear)return this.translate(this.normal(0),n?n(0):t,n?n(1):t);const o=n?n(0):t,s=n?n(1):t,c=[this.offset(0,10),this.offset(1,10)],a=[],u=j.lli4(c[0],c[0].c,c[1],c[1].c);if(!u)throw new Error("cannot scale this curve. Try reducing it first.");return[0,1].forEach((function(t){const n=a[t*e]=j.copy(i[t*e]);n.x+=(t?s:o)*c[t].n.x,n.y+=(t?s:o)*c[t].n.y})),n?([0,1].forEach((function(o){if(2!==e||!o){var s=i[o+1],c={x:s.x-u.x,y:s.y-u.y},l=n?n((o+1)/e):t;n&&!r&&(l=-l);var h=A(c.x*c.x+c.y*c.y);c.x/=h,c.y/=h,a[o+1]={x:s.x+l*c.x,y:s.y+l*c.y}}})),new H(a)):([0,1].forEach((t=>{if(2===e&&t)return;const n=a[t*e],r=this.derivative(t),o={x:n.x+r.x,y:n.y+r.y};a[t+1]=j.lli4(n,o,u,i[t+1])})),new H(a))}outline(t,e,n,r){if(e=void 0===e?t:e,this._linear){const i=this.normal(0),o=this.points[0],s=this.points[this.points.length-1];let c,a,u;void 0===n&&(n=t,r=e),c={x:o.x+i.x*t,y:o.y+i.y*t},u={x:s.x+i.x*n,y:s.y+i.y*n},a={x:(c.x+u.x)/2,y:(c.y+u.y)/2};const l=[c,a,u];c={x:o.x-i.x*e,y:o.y-i.y*e},u={x:s.x-i.x*r,y:s.y-i.y*r},a={x:(c.x+u.x)/2,y:(c.y+u.y)/2};const h=[u,a,c],x=j.makeline(h[2],l[0]),f=j.makeline(l[2],h[0]),y=[x,new H(l),f,new H(h)];return new B(y)}const i=this.reduce(),o=i.length,s=[];let c,a=[],u=0,l=this.length();const h=void 0!==n&&void 0!==r;function x(t,e,n,r,i){return function(o){const s=r/n,c=(r+i)/n,a=e-t;return j.map(o,0,1,t+s*a,t+c*a)}}i.forEach((function(i){const o=i.length();h?(s.push(i.scale(x(t,n,l,u,o))),a.push(i.scale(x(-e,-r,l,u,o)))):(s.push(i.scale(t)),a.push(i.scale(-e))),u+=o})),a=a.map((function(t){return c=t.points,c[3]?t.points=[c[3],c[2],c[1],c[0]]:t.points=[c[2],c[1],c[0]],t})).reverse();const f=s[0].points[0],y=s[o-1].points[s[o-1].points.length-1],p=a[o-1].points[a[o-1].points.length-1],d=a[0].points[0],m=j.makeline(p,f),g=j.makeline(y,d),v=[m].concat(s).concat([g]).concat(a);return new B(v)}outlineshapes(t,e,n){e=e||t;const r=this.outline(t,e).curves,i=[];for(let t=1,e=r.length;t<e/2;t++){const o=j.makeshape(r[t],r[e-t],n);o.startcap.virtual=t>1,o.endcap.virtual=t<e/2-1,i.push(o)}return i}intersects(t,e){return t?t.p1&&t.p2?this.lineIntersects(t):(t instanceof H&&(t=t.reduce()),this.curveintersects(this.reduce(),t,e)):this.selfintersects(e)}lineIntersects(t){const e=S(t.p1.x,t.p2.x),n=S(t.p1.y,t.p2.y),r=D(t.p1.x,t.p2.x),i=D(t.p1.y,t.p2.y);return j.roots(this.points,t).filter((t=>{var o=this.get(t);return j.between(o.x,e,r)&&j.between(o.y,n,i)}))}selfintersects(t){const e=this.reduce(),n=e.length-2,r=[];for(let i,o,s,c=0;c<n;c++)o=e.slice(c,c+1),s=e.slice(c+2),i=this.curveintersects(o,s,t),r.push(...i);return r}curveintersects(t,e,n){const r=[];t.forEach((function(t){e.forEach((function(e){t.overlaps(e)&&r.push({left:t,right:e})}))}));let i=[];return r.forEach((function(t){const e=j.pairiteration(t.left,t.right,n);e.length>0&&(i=i.concat(e))})),i}arcs(t){return t=t||.5,this._iterate(t,[])}_error(t,e,n,r){const i=(r-n)/4,o=this.get(n+i),s=this.get(r-i),c=j.dist(t,e),a=j.dist(t,o),u=j.dist(t,s);return P(a-c)+P(u-c)}_iterate(t,e){let n,r=0,i=1;do{n=0,i=1;let o,s,c,a,u,l=this.get(r),h=!1,x=!1,f=i,y=1;do{if(x=h,a=c,f=(r+i)/2,o=this.get(f),s=this.get(i),c=j.getccenter(l,o,s),c.interval={start:r,end:i},h=this._error(c,l,r,i)<=t,u=x&&!h,u||(y=i),h){if(i>=1){if(c.interval.end=y=1,a=c,i>1){let t={x:c.x+c.r*R(c.e),y:c.y+c.r*F(c.e)};c.e+=j.angle({x:c.x,y:c.y},t,this.get(1))}break}i+=(i-r)/2}else i=f}while(!u&&n++<100);if(n>=100)break;a=a||c,e.push(a),r=y}while(i<1);return e}}const L=t=>{const e=[],n=(t,e,n,r)=>{if(t===e)return n/r;const i=1-(1/(1+n/r*(t/e-1))*t-e)/(t-e);return Math.min(1,Math.max(0,i))};return t.regionBoundaryCurves.forEach(((r,i)=>{const o=[];r.forEach(((e,r)=>{const s=[],c=e,a={tl:c.top.points[0],tr:c.top.points[3],bl:c.bottom.points[0],br:c.bottom.points[3]};if((t=>{const e=d.calcIntersection(t.tl,t.tr,t.bl,t.br),n=d.calcIntersection(t.tl,t.bl,t.tr,t.br),r=e=>e&&Object.values(t).some(((t,n,r)=>d.isTriangleContainsPoint(e,r[n],r[(n+1)%4],r[(n+2)%4])));return r(e)||r(n)})(a))throw new Error("[Warpvas: Perspective] Invalid perspective shape: The four control points cannot form a triangle or cross each other");const u={left:new H(c.left.points).length(),right:new H(c.right.points).length(),top:new H(c.top.points).length(),bottom:new H(c.bottom.points).length()},l=d.calcIntersection(a.tl,a.bl,a.tr,a.br),h=d.calcIntersection(a.tl,a.tr,a.bl,a.br),{vertical:x,horizontal:f}=t.regionCurves[i][r],y=f.length-1,p=x.length-1;for(let t=0;t<f.length;t++)for(let e=0;e<x.length;e++){let r=c.top.get(e/p),i=c.bottom.get(e/p);if(h){const t=d.calcCoordDistance(a.tl,h)/d.calcCoordDistance(a.tr,h),o=n(u.left,u.left/t,e,p);r=c.top.get(o);const s=d.calcCoordDistance(a.bl,h)/d.calcCoordDistance(a.br,h),l=n(u.left,u.left/s,e,p);i=c.bottom.get(l)}let o=c.left.get(t/y),x=c.right.get(t/y);if(l){const e=d.calcCoordDistance(a.tl,l)/d.calcCoordDistance(a.bl,l),r=n(u.top,u.top/e,t,y);o=c.left.get(r);const i=d.calcCoordDistance(a.tr,l)/d.calcCoordDistance(a.br,l),s=n(u.top,u.top/i,t,y);x=c.right.get(s)}const f=d.calcIntersection(r,i,o,x);f?s.push(f):s.push(r)}o.push(s)})),e.push(o)})),e};var U;!function(t){t.TOP_LEFT="tl",t.TOP_RIGHT="tr",t.BOTTOM_LEFT="bl",t.BOTTOM_RIGHT="br"}(U||(U={}));class V extends e{constructor(t={}){super(t),this.name="perspective",this.options={themeColor:n,subThemeColor:r,enableDragResize:!0,minimumDragThreshold:50,enableConstraintKey:"None"},this._objectControlMap=new Map([]),this.options=i(t,this.options)}static execute(t){return L(t)}get controlObjects(){return Array.from(this._objectControlMap.keys())}execute(t){return V.execute(t)}dirtyRender(t){var e;const n=t.canvas;if(!n)return;const r=null===(e=t.warpvas)||void 0===e?void 0:e.regionBoundaryCurves;if(!r)return;r.forEach(((e,r)=>{e.forEach(((e,i)=>{const o=e;Object.entries(o).forEach((([e])=>{var c,a,u;const l=o[e],h={top:U.TOP_LEFT,bottom:U.BOTTOM_RIGHT,right:U.TOP_RIGHT,left:U.BOTTOM_LEFT}[e],x=t.curvePathMap.get(l),f=l.points.map((t=>new s(t.x-x.pathOffset.x,t.y-x.pathOffset.y).transform(x.calcOwnMatrix()))),y=null!==(u=null===(a=(c=this._styleSetters).control)||void 0===a?void 0:a.call(c,this._createDefaultControl()))&&void 0!==u?u:this._createDefaultControl(),p={first:0,last:3}[{top:"first",right:"first",bottom:"last",left:"last"}[e]];y.set({left:f[p].x,top:f[p].y}),n.add(y),this._objectControlMap.set(y,{curve:l,targetPointIdx:p,rowIndex:r,colIndex:i,vertexType:h})}))}))}));const{enableDragResize:i,enableConstraintKey:a}=this.options,h=[["NONE"!==a.toUpperCase(),c(n,a)],[i,()=>{const e=e=>{if(1!==e.e.buttons)return;if(e.target&&e.target.selectable)return;const{minimumDragThreshold:r}=this.options;let i=!1;n.selection=!1;const o=l(n,e.viewportPoint),s=e=>{const s=l(n,e.viewportPoint);if(!i&&(Math.abs(s.x-o.x)<r||Math.abs(s.y-o.y)<r))return;const c=u({left:o.x,top:o.y},t.curvePathMap.get(t.warpvas.regionBoundaryCurves[0][0].top)),a=u({left:s.x,top:s.y},t.curvePathMap.get(t.warpvas.regionBoundaryCurves[0][0].bottom));t.warpvas.updateVertexCoord(0,0,U.TOP_LEFT,c),t.warpvas.updateVertexCoord(0,0,U.TOP_RIGHT,{x:a.x,y:c.y}),t.warpvas.updateVertexCoord(0,0,U.BOTTOM_LEFT,{x:c.x,y:a.y}),t.warpvas.updateVertexCoord(0,0,U.BOTTOM_RIGHT,a),t.render(!0,{skipHistoryRecording:!0}),i=!0},c=e=>{i&&t.record(),n.selection=!0,n.off("mouse:move",s),n.off("mouse:up",c)};n.on("mouse:move",s),n.on("mouse:up",c)};return n.on("mouse:down:before",e),()=>{n.off("mouse:down:before",e)}}],[!0,()=>{const e=e=>{if(!e.target)return;const r=this._objectControlMap.get(e.target);if(!r)return;const i=e.target,{curve:c,targetPointIdx:a,rowIndex:l,colIndex:h,vertexType:x}=r,f=t.curvePathMap.get(c),y=f.calcOwnMatrix(),p=u(i,f);try{t.warpvas.updateVertexCoord(l,h,x,p),t.render(!1,{skipHistoryRecording:!0})}catch(n){const r={x:i.left,y:i.top},p={x:e.transform.original.left,y:e.transform.original.top},d=o.calcCoordDistance(r,p),m=this.controlObjects.filter((t=>t!==i)).map(((t,e,n)=>{const i=t,s=n[(e+1)%n.length],c=[{x:i.left,y:i.top},{x:s.left,y:s.top}],a=[r,p],u=o.calcIntersection(c[0],c[1],a[0],a[1]);if(u&&!(o.calcCoordDistance(u,p)>d||o.calcCoordDistance(u,r)>d))return u})).filter(Boolean);m.sort(((t,e)=>o.calcCoordDistance(p,t)-o.calcCoordDistance(p,e)));const g=m[0],v=o.calcRelativeCoord(g,p,1);t.warpvas.updateVertexCoord(l,h,x,u({left:v.x,top:v.y},f)),t.render(!1,{skipHistoryRecording:!0});const b=c.points.map((t=>new s(t.x-f.pathOffset.x,t.y-f.pathOffset.y).transform(y)));i.set({left:b[a].x,top:b[a].y}),i.setCoords()}finally{this.controlObjects.forEach((t=>{n.bringObjectToFront(t)}))}},r=e=>{const n=e.target;n&&"drag"===e.transform.action&&this.controlObjects.includes(n)&&t.record()};return n.on("object:moving",e),n.on("object:modified",r),()=>{n.off("object:moving",e),n.off("object:modified",r)}}]],x=h.map((([t,e])=>t&&e()));return()=>{x.forEach((t=>{"function"==typeof t&&t()})),n.remove(...this._objectControlMap.keys()),this._objectControlMap.clear()}}_createDefaultControl(){return new a({radius:4,fill:this.options.themeColor,stroke:"#ffffff",paintFirst:"fill",strokeWidth:1,originX:"center",originY:"center",hasControls:!1,hasBorders:!1})}}export{U as VertexType,V as default};
//# sourceMappingURL=perspective.esm.js.map