UNPKG

fabric-texture

Version:

This JavaScript library enables fast and efficient image distortion transformations using Canvas 2D.

3 lines (2 loc) 26.6 kB
import{fabric as e}from"fabric";import{B as n,d as r,T as i,S as o}from"./base.class-G1Hpx_TY.js";import{c as s}from"./calc-intersection-_ACVCUgT.js";import{c,a as u}from"./calc-fabric-relative-coord--qdng7YE.js";const a=(t,e)=>Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2)),{abs:l,cos:h,sin:f,acos:x,atan2:p,sqrt:y,pow:d}=Math;function m(t){return t<0?-d(-t,1/3):d(t,1/3)}const g=Math.PI,b=2*g,z=g/2,v=Number.MAX_SAFE_INTEGER||9007199254740991,_=Number.MIN_SAFE_INTEGER||-9007199254740991,w={x:0,y:0,z:0},T={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),y(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,u=i*i,a=t*t,l=0;2===r?(o=[o[0],o[1],o[2],w],e=u,s=i*t*2,c=a):3===r&&(e=u*i,s=u*t*3,c=i*a*3,l=t*a);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,u=o[0],a=o[1],l=o[2],h=o[3];return u*=i,a*=t,2===s.length?(c=u+a,{x:(u*s[0].x+a*s[1].x)/c,y:(u*s[0].y+a*s[1].y)/c,z:!!r&&(u*s[0].z+a*s[1].z)/c,t:t}):(u*=i,a*=2*i,l*=t*t,3===s.length?(c=u+a+l,{x:(u*s[0].x+a*s[1].x+l*s[2].x)/c,y:(u*s[0].y+a*s[1].y+l*s[2].y)/c,z:!!r&&(u*s[0].z+a*s[1].z+l*s[2].z)/c,t:t}):(u*=i,a*=1.5*i,l*=3*i,h*=t*t*t,4===s.length?(c=u+a+l+h,{x:(u*s[0].x+a*s[1].x+l*s[2].x+h*s[3].x)/c,y:(u*s[0].y+a*s[1].y+l*s[2].y+h*s[3].y)/c,z:!!r&&(u*s[0].z+a*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||T.approximately(t,e)||T.approximately(t,n)},approximately:function(t,e,n){return l(t-e)<=(n||1e-6)},length:function(t){const e=T.Tvalues.length;let n=0;for(let r,i=0;i<e;i++)r=.5*T.Tvalues[i]+.5,n+=T.Cvalues[i]*T.arcfn(r,t);return.5*n},map:function(t,e,n,r,i){return r+(i-r)*((t-e)/(n-e))},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(T.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 p(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 y(n*n+r*r)},closest:function(t,e){let n,r,i=d(2,63);return t.forEach((function(t,o){r=T.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=d(t,e)+d(1-t,e);return l((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=d(1-t,e);return n/(d(t,e)+n)},lli8:function(t,e,n,r,i,o,s,c){const u=(t-n)*(o-c)-(e-r)*(i-s);return 0!=u&&{x:((t*r-e*n)*(i-s)-(t-n)*(i*c-o*s))/u,y:((t*r-e*n)*(o-c)-(e-r)*(i*c-o*s))/u}},lli4:function(t,e,n,r){const i=t.x,o=t.y,s=e.x,c=e.y,u=n.x,a=n.y,l=r.x,h=r.y;return T.lli8(i,o,s,c,u,a,l,h)},lli:function(t,e){return T.lli4(t,t.c,e,e.c)},makeline:function(t,e){return new I(t.x,t.y,(t.x+e.x)/2,(t.y+e.y)/2,e.x,e.y)},findbbox:function(t){let e=v,n=v,r=_,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(!T.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=T.makeline(e.points[r-1],t.points[0]),s=T.makeline(t.points[i-1],e.points[0]),c={startcap:o,forward:t,back:e,endcap:s,bbox:T.findbbox([o,t,e,s]),intersections:function(t){return T.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=v,s=_;-1===n.indexOf(0)&&(n=[0].concat(n)),-1===n.indexOf(1)&&n.push(1);for(let c=0,u=n.length;c<u;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=-p(e.p2.y-r,e.p2.x-n);return t.map((function(t){return{x:(t.x-n)*h(i)-(t.y-r)*f(i),y:(t.x-n)*f(i)+(t.y-r)*h(i)}}))},roots:function(t,e){e=e||{p1:{x:0,y:0},p2:{x:1,y:0}};const n=t.length-1,r=T.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=-y(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 u=3*s-o-3*c+r[3].y,a=3*o-6*s+3*c,l=-3*o+3*s,f=o;if(T.approximately(u,0)){if(T.approximately(a,0))return T.approximately(l,0)?[]:[-f/l].filter(i);const t=y(l*l-4*a*f),e=2*a;return[(t-l)/e,(-l-t)/e].filter(i)}a/=u,l/=u,f/=u;const p=(3*l-a*a)/3,d=p/3,g=(2*a*a*a-9*a*l+27*f)/27,z=g/2,v=z*z+d*d*d;let _,w,E,O,C;if(v<0){const t=-p/3,e=y(t*t*t),n=-g/(2*e),r=x(n<-1?-1:n>1?1:n),o=2*m(e);return E=o*h(r/3)-a/3,O=o*h((r+b)/3)-a/3,C=o*h((r+2*b)/3)-a/3,[E,O,C].filter(i)}if(0===v)return _=z<0?m(-z):-m(z),E=2*_-a/3,O=-_-a/3,[E,O].filter(i);{const t=y(v);return _=m(-z+t),w=m(z+t),[_-w-a/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=-y(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,u,a=0,h=0;const f=T.compute(t,e),x=T.compute(t,n),p=f.x*f.x+f.y*f.y;if(r?(o=y(d(f.y*x.z-x.y*f.z,2)+d(f.z*x.x-x.z*f.x,2)+d(f.x*x.y-x.x*f.y,2)),s=d(p+f.z*f.z,1.5)):(o=f.x*x.y-f.y*x.x,s=d(p,1.5)),0===o||0===s)return{k:0,r:0};if(a=o/s,h=s/o,!i){const i=T.curvature(t-.001,e,n,r,!0).k,o=T.curvature(t+.001,e,n,r,!0).k;u=(o-a+(a-i))/2,c=(l(o-a)+l(a-i))/2}return{k:a,r:h,dk:u,adk:c}},inflections:function(t){if(t.length<4)return[];const e=T.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(T.approximately(o,0)){if(!T.approximately(s,0)){let t=-c/s;if(0<=t&&t<=1)return[t]}return[]}const u=2*o;if(T.approximately(u,0))return[];const a=s*s-4*o*c;if(a<0)return[];const l=Math.sqrt(a);return[(l-s)/u,-(s+l)/u].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,u=0;u<r;u++)if(i=n[u],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,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),u=e.split(.5),a=[{left:c.left,right:u.left},{left:c.left,right:u.right},{left:c.right,right:u.right},{left:c.right,right:u.left}];a=a.filter((function(t){return T.bboxoverlap(t.left.bbox(),t.right.bbox())}));let l=[];return 0===a.length||(a.forEach((function(t){l=l.concat(T.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*h(z)-i*f(z),u=r*f(z)+i*h(z),a=o*h(z)-s*f(z),l=o*f(z)+s*h(z),x=(t.x+e.x)/2,y=(t.y+e.y)/2,d=(e.x+n.x)/2,m=(e.y+n.y)/2,g=x+c,v=y+u,_=d+a,w=m+l,E=T.lli8(x,y,g,v,d,m,_,w),O=T.dist(E,t);let C,M=p(t.y-E.y,t.x-E.x),k=p(e.y-E.y,e.x-E.x),j=p(n.y-E.y,n.x-E.x);return M<j?((M>k||k>j)&&(M+=b),M>j&&(C=j,j=M,M=C)):j<k&&k<M?(C=j,j=M,M=C):j+=b,E.s=M,E.e=j,E.r=O,E},numberSort:function(t,e){return t-e}};class E{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 T.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++)T.expandbox(e,t[n].bbox());return e}offset(t){const e=[];return this.curves.forEach((function(n){e.push(...n.offset(t))})),new E(e)}}const{abs:O,min:C,max:M,cos:k,sin:j,acos:S,sqrt:P}=Math,B=Math.PI;class I{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 u=this.order=s.length-1,a=this.dims=["x","y"];o&&a.push("z"),this.dimlen=a.length;const l=T.align(s,{p1:s[0],p2:s[u]}),h=T.dist(s[0],s[u]);this._linear=l.reduce(((t,e)=>t+O(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 I(e,e,n);if(1===r)return new I(t,e,e);const i=I.getABC(2,t,e,n,r);return new I(t,i.A,n)}static cubicFromPoints(t,e,n,r,i){void 0===r&&(r=.5);const o=I.getABC(3,t,e,n,r);void 0===i&&(i=T.dist(e,o.C));const s=i*(1-r)/r,c=T.dist(t,n),u=(n.x-t.x)/c,a=(n.y-t.y)/c,l=i*u,h=i*a,f=s*u,x=s*a,p=e.x-l,y=e.y-h,d=e.x+f,m=e.y+x,g=o.A,b=g.x+(p-g.x)/(1-r),z=g.y+(y-g.y)/(1-r),v=g.x+(d-g.x)/r,_=g.y+(m-g.y)/r,w={x:t.x+(b-t.x)/r,y:t.y+(z-t.y)/r},E={x:n.x+(v-n.x)/(1-r),y:n.y+(_-n.y)/(1-r)};return new I(t,w,E,n)}static getUtils(){return T}getUtils(){return I.getUtils()}static get PolyBezier(){return E}valueOf(){return this.toString()}toString(){return T.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=T.derive(this.points,this._3d),this.computedirection()}computedirection(){const t=this.points,e=T.angle(t[0],t[this.order],t[1]);this.clockwise=e>0}length(){return T.length(this.derivative.bind(this))}static getABC(t=2,e,n,r,i=.5){const o=T.projectionratio(i,t),s=1-o,c={x:o*e.x+s*r.x,y:o*e.y+s*r.y},u=T.abcratio(i,t);return{A:{x:n.x+(n.x-c.x)/u,y:n.y+(n.y-c.y)/u},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 I.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],T.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=T.closest(e,t),i=r.mpos,o=(i-1)/n,s=(i+1)/n,c=.1/n;let u,a=r.mdist,l=o,h=l;a+=1;for(let e;l<s+c;l+=c)u=this.compute(l),e=T.dist(t,u),e<a&&(a=e,h=l);return h=h<0?0:h>1?1:h,u=this.compute(h),u.t=h,u.d=a,u}get(t){return this.compute(t)}point(t){return this.points[t]}compute(t){return this.ratios?T.computeWithRatios(t,this.points,this.ratios,this._3d):T.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 I(e)}derivative(t){return T.compute(t,this.dpoints[0],this._3d)}dderivative(t){return T.compute(t,this.dpoints[1],this._3d)}align(){let t=this.points;return new I(T.align(t,{p1:t[0],p2:t[t.length-1]}))}curvature(t){return T.curvature(t,this.dpoints[0],this.dpoints[1],this._3d)}inflections(){return T.inflections(this.points)}normal(t){return this._3d?this.__normal3(t):this.__normal2(t)}__normal2(t){const e=this.derivative(t),n=P(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=P(e.x*e.x+e.y*e.y+e.z*e.z),i=P(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=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,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=T.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 I([n[0],n[3],n[5]]):new I([n[0],n[4],n[7],n[9]]),right:2===this.order?new I([n[5],n[4],n[2]]):new I([n[9],n[8],n[6],n[3]]),span:n};return r.left._t1=T.map(0,0,1,this._t1,this._t2),r.left._t2=T.map(t,0,1,this._t1,this._t2),r.right._t1=T.map(t,0,1,this._t1,this._t2),r.right._t2=T.map(1,0,1,this._t1,this._t2),e?(e=T.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]=T.droots(i),3===this.order&&(i=this.dpoints[1].map(r),t[n]=t[n].concat(T.droots(i))),t[n]=t[n].filter((function(t){return t>=0&&t<=1})),e=e.concat(t[n].sort(T.numberSort))}.bind(this)),t.values=e.sort(T.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]=T.getminmax(this,n,t[n])}.bind(this)),e}overlaps(t){const e=this.bbox(),n=t.bbox();return T.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 I(n)]}return this.reduce().map((function(e){return e._linear?e.offset(t)[0]:e.scale(t)}))}simple(){if(3===this.order){const t=T.angle(this.points[0],this.points[3],this.points[1]),e=T.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),O(S(n))<B/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,O(n-r)<i)return[];e=t.split(n,r),e._t1=T.map(n,0,1,t._t1,t._t2),e._t2=T.map(r,0,1,t._t1,t._t2),s.push(e),n=r;break}n<1&&(e=t.split(n,1),e._t1=T.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 I(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)],u=[],a=T.lli4(c[0],c[0].c,c[1],c[1].c);if(!a)throw new Error("cannot scale this curve. Try reducing it first.");return[0,1].forEach((function(t){const n=u[t*e]=T.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-a.x,y:s.y-a.y},l=n?n((o+1)/e):t;n&&!r&&(l=-l);var h=P(c.x*c.x+c.y*c.y);c.x/=h,c.y/=h,u[o+1]={x:s.x+l*c.x,y:s.y+l*c.y}}})),new I(u)):([0,1].forEach((t=>{if(2===e&&t)return;const n=u[t*e],r=this.derivative(t),o={x:n.x+r.x,y:n.y+r.y};u[t+1]=T.lli4(n,o,a,i[t+1])})),new I(u))}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,u,a;void 0===n&&(n=t,r=e),c={x:o.x+i.x*t,y:o.y+i.y*t},a={x:s.x+i.x*n,y:s.y+i.y*n},u={x:(c.x+a.x)/2,y:(c.y+a.y)/2};const l=[c,u,a];c={x:o.x-i.x*e,y:o.y-i.y*e},a={x:s.x-i.x*r,y:s.y-i.y*r},u={x:(c.x+a.x)/2,y:(c.y+a.y)/2};const h=[a,u,c],f=T.makeline(h[2],l[0]),x=T.makeline(l[2],h[0]),p=[f,new I(l),x,new I(h)];return new E(p)}const i=this.reduce(),o=i.length,s=[];let c,u=[],a=0,l=this.length();const h=void 0!==n&&void 0!==r;function f(t,e,n,r,i){return function(o){const s=r/n,c=(r+i)/n,u=e-t;return T.map(o,0,1,t+s*u,t+c*u)}}i.forEach((function(i){const o=i.length();h?(s.push(i.scale(f(t,n,l,a,o))),u.push(i.scale(f(-e,-r,l,a,o)))):(s.push(i.scale(t)),u.push(i.scale(-e))),a+=o})),u=u.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 x=s[0].points[0],p=s[o-1].points[s[o-1].points.length-1],y=u[o-1].points[u[o-1].points.length-1],d=u[0].points[0],m=T.makeline(y,x),g=T.makeline(p,d),b=[m].concat(s).concat([g]).concat(u);return new E(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=T.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 I&&(t=t.reduce()),this.curveintersects(this.reduce(),t,e)):this.selfintersects(e)}lineIntersects(t){const e=C(t.p1.x,t.p2.x),n=C(t.p1.y,t.p2.y),r=M(t.p1.x,t.p2.x),i=M(t.p1.y,t.p2.y);return T.roots(this.points,t).filter((t=>{var o=this.get(t);return T.between(o.x,e,r)&&T.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=T.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=T.dist(t,e),u=T.dist(t,o),a=T.dist(t,s);return O(u-c)+O(a-c)}_iterate(t,e){let n,r=0,i=1;do{n=0,i=1;let o,s,c,u,a,l=this.get(r),h=!1,f=!1,x=i,p=1;do{if(f=h,u=c,x=(r+i)/2,o=this.get(x),s=this.get(i),c=T.getccenter(l,o,s),c.interval={start:r,end:i},h=this._error(c,l,r,i)<=t,a=f&&!h,a||(p=i),h){if(i>=1){if(c.interval.end=p=1,u=c,i>1){let t={x:c.x+c.r*k(c.e),y:c.y+c.r*j(c.e)};c.e+=T.angle({x:c.x,y:c.y},t,this.get(1))}break}i+=(i-r)/2}else i=x}while(!a&&n++<100);if(n>=100)break;u=u||c,e.push(u),r=p}while(i<1);return e}}var R;!function(t){t.TOP_LEFT="tl",t.TOP_RIGHT="tr",t.BOTTOM_LEFT="bl",t.BOTTOM_RIGHT="br"}(R||(R={}));class F extends n{constructor(t=i,e=o,n={}){super(t,e),this.name="Perspective",this.options={enableDragResize:!0,enableShiftConstraint:!0},this._objectControlMap=new Map([]),this._styleSetters={texture:()=>{},path:()=>{},control:t=>t},this.options=r(n,this.options)}static execute(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))},r=t=>{const e=s(t.tl,t.tr,t.bl,t.br),n=s(t.tl,t.bl,t.tr,t.br),r=e=>e&&Object.values(t).some(((t,n,r)=>((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})(e,r[n],r[(n+1)%4],r[(n+2)%4])));return r(e)||r(n)};return t.areaBoundCurves.forEach(((i,o)=>{const c=[];i.forEach(((e,i)=>{const u=[],l=e,h={tl:l.top.points[0],tr:l.top.points[3],bl:l.bottom.points[0],br:l.bottom.points[3]};if(r(h))throw new Error("[Fabric-Texture Error] Invalid perspective shape: The four control points cannot form a triangle or cross each other");const f={left:new I(l.left.points).length(),right:new I(l.right.points).length(),top:new I(l.top.points).length(),bottom:new I(l.bottom.points).length()},x=s(h.tl,h.bl,h.tr,h.br),p=s(h.tl,h.tr,h.bl,h.br),{vertical:y,horizontal:d}=t.mesh[o][i],m=d.length-1,g=y.length-1;for(let t=0;t<d.length;t++)for(let e=0;e<y.length;e++){let r=l.top.get(e/g),i=l.bottom.get(e/g);if(p){const t=a(h.tl,p)/a(h.tr,p),o=n(f.left,f.left/t,e,g);r=l.top.get(o);const s=a(h.bl,p)/a(h.br,p),c=n(f.left,f.left/s,e,g);i=l.bottom.get(c)}let o=l.left.get(t/m),c=l.right.get(t/m);if(x){const e=a(h.tl,x)/a(h.bl,x),r=n(f.top,f.top/e,t,m);o=l.left.get(r);const i=a(h.tr,x)/a(h.br,x),s=n(f.top,f.top/i,t,m);c=l.right.get(s)}const y=s(r,i,o,c);y?u.push(y):u.push(r)}c.push(u)})),e.push(c)})),e}static createDefaultControl(t){return new e.Circle({radius:7,fill:t,stroke:"#ffffff",paintFirst:"fill",strokeWidth:2,originX:"center",originY:"center",hasControls:!1,hasBorders:!1})}get controlObjects(){return Array.from(this._objectControlMap.keys())}execute(t){return F.execute(t)}dirtyRender(t){var n;const r=t.canvas;if(!r)return;const i=null===(n=t.texture)||void 0===n?void 0:n.areaBoundCurves;if(!i)return;i.forEach(((n,i)=>{n.forEach(((n,o)=>{const s=n;Object.entries(s).forEach((([n])=>{const c=s[n],u={top:R.TOP_LEFT,bottom:R.BOTTOM_RIGHT,right:R.TOP_RIGHT,left:R.BOTTOM_LEFT}[n],a=t.curvePathMap.get(c),l=c.points.map((t=>e.util.transformPoint(new e.Point(t.x-a.pathOffset.x,t.y-a.pathOffset.y),a.calcOwnMatrix()))),h=this._styleSetters.control(F.createDefaultControl(this.themeColor)),f={first:0,last:3}[{top:"first",right:"first",bottom:"last",left:"last"}[n]];h.set({left:l[f].x,top:l[f].y}),r.add(h),this._objectControlMap.set(h,{curve:c,targetPointIdx:f,rowIndex:i,colIndex:o,vertexType:u})}))}))}));const{enableDragResize:o,enableShiftConstraint:s}=this.options,a=[[s,()=>{let t=!1;const e=e=>{t=e.shiftKey},n=e=>{t=e.shiftKey},i=e=>{if(t&&e.target&&e.transform){const{left:t=0,top:n=0}=e.target,{left:r,top:i}=e.transform.original;Math.abs(t-r)>Math.abs(n-i)?e.target.set({top:i}).setCoords():e.target.set({left:r}).setCoords()}};return r.on("object:moving",i),window.addEventListener("keydown",e),window.addEventListener("keyup",n),()=>{r.off("object:moving",i),window.removeEventListener("keydown",e),window.removeEventListener("keyup",n)}}],[o,()=>{const e=e=>{if(1!==e.e.buttons)return;if(e.target&&e.target.selectable)return;let n=!1;r.selection=!1;const i=u(r,e.pointer),o=e=>{const o=u(r,e.pointer);if(!n&&(Math.abs(o.x-i.x)<50||Math.abs(o.y-i.y)<50))return;const s=c({left:i.x,top:i.y},t.curvePathMap.get(t.texture.areaBoundCurves[0][0].top)),a=c({left:o.x,top:o.y},t.curvePathMap.get(t.texture.areaBoundCurves[0][0].bottom));t.texture.setVertexCoord(0,0,R.TOP_LEFT,s),t.texture.setVertexCoord(0,0,R.TOP_RIGHT,{x:a.x,y:s.y}),t.texture.setVertexCoord(0,0,R.BOTTOM_LEFT,{x:s.x,y:a.y}),t.texture.setVertexCoord(0,0,R.BOTTOM_RIGHT,a),t.render(!0,{withoutRecord:!0}),n=!0},s=e=>{n&&t.record(),r.selection=!0,r.off("mouse:move",o),r.off("mouse:up",s)};r.on("mouse:move",o),r.on("mouse:up",s)};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:u,rowIndex:a,colIndex:l,vertexType:h}=i,f=t.curvePathMap.get(s),x=f.calcOwnMatrix(),p=c(o,f),y=Object.assign({},s.points[u]);try{t.texture.setVertexCoord(a,l,h,p),t.render(!1,{withoutRecord:!0})}catch(n){t.texture.setVertexCoord(a,l,h,y),t.render(!1,{withoutRecord:!0});const r=s.points.map((t=>e.util.transformPoint(new e.Point(t.x-f.pathOffset.x,t.y-f.pathOffset.y),x)));o.set({left:r[u].x,top:r[u].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)}}]].map((([t,e])=>t&&e()));return()=>{a.forEach((t=>{"function"==typeof t&&t()})),r.remove(...this._objectControlMap.keys()),this._objectControlMap.clear()}}}export{R as VertexType,F as default}; //# sourceMappingURL=perspective.esm.js.map