fabric-warpvas
Version:
This JavaScript library helps you efficiently build image warping tools on Fabric.js
3 lines (2 loc) • 28.5 kB
JavaScript
import{fabric as e}from"fabric";import{B as n,T as r,S as i,d as o}from"./base.class-_LMTQ4WF.js";import{r as s,c,a}from"./register-limit-move-event-MAdqicvl.js";"function"==typeof SuppressedError&&SuppressedError;const u=(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}},l=(t,e)=>Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2)),h=(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 x,f,y=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:l,calcExpandCoord:(t,e,n,r=1)=>{const i=u(e,n,t),o=l(t,i),s=h(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,b=(p/d*m-(s-i*l/a))/(p/d*g-(r-i*c/a)),v=(m-g*b)/d;return{x:b,y:(s-r*b-o*v)/i,z:v}},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:u,calcRelativeCoord:h,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"}(x||(x={})),function(t){t.TOP="top",t.BOTTOM="bottom",t.LEFT="left",t.RIGHT="right"}(f||(f={}));const{abs:p,cos:d,sin:m,acos:g,atan2:b,sqrt:v,pow:z}=Math;function _(t){return t<0?-z(-t,1/3):z(t,1/3)}const w=Math.PI,T=2*w,O=w/2,M=Number.MAX_SAFE_INTEGER||9007199254740991,C=Number.MIN_SAFE_INTEGER||-9007199254740991,E={x:0,y:0,z:0},k={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),v(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],E],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||k.approximately(t,e)||k.approximately(t,n)},approximately:function(t,e,n){return p(t-e)<=(n||1e-6)},length:function(t){const e=k.Tvalues.length;let n=0;for(let r,i=0;i<e;i++)r=.5*k.Tvalues[i]+.5,n+=k.Cvalues[i]*k.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(k.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 b(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 v(n*n+r*r)},closest:function(t,e){let n,r,i=z(2,63);return t.forEach((function(t,o){r=k.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=z(t,e)+z(1-t,e);return p((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=z(1-t,e);return n/(z(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 k.lli8(i,o,s,c,a,u,l,h)},lli:function(t,e){return k.lli4(t,t.c,e,e.c)},makeline:function(t,e){return new A(t.x,t.y,(t.x+e.x)/2,(t.y+e.y)/2,e.x,e.y)},findbbox:function(t){let e=M,n=M,r=C,i=C;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(!k.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=k.makeline(e.points[r-1],t.points[0]),s=k.makeline(t.points[i-1],e.points[0]),c={startcap:o,forward:t,back:e,endcap:s,bbox:k.findbbox([o,t,e,s]),intersections:function(t){return k.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=M,s=C;-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=-b(e.p2.y-r,e.p2.x-n);return t.map((function(t){return{x:(t.x-n)*d(i)-(t.y-r)*m(i),y:(t.x-n)*m(i)+(t.y-r)*d(i)}}))},roots:function(t,e){e=e||{p1:{x:0,y:0},p2:{x:1,y:0}};const n=t.length-1,r=k.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=-v(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(k.approximately(a,0)){if(k.approximately(u,0))return k.approximately(l,0)?[]:[-h/l].filter(i);const t=v(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,m=p*p+f*f*f;let b,z,w,O,M;if(m<0){const t=-x/3,e=v(t*t*t),n=-y/(2*e),r=g(n<-1?-1:n>1?1:n),o=2*_(e);return w=o*d(r/3)-u/3,O=o*d((r+T)/3)-u/3,M=o*d((r+2*T)/3)-u/3,[w,O,M].filter(i)}if(0===m)return b=p<0?_(-p):-_(p),w=2*b-u/3,O=-b-u/3,[w,O].filter(i);{const t=v(m);return b=_(-p+t),z=_(p+t),[b-z-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=-v(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=k.compute(t,e),x=k.compute(t,n),f=h.x*h.x+h.y*h.y;if(r?(o=v(z(h.y*x.z-x.y*h.z,2)+z(h.z*x.x-x.z*h.x,2)+z(h.x*x.y-x.x*h.y,2)),s=z(f+h.z*h.z,1.5)):(o=h.x*x.y-h.y*x.x,s=z(f,1.5)),0===o||0===s)return{k:0,r:0};if(u=o/s,l=s/o,!i){const i=k.curvature(t-.001,e,n,r,!0).k,o=k.curvature(t+.001,e,n,r,!0).k;a=(o-u+(u-i))/2,c=(p(o-u)+p(u-i))/2}return{k:u,r:l,dk:a,adk:c}},inflections:function(t){if(t.length<4)return[];const e=k.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(k.approximately(o,0)){if(!k.approximately(s,0)){let t=-c/s;if(0<=t&&t<=1)return[t]}return[]}const a=2*o;if(k.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,p(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 k.bboxoverlap(t.left.bbox(),t.right.bbox())}));let l=[];return 0===u.length||(u.forEach((function(t){l=l.concat(k.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*d(O)-i*m(O),a=r*m(O)+i*d(O),u=o*d(O)-s*m(O),l=o*m(O)+s*d(O),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,g=x+a,v=f+u,z=y+l,_=k.lli8(h,x,p,g,f,y,v,z),w=k.dist(_,t);let M,C=b(t.y-_.y,t.x-_.x),E=b(e.y-_.y,e.x-_.x),I=b(n.y-_.y,n.x-_.x);return C<I?((C>E||E>I)&&(C+=T),C>I&&(M=I,I=C,C=M)):I<E&&E<C?(M=I,I=C,C=M):I+=T,_.s=C,_.e=I,_.r=w,_},numberSort:function(t,e){return t-e}};class I{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 k.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++)k.expandbox(e,t[n].bbox());return e}offset(t){const e=[];return this.curves.forEach((function(n){e.push(...n.offset(t))})),new I(e)}}const{abs:j,min:P,max:B,cos:S,sin:R,acos:D,sqrt:F}=Math,N=Math.PI;class A{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=k.align(s,{p1:s[0],p2:s[a]}),h=k.dist(s[0],s[a]);this._linear=l.reduce(((t,e)=>t+j(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 A(e,e,n);if(1===r)return new A(t,e,e);const i=A.getABC(2,t,e,n,r);return new A(t,i.A,n)}static cubicFromPoints(t,e,n,r,i){void 0===r&&(r=.5);const o=A.getABC(3,t,e,n,r);void 0===i&&(i=k.dist(e,o.C));const s=i*(1-r)/r,c=k.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,b=g.x+(y-g.x)/(1-r),v=g.y+(p-g.y)/(1-r),z=g.x+(d-g.x)/r,_=g.y+(m-g.y)/r,w={x:t.x+(b-t.x)/r,y:t.y+(v-t.y)/r},T={x:n.x+(z-n.x)/(1-r),y:n.y+(_-n.y)/(1-r)};return new A(t,w,T,n)}static getUtils(){return k}getUtils(){return A.getUtils()}static get PolyBezier(){return I}valueOf(){return this.toString()}toString(){return k.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=k.derive(this.points,this._3d),this.computedirection()}computedirection(){const t=this.points,e=k.angle(t[0],t[this.order],t[1]);this.clockwise=e>0}length(){return k.length(this.derivative.bind(this))}static getABC(t=2,e,n,r,i=.5){const o=k.projectionratio(i,t),s=1-o,c={x:o*e.x+s*r.x,y:o*e.y+s*r.y},a=k.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 A.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],k.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=k.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=k.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?k.computeWithRatios(t,this.points,this.ratios,this._3d):k.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 A(e)}derivative(t){return k.compute(t,this.dpoints[0],this._3d)}dderivative(t){return k.compute(t,this.dpoints[1],this._3d)}align(){let t=this.points;return new A(k.align(t,{p1:t[0],p2:t[t.length-1]}))}curvature(t){return k.curvature(t,this.dpoints[0],this.dpoints[1],this._3d)}inflections(){return k.inflections(this.points)}normal(t){return this._3d?this.__normal3(t):this.__normal2(t)}__normal2(t){const e=this.derivative(t),n=F(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=F(e.x*e.x+e.y*e.y+e.z*e.z),i=F(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=F(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=k.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 A([n[0],n[3],n[5]]):new A([n[0],n[4],n[7],n[9]]),right:2===this.order?new A([n[5],n[4],n[2]]):new A([n[9],n[8],n[6],n[3]]),span:n};return r.left._t1=k.map(0,0,1,this._t1,this._t2),r.left._t2=k.map(t,0,1,this._t1,this._t2),r.right._t1=k.map(t,0,1,this._t1,this._t2),r.right._t2=k.map(1,0,1,this._t1,this._t2),e?(e=k.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]=k.droots(i),3===this.order&&(i=this.dpoints[1].map(r),t[n]=t[n].concat(k.droots(i))),t[n]=t[n].filter((function(t){return t>=0&&t<=1})),e=e.concat(t[n].sort(k.numberSort))}.bind(this)),t.values=e.sort(k.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]=k.getminmax(this,n,t[n])}.bind(this)),e}overlaps(t){const e=this.bbox(),n=t.bbox();return k.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 A(n)]}return this.reduce().map((function(e){return e._linear?e.offset(t)[0]:e.scale(t)}))}simple(){if(3===this.order){const t=k.angle(this.points[0],this.points[3],this.points[1]),e=k.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),j(D(n))<N/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,j(n-r)<i)return[];e=t.split(n,r),e._t1=k.map(n,0,1,t._t1,t._t2),e._t2=k.map(r,0,1,t._t1,t._t2),s.push(e),n=r;break}n<1&&(e=t.split(n,1),e._t1=k.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 A(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=k.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]=k.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=F(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 A(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]=k.lli4(n,o,u,i[t+1])})),new A(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=k.makeline(h[2],l[0]),f=k.makeline(l[2],h[0]),y=[x,new A(l),f,new A(h)];return new I(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 k.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=k.makeline(p,f),g=k.makeline(y,d),b=[m].concat(s).concat([g]).concat(a);return new I(b)}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=k.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 A&&(t=t.reduce()),this.curveintersects(this.reduce(),t,e)):this.selfintersects(e)}lineIntersects(t){const e=P(t.p1.x,t.p2.x),n=P(t.p1.y,t.p2.y),r=B(t.p1.x,t.p2.x),i=B(t.p1.y,t.p2.y);return k.roots(this.points,t).filter((t=>{var o=this.get(t);return k.between(o.x,e,r)&&k.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=k.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=k.dist(t,e),a=k.dist(t,o),u=k.dist(t,s);return j(a-c)+j(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=k.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*S(c.e),y:c.y+c.r*R(c.e)};c.e+=k.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 G=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=y.calcIntersection(t.tl,t.tr,t.bl,t.br),n=y.calcIntersection(t.tl,t.bl,t.tr,t.br),r=e=>e&&Object.values(t).some(((t,n,r)=>y.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 A(c.left.points).length(),right:new A(c.right.points).length(),top:new A(c.top.points).length(),bottom:new A(c.bottom.points).length()},l=y.calcIntersection(a.tl,a.bl,a.tr,a.br),h=y.calcIntersection(a.tl,a.tr,a.bl,a.br),{vertical:x,horizontal:f}=t.regionCurves[i][r],p=f.length-1,d=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/d),i=c.bottom.get(e/d);if(h){const t=y.calcCoordDistance(a.tl,h)/y.calcCoordDistance(a.tr,h),o=n(u.left,u.left/t,e,d);r=c.top.get(o);const s=y.calcCoordDistance(a.bl,h)/y.calcCoordDistance(a.br,h),l=n(u.left,u.left/s,e,d);i=c.bottom.get(l)}let o=c.left.get(t/p),x=c.right.get(t/p);if(l){const e=y.calcCoordDistance(a.tl,l)/y.calcCoordDistance(a.bl,l),r=n(u.top,u.top/e,t,p);o=c.left.get(r);const i=y.calcCoordDistance(a.tr,l)/y.calcCoordDistance(a.br,l),s=n(u.top,u.top/i,t,p);x=c.right.get(s)}const f=y.calcIntersection(r,i,o,x);f?s.push(f):s.push(r)}o.push(s)})),e.push(o)})),e};var H;!function(t){t.TOP_LEFT="tl",t.TOP_RIGHT="tr",t.BOTTOM_LEFT="bl",t.BOTTOM_RIGHT="br"}(H||(H={}));class L extends n{constructor(t={}){super(t),this.name="perspective",this.options={themeColor:r,subThemeColor:i,enableDragResize:!0,minimumDragThreshold:50,enableConstraintKey:"None"},this._objectControlMap=new Map([]),this._styleSetters={image:()=>{},path:()=>{},control:t=>t},this.options=o(t,this.options)}static execute(t){return G(t)}get controlObjects(){return Array.from(this._objectControlMap.keys())}execute(t){return L.execute(t)}dirtyRender(t){var n;const r=t.canvas;if(!r)return;const i=null===(n=t.warpvas)||void 0===n?void 0:n.regionBoundaryCurves;if(!i)return;i.forEach(((n,i)=>{n.forEach(((n,o)=>{const s=n;Object.entries(s).forEach((([n])=>{const c=s[n],a={top:H.TOP_LEFT,bottom:H.BOTTOM_RIGHT,right:H.TOP_RIGHT,left:H.BOTTOM_LEFT}[n],u=t.curvePathMap.get(c),l=c.points.map((t=>e.util.transformPoint(new e.Point(t.x-u.pathOffset.x,t.y-u.pathOffset.y),u.calcOwnMatrix()))),h=this._styleSetters.control(this._createDefaultControl()),x={first:0,last:3}[{top:"first",right:"first",bottom:"last",left:"last"}[n]];h.set({left:l[x].x,top:l[x].y}),r.add(h),this._objectControlMap.set(h,{curve:c,targetPointIdx:x,rowIndex:i,colIndex:o,vertexType:a})}))}))}));const{enableDragResize:o,enableConstraintKey:u}=this.options,l=[["NONE"!==u.toUpperCase(),s(r,u)],[o,()=>{const e=e=>{if(1!==e.e.buttons)return;if(e.target&&e.target.selectable)return;const{minimumDragThreshold:n}=this.options;let i=!1;r.selection=!1;const o=a(r,e.pointer),s=e=>{const s=a(r,e.pointer);if(!i&&(Math.abs(s.x-o.x)<n||Math.abs(s.y-o.y)<n))return;const u=c({left:o.x,top:o.y},t.curvePathMap.get(t.warpvas.regionBoundaryCurves[0][0].top)),l=c({left:s.x,top:s.y},t.curvePathMap.get(t.warpvas.regionBoundaryCurves[0][0].bottom));t.warpvas.updateVertexCoord(0,0,H.TOP_LEFT,u),t.warpvas.updateVertexCoord(0,0,H.TOP_RIGHT,{x:l.x,y:u.y}),t.warpvas.updateVertexCoord(0,0,H.BOTTOM_LEFT,{x:u.x,y:l.y}),t.warpvas.updateVertexCoord(0,0,H.BOTTOM_RIGHT,l),t.render(!0,{skipHistoryRecording:!0}),i=!0},u=e=>{i&&t.record(),r.selection=!0,r.off("mouse:move",s),r.off("mouse:up",u)};r.on("mouse:move",s),r.on("mouse:up",u)};return r.on("mouse:down:before",e),()=>{r.off("mouse:down:before",e)}}],[!0,()=>{const n=n=>{if(!n.target)return;const i=this._objectControlMap.get(n.target);if(!i)return;const o=n.target,{curve:s,targetPointIdx:a,rowIndex:u,colIndex:l,vertexType:h}=i,x=t.curvePathMap.get(s),f=x.calcOwnMatrix(),y=c(o,x),p=Object.assign({},s.points[a]);try{t.warpvas.updateVertexCoord(u,l,h,y),t.render(!1,{skipHistoryRecording:!0})}catch(n){t.warpvas.updateVertexCoord(u,l,h,p),t.render(!1,{skipHistoryRecording:!0});const r=s.points.map((t=>e.util.transformPoint(new e.Point(t.x-x.pathOffset.x,t.y-x.pathOffset.y),f)));o.set({left:r[a].x,top:r[a].y}),o.setCoords()}finally{this.controlObjects.forEach((t=>{r.bringToFront(t)}))}},i=e=>{const n=e.target;n&&"drag"===e.action&&this.controlObjects.includes(n)&&t.record()};return r.on("object:moving",n),r.on("object:modified",i),()=>{r.off("object:moving",n),r.off("object:modified",i)}}]],h=l.map((([t,e])=>t&&e()));return()=>{h.forEach((t=>{"function"==typeof t&&t()})),r.remove(...this._objectControlMap.keys()),this._objectControlMap.clear()}}_createDefaultControl(){return new e.Circle({radius:4,fill:this.options.themeColor,stroke:"#ffffff",paintFirst:"fill",strokeWidth:1,originX:"center",originY:"center",hasControls:!1,hasBorders:!1})}}export{H as VertexType,L as default};
//# sourceMappingURL=perspective.esm.js.map