UNPKG

fabric-warpvas

Version:

This JavaScript library helps you efficiently build image warping tools on Fabric.js

3 lines (2 loc) 28.9 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("fabric"),r=require("./base.class-QKOkESKk.js"),n=require("./register-limit-move-event-R3khX-xX.js");"function"==typeof SuppressedError&&SuppressedError;const i=(t,e,r)=>{if(t.x===e.x)return{x:t.x,y:r.y};if(t.y===e.y)return{x:r.x,y:t.y};const n=(e.y-t.y)/(e.x-t.x),i=-1/n,o=(-n*t.x+t.y+i*r.x-r.y)/(i-n);return{x:o,y:n*(o-t.x)+t.y}},o=(t,e)=>Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2)),s=(t,e,r)=>{const n=Object.assign({},t),i={x:e.x-t.x,y:e.y-t.y};if(0===i.x&&0===i.y)return n;if(0===i.x)n.y+=r*Math.sign(i.y);else if(0===i.y)n.x+=r*Math.sign(i.x);else{const t=Math.sqrt(Math.pow(i.x,2)+Math.pow(i.y,2));n.x+=i.x*(r/t),n.y+=i.y*(r/t)}return n};var c,a,u=Object.freeze({__proto__:null,calcBoundingBox:(t,e,r)=>{const n=Math.min(t.x,e.x,r.x),i=Math.max(t.x,e.x,r.x),o=Math.min(t.y,e.y,r.y);return{width:i-n,height:Math.max(t.y,e.y,r.y)-o}},calcCoordDistance:o,calcExpandCoord:(t,e,r,n=1)=>{const c=i(e,r,t),a=o(t,c),u=s(t,c,a+n);return{x:u.x-c.x+e.x,y:u.y-c.y+e.y}},calcIntersection:(t,e,r,n)=>{const i=(t,e)=>Math.abs(t-e)<Math.pow(.1,12);if(i(t.x,e.x)&&i(r.x,n.x))return null;if(i(t.y,e.y)&&i(r.y,n.y))return null;const o=1e4*(e.y-t.y)/Math.round(1e4*(e.x-t.x)),s=1e4*(n.y-r.y)/Math.round(1e4*(n.x-r.x)),c=t.y-o*t.x,a=r.y-s*r.x;if(Math.abs(o)===1/0)return{x:t.x,y:s*t.x+a};if(Math.abs(s)===1/0)return{x:r.x,y:o*r.x+c};const u=(a-c)/(o-s);return{x:u,y:o*u+c}},calcMatrix:(t,e)=>{const[r,n,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,r)=>{const[n,i,o,s]=t.map(Number),[c,a,u,l]=e.map(Number),[x,h,p,y]=r.map(Number),f=o-i*u/a,d=u-a*p/h,m=l-a*y/h,g=c-a*x/h,b=(f/d*m-(s-i*l/a))/(f/d*g-(n-i*c/a)),v=(m-g*b)/d;return{x:b,y:(s-n*b-o*v)/i,z:v}},l=[o.x+a,o.y+a,1,r.x+a],x=[s.x+a,s.y+a,1,n.x+a],h=[c.x+a,c.y+a,1,i.x+a],p=u(l,x,h);l[3]=r.y+a,x[3]=n.y+a,h[3]=i.y+a;const y=u(l,x,h),f=p.x,d=p.y,m=p.z;return[f,y.x,d,y.y,m,y.z]},calcPerpendicularIntersection:i,calcRelativeCoord:s,isTriangleContainsPoint:(t,e,r,n,i=.1)=>{const o=(t,e,r)=>Math.abs(t.x*(e.y-r.y)+e.x*(r.y-t.y)+r.x*(t.y-e.y))/2,s=o(e,r,n);return o(e,r,t)+o(r,n,t)+o(n,e,t)<=s+i}});!function(t){t.TOP_LEFT="tl",t.TOP_RIGHT="tr",t.BOTTOM_LEFT="bl",t.BOTTOM_RIGHT="br"}(c||(c={})),function(t){t.TOP="top",t.BOTTOM="bottom",t.LEFT="left",t.RIGHT="right"}(a||(a={}));const{abs:l,cos:x,sin:h,acos:p,atan2:y,sqrt:f,pow:d}=Math;function m(t){return t<0?-d(-t,1/3):d(t,1/3)}const g=Math.PI,b=2*g,v=g/2,z=Number.MAX_SAFE_INTEGER||9007199254740991,_=Number.MIN_SAFE_INTEGER||-9007199254740991,T={x:0,y:0,z:0},w={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 r=e(t);let n=r.x*r.x+r.y*r.y;return void 0!==r.z&&(n+=r.z*r.z),f(n)},compute:function(t,e,r){if(0===t)return e[0].t=0,e[0];const n=e.length-1;if(1===t)return e[n].t=1,e[n];const i=1-t;let o=e;if(0===n)return e[0].t=t,e[0];if(1===n){const e={x:i*o[0].x+t*o[1].x,y:i*o[0].y+t*o[1].y,t:t};return r&&(e.z=i*o[0].z+t*o[1].z),e}if(n<4){let e,s,c,a=i*i,u=t*t,l=0;2===n?(o=[o[0],o[1],o[2],T],e=a,s=i*t*2,c=u):3===n&&(e=a*i,s=a*t*3,c=i*u*3,l=t*u);const x={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 r&&(x.z=e*o[0].z+s*o[1].z+c*o[2].z+l*o[3].z),x}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,r,n){const i=1-t,o=r,s=e;let c,a=o[0],u=o[1],l=o[2],x=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:!!n&&(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:!!n&&(a*s[0].z+u*s[1].z+l*s[2].z)/c,t:t}):(a*=i,u*=1.5*i,l*=3*i,x*=t*t*t,4===s.length?(c=a+u+l+x,{x:(a*s[0].x+u*s[1].x+l*s[2].x+x*s[3].x)/c,y:(a*s[0].y+u*s[1].y+l*s[2].y+x*s[3].y)/c,z:!!n&&(a*s[0].z+u*s[1].z+l*s[2].z+x*s[3].z)/c,t:t}):void 0))},derive:function(t,e){const r=[];for(let n=t,i=n.length,o=i-1;i>1;i--,o--){const t=[];for(let r,i=0;i<o;i++)r={x:o*(n[i+1].x-n[i].x),y:o*(n[i+1].y-n[i].y)},e&&(r.z=o*(n[i+1].z-n[i].z)),t.push(r);r.push(t),n=t}return r},between:function(t,e,r){return e<=t&&t<=r||w.approximately(t,e)||w.approximately(t,r)},approximately:function(t,e,r){return l(t-e)<=(r||1e-6)},length:function(t){const e=w.Tvalues.length;let r=0;for(let n,i=0;i<e;i++)n=.5*w.Tvalues[i]+.5,r+=w.Cvalues[i]*w.arcfn(n,t);return.5*r},map:function(t,e,r,n,i){return n+(t-e)/(r-e)*(i-n)},lerp:function(t,e,r){const n={x:e.x+t*(r.x-e.x),y:e.y+t*(r.y-e.y)};return void 0!==e.z&&void 0!==r.z&&(n.z=e.z+t*(r.z-e.z)),n},pointToString:function(t){let e=t.x+"/"+t.y;return void 0!==t.z&&(e+="/"+t.z),e},pointsToString:function(t){return"["+t.map(w.pointToString).join(", ")+"]"},copy:function(t){return JSON.parse(JSON.stringify(t))},angle:function(t,e,r){const n=e.x-t.x,i=e.y-t.y,o=r.x-t.x,s=r.y-t.y;return y(n*s-i*o,n*o+i*s)},round:function(t,e){const r=""+t,n=r.indexOf(".");return parseFloat(r.substring(0,n+1+e))},dist:function(t,e){const r=t.x-e.x,n=t.y-e.y;return f(r*r+n*n)},closest:function(t,e){let r,n,i=d(2,63);return t.forEach((function(t,o){n=w.dist(e,t),n<i&&(i=n,r=o)})),{mdist:i,mpos:r}},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 r=d(t,e)+d(1-t,e);return l((r-1)/r)},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 r=d(1-t,e);return r/(d(t,e)+r)},lli8:function(t,e,r,n,i,o,s,c){const a=(t-r)*(o-c)-(e-n)*(i-s);return 0!=a&&{x:((t*n-e*r)*(i-s)-(t-r)*(i*c-o*s))/a,y:((t*n-e*r)*(o-c)-(e-n)*(i*c-o*s))/a}},lli4:function(t,e,r,n){const i=t.x,o=t.y,s=e.x,c=e.y,a=r.x,u=r.y,l=n.x,x=n.y;return w.lli8(i,o,s,c,a,u,l,x)},lli:function(t,e){return w.lli4(t,t.c,e,e.c)},makeline:function(t,e){return new R(t.x,t.y,(t.x+e.x)/2,(t.y+e.y)/2,e.x,e.y)},findbbox:function(t){let e=z,r=z,n=_,i=_;return t.forEach((function(t){const o=t.bbox();e>o.x.min&&(e=o.x.min),r>o.y.min&&(r=o.y.min),n<o.x.max&&(n=o.x.max),i<o.y.max&&(i=o.y.max)})),{x:{min:e,mid:(e+n)/2,max:n,size:n-e},y:{min:r,mid:(r+i)/2,max:i,size:i-r}}},shapeintersections:function(t,e,r,n,i){if(!w.bboxoverlap(e,n))return[];const o=[],s=[t.startcap,t.forward,t.back,t.endcap],c=[r.startcap,r.forward,r.back,r.endcap];return s.forEach((function(e){e.virtual||c.forEach((function(n){if(n.virtual)return;const s=e.intersects(n,i);s.length>0&&(s.c1=e,s.c2=n,s.s1=t,s.s2=r,o.push(s))}))})),o},makeshape:function(t,e,r){const n=e.points.length,i=t.points.length,o=w.makeline(e.points[n-1],t.points[0]),s=w.makeline(t.points[i-1],e.points[0]),c={startcap:o,forward:t,back:e,endcap:s,bbox:w.findbbox([o,t,e,s]),intersections:function(t){return w.shapeintersections(c,c.bbox,t,t.bbox,r)}};return c},getminmax:function(t,e,r){if(!r)return{min:0,max:0};let n,i,o=z,s=_;-1===r.indexOf(0)&&(r=[0].concat(r)),-1===r.indexOf(1)&&r.push(1);for(let c=0,a=r.length;c<a;c++)n=r[c],i=t.get(n),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 r=e.p1.x,n=e.p1.y,i=-y(e.p2.y-n,e.p2.x-r);return t.map((function(t){return{x:(t.x-r)*x(i)-(t.y-n)*h(i),y:(t.x-r)*h(i)+(t.y-n)*x(i)}}))},roots:function(t,e){e=e||{p1:{x:0,y:0},p2:{x:1,y:0}};const r=t.length-1,n=w.align(t,e),i=function(t){return 0<=t&&t<=1};if(2===r){const t=n[0].y,e=n[1].y,r=n[2].y,o=t-2*e+r;if(0!==o){const n=-f(e*e-t*r),s=-t+e;return[-(n+s)/o,-(-n+s)/o].filter(i)}return e!==r&&0===o?[(2*e-r)/(2*e-2*r)].filter(i):[]}const o=n[0].y,s=n[1].y,c=n[2].y;let a=3*s-o-3*c+n[3].y,u=3*o-6*s+3*c,l=-3*o+3*s,h=o;if(w.approximately(a,0)){if(w.approximately(u,0))return w.approximately(l,0)?[]:[-h/l].filter(i);const t=f(l*l-4*u*h),e=2*u;return[(t-l)/e,(-l-t)/e].filter(i)}u/=a,l/=a,h/=a;const y=(3*l-u*u)/3,d=y/3,g=(2*u*u*u-9*u*l+27*h)/27,v=g/2,z=v*v+d*d*d;let _,T,O,C,M;if(z<0){const t=-y/3,e=f(t*t*t),r=-g/(2*e),n=p(r<-1?-1:r>1?1:r),o=2*m(e);return O=o*x(n/3)-u/3,C=o*x((n+b)/3)-u/3,M=o*x((n+2*b)/3)-u/3,[O,C,M].filter(i)}if(0===z)return _=v<0?m(-v):-m(v),O=2*_-u/3,C=-_-u/3,[O,C].filter(i);{const t=f(z);return _=m(-v+t),T=m(v+t),[_-T-u/3].filter(i)}},droots:function(t){if(3===t.length){const e=t[0],r=t[1],n=t[2],i=e-2*r+n;if(0!==i){const t=-f(r*r-e*n),o=-e+r;return[-(t+o)/i,-(-t+o)/i]}return r!==n&&0===i?[(2*r-n)/(2*(r-n))]:[]}if(2===t.length){const e=t[0],r=t[1];return e!==r?[e/(e-r)]:[]}return[]},curvature:function(t,e,r,n,i){let o,s,c,a,u=0,x=0;const h=w.compute(t,e),p=w.compute(t,r),y=h.x*h.x+h.y*h.y;if(n?(o=f(d(h.y*p.z-p.y*h.z,2)+d(h.z*p.x-p.z*h.x,2)+d(h.x*p.y-p.x*h.y,2)),s=d(y+h.z*h.z,1.5)):(o=h.x*p.y-h.y*p.x,s=d(y,1.5)),0===o||0===s)return{k:0,r:0};if(u=o/s,x=s/o,!i){const i=w.curvature(t-.001,e,r,n,!0).k,o=w.curvature(t+.001,e,r,n,!0).k;a=(o-u+(u-i))/2,c=(l(o-u)+l(u-i))/2}return{k:u,r:x,dk:a,adk:c}},inflections:function(t){if(t.length<4)return[];const e=w.align(t,{p1:t[0],p2:t.slice(-1)[0]}),r=e[2].x*e[1].y,n=e[3].x*e[1].y,i=e[1].x*e[2].y,o=18*(-3*r+2*n+3*i-e[3].x*e[2].y),s=18*(3*r-n-3*i),c=18*(i-r);if(w.approximately(o,0)){if(!w.approximately(s,0)){let t=-c/s;if(0<=t&&t<=1)return[t]}return[]}const a=2*o;if(w.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 r=["x","y"],n=r.length;for(let i,o,s,c,a=0;a<n;a++)if(i=r[a],o=t[i].mid,s=e[i].mid,c=(t[i].size+e[i].size)/2,l(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,r){const n=t.bbox(),i=e.bbox(),o=1e5,s=r||.5;if(n.x.size+n.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 w.bboxoverlap(t.left.bbox(),t.right.bbox())}));let l=[];return 0===u.length||(u.forEach((function(t){l=l.concat(w.pairiteration(t.left,t.right,s))})),l=l.filter((function(t,e){return l.indexOf(t)===e}))),l},getccenter:function(t,e,r){const n=e.x-t.x,i=e.y-t.y,o=r.x-e.x,s=r.y-e.y,c=n*x(v)-i*h(v),a=n*h(v)+i*x(v),u=o*x(v)-s*h(v),l=o*h(v)+s*x(v),p=(t.x+e.x)/2,f=(t.y+e.y)/2,d=(e.x+r.x)/2,m=(e.y+r.y)/2,g=p+c,z=f+a,_=d+u,T=m+l,O=w.lli8(p,f,g,z,d,m,_,T),C=w.dist(O,t);let M,E=y(t.y-O.y,t.x-O.x),k=y(e.y-O.y,e.x-O.x),I=y(r.y-O.y,r.x-O.x);return E<I?((E>k||k>I)&&(E+=b),E>I&&(M=I,I=E,E=M)):I<k&&k<E?(M=I,I=E,E=M):I+=b,O.s=E,O.e=I,O.r=C,O},numberSort:function(t,e){return t-e}};class O{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 w.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(),r=1;r<t.length;r++)w.expandbox(e,t[r].bbox());return e}offset(t){const e=[];return this.curves.forEach((function(r){e.push(...r.offset(t))})),new O(e)}}const{abs:C,min:M,max:E,cos:k,sin:I,acos:j,sqrt:P}=Math,B=Math.PI;class R{constructor(t){let e=t&&t.forEach?t:Array.from(arguments).slice(),r=!1;if("object"==typeof e[0]){r=e.length;const t=[];e.forEach((function(e){["x","y","z"].forEach((function(r){void 0!==e[r]&&t.push(e[r])}))})),e=t}let n=!1;const i=e.length;if(r){if(r>4){if(1!==arguments.length)throw new Error("Only new Bezier(point[]) is accepted for 4th and higher order curves");n=!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=!n&&(9===i||12===i)||t&&t[0]&&void 0!==t[0].z,s=this.points=[];for(let t=0,r=o?3:2;t<i;t+=r){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=w.align(s,{p1:s[0],p2:s[a]}),x=w.dist(s[0],s[a]);this._linear=l.reduce(((t,e)=>t+C(e.y)),0)<x/50,this._lut=[],this._t1=0,this._t2=1,this.update()}static quadraticFromPoints(t,e,r,n){if(void 0===n&&(n=.5),0===n)return new R(e,e,r);if(1===n)return new R(t,e,e);const i=R.getABC(2,t,e,r,n);return new R(t,i.A,r)}static cubicFromPoints(t,e,r,n,i){void 0===n&&(n=.5);const o=R.getABC(3,t,e,r,n);void 0===i&&(i=w.dist(e,o.C));const s=i*(1-n)/n,c=w.dist(t,r),a=(r.x-t.x)/c,u=(r.y-t.y)/c,l=i*a,x=i*u,h=s*a,p=s*u,y=e.x-l,f=e.y-x,d=e.x+h,m=e.y+p,g=o.A,b=g.x+(y-g.x)/(1-n),v=g.y+(f-g.y)/(1-n),z=g.x+(d-g.x)/n,_=g.y+(m-g.y)/n,T={x:t.x+(b-t.x)/n,y:t.y+(v-t.y)/n},O={x:r.x+(z-r.x)/(1-n),y:r.y+(_-r.y)/(1-n)};return new R(t,T,O,r)}static getUtils(){return w}getUtils(){return R.getUtils()}static get PolyBezier(){return O}valueOf(){return this.toString()}toString(){return w.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 r=1,n=t.length;r<n;r++)e.push(t[r].x),e.push(t[r].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=w.derive(this.points,this._3d),this.computedirection()}computedirection(){const t=this.points,e=w.angle(t[0],t[this.order],t[1]);this.clockwise=e>0}length(){return w.length(this.derivative.bind(this))}static getABC(t=2,e,r,n,i=.5){const o=w.projectionratio(i,t),s=1-o,c={x:o*e.x+s*n.x,y:o*e.y+s*n.y},a=w.abcratio(i,t);return{A:{x:r.x+(r.x-c.x)/a,y:r.y+(r.y-c.y)/a},B:r,C:c,S:e,E:n}}getABC(t,e){e=e||this.get(t);let r=this.points[0],n=this.points[this.order];return R.getABC(this.order,r,e,n,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,r,n=0;n<t;n++)r=n/(t-1),e=this.compute(r),e.t=r,this._lut.push(e);return this._lut}on(e,r){r=r||5;const n=this.getLUT(),i=[];for(let t,o=0,s=0;o<n.length;o++)t=n[o],w.dist(t,e)<r&&(i.push(t),s+=o/n.length);return!!i.length&&(t/=i.length)}project(t){const e=this.getLUT(),r=e.length-1,n=w.closest(e,t),i=n.mpos,o=(i-1)/r,s=(i+1)/r,c=.1/r;let a,u=n.mdist,l=o,x=l;u+=1;for(let e;l<s+c;l+=c)a=this.compute(l),e=w.dist(t,a),e<u&&(u=e,x=l);return x=x<0?0:x>1?1:x,a=this.compute(x),a.t=x,a.d=u,a}get(t){return this.compute(t)}point(t){return this.points[t]}compute(t){return this.ratios?w.computeWithRatios(t,this.points,this.ratios,this._3d):w.compute(t,this.points,this._3d,this.ratios)}raise(){const t=this.points,e=[t[0]],r=t.length;for(let n,i,o=1;o<r;o++)n=t[o],i=t[o-1],e[o]={x:(r-o)/r*n.x+o/r*i.x,y:(r-o)/r*n.y+o/r*i.y};return e[r]=t[r-1],new R(e)}derivative(t){return w.compute(t,this.dpoints[0],this._3d)}dderivative(t){return w.compute(t,this.dpoints[1],this._3d)}align(){let t=this.points;return new R(w.align(t,{p1:t[0],p2:t[t.length-1]}))}curvature(t){return w.curvature(t,this.dpoints[0],this.dpoints[1],this._3d)}inflections(){return w.inflections(this.points)}normal(t){return this._3d?this.__normal3(t):this.__normal2(t)}__normal2(t){const e=this.derivative(t),r=P(e.x*e.x+e.y*e.y);return{t:t,x:-e.y/r,y:e.x/r}}__normal3(t){const e=this.derivative(t),r=this.derivative(t+.01),n=P(e.x*e.x+e.y*e.y+e.z*e.z),i=P(r.x*r.x+r.y*r.y+r.z*r.z);e.x/=n,e.y/=n,e.z/=n,r.x/=i,r.y/=i,r.z/=i;const o={x:r.y*e.z-r.z*e.y,y:r.z*e.x-r.x*e.z,z:r.x*e.y-r.y*e.x},s=P(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,r=[],n=[],i=0;for(n[i++]=e[0],n[i++]=e[1],n[i++]=e[2],3===this.order&&(n[i++]=e[3]);e.length>1;){r=[];for(let o,s=0,c=e.length-1;s<c;s++)o=w.lerp(t,e[s],e[s+1]),n[i++]=o,r.push(o);e=r}return n}split(t,e){if(0===t&&e)return this.split(e).left;if(1===e)return this.split(t).right;const r=this.hull(t),n={left:2===this.order?new R([r[0],r[3],r[5]]):new R([r[0],r[4],r[7],r[9]]),right:2===this.order?new R([r[5],r[4],r[2]]):new R([r[9],r[8],r[6],r[3]]),span:r};return n.left._t1=w.map(0,0,1,this._t1,this._t2),n.left._t2=w.map(t,0,1,this._t1,this._t2),n.right._t1=w.map(t,0,1,this._t1,this._t2),n.right._t2=w.map(1,0,1,this._t1,this._t2),e?(e=w.map(e,t,1,0,1),n.right.split(e).left):n}extrema(){const t={};let e=[];return this.dims.forEach(function(r){let n=function(t){return t[r]},i=this.dpoints[0].map(n);t[r]=w.droots(i),3===this.order&&(i=this.dpoints[1].map(n),t[r]=t[r].concat(w.droots(i))),t[r]=t[r].filter((function(t){return t>=0&&t<=1})),e=e.concat(t[r].sort(w.numberSort))}.bind(this)),t.values=e.sort(w.numberSort).filter((function(t,r){return e.indexOf(t)===r})),t}bbox(){const t=this.extrema(),e={};return this.dims.forEach(function(r){e[r]=w.getminmax(this,r,t[r])}.bind(this)),e}overlaps(t){const e=this.bbox(),r=t.bbox();return w.bboxoverlap(e,r)}offset(t,e){if(void 0!==e){const r=this.get(t),n=this.normal(t),i={c:r,n:n,x:r.x+n.x*e,y:r.y+n.y*e};return this._3d&&(i.z=r.z+n.z*e),i}if(this._linear){const e=this.normal(0),r=this.points.map((function(r){const n={x:r.x+t*e.x,y:r.y+t*e.y};return r.z&&e.z&&(n.z=r.z+t*e.z),n}));return[new R(r)]}return this.reduce().map((function(e){return e._linear?e.offset(t)[0]:e.scale(t)}))}simple(){if(3===this.order){const t=w.angle(this.points[0],this.points[3],this.points[1]),e=w.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 r=t.x*e.x+t.y*e.y;return this._3d&&(r+=t.z*e.z),C(j(r))<B/3}reduce(){let t,e,r=0,n=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),r=c[0],t=1;t<c.length;t++)n=c[t],e=this.split(r,n),e._t1=r,e._t2=n,o.push(e),r=n;return o.forEach((function(t){for(r=0,n=0;n<=1;)for(n=r+i;n<=1.01;n+=i)if(e=t.split(r,n),!e.simple()){if(n-=i,C(r-n)<i)return[];e=t.split(r,n),e._t1=w.map(r,0,1,t._t1,t._t2),e._t2=w.map(n,0,1,t._t1,t._t2),s.push(e),r=n;break}r<1&&(e=t.split(r,1),e._t1=w.map(r,0,1,t._t1,t._t2),e._t2=t._t2,s.push(e))})),s}translate(t,e,r){r="number"==typeof r?r:e;const n=this.order;let i=this.points.map(((t,i)=>(1-i/n)*e+i/n*r));return new R(this.points.map(((e,r)=>({x:e.x+t.x*i[r],y:e.y+t.y*i[r]}))))}scale(t){const e=this.order;let r=!1;if("function"==typeof t&&(r=t),r&&2===e)return this.raise().scale(r);const n=this.clockwise,i=this.points;if(this._linear)return this.translate(this.normal(0),r?r(0):t,r?r(1):t);const o=r?r(0):t,s=r?r(1):t,c=[this.offset(0,10),this.offset(1,10)],a=[],u=w.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 r=a[t*e]=w.copy(i[t*e]);r.x+=(t?s:o)*c[t].n.x,r.y+=(t?s:o)*c[t].n.y})),r?([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=r?r((o+1)/e):t;r&&!n&&(l=-l);var x=P(c.x*c.x+c.y*c.y);c.x/=x,c.y/=x,a[o+1]={x:s.x+l*c.x,y:s.y+l*c.y}}})),new R(a)):([0,1].forEach((t=>{if(2===e&&t)return;const r=a[t*e],n=this.derivative(t),o={x:r.x+n.x,y:r.y+n.y};a[t+1]=w.lli4(r,o,u,i[t+1])})),new R(a))}outline(t,e,r,n){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===r&&(r=t,n=e),c={x:o.x+i.x*t,y:o.y+i.y*t},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 l=[c,a,u];c={x:o.x-i.x*e,y:o.y-i.y*e},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 x=[u,a,c],h=w.makeline(x[2],l[0]),p=w.makeline(l[2],x[0]),y=[h,new R(l),p,new R(x)];return new O(y)}const i=this.reduce(),o=i.length,s=[];let c,a=[],u=0,l=this.length();const x=void 0!==r&&void 0!==n;function h(t,e,r,n,i){return function(o){const s=n/r,c=(n+i)/r,a=e-t;return w.map(o,0,1,t+s*a,t+c*a)}}i.forEach((function(i){const o=i.length();x?(s.push(i.scale(h(t,r,l,u,o))),a.push(i.scale(h(-e,-n,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 p=s[0].points[0],y=s[o-1].points[s[o-1].points.length-1],f=a[o-1].points[a[o-1].points.length-1],d=a[0].points[0],m=w.makeline(f,p),g=w.makeline(y,d),b=[m].concat(s).concat([g]).concat(a);return new O(b)}outlineshapes(t,e,r){e=e||t;const n=this.outline(t,e).curves,i=[];for(let t=1,e=n.length;t<e/2;t++){const o=w.makeshape(n[t],n[e-t],r);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 R&&(t=t.reduce()),this.curveintersects(this.reduce(),t,e)):this.selfintersects(e)}lineIntersects(t){const e=M(t.p1.x,t.p2.x),r=M(t.p1.y,t.p2.y),n=E(t.p1.x,t.p2.x),i=E(t.p1.y,t.p2.y);return w.roots(this.points,t).filter((t=>{var o=this.get(t);return w.between(o.x,e,n)&&w.between(o.y,r,i)}))}selfintersects(t){const e=this.reduce(),r=e.length-2,n=[];for(let i,o,s,c=0;c<r;c++)o=e.slice(c,c+1),s=e.slice(c+2),i=this.curveintersects(o,s,t),n.push(...i);return n}curveintersects(t,e,r){const n=[];t.forEach((function(t){e.forEach((function(e){t.overlaps(e)&&n.push({left:t,right:e})}))}));let i=[];return n.forEach((function(t){const e=w.pairiteration(t.left,t.right,r);e.length>0&&(i=i.concat(e))})),i}arcs(t){return t=t||.5,this._iterate(t,[])}_error(t,e,r,n){const i=(n-r)/4,o=this.get(r+i),s=this.get(n-i),c=w.dist(t,e),a=w.dist(t,o),u=w.dist(t,s);return C(a-c)+C(u-c)}_iterate(t,e){let r,n=0,i=1;do{r=0,i=1;let o,s,c,a,u,l=this.get(n),x=!1,h=!1,p=i,y=1;do{if(h=x,a=c,p=(n+i)/2,o=this.get(p),s=this.get(i),c=w.getccenter(l,o,s),c.interval={start:n,end:i},x=this._error(c,l,n,i)<=t,u=h&&!x,u||(y=i),x){if(i>=1){if(c.interval.end=y=1,a=c,i>1){let t={x:c.x+c.r*k(c.e),y:c.y+c.r*I(c.e)};c.e+=w.angle({x:c.x,y:c.y},t,this.get(1))}break}i+=(i-n)/2}else i=p}while(!u&&r++<100);if(r>=100)break;a=a||c,e.push(a),n=y}while(i<1);return e}}const S=t=>{const e=[],r=(t,e,r,n)=>{if(t===e)return r/n;const i=1-(1/(1+r/n*(t/e-1))*t-e)/(t-e);return Math.min(1,Math.max(0,i))};return t.regionBoundaryCurves.forEach(((n,i)=>{const o=[];n.forEach(((e,n)=>{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=u.calcIntersection(t.tl,t.tr,t.bl,t.br),r=u.calcIntersection(t.tl,t.bl,t.tr,t.br),n=e=>e&&Object.values(t).some(((t,r,n)=>u.isTriangleContainsPoint(e,n[r],n[(r+1)%4],n[(r+2)%4])));return n(e)||n(r)})(a))throw new Error("[Warpvas: Perspective] Invalid perspective shape: The four control points cannot form a triangle or cross each other");const l={left:new R(c.left.points).length(),right:new R(c.right.points).length(),top:new R(c.top.points).length(),bottom:new R(c.bottom.points).length()},x=u.calcIntersection(a.tl,a.bl,a.tr,a.br),h=u.calcIntersection(a.tl,a.tr,a.bl,a.br),{vertical:p,horizontal:y}=t.regionCurves[i][n],f=y.length-1,d=p.length-1;for(let t=0;t<y.length;t++)for(let e=0;e<p.length;e++){let n=c.top.get(e/d),i=c.bottom.get(e/d);if(h){const t=u.calcCoordDistance(a.tl,h)/u.calcCoordDistance(a.tr,h),o=r(l.left,l.left/t,e,d);n=c.top.get(o);const s=u.calcCoordDistance(a.bl,h)/u.calcCoordDistance(a.br,h),x=r(l.left,l.left/s,e,d);i=c.bottom.get(x)}let o=c.left.get(t/f),p=c.right.get(t/f);if(x){const e=u.calcCoordDistance(a.tl,x)/u.calcCoordDistance(a.bl,x),n=r(l.top,l.top/e,t,f);o=c.left.get(n);const i=u.calcCoordDistance(a.tr,x)/u.calcCoordDistance(a.br,x),s=r(l.top,l.top/i,t,f);p=c.right.get(s)}const y=u.calcIntersection(n,i,o,p);y?s.push(y):s.push(n)}o.push(s)})),e.push(o)})),e};var F;exports.VertexType=void 0,(F=exports.VertexType||(exports.VertexType={})).TOP_LEFT="tl",F.TOP_RIGHT="tr",F.BOTTOM_LEFT="bl",F.BOTTOM_RIGHT="br";class V extends r.BaseMode{constructor(t={}){super(t),this.name="perspective",this.options={themeColor:r.THEME_COLOR,subThemeColor:r.SUB_THEME_COLOR,enableDragResize:!0,minimumDragThreshold:50,enableConstraintKey:"None"},this._objectControlMap=new Map([]),this._styleSetters={image:()=>{},path:()=>{},control:t=>t},this.options=r.defaults(t,this.options)}static execute(t){return S(t)}get controlObjects(){return Array.from(this._objectControlMap.keys())}execute(t){return V.execute(t)}dirtyRender(t){var r;const i=t.canvas;if(!i)return;const o=null===(r=t.warpvas)||void 0===r?void 0:r.regionBoundaryCurves;if(!o)return;o.forEach(((r,n)=>{r.forEach(((r,o)=>{const s=r;Object.entries(s).forEach((([r])=>{const c=s[r],a={top:exports.VertexType.TOP_LEFT,bottom:exports.VertexType.BOTTOM_RIGHT,right:exports.VertexType.TOP_RIGHT,left:exports.VertexType.BOTTOM_LEFT}[r],u=t.curvePathMap.get(c),l=c.points.map((t=>e.fabric.util.transformPoint(new e.fabric.Point(t.x-u.pathOffset.x,t.y-u.pathOffset.y),u.calcOwnMatrix()))),x=this._styleSetters.control(this._createDefaultControl()),h={first:0,last:3}[{top:"first",right:"first",bottom:"last",left:"last"}[r]];x.set({left:l[h].x,top:l[h].y}),i.add(x),this._objectControlMap.set(x,{curve:c,targetPointIdx:h,rowIndex:n,colIndex:o,vertexType:a})}))}))}));const{enableDragResize:s,enableConstraintKey:c}=this.options,a=[["NONE"!==c.toUpperCase(),n.registerLimitMoveEvent(i,c)],[s,()=>{const e=e=>{if(1!==e.e.buttons)return;if(e.target&&e.target.selectable)return;const{minimumDragThreshold:r}=this.options;let o=!1;i.selection=!1;const s=n.calcFabricCanvasCoord(i,e.pointer),c=e=>{const c=n.calcFabricCanvasCoord(i,e.pointer);if(!o&&(Math.abs(c.x-s.x)<r||Math.abs(c.y-s.y)<r))return;const a=n.calcFabricRelativeCoord({left:s.x,top:s.y},t.curvePathMap.get(t.warpvas.regionBoundaryCurves[0][0].top)),u=n.calcFabricRelativeCoord({left:c.x,top:c.y},t.curvePathMap.get(t.warpvas.regionBoundaryCurves[0][0].bottom));t.warpvas.updateVertexCoord(0,0,exports.VertexType.TOP_LEFT,a),t.warpvas.updateVertexCoord(0,0,exports.VertexType.TOP_RIGHT,{x:u.x,y:a.y}),t.warpvas.updateVertexCoord(0,0,exports.VertexType.BOTTOM_LEFT,{x:a.x,y:u.y}),t.warpvas.updateVertexCoord(0,0,exports.VertexType.BOTTOM_RIGHT,u),t.render(!0,{skipHistoryRecording:!0}),o=!0},a=e=>{o&&t.record(),i.selection=!0,i.off("mouse:move",c),i.off("mouse:up",a)};i.on("mouse:move",c),i.on("mouse:up",a)};return i.on("mouse:down:before",e),()=>{i.off("mouse:down:before",e)}}],[!0,()=>{const r=r=>{if(!r.target)return;const o=this._objectControlMap.get(r.target);if(!o)return;const s=r.target,{curve:c,targetPointIdx:a,rowIndex:u,colIndex:l,vertexType:x}=o,h=t.curvePathMap.get(c),p=h.calcOwnMatrix(),y=n.calcFabricRelativeCoord(s,h),f=Object.assign({},c.points[a]);try{t.warpvas.updateVertexCoord(u,l,x,y),t.render(!1,{skipHistoryRecording:!0})}catch(r){t.warpvas.updateVertexCoord(u,l,x,f),t.render(!1,{skipHistoryRecording:!0});const n=c.points.map((t=>e.fabric.util.transformPoint(new e.fabric.Point(t.x-h.pathOffset.x,t.y-h.pathOffset.y),p)));s.set({left:n[a].x,top:n[a].y}),s.setCoords()}finally{this.controlObjects.forEach((t=>{i.bringToFront(t)}))}},o=e=>{const r=e.target;r&&"drag"===e.action&&this.controlObjects.includes(r)&&t.record()};return i.on("object:moving",r),i.on("object:modified",o),()=>{i.off("object:moving",r),i.off("object:modified",o)}}]],u=a.map((([t,e])=>t&&e()));return()=>{u.forEach((t=>{"function"==typeof t&&t()})),i.remove(...this._objectControlMap.keys()),this._objectControlMap.clear()}}_createDefaultControl(){return new e.fabric.Circle({radius:4,fill:this.options.themeColor,stroke:"#ffffff",paintFirst:"fill",strokeWidth:1,originX:"center",originY:"center",hasControls:!1,hasBorders:!1})}}exports.default=V; //# sourceMappingURL=perspective.js.map