fabric-texture
Version:
This JavaScript library enables fast and efficient image distortion transformations using Canvas 2D.
3 lines (2 loc) • 27.1 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("fabric"),r=require("./base.class-7YPThjJT.js"),n=require("./calc-intersection-NLuQXGEN.js"),i=require("./calc-fabric-relative-coord-57kWtQps.js");const o=(t,e)=>Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2)),{abs:s,cos:c,sin:a,acos:u,atan2:l,sqrt:h,pow:x}=Math;function f(t){return t<0?-x(-t,1/3):x(t,1/3)}const p=Math.PI,y=2*p,d=p/2,m=Number.MAX_SAFE_INTEGER||9007199254740991,g=Number.MIN_SAFE_INTEGER||-9007199254740991,b={x:0,y:0,z:0},v={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),h(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],b],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 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 r&&(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,r,n){const i=1-t,o=r,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:!!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,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:!!n&&(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 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||v.approximately(t,e)||v.approximately(t,r)},approximately:function(t,e,r){return s(t-e)<=(r||1e-6)},length:function(t){const e=v.Tvalues.length;let r=0;for(let n,i=0;i<e;i++)n=.5*v.Tvalues[i]+.5,r+=v.Cvalues[i]*v.arcfn(n,t);return.5*r},map:function(t,e,r,n,i){return n+(i-n)*((t-e)/(r-e))},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(v.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 l(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 h(r*r+n*n)},closest:function(t,e){let r,n,i=x(2,63);return t.forEach((function(t,o){n=v.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=x(t,e)+x(1-t,e);return s((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=x(1-t,e);return r/(x(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,h=n.y;return v.lli8(i,o,s,c,a,u,l,h)},lli:function(t,e){return v.lli4(t,t.c,e,e.c)},makeline:function(t,e){return new j(t.x,t.y,(t.x+e.x)/2,(t.y+e.y)/2,e.x,e.y)},findbbox:function(t){let e=m,r=m,n=g,i=g;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(!v.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=v.makeline(e.points[n-1],t.points[0]),s=v.makeline(t.points[i-1],e.points[0]),c={startcap:o,forward:t,back:e,endcap:s,bbox:v.findbbox([o,t,e,s]),intersections:function(t){return v.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=m,s=g;-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=-l(e.p2.y-n,e.p2.x-r);return t.map((function(t){return{x:(t.x-r)*c(i)-(t.y-n)*a(i),y:(t.x-r)*a(i)+(t.y-n)*c(i)}}))},roots:function(t,e){e=e||{p1:{x:0,y:0},p2:{x:1,y:0}};const r=t.length-1,n=v.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=-h(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,a=n[2].y;let l=3*s-o-3*a+n[3].y,x=3*o-6*s+3*a,p=-3*o+3*s,d=o;if(v.approximately(l,0)){if(v.approximately(x,0))return v.approximately(p,0)?[]:[-d/p].filter(i);const t=h(p*p-4*x*d),e=2*x;return[(t-p)/e,(-p-t)/e].filter(i)}x/=l,p/=l,d/=l;const m=(3*p-x*x)/3,g=m/3,b=(2*x*x*x-9*x*p+27*d)/27,z=b/2,_=z*z+g*g*g;let w,T,E,O,C;if(_<0){const t=-m/3,e=h(t*t*t),r=-b/(2*e),n=u(r<-1?-1:r>1?1:r),o=2*f(e);return E=o*c(n/3)-x/3,O=o*c((n+y)/3)-x/3,C=o*c((n+2*y)/3)-x/3,[E,O,C].filter(i)}if(0===_)return w=z<0?f(-z):-f(z),E=2*w-x/3,O=-w-x/3,[E,O].filter(i);{const t=h(_);return w=f(-z+t),T=f(z+t),[w-T-x/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=-h(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,c,a,u,l=0,f=0;const p=v.compute(t,e),y=v.compute(t,r),d=p.x*p.x+p.y*p.y;if(n?(o=h(x(p.y*y.z-y.y*p.z,2)+x(p.z*y.x-y.z*p.x,2)+x(p.x*y.y-y.x*p.y,2)),c=x(d+p.z*p.z,1.5)):(o=p.x*y.y-p.y*y.x,c=x(d,1.5)),0===o||0===c)return{k:0,r:0};if(l=o/c,f=c/o,!i){const i=v.curvature(t-.001,e,r,n,!0).k,o=v.curvature(t+.001,e,r,n,!0).k;u=(o-l+(l-i))/2,a=(s(o-l)+s(l-i))/2}return{k:l,r:f,dk:u,adk:a}},inflections:function(t){if(t.length<4)return[];const e=v.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(v.approximately(o,0)){if(!v.approximately(s,0)){let t=-c/s;if(0<=t&&t<=1)return[t]}return[]}const a=2*o;if(v.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,c,a,u=0;u<n;u++)if(i=r[u],o=t[i].mid,c=e[i].mid,a=(t[i].size+e[i].size)/2,s(o-c)>=a)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 v.bboxoverlap(t.left.bbox(),t.right.bbox())}));let l=[];return 0===u.length||(u.forEach((function(t){l=l.concat(v.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,u=n*c(d)-i*a(d),h=n*a(d)+i*c(d),x=o*c(d)-s*a(d),f=o*a(d)+s*c(d),p=(t.x+e.x)/2,m=(t.y+e.y)/2,g=(e.x+r.x)/2,b=(e.y+r.y)/2,z=p+u,_=m+h,w=g+x,T=b+f,E=v.lli8(p,m,z,_,g,b,w,T),O=v.dist(E,t);let C,M=l(t.y-E.y,t.x-E.x),k=l(e.y-E.y,e.x-E.x),j=l(r.y-E.y,r.x-E.x);return M<j?((M>k||k>j)&&(M+=y),M>j&&(C=j,j=M,M=C)):j<k&&k<M?(C=j,j=M,M=C):j+=y,E.s=M,E.e=j,E.r=O,E},numberSort:function(t,e){return t-e}};class z{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 v.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++)v.expandbox(e,t[r].bbox());return e}offset(t){const e=[];return this.curves.forEach((function(r){e.push(...r.offset(t))})),new z(e)}}const{abs:_,min:w,max:T,cos:E,sin:O,acos:C,sqrt:M}=Math,k=Math.PI;class j{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=v.align(s,{p1:s[0],p2:s[a]}),h=v.dist(s[0],s[a]);this._linear=l.reduce(((t,e)=>t+_(e.y)),0)<h/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 j(e,e,r);if(1===n)return new j(t,e,e);const i=j.getABC(2,t,e,r,n);return new j(t,i.A,r)}static cubicFromPoints(t,e,r,n,i){void 0===n&&(n=.5);const o=j.getABC(3,t,e,r,n);void 0===i&&(i=v.dist(e,o.C));const s=i*(1-n)/n,c=v.dist(t,r),a=(r.x-t.x)/c,u=(r.y-t.y)/c,l=i*a,h=i*u,x=s*a,f=s*u,p=e.x-l,y=e.y-h,d=e.x+x,m=e.y+f,g=o.A,b=g.x+(p-g.x)/(1-n),z=g.y+(y-g.y)/(1-n),_=g.x+(d-g.x)/n,w=g.y+(m-g.y)/n,T={x:t.x+(b-t.x)/n,y:t.y+(z-t.y)/n},E={x:r.x+(_-r.x)/(1-n),y:r.y+(w-r.y)/(1-n)};return new j(t,T,E,r)}static getUtils(){return v}getUtils(){return j.getUtils()}static get PolyBezier(){return z}valueOf(){return this.toString()}toString(){return v.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=v.derive(this.points,this._3d),this.computedirection()}computedirection(){const t=this.points,e=v.angle(t[0],t[this.order],t[1]);this.clockwise=e>0}length(){return v.length(this.derivative.bind(this))}static getABC(t=2,e,r,n,i=.5){const o=v.projectionratio(i,t),s=1-o,c={x:o*e.x+s*n.x,y:o*e.y+s*n.y},a=v.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 j.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],v.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=v.closest(e,t),i=n.mpos,o=(i-1)/r,s=(i+1)/r,c=.1/r;let a,u=n.mdist,l=o,h=l;u+=1;for(let e;l<s+c;l+=c)a=this.compute(l),e=v.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?v.computeWithRatios(t,this.points,this.ratios,this._3d):v.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 j(e)}derivative(t){return v.compute(t,this.dpoints[0],this._3d)}dderivative(t){return v.compute(t,this.dpoints[1],this._3d)}align(){let t=this.points;return new j(v.align(t,{p1:t[0],p2:t[t.length-1]}))}curvature(t){return v.curvature(t,this.dpoints[0],this.dpoints[1],this._3d)}inflections(){return v.inflections(this.points)}normal(t){return this._3d?this.__normal3(t):this.__normal2(t)}__normal2(t){const e=this.derivative(t),r=M(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=M(e.x*e.x+e.y*e.y+e.z*e.z),i=M(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=M(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=v.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 j([r[0],r[3],r[5]]):new j([r[0],r[4],r[7],r[9]]),right:2===this.order?new j([r[5],r[4],r[2]]):new j([r[9],r[8],r[6],r[3]]),span:r};return n.left._t1=v.map(0,0,1,this._t1,this._t2),n.left._t2=v.map(t,0,1,this._t1,this._t2),n.right._t1=v.map(t,0,1,this._t1,this._t2),n.right._t2=v.map(1,0,1,this._t1,this._t2),e?(e=v.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]=v.droots(i),3===this.order&&(i=this.dpoints[1].map(n),t[r]=t[r].concat(v.droots(i))),t[r]=t[r].filter((function(t){return t>=0&&t<=1})),e=e.concat(t[r].sort(v.numberSort))}.bind(this)),t.values=e.sort(v.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]=v.getminmax(this,r,t[r])}.bind(this)),e}overlaps(t){const e=this.bbox(),r=t.bbox();return v.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 j(r)]}return this.reduce().map((function(e){return e._linear?e.offset(t)[0]:e.scale(t)}))}simple(){if(3===this.order){const t=v.angle(this.points[0],this.points[3],this.points[1]),e=v.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(r))<k/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,_(r-n)<i)return[];e=t.split(r,n),e._t1=v.map(r,0,1,t._t1,t._t2),e._t2=v.map(n,0,1,t._t1,t._t2),s.push(e),r=n;break}r<1&&(e=t.split(r,1),e._t1=v.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 j(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=v.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]=v.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 h=M(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 j(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]=v.lli4(r,o,u,i[t+1])})),new j(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 h=[u,a,c],x=v.makeline(h[2],l[0]),f=v.makeline(l[2],h[0]),p=[x,new j(l),f,new j(h)];return new z(p)}const i=this.reduce(),o=i.length,s=[];let c,a=[],u=0,l=this.length();const h=void 0!==r&&void 0!==n;function x(t,e,r,n,i){return function(o){const s=n/r,c=(n+i)/r,a=e-t;return v.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,r,l,u,o))),a.push(i.scale(x(-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 f=s[0].points[0],p=s[o-1].points[s[o-1].points.length-1],y=a[o-1].points[a[o-1].points.length-1],d=a[0].points[0],m=v.makeline(y,f),g=v.makeline(p,d),b=[m].concat(s).concat([g]).concat(a);return new z(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=v.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 j&&(t=t.reduce()),this.curveintersects(this.reduce(),t,e)):this.selfintersects(e)}lineIntersects(t){const e=w(t.p1.x,t.p2.x),r=w(t.p1.y,t.p2.y),n=T(t.p1.x,t.p2.x),i=T(t.p1.y,t.p2.y);return v.roots(this.points,t).filter((t=>{var o=this.get(t);return v.between(o.x,e,n)&&v.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=v.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=v.dist(t,e),a=v.dist(t,o),u=v.dist(t,s);return _(a-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),h=!1,x=!1,f=i,p=1;do{if(x=h,a=c,f=(n+i)/2,o=this.get(f),s=this.get(i),c=v.getccenter(l,o,s),c.interval={start:n,end:i},h=this._error(c,l,n,i)<=t,u=x&&!h,u||(p=i),h){if(i>=1){if(c.interval.end=p=1,a=c,i>1){let t={x:c.x+c.r*E(c.e),y:c.y+c.r*O(c.e)};c.e+=v.angle({x:c.x,y:c.y},t,this.get(1))}break}i+=(i-n)/2}else i=f}while(!u&&r++<100);if(r>=100)break;a=a||c,e.push(a),n=p}while(i<1);return e}}var I;exports.VertexType=void 0,(I=exports.VertexType||(exports.VertexType={})).TOP_LEFT="tl",I.TOP_RIGHT="tr",I.BOTTOM_LEFT="bl",I.BOTTOM_RIGHT="br";class S extends r.BaseMode{constructor(t=r.THEME_COLOR,e=r.SUB_THEME_COLOR,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.defaults(n,this.options)}static execute(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))},i=t=>{const e=n.calcIntersection(t.tl,t.tr,t.bl,t.br),r=n.calcIntersection(t.tl,t.bl,t.tr,t.br),i=e=>e&&Object.values(t).some(((t,r,n)=>((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})(e,n[r],n[(r+1)%4],n[(r+2)%4])));return i(e)||i(r)};return t.areaBoundCurves.forEach(((s,c)=>{const a=[];s.forEach(((e,s)=>{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(i(h))throw new Error("[Fabric-Texture Error] Invalid perspective shape: The four control points cannot form a triangle or cross each other");const x={left:new j(l.left.points).length(),right:new j(l.right.points).length(),top:new j(l.top.points).length(),bottom:new j(l.bottom.points).length()},f=n.calcIntersection(h.tl,h.bl,h.tr,h.br),p=n.calcIntersection(h.tl,h.tr,h.bl,h.br),{vertical:y,horizontal:d}=t.mesh[c][s],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 i=l.top.get(e/g),s=l.bottom.get(e/g);if(p){const t=o(h.tl,p)/o(h.tr,p),n=r(x.left,x.left/t,e,g);i=l.top.get(n);const c=o(h.bl,p)/o(h.br,p),a=r(x.left,x.left/c,e,g);s=l.bottom.get(a)}let c=l.left.get(t/m),a=l.right.get(t/m);if(f){const e=o(h.tl,f)/o(h.bl,f),n=r(x.top,x.top/e,t,m);c=l.left.get(n);const i=o(h.tr,f)/o(h.br,f),s=r(x.top,x.top/i,t,m);a=l.right.get(s)}const y=n.calcIntersection(i,s,c,a);y?u.push(y):u.push(i)}a.push(u)})),e.push(a)})),e}static createDefaultControl(t){return new e.fabric.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 S.execute(t)}dirtyRender(t){var r;const n=t.canvas;if(!n)return;const o=null===(r=t.texture)||void 0===r?void 0:r.areaBoundCurves;if(!o)return;o.forEach(((r,i)=>{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()))),h=this._styleSetters.control(S.createDefaultControl(this.themeColor)),x={first:0,last:3}[{top:"first",right:"first",bottom:"last",left:"last"}[r]];h.set({left:l[x].x,top:l[x].y}),n.add(h),this._objectControlMap.set(h,{curve:c,targetPointIdx:x,rowIndex:i,colIndex:o,vertexType:a})}))}))}));const{enableDragResize:s,enableShiftConstraint:c}=this.options,a=[[c,()=>{let t=!1;const e=e=>{t=e.shiftKey},r=e=>{t=e.shiftKey},i=e=>{if(t&&e.target&&e.transform){const{left:t=0,top:r=0}=e.target,{left:n,top:i}=e.transform.original;Math.abs(t-n)>Math.abs(r-i)?e.target.set({top:i}).setCoords():e.target.set({left:n}).setCoords()}};return n.on("object:moving",i),window.addEventListener("keydown",e),window.addEventListener("keyup",r),()=>{n.off("object:moving",i),window.removeEventListener("keydown",e),window.removeEventListener("keyup",r)}}],[s,()=>{const e=e=>{if(1!==e.e.buttons)return;if(e.target&&e.target.selectable)return;let r=!1;n.selection=!1;const o=i.calcFabricCanvasCoord(n,e.pointer),s=e=>{const s=i.calcFabricCanvasCoord(n,e.pointer);if(!r&&(Math.abs(s.x-o.x)<50||Math.abs(s.y-o.y)<50))return;const c=i.calcFabricRelativeCoord({left:o.x,top:o.y},t.curvePathMap.get(t.texture.areaBoundCurves[0][0].top)),a=i.calcFabricRelativeCoord({left:s.x,top:s.y},t.curvePathMap.get(t.texture.areaBoundCurves[0][0].bottom));t.texture.setVertexCoord(0,0,exports.VertexType.TOP_LEFT,c),t.texture.setVertexCoord(0,0,exports.VertexType.TOP_RIGHT,{x:a.x,y:c.y}),t.texture.setVertexCoord(0,0,exports.VertexType.BOTTOM_LEFT,{x:c.x,y:a.y}),t.texture.setVertexCoord(0,0,exports.VertexType.BOTTOM_RIGHT,a),t.render(!0,{withoutRecord:!0}),r=!0},c=e=>{r&&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 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:h}=o,x=t.curvePathMap.get(c),f=x.calcOwnMatrix(),p=i.calcFabricRelativeCoord(s,x),y=Object.assign({},c.points[a]);try{t.texture.setVertexCoord(u,l,h,p),t.render(!1,{withoutRecord:!0})}catch(r){t.texture.setVertexCoord(u,l,h,y),t.render(!1,{withoutRecord:!0});const n=c.points.map((t=>e.fabric.util.transformPoint(new e.fabric.Point(t.x-x.pathOffset.x,t.y-x.pathOffset.y),f)));s.set({left:n[a].x,top:n[a].y}),s.setCoords()}finally{this.controlObjects.forEach((t=>{n.bringToFront(t)}))}},o=e=>{const r=e.target;r&&"drag"===e.action&&this.controlObjects.includes(r)&&t.record()};return n.on("object:moving",r),n.on("object:modified",o),()=>{n.off("object:moving",r),n.off("object:modified",o)}}]].map((([t,e])=>t&&e()));return()=>{a.forEach((t=>{"function"==typeof t&&t()})),n.remove(...this._objectControlMap.keys()),this._objectControlMap.clear()}}}exports.default=S;
//# sourceMappingURL=perspective.js.map