fabric-warpvas
Version:
This JavaScript library helps you efficiently build image warping tools on Fabric.js
1 lines • 59.2 kB
JavaScript
function e(t,e,n,r){return new(n||(n=Promise))((function(i,o){function s(t){try{c(r.next(t))}catch(t){o(t)}}function a(t){try{c(r.throw(t))}catch(t){o(t)}}function c(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,a)}c((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const{abs:n,cos:r,sin:i,acos:o,atan2:s,sqrt:a,pow:c}=Math;function l(t){return t<0?-c(-t,1/3):c(t,1/3)}const h=Math.PI,u=2*h,p=h/2,g=Number.MAX_SAFE_INTEGER||9007199254740991,f=Number.MIN_SAFE_INTEGER||-9007199254740991,x={x:0,y:0,z:0},y={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),a(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,a,c=i*i,l=t*t,h=0;2===r?(o=[o[0],o[1],o[2],x],e=c,s=i*t*2,a=l):3===r&&(e=c*i,s=c*t*3,a=i*l*3,h=t*l);const u={x:e*o[0].x+s*o[1].x+a*o[2].x+h*o[3].x,y:e*o[0].y+s*o[1].y+a*o[2].y+h*o[3].y,t:t};return n&&(u.z=e*o[0].z+s*o[1].z+a*o[2].z+h*o[3].z),u}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 a,c=o[0],l=o[1],h=o[2],u=o[3];return c*=i,l*=t,2===s.length?(a=c+l,{x:(c*s[0].x+l*s[1].x)/a,y:(c*s[0].y+l*s[1].y)/a,z:!!r&&(c*s[0].z+l*s[1].z)/a,t:t}):(c*=i,l*=2*i,h*=t*t,3===s.length?(a=c+l+h,{x:(c*s[0].x+l*s[1].x+h*s[2].x)/a,y:(c*s[0].y+l*s[1].y+h*s[2].y)/a,z:!!r&&(c*s[0].z+l*s[1].z+h*s[2].z)/a,t:t}):(c*=i,l*=1.5*i,h*=3*i,u*=t*t*t,4===s.length?(a=c+l+h+u,{x:(c*s[0].x+l*s[1].x+h*s[2].x+u*s[3].x)/a,y:(c*s[0].y+l*s[1].y+h*s[2].y+u*s[3].y)/a,z:!!r&&(c*s[0].z+l*s[1].z+h*s[2].z+u*s[3].z)/a,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||y.approximately(t,e)||y.approximately(t,n)},approximately:function(t,e,r){return n(t-e)<=(r||1e-6)},length:function(t){const e=y.Tvalues.length;let n=0;for(let r,i=0;i<e;i++)r=.5*y.Tvalues[i]+.5,n+=y.Cvalues[i]*y.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(y.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,a=n.y-t.y;return s(r*a-i*o,r*o+i*a)},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 a(n*n+r*r)},closest:function(t,e){let n,r,i=c(2,63);return t.forEach((function(t,o){r=y.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 r=c(t,e)+c(1-t,e);return n((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 n=c(1-t,e);return n/(c(t,e)+n)},lli8:function(t,e,n,r,i,o,s,a){const c=(t-n)*(o-a)-(e-r)*(i-s);return 0!=c&&{x:((t*r-e*n)*(i-s)-(t-n)*(i*a-o*s))/c,y:((t*r-e*n)*(o-a)-(e-r)*(i*a-o*s))/c}},lli4:function(t,e,n,r){const i=t.x,o=t.y,s=e.x,a=e.y,c=n.x,l=n.y,h=r.x,u=r.y;return y.lli8(i,o,s,a,c,l,h,u)},lli:function(t,e){return y.lli4(t,t.c,e,e.c)},makeline:function(t,e){return new z(t.x,t.y,(t.x+e.x)/2,(t.y+e.y)/2,e.x,e.y)},findbbox:function(t){let e=g,n=g,r=f,i=f;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(!y.bboxoverlap(e,r))return[];const o=[],s=[t.startcap,t.forward,t.back,t.endcap],a=[n.startcap,n.forward,n.back,n.endcap];return s.forEach((function(e){e.virtual||a.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=y.makeline(e.points[r-1],t.points[0]),s=y.makeline(t.points[i-1],e.points[0]),a={startcap:o,forward:t,back:e,endcap:s,bbox:y.findbbox([o,t,e,s]),intersections:function(t){return y.shapeintersections(a,a.bbox,t,t.bbox,n)}};return a},getminmax:function(t,e,n){if(!n)return{min:0,max:0};let r,i,o=g,s=f;-1===n.indexOf(0)&&(n=[0].concat(n)),-1===n.indexOf(1)&&n.push(1);for(let a=0,c=n.length;a<c;a++)r=n[a],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,o=e.p1.y,a=-s(e.p2.y-o,e.p2.x-n);return t.map((function(t){return{x:(t.x-n)*r(a)-(t.y-o)*i(a),y:(t.x-n)*i(a)+(t.y-o)*r(a)}}))},roots:function(t,e){e=e||{p1:{x:0,y:0},p2:{x:1,y:0}};const n=t.length-1,i=y.align(t,e),s=function(t){return 0<=t&&t<=1};if(2===n){const t=i[0].y,e=i[1].y,n=i[2].y,r=t-2*e+n;if(0!==r){const i=-a(e*e-t*n),o=-t+e;return[-(i+o)/r,-(-i+o)/r].filter(s)}return e!==n&&0===r?[(2*e-n)/(2*e-2*n)].filter(s):[]}const c=i[0].y,h=i[1].y,p=i[2].y;let g=3*h-c-3*p+i[3].y,f=3*c-6*h+3*p,x=-3*c+3*h,d=c;if(y.approximately(g,0)){if(y.approximately(f,0))return y.approximately(x,0)?[]:[-d/x].filter(s);const t=a(x*x-4*f*d),e=2*f;return[(t-x)/e,(-x-t)/e].filter(s)}f/=g,x/=g,d/=g;const m=(3*x-f*f)/3,v=m/3,b=(2*f*f*f-9*f*x+27*d)/27,_=b/2,w=_*_+v*v*v;let E,C,T,z,S;if(w<0){const t=-m/3,e=a(t*t*t),n=-b/(2*e),i=o(n<-1?-1:n>1?1:n),c=2*l(e);return T=c*r(i/3)-f/3,z=c*r((i+u)/3)-f/3,S=c*r((i+2*u)/3)-f/3,[T,z,S].filter(s)}if(0===w)return E=_<0?l(-_):-l(_),T=2*E-f/3,z=-E-f/3,[T,z].filter(s);{const t=a(w);return E=l(-_+t),C=l(_+t),[E-C-f/3].filter(s)}},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=-a(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,r,i,o){let s,l,h,u,p=0,g=0;const f=y.compute(t,e),x=y.compute(t,r),d=f.x*f.x+f.y*f.y;if(i?(s=a(c(f.y*x.z-x.y*f.z,2)+c(f.z*x.x-x.z*f.x,2)+c(f.x*x.y-x.x*f.y,2)),l=c(d+f.z*f.z,1.5)):(s=f.x*x.y-f.y*x.x,l=c(d,1.5)),0===s||0===l)return{k:0,r:0};if(p=s/l,g=l/s,!o){const o=y.curvature(t-.001,e,r,i,!0).k,s=y.curvature(t+.001,e,r,i,!0).k;u=(s-p+(p-o))/2,h=(n(s-p)+n(p-o))/2}return{k:p,r:g,dk:u,adk:h}},inflections:function(t){if(t.length<4)return[];const e=y.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),a=18*(i-n);if(y.approximately(o,0)){if(!y.approximately(s,0)){let t=-a/s;if(0<=t&&t<=1)return[t]}return[]}const c=2*o;if(y.approximately(c,0))return[];const l=s*s-4*o*a;if(l<0)return[];const h=Math.sqrt(l);return[(h-s)/c,-(s+h)/c].filter((function(t){return 0<=t&&t<=1}))},bboxoverlap:function(t,e){const r=["x","y"],i=r.length;for(let o,s,a,c,l=0;l<i;l++)if(o=r[l],s=t[o].mid,a=e[o].mid,c=(t[o].size+e[o].size)/2,n(s-a)>=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 a=t.split(.5),c=e.split(.5),l=[{left:a.left,right:c.left},{left:a.left,right:c.right},{left:a.right,right:c.right},{left:a.right,right:c.left}];l=l.filter((function(t){return y.bboxoverlap(t.left.bbox(),t.right.bbox())}));let h=[];return 0===l.length||(l.forEach((function(t){h=h.concat(y.pairiteration(t.left,t.right,s))})),h=h.filter((function(t,e){return h.indexOf(t)===e}))),h},getccenter:function(t,e,n){const o=e.x-t.x,a=e.y-t.y,c=n.x-e.x,l=n.y-e.y,h=o*r(p)-a*i(p),g=o*i(p)+a*r(p),f=c*r(p)-l*i(p),x=c*i(p)+l*r(p),d=(t.x+e.x)/2,m=(t.y+e.y)/2,v=(e.x+n.x)/2,b=(e.y+n.y)/2,_=d+h,w=m+g,E=v+f,C=b+x,T=y.lli8(d,m,_,w,v,b,E,C),z=y.dist(T,t);let S,R=s(t.y-T.y,t.x-T.x),P=s(e.y-T.y,e.x-T.x),O=s(n.y-T.y,n.x-T.x);return R<O?((R>P||P>O)&&(R+=u),R>O&&(S=O,O=R,R=S)):O<P&&P<R?(S=O,O=R,R=S):O+=u,T.s=R,T.e=O,T.r=z,T},numberSort:function(t,e){return t-e}};class d{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 y.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++)y.expandbox(e,t[n].bbox());return e}offset(t){const e=[];return this.curves.forEach((function(n){e.push(...n.offset(t))})),new d(e)}}const{abs:m,min:v,max:b,cos:_,sin:w,acos:E,sqrt:C}=Math,T=Math.PI;class z{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 a={x:e[t],y:e[t+1]};o&&(a.z=e[t+2]),s.push(a)}const c=this.order=s.length-1,l=this.dims=["x","y"];o&&l.push("z"),this.dimlen=l.length;const h=y.align(s,{p1:s[0],p2:s[c]}),u=y.dist(s[0],s[c]);this._linear=h.reduce(((t,e)=>t+m(e.y)),0)<u/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 z(e,e,n);if(1===r)return new z(t,e,e);const i=z.getABC(2,t,e,n,r);return new z(t,i.A,n)}static cubicFromPoints(t,e,n,r,i){void 0===r&&(r=.5);const o=z.getABC(3,t,e,n,r);void 0===i&&(i=y.dist(e,o.C));const s=i*(1-r)/r,a=y.dist(t,n),c=(n.x-t.x)/a,l=(n.y-t.y)/a,h=i*c,u=i*l,p=s*c,g=s*l,f=e.x-h,x=e.y-u,d=e.x+p,m=e.y+g,v=o.A,b=v.x+(f-v.x)/(1-r),_=v.y+(x-v.y)/(1-r),w=v.x+(d-v.x)/r,E=v.y+(m-v.y)/r,C={x:t.x+(b-t.x)/r,y:t.y+(_-t.y)/r},T={x:n.x+(w-n.x)/(1-r),y:n.y+(E-n.y)/(1-r)};return new z(t,C,T,n)}static getUtils(){return y}getUtils(){return z.getUtils()}static get PolyBezier(){return d}valueOf(){return this.toString()}toString(){return y.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=y.derive(this.points,this._3d),this.computedirection()}computedirection(){const t=this.points,e=y.angle(t[0],t[this.order],t[1]);this.clockwise=e>0}length(){return y.length(this.derivative.bind(this))}static getABC(t=2,e,n,r,i=.5){const o=y.projectionratio(i,t),s=1-o,a={x:o*e.x+s*r.x,y:o*e.y+s*r.y},c=y.abcratio(i,t);return{A:{x:n.x+(n.x-a.x)/c,y:n.y+(n.y-a.y)/c},B:n,C:a,S:e,E:r}}getABC(t,e){e=e||this.get(t);let n=this.points[0],r=this.points[this.order];return z.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],y.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=y.closest(e,t),i=r.mpos,o=(i-1)/n,s=(i+1)/n,a=.1/n;let c,l=r.mdist,h=o,u=h;l+=1;for(let e;h<s+a;h+=a)c=this.compute(h),e=y.dist(t,c),e<l&&(l=e,u=h);return u=u<0?0:u>1?1:u,c=this.compute(u),c.t=u,c.d=l,c}get(t){return this.compute(t)}point(t){return this.points[t]}compute(t){return this.ratios?y.computeWithRatios(t,this.points,this.ratios,this._3d):y.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 z(e)}derivative(t){return y.compute(t,this.dpoints[0],this._3d)}dderivative(t){return y.compute(t,this.dpoints[1],this._3d)}align(){let t=this.points;return new z(y.align(t,{p1:t[0],p2:t[t.length-1]}))}curvature(t){return y.curvature(t,this.dpoints[0],this.dpoints[1],this._3d)}inflections(){return y.inflections(this.points)}normal(t){return this._3d?this.__normal3(t):this.__normal2(t)}__normal2(t){const e=this.derivative(t),n=C(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=C(e.x*e.x+e.y*e.y+e.z*e.z),i=C(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=C(o.x*o.x+o.y*o.y+o.z*o.z);o.x/=s,o.y/=s,o.z/=s;const a=[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:a[0]*e.x+a[1]*e.y+a[2]*e.z,y:a[3]*e.x+a[4]*e.y+a[5]*e.z,z:a[6]*e.x+a[7]*e.y+a[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,a=e.length-1;s<a;s++)o=y.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 z([n[0],n[3],n[5]]):new z([n[0],n[4],n[7],n[9]]),right:2===this.order?new z([n[5],n[4],n[2]]):new z([n[9],n[8],n[6],n[3]]),span:n};return r.left._t1=y.map(0,0,1,this._t1,this._t2),r.left._t2=y.map(t,0,1,this._t1,this._t2),r.right._t1=y.map(t,0,1,this._t1,this._t2),r.right._t2=y.map(1,0,1,this._t1,this._t2),e?(e=y.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]=y.droots(i),3===this.order&&(i=this.dpoints[1].map(r),t[n]=t[n].concat(y.droots(i))),t[n]=t[n].filter((function(t){return t>=0&&t<=1})),e=e.concat(t[n].sort(y.numberSort))}.bind(this)),t.values=e.sort(y.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]=y.getminmax(this,n,t[n])}.bind(this)),e}overlaps(t){const e=this.bbox(),n=t.bbox();return y.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 z(n)]}return this.reduce().map((function(e){return e._linear?e.offset(t)[0]:e.scale(t)}))}simple(){if(3===this.order){const t=y.angle(this.points[0],this.points[3],this.points[1]),e=y.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),m(E(n))<T/3}reduce(){let t,e,n=0,r=0,i=.01,o=[],s=[],a=this.extrema().values;for(-1===a.indexOf(0)&&(a=[0].concat(a)),-1===a.indexOf(1)&&a.push(1),n=a[0],t=1;t<a.length;t++)r=a[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,m(n-r)<i)return[];e=t.split(n,r),e._t1=y.map(n,0,1,t._t1,t._t2),e._t2=y.map(r,0,1,t._t1,t._t2),s.push(e),n=r;break}n<1&&(e=t.split(n,1),e._t1=y.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 z(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,a=[this.offset(0,10),this.offset(1,10)],c=[],l=y.lli4(a[0],a[0].c,a[1],a[1].c);if(!l)throw new Error("cannot scale this curve. Try reducing it first.");return[0,1].forEach((function(t){const n=c[t*e]=y.copy(i[t*e]);n.x+=(t?s:o)*a[t].n.x,n.y+=(t?s:o)*a[t].n.y})),n?([0,1].forEach((function(o){if(2!==e||!o){var s=i[o+1],a={x:s.x-l.x,y:s.y-l.y},h=n?n((o+1)/e):t;n&&!r&&(h=-h);var u=C(a.x*a.x+a.y*a.y);a.x/=u,a.y/=u,c[o+1]={x:s.x+h*a.x,y:s.y+h*a.y}}})),new z(c)):([0,1].forEach((t=>{if(2===e&&t)return;const n=c[t*e],r=this.derivative(t),o={x:n.x+r.x,y:n.y+r.y};c[t+1]=y.lli4(n,o,l,i[t+1])})),new z(c))}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 a,c,l;void 0===n&&(n=t,r=e),a={x:o.x+i.x*t,y:o.y+i.y*t},l={x:s.x+i.x*n,y:s.y+i.y*n},c={x:(a.x+l.x)/2,y:(a.y+l.y)/2};const h=[a,c,l];a={x:o.x-i.x*e,y:o.y-i.y*e},l={x:s.x-i.x*r,y:s.y-i.y*r},c={x:(a.x+l.x)/2,y:(a.y+l.y)/2};const u=[l,c,a],p=y.makeline(u[2],h[0]),g=y.makeline(h[2],u[0]),f=[p,new z(h),g,new z(u)];return new d(f)}const i=this.reduce(),o=i.length,s=[];let a,c=[],l=0,h=this.length();const u=void 0!==n&&void 0!==r;function p(t,e,n,r,i){return function(o){const s=r/n,a=(r+i)/n,c=e-t;return y.map(o,0,1,t+s*c,t+a*c)}}i.forEach((function(i){const o=i.length();u?(s.push(i.scale(p(t,n,h,l,o))),c.push(i.scale(p(-e,-r,h,l,o)))):(s.push(i.scale(t)),c.push(i.scale(-e))),l+=o})),c=c.map((function(t){return a=t.points,a[3]?t.points=[a[3],a[2],a[1],a[0]]:t.points=[a[2],a[1],a[0]],t})).reverse();const g=s[0].points[0],f=s[o-1].points[s[o-1].points.length-1],x=c[o-1].points[c[o-1].points.length-1],m=c[0].points[0],v=y.makeline(x,g),b=y.makeline(f,m),_=[v].concat(s).concat([b]).concat(c);return new d(_)}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=y.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 z&&(t=t.reduce()),this.curveintersects(this.reduce(),t,e)):this.selfintersects(e)}lineIntersects(t){const e=v(t.p1.x,t.p2.x),n=v(t.p1.y,t.p2.y),r=b(t.p1.x,t.p2.x),i=b(t.p1.y,t.p2.y);return y.roots(this.points,t).filter((t=>{var o=this.get(t);return y.between(o.x,e,r)&&y.between(o.y,n,i)}))}selfintersects(t){const e=this.reduce(),n=e.length-2,r=[];for(let i,o,s,a=0;a<n;a++)o=e.slice(a,a+1),s=e.slice(a+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=y.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),a=y.dist(t,e),c=y.dist(t,o),l=y.dist(t,s);return m(c-a)+m(l-a)}_iterate(t,e){let n,r=0,i=1;do{n=0,i=1;let o,s,a,c,l,h=this.get(r),u=!1,p=!1,g=i,f=1;do{if(p=u,c=a,g=(r+i)/2,o=this.get(g),s=this.get(i),a=y.getccenter(h,o,s),a.interval={start:r,end:i},u=this._error(a,h,r,i)<=t,l=p&&!u,l||(f=i),u){if(i>=1){if(a.interval.end=f=1,c=a,i>1){let t={x:a.x+a.r*_(a.e),y:a.y+a.r*w(a.e)};a.e+=y.angle({x:a.x,y:a.y},t,this.get(1))}break}i+=(i-r)/2}else i=g}while(!l&&n++<100);if(n>=100)break;c=c||a,e.push(c),r=f}while(i<1);return e}}const S=(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}},R=(t,e)=>Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2)),P=(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}},O=(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},M=(t,e)=>{const[n,r,i]=t,[o,s,a]=e,c=.1/Math.abs([...t,...e].reduce(((t,e)=>Math.min(t,e.x,e.y)),-1)),l=(t,e,n)=>{const[r,i,o,s]=t.map(Number),[a,c,l,h]=e.map(Number),[u,p,g,f]=n.map(Number),x=o-i*l/c,y=l-c*g/p,d=h-c*f/p,m=a-c*u/p,v=(x/y*d-(s-i*h/c))/(x/y*m-(r-i*a/c)),b=(d-m*v)/y;return{x:v,y:(s-r*v-o*b)/i,z:b}},h=[o.x+c,o.y+c,1,n.x+c],u=[s.x+c,s.y+c,1,r.x+c],p=[a.x+c,a.y+c,1,i.x+c],g=l(h,u,p);h[3]=n.y+c,u[3]=r.y+c,p[3]=i.y+c;const f=l(h,u,p),x=g.x,y=g.y,d=g.z;return[x,f.x,y,f.y,d,f.z]},B=(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};var A=Object.freeze({__proto__:null,calcBoundingBox:P,calcCoordDistance:R,calcExpandCoord:(t,e,n,r=1)=>{const i=S(e,n,t),o=R(t,i),s=O(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)),a=t.y-o*t.x,c=n.y-s*n.x;if(Math.abs(o)===1/0)return{x:t.x,y:s*t.x+c};if(Math.abs(s)===1/0)return{x:n.x,y:o*n.x+a};const l=(c-a)/(o-s);return{x:l,y:o*l+a}},calcMatrix:M,calcPerpendicularIntersection:S,calcRelativeCoord:O,isTriangleContainsPoint:B});const I=(t,e,n,r,i)=>{var o;const{x:s,y:a,sourceScale:c=1,destinationScale:l=1}=r,{gridColor:h={r:255,g:0,b:0,a:1},enableContentDisplay:u=!0,enableGridDisplay:p=!1,enableGridVertexDisplay:g=!1}=null!==(o=t.renderingConfig)&&void 0!==o?o:{},{calcBoundingBox:f,calcPerpendicularIntersection:x,calcCoordDistance:y,calcRelativeCoord:d,calcMatrix:m}=i,v=(t,e,n,r=1)=>{const i=x(e,n,t),o=y(t,i),s=d(t,i,o+r);return{x:s.x-i.x+e.x,y:s.y-i.y+e.y}},b=n.getContext("2d");b.clearRect(0,0,n.width,n.height);const _=Math.ceil(1/(c*l));b.save(),b.transform(c*l,0,0,c*l,s*c*l,a*c*l);const w=(t,n,r,i,o,s,a)=>{const l=[t,r,o],x=new Path2D;for(let t=0;t<3;t++){const e=l[t],n=l[(t+1)%3],r=l[(t+2)%3],i=v(e,n,r,_),o=v(e,r,n,_);x.lineTo(i.x,i.y),x.lineTo(o.x,o.y)}if(u){b.save(),b.clip(x);const l=m([t,r,o],[n,i,s]);b.transform(...l);const h=f(n,i,s),u=(a.x-1)*c,p=(a.y-1)*c,g=(h.width+2)*c,y=(h.height+2)*c,d=a.x-1/c,v=a.y-1/c,_=h.width+2/c,w=h.height+2/c;b.drawImage(e,u,p,g,y,d,v,_,w),b.restore()}if(g){const e=Math.floor(2/c);b.fillStyle=`rgba(${h.r}, ${h.g}, ${h.b}, ${h.a})`,b.fillRect(t.x-e/2,t.y-e/2,e,e)}if(p){const t=Math.floor(1/c);b.lineWidth=t,b.strokeStyle="rgba(255, 0, 0, 0.5)",b.stroke(x)}};return t.regionPoints.forEach(((e,n)=>{e.forEach(((e,r)=>{const i=e,o=t.regionCurves[n][r].vertical.length-1;i.forEach(((e,s)=>{const a=i[s],c=i[s+1],l=i[s+o+2],h=i[s+o+1],u=t.originalRegionPoints[n][r][s],p=t.originalRegionPoints[n][r][s+1],g=t.originalRegionPoints[n][r][s+o+2],f=t.originalRegionPoints[n][r][s+o+1];c&&l&&s%(o+1)<o&&(w(a,u,c,p,h,f,u),w(l,g,c,p,h,f,u))}))}))})),b.restore(),n},L=(t,e,n,r)=>{const{width:i,height:o,sourceScale:s=1,destinationScale:a=1}=r;return(n=null!=n?n:document.createElement("canvas")).width=i*a*s,n.height=o*a*s,I(t,e,n,r,{calcBoundingBox:P,calcPerpendicularIntersection:S,calcCoordDistance:R,calcRelativeCoord:O,calcMatrix:M})},j=(t,e,n,r)=>{var i;const{x:o,y:s,width:a,height:c,sourceScale:l=1}=r,{enableAntialias:h=!0,gridColor:u={r:255,g:0,b:0,a:1},enableContentDisplay:p=!0,enableGridDisplay:g=!1,enableGridVertexDisplay:f=!1}=null!==(i=t.renderingConfig)&&void 0!==i?i:{},{width:x,height:y}=e,d=n.getContext("webgl",{antialias:h});if(!d)throw Error("[Warpvas] Failed to initialize WebGL. Your browser or device may not support it.");const m={before:[],after:[]};t.regionPoints.forEach(((e,n)=>{e.forEach(((e,r)=>{const i=e,o=t.regionCurves[n][r].vertical.length-1;i.forEach(((e,s)=>{const a=i[s],c=i[s+1],l=i[s+o+2],h=i[s+o+1],u=t.originalRegionPoints[n][r][s],p=t.originalRegionPoints[n][r][s+1],g=t.originalRegionPoints[n][r][s+o+2],f=t.originalRegionPoints[n][r][s+o+1];c&&l&&s%(o+1)<o&&(m.before.push(u,p,f,g,p,f),m.after.push(a,c,h,l,c,h))}))}))}));const v="\n attribute vec4 aVertexPosition;\n attribute vec2 aTextureCoord;\n varying highp vec2 vTextureCoord;\n void main(void) {\n gl_Position = aVertexPosition;\n gl_PointSize = 2.0;\n vTextureCoord = aTextureCoord;\n }\n ",b=(t,e)=>{const n=d.createShader(t);return n?(d.shaderSource(n,e),d.compileShader(n),d.getShaderParameter(n,d.COMPILE_STATUS)?n:(console.error("[Warpvas] Shader compilation error:",d.getShaderInfoLog(n)),d.deleteShader(n),null)):(console.error("[Warpvas] Failed to create shader."),null)},_=(t,e)=>{const n=b(d.VERTEX_SHADER,t);if(!n)return console.error("[Warpvas] Failed to initialize vertex shader."),null;const r=b(d.FRAGMENT_SHADER,e);if(!r)return console.error("[Warpvas] Failed to initialize fragment shader."),null;const i=d.createProgram();return i?(d.attachShader(i,n),d.attachShader(i,r),d.linkProgram(i),d.getProgramParameter(i,d.LINK_STATUS)?i:(console.error("[Warpvas] Shader program linking error:",d.getProgramInfoLog(i)),null)):(console.error("[Warpvas] Failed to create shader program."),null)},w=t=>!(t&t-1),E=_(v,"\n varying highp vec2 vTextureCoord;\n uniform sampler2D uSampler;\n void main(void) {\n gl_FragColor = texture2D(uSampler, vTextureCoord);\n }\n ");if(!E)throw Error("[Warpvas] Failed to initialize shader program.");const C={program:E,attribLocations:{vertexPosition:d.getAttribLocation(E,"aVertexPosition"),textureCoord:d.getAttribLocation(E,"aTextureCoord")},uniformLocations:{uSampler:d.getUniformLocation(E,"uSampler")}},T=d.createTexture();if(!T)throw Error("[Warpvas] Failed to create WebGL texture object.");return d.bindTexture(d.TEXTURE_2D,T),d.texImage2D(d.TEXTURE_2D,0,d.RGBA,d.RGBA,d.UNSIGNED_BYTE,e),w(e.width)&&w(e.height)&&d.generateMipmap(d.TEXTURE_2D),d.enable(d.BLEND),d.blendFunc(d.SRC_ALPHA,d.ONE_MINUS_SRC_ALPHA),d.texParameteri(d.TEXTURE_2D,d.TEXTURE_WRAP_S,d.CLAMP_TO_EDGE),d.texParameteri(d.TEXTURE_2D,d.TEXTURE_WRAP_T,d.CLAMP_TO_EDGE),d.texParameteri(d.TEXTURE_2D,d.TEXTURE_MIN_FILTER,d.LINEAR),d.viewport(0,0,n.width,n.height),((t,e,n)=>{d.clearColor(0,0,0,0),d.clear(d.COLOR_BUFFER_BIT);const r=d.FLOAT,i=!1,o=4*Float32Array.BYTES_PER_ELEMENT;d.bindBuffer(d.ARRAY_BUFFER,e.position),d.vertexAttribPointer(t.attribLocations.vertexPosition,2,r,i,o,0),d.enableVertexAttribArray(t.attribLocations.vertexPosition),d.bindBuffer(d.ARRAY_BUFFER,e.textureCoord),d.vertexAttribPointer(t.attribLocations.textureCoord,2,r,i,o,0),d.enableVertexAttribArray(t.attribLocations.textureCoord),d.useProgram(t.program),d.activeTexture(d.TEXTURE0),d.bindTexture(d.TEXTURE_2D,n),d.uniform1i(t.uniformLocations.uSampler,0),p&&d.drawArrays(d.TRIANGLES,0,m.before.length);const{r:s,g:a,b:c,a:l}=u,h=`${(s/255).toFixed(1)}, ${(a/255).toFixed(1)}, ${(c/255).toFixed(1)}, ${l.toFixed(1)}`,x=_(v,`\n void main(void) {\n gl_FragColor = vec4(${h});\n }\n `);if(!x)throw Error("[Warpvas] Failed to initialize outline shader program.");d.bindBuffer(d.ARRAY_BUFFER,e.position),d.vertexAttribPointer(d.getAttribLocation(x,"aVertexPosition"),2,r,i,0,0),d.enableVertexAttribArray(d.getAttribLocation(x,"aVertexPosition")),d.bindBuffer(d.ARRAY_BUFFER,e.textureCoord),d.vertexAttribPointer(d.getAttribLocation(x,"aTextureCoord"),2,r,i,0,0),d.enableVertexAttribArray(d.getAttribLocation(x,"aTextureCoord")),d.useProgram(x),g&&d.drawArrays(d.LINES,0,2*m.before.length),f&&d.drawArrays(d.POINTS,0,2*m.before.length)})(C,(()=>{const t=d.createBuffer();d.bindBuffer(d.ARRAY_BUFFER,t);const e=m.before.map(((t,e)=>{const n=[t.x/(x/l),t.y/(y/l)];return(e+1)%3==0?n.push(m.before[e-2].x/(x/l),m.before[e-2].y/(y/l)):n.push(m.before[e+1].x/(x/l),m.before[e+1].y/(y/l)),n})).flat(1);d.bufferData(d.ARRAY_BUFFER,new Float32Array(e),d.STATIC_DRAW);const n=d.createBuffer();d.bindBuffer(d.ARRAY_BUFFER,n);const r=m.after.map(((t,e)=>{const n=[(t.x+o)/(a/2)-1,-((t.y+s)/(c/2)-1)];return(e+1)%3==0?n.push((m.after[e-2].x+o)/(a/2)-1,-((m.after[e-2].y+s)/(c/2)-1)):n.push((m.after[e+1].x+o)/(a/2)-1,-((m.after[e+1].y+s)/(c/2)-1)),n})).flat(1);return d.bufferData(d.ARRAY_BUFFER,new Float32Array(r),d.STATIC_DRAW),{position:n,textureCoord:t}})(),T),n},F=(t,e,n,r)=>{const{width:i,height:o,sourceScale:s=1,destinationScale:a=1}=r;return(n=null!=n?n:document.createElement("canvas")).width=i*a*s,n.height=o*a*s,j(t,e,n,r)},D="WORKER_MESSAGE",U="JOB_RESULT",W=t=>t instanceof ArrayBuffer||t instanceof MessagePort||self.ImageBitmap&&t instanceof ImageBitmap,k=()=>{const t=[];let e,n=new Map;const r={collect:t=>{for(let e in t)n.set(e,t[e]);return r},onMessage:t=>(e=t,r),create:r=>{const i=r.toString(),o=`\n // 将收集到的变量声明和定义加入脚本字符串,后面函数执行的时候便不会出现not defined的错误了\n $collections = {};\n ${Object.entries(Object.fromEntries(n)).reduce(((t,[e,n])=>t+`$collections['${e}']=`+("function"==typeof n?`${n};`:`JSON.parse(\`${JSON.stringify(n)}\`);`)),"")}\n \n // 声明定义用于判断是否是可转移对象的函数\n $isTransferables = ${W}\n \n // 在脚本中声明并定义一个包含工作流程的函数体\n $job=${i};\n \n // 给worker线程添加消息监听,等待主线程的发号施令\n onmessage=e=>{\n const { index, args } = e.data;\n \n Promise.resolve(\n $job.apply($job, args.concat([{\n collections: $collections,\n postMessage: (message) => postMessage({ type: '${D}', message }),\n close: self.close,\n }]))\n ).then(result => {\n postMessage({ type: '${U}', message: { index, result } }, [result].filter($isTransferables))\n }).catch(error => {\n postMessage({ type: '${U}', message: { index, error } })\n })\n }\n `,s=URL.createObjectURL(new Blob([o],{type:"text/javascript"})),a=new Worker(s);return a.onmessage=function(n){const r=n.data;if(r.type===D&&e&&e.call(e,r.message),r.type===U){const{index:e,result:n,error:i}=r.message,{done:o}=t[e];o(i,n)}},{run:(...e)=>new Promise(((n,r)=>{const i=t.length;t.push({done:(t,e)=>{t?r(t):n(e)}}),a.postMessage({index:i,args:e},e.filter(W))})),terminate:()=>{a.terminate(),URL.revokeObjectURL(s),t.length=0,n.clear(),e=void 0}}}};return r},G=(t,n,r,i)=>e(void 0,void 0,void 0,(function*(){const{regionPoints:e,renderingConfig:o,regionCurves:s,originalRegionPoints:a}=t,c={renderingConfig:o,regionCurves:s.map((t=>t.map((t=>({horizontal:{length:t.horizontal.length},vertical:{length:t.vertical.length}}))))),regionPoints:e,originalRegionPoints:a},l=k().collect({warpvas:c,options:i,createWarpedCanvas:I,calcBoundingBox:P,calcPerpendicularIntersection:S,calcCoordDistance:R,calcRelativeCoord:O,calcMatrix:M}).create(((t,{collections:e})=>{const{options:n,warpvas:r,createWarpedCanvas:i,calcBoundingBox:o,calcPerpendicularIntersection:s,calcCoordDistance:a,calcRelativeCoord:c,calcMatrix:l}=e,{width:h,height:u,sourceScale:p=1,destinationScale:g=1}=n,f=new OffscreenCanvas(h*g*p,u*g*p),x=new OffscreenCanvas(t.width,t.height),y=x.getContext("2d");return null==y||y.putImageData(t,0,0),i(r,x,f,n,{calcBoundingBox:o,calcPerpendicularIntersection:s,calcCoordDistance:a,calcRelativeCoord:c,calcMatrix:l}).transferToImageBitmap()})),h=yield l.run(n);return l.terminate(),(r=null!=r?r:document.createElement("canvas")).width=h.width,r.height=h.height,r.getContext("2d").drawImage(h,0,0),r})),$=(t,n,r,i)=>e(void 0,void 0,void 0,(function*(){const{regionPoints:e,renderingConfig:o,regionCurves:s,originalRegionPoints:a}=t,c={renderingConfig:o,regionCurves:s.map((t=>t.map((t=>({horizontal:{length:t.horizontal.length},vertical:{length:t.vertical.length}}))))),regionPoints:e,originalRegionPoints:a},l=k().collect({warpvas:c,options:i,createWarpedCanvas:j}).create(((t,{collections:e})=>{const{options:n,warpvas:r,createWarpedCanvas:i}=e,{width:o,height:s,sourceScale:a=1,destinationScale:c=1}=n;return i(r,t,new OffscreenCanvas(o*c*a,s*c*a),n).transferToImageBitmap()})),h=yield l.run(n);return l.terminate(),(r=null!=r?r:document.createElement("canvas")).width=h.width,r.height=h.height,r.getContext("2d").drawImage(h,0,0),r}));var N,H;!function(t){t.TOP_LEFT="tl",t.TOP_RIGHT="tr",t.BOTTOM_LEFT="bl",t.BOTTOM_RIGHT="br"}(N||(N={})),function(t){t.TOP="top",t.BOTTOM="bottom",t.LEFT="left",t.RIGHT="right"}(H||(H={}));class V{constructor(t,e=1,n=1){if(this.splitPoints=[],this.originalRegions=[],this.originalRegionPoints=[],this.regionBoundaryCurves=[],this.regionCurves=[],this.regionPoints=[],this.splitUnit=.05,this.splitStrategy={name:"default",execute:V.strategy},this.renderingContext="webgl",this.renderingConfig={padding:0,enableAntialias:!0,enableSafeRendering:!0,enableContentDisplay:!0,enableGridDisplay:!1,enableGridVertexDisplay:!1,gridColor:{r:255,g:0,b:0,a:1}},this._safeModeEnabled=!1,this._cacheSourceSize=null,this._cacheSourceImageData=null,this._cacheInputCanvas=null,this._cacheOutputCanvas=null,this._inputLimitScale=1,this._outputLimitScale=1,!(t instanceof HTMLCanvasElement||t instanceof HTMLImageElement))throw new TypeError("[Warpvas] source must be either HTMLCanvasElement or HTMLImageElement!");if(t instanceof HTMLImageElement){const e=document.createElement("canvas"),n=e.getContext("2d");if(!n)throw new Error("Failed to get 2D rendering context for canvas");e.width=t.naturalWidth,e.height=t.naturalHeight,n.drawImage(t,0,0),this.source=e}else this.source=t;this.splitPoints=this._initializeSplitPoints(e,n),this.setWarpState(this.splitPoints)}get maxSplitUnitPixel(){return this.splitUnit*this.source.width}get scale(){return{x:this._inputLimitScale*this._outputLimitScale,y:this._inputLimitScale*this._outputLimitScale}}static strategy(t){if(!t.regionCurves)return[];const e=[];return t.regionBoundaryCurves.forEach(((n,r)=>{const i=[];n.forEach(((e,n)=>{const o=[],{vertical:s,horizontal:a}=t.regionCurves[r][n];for(let t=0;t<a.length;t++)for(let e=0;e<s.length;e++){const n=s[e].get(t/(a.length-1)),r=a[t].get(e/(s.length-1));o.push({x:(n.x+r.x)/2,y:(n.y+r.y)/2})}i.push(o)})),e.push(i)})),e}static serializeWarpState(t){const{splitPoints:e,regionBounds:n}=t,r=[e.length,n.length,n[0].length];e.forEach((t=>{r.push(t.x,t.y)}));const i=["top","right","bottom","left"];n.forEach((t=>t.forEach((t=>[i.forEach((e=>t[e].forEach((t=>{r.push(t.x,t.y)}))))]))));const o=new Float32Array(r),s=new Uint8Array(o.buffer);return btoa(String.fromCharCode.apply(null,s))}static deserializeWarpState(t){const e=atob(t),n=new Uint8Array(e.length);for(let t=0;t<e.length;t++)n[t]=e.charCodeAt(t);const r=new Float32Array(n.buffer),i=Array.from(r),o=["top","right","bottom","left"],s=i[0],a=i[1],c=i[2];let l=3;const h=[];for(let t=0;t<s;t++)h.push({x:i[l],y:i[l+1]}),l+=2;const u=[];for(let t=0;t<a;t++){const t=[];for(let e=0;e<c;e++){const e={};for(const t of o){e[t]=[];for(let n=0;n<4;n++)e[t].push({x:i[l],y:i[l+1]}),l+=2}t.push(e)}u.push(t)}return{splitPoints:h,regionBounds:u}}_initializeSplitPoints(t=1,e=1){const n=Math.max(Math.floor(t),1),r=Math.max(Math.floor(e),1),i=[];for(let o=1;o<n;o++)for(let n=1;n<r;n++)o===n&&i.push({x:n/e,y:o/t});return i}_initializeOriginalRegions(){const{width:t,height:e}=this.source;let n=[],r=[];this.splitPoints.forEach((t=>{t.x<0||t.x>1||t.y<0||t.y>1||(n.push(t.x),r.push(t.y))})),n=[...new Set([0,...n,1])],r=[...new Set([0,...r,1])],n.sort(((t,e)=>t-e)),r.sort(((t,e)=>t-e));const i=[];for(let o=0;o<r.length-1;o++){i.push([]);for(let s=0;s<n.length-1;s++)i[i.length-1].push({tl:{x:n[s]*t,y:r[o]*e},tr:{x:n[s+1]*t,y:r[o]*e},bl:{x:n[s]*t,y:r[o+1]*e},br:{x:n[s+1]*t,y:r[o+1]*e}})}return i}_calculateSamplingPoints(t){const e=t.tr.x-t.tl.x,n=t.bl.y-t.tl.y,r=Math.max(Math.ceil(e/this.maxSplitUnitPixel),1)+1,i=Math.max(Math.ceil(n/this.maxSplitUnitPixel),1)+1;return{hts:Array.from({length:r}).map(((t,e)=>e/(r-1))),vts:Array.from({length:i}).map(((t,e)=>e/(i-1)))}}_initializeOriginalRegionPoints(){return this.originalRegions.map((t=>t.map((t=>{const{hts:e,vts:n}=this._calculateSamplingPoints(t),r=[],{tl:i,tr:o,bl:s}=t,a=o.x-i.x,c=s.y-i.y;for(let t=0;t<n.length;t++)for(let o=0;o<e.length;o++)r.push({x:i.x+a*e[o],y:i.y+c*n[t]});return r}))))}_initializeBoundaryControlPoints(t){const e=[];return t.forEach((t=>{e.push([]),t.forEach((t=>{const{tl:n,tr:r,br:i,bl:o}=t,s={top:[n,r],bottom:[o,i],left:[n,o],right:[r,i]},a={};for(const t in s){const[e,n]=s[t];a[t]=[e,{x:e.x+1*(n.x-e.x)/3,y:e.y+1*(n.y-e.y)/3},{x:n.x-1*(n.x-e.x)/3,y:n.y-1*(n.y-e.y)/3},n]}e[e.length-1].push(a)}))})),e}_generateRegionCurves(t,e,n){const r={horizontal:[],vertical:[]};return e.forEach((e=>{if(0===e)return void r.vertical.push(t.left);if(1===e)return void r.vertical.push(t.right);const n=t.top.get(e),i=t.bottom.get(e),o=new z([n,{x:n.x+((t.left.points[1].x-t.left.points[0].x)*(1-e)+(t.right.points[1].x-t.right.points[0].x)*e),y:n.y+((t.left.points[1].y-t.left.points[0].y)*(1-e)+(t.right.points[1].y-t.right.points[0].y)*e)},{x:i.x+((t.left.points[2].x-t.left.points[3].x)*(1-e)+(t.right.points[2].x-t.right.points[3].x)*e),y:i.y+((t.left.points[2].y-t.left.points[3].y)*(1-e)+(t.right.points[2].y-t.right.points[3].y)*e)},i]);r.vertical.push(o)})),n.forEach((e=>{if(0===e)return void r.horizontal.push(t.top);if(1===e)return void r.horizontal.push(t.bottom);const n=t.left.get(e),i=t.right.get(e),o=new z([n,{x:n.x+((t.top.points[1].x-t.top.points[0].x)*(1-e)+(t.bottom.points[1].x-t.bottom.points[0].x)*e),y:n.y+((t.top.points[1].y-t.top.points[0].y)*(1-e)+(t.bottom.points[1].y-t.bottom.points[0].y)*e)},{x:i.x+((t.top.points[2].x-t.top.points[3].x)*(1-e)+(t.bottom.points[2].x-t.bottom.points[3].x)*e),y:i.y+((t.top.points[2].y-t.top.points[3].y)*(1-e)+(t.bottom.points[2].y-t.bottom.points[3].y)*e)},i]);r.horizontal.push(o)})),r}_generateAllRegionCurves(t){const e=[];return t.forEach(((t,n)=>{const r=[];e.push(r),t.forEach(((t,e)=>{const i=t,o=this.originalRegions[n][e],{hts:s,vts:a}=this._calculateSamplingPoints(o);r.push(this._generateRegionCurves(i,s,a))}))})),e}setSplitUnit(t){return this.splitUnit=t<=0?1:Math.min(1,t),this.originalRegionPoints=this._initializeOriginalRegionPoints(),this}setSplitStrategy(t){return this.splitStrategy=t,this}setInputLimitSize(t){return this._inputLimitSize=t,t||(this._inputLimitScale=1),this._cacheInputCanvas=null,this._cacheSourceImageData=null,this}setOutputLimitSize(t){return this._outputLimitSize=t,t||(this._outputLimitScale=1),this}setRenderingContext(t){return this.renderingContext=t,this}setRenderingCanvas(t){return this._cacheOutputCanvas=t,this}setRenderingConfig(t){return this.renderingConfig=Object.assign(Object.assign({},this.renderingConfig),t),this}getBoundingBoxInfo(){const{padding:t}=this.renderingConfig,e=this.regionCurves.length,n=this.regionCurves[0].length,r={left:1/0,right:-1/0,top:1/0,bottom:-1/0};for(let t=0;t<e;t++)for(let e=0;e<n;e++){const n=this.regionCurves[t][e];Object.values(n).forEach((t=>{const e=t.length;for(let n=0;n<e;n++){const e=t[n].bbox();r.left=Math.min(r.left,e.x.min),r.right=Math.max(r.right,e.x.max),r.top=Math.min(r.top,e.y.min),r.bottom=Math.max(r.bottom,e.y.max)}}))}return{offsetX:-r.left+t,offsetY:-r.top+t,width:r.right-r.left+2*t,height:r.bottom-r.top+2*t}}_cloneCanvas(t,e=t.width,n=t.height){const r=document.createElement("canvas");r.width=e,r.height=n;const i=r.getContext("2d");return null==i||i.drawImage(t,0,0,e,n),r}_createLinearBezier(t,e){return[t,{x:t.x+1*(e.x-t.x)/3,y:t.y+1*(e.y-t.y)/3},{x:e.x-1*(e.x-t.x)/3,y:e.y-1*(e.y-t.y)/3},e]}_mergeBezierCurves(t,e){const n=(t,e,n)=>{const{x:r,y:i}=e,{x:o,y:s}=n,a=Math.atan2(i,r),c=Math.atan2(s,o)-a,l=Math.sqrt(Math.pow(o,2)+Math.pow(s,2))*Math.cos(c),h=l*Math.cos(a),u=l*Math.sin(a);return{x:t.x+h,y:t.y+u}},r=n(t[1],{x:t[1].x-t[0].x,y:t[1].y-t[0].y},{x:e[1].x-e[0].x,y:e[1].y-e[0].y}),i=n(e[2],{x:e[2].x-e[3].x,y:e[2].y-e[3].y},{x:t[2].x-t[3].x,y:t[2].y-t[3].y});return[t[0],r,i,e[3]]}_findConnectedVertex(t,e){const n={top:[["left",0],["right",0]],bottom:[["left",3],["right",3]],left:[["top",0],["bottom",0]],right:[["top",3],["bottom",3]]}[t];if(!n||!["first","last"].includes(e))throw TypeError(`[Warpvas] Invalid vertex connection parameters: direction="${t}", position="${e}"\nExpected values:\n- direction: "top" | "bottom" | "left" | "right"\n- position: "first" | "last"`);const r=n[{first:0,last:1}[e]];return{direction:r[0],position:{0:"first",3:"last"}[r[1]]}}_setSingleRegionVertexCoord(t,e,n,r,i=!0){const o=(n,r,o)=>{var s,a;const c=null===(a=null===(s=this.regionBoundaryCurves[t])||void 0===s?void 0:s[e])||void 0===a?void 0:a[n];if(!c)return;const l="first"===r?o:c.points[0],h="last"===r?o:c.points[3];c.points.forEach(((t,e)=>{if(!i&&[1,2].includes(e))return;const n=this._createLinearBezier(l,h)[e];t.x=n.x,t.y=n.y}))},[s,a]={[N.TOP_LEFT]:["top","first"],[N.TOP_RIGHT]:["top","last"],[N.BOTTOM_LEFT]:["bottom","first"],[N.BOTTOM_RIGHT]:["bottom","last"]}[n];o(s,a,r);const c=this._findConnectedVertex(s,a);o(c.direction,c.position,r)}updateVertexCoord(t,e,n,r,i=!0){return this._setSingleRegionVertexCoord(t,e,n,r,i),{[N.TOP_LEFT]:[{row:t-1,col:e-1,type:N.BOTTOM_RIGHT},{row:t-1,col:e,type:N.BOTTOM_LEFT},{row:t,col:e-1,type:N.TOP_RIGHT}],[N.TOP_RIGHT]:[{row:t-1,col:e+1,type:N.BOTTOM_LEFT},{row:t,col:e+1,type:N.TOP_LEFT},{row:t-1,col:e,type:N.BOTTOM_RIGHT}],[N.BOTTOM_LEFT]:[{row:t,col:e-1,type:N.BOTTOM_RIGHT},{row:t+1,col:e,type:N.TOP_LEFT},{row:t+1,col:e-1,type:N.TOP_RIGHT}],[N.BOTTOM_RIGHT]:[{row:t,col:e+1,type:N.BOTTOM_LEFT},{row:t+1,col:e,type:N.TOP_RIGHT},{row:t+1,col:e+1,type:N.TOP_LEFT}]}[n].forEach((({row:t,col:e,type:n})=>{this._setSingleRegionVertexCoord(t,e,n,r,i)})),this}updateRegionBoundCoords(t,e,n,r){return this.regionBoundaryCurves=this._initializeRegionBoundaryCurves(this.forEachRegionBoundCoords(((i,o,s,a)=>i===t&&o===e&&n===s?r:a.points.map((t=>({x:t.x,y:t.y})))))),this.updateVertexCoord(t,e,{top:N.TOP_LEFT,right:N.TOP_RIGHT,bottom:N.BOTTOM_LEFT,left:N.TOP_LEFT}[n],r[0],!1),this.updateVertexCoord(t,e,{top:N.TOP_RIGHT,right:N.BOTTOM_RIGHT,bottom:N.BOTTOM_RIGHT,left:N.BOTTOM_LEFT}[n],r[3],!1),this}forEachRegionBoundCoords(t=((t,e,n,r)=>r.points)){const e=[];return this.regionBoundaryCurves.forEach(((n,r)=>{e.push([]),n.forEach(((n,i)=>{e[e.length-1].push(["top","right","left","bottom"].reduce(((e,o)=>(e[o]=t(r,i,o,n[o]),e)),{}))}))})),e}_initializeRegionBoundaryCurves(t){const e=[];return t.forEach(((t,n)=>{e.push([]),t.forEach(((t,r)=>{var i,o,s,a;const c={};for(const l in t){switch(l){case"top":{const t=null===(o=null===(i=e[n-1])||void 0===i?void 0:i[r])||void 0===o?void 0:o.bottom;t&&(c[l]=t);break}case"left":{const t=null===(a=null===(s=e[n])||void 0===s?void 0:s[r-1])||void 0===a?void 0:a.right;t&&(c[l]=t);break}}if(!c[l]){const e=t[l];c[l]=new z(e[0].x,e[0].y,e[1].x,e[1].y,e[2].x,e[2].y,e[3].x,e[3].y)}}e[e.length-1].push(c)}))})),e}getWarpState(){const t=this.forEachRegionBoundCoords();return{splitPoints:this.splitPoints,regionBounds:t}}setWarpState(t,e=null){this.splitPoints=t;const{width:n,height:r}=this.source;if(this._cacheSourceSize={width:n,height:r},this.originalRegions=this._initializeOriginalRegions(),this.originalRegionPoints=this._initializeOriginalRegionPoints(),e)this.regionBoundaryCurves=this._initializeRegionBoundaryCurves(e);else{const t=this._initializeBoundaryControlPoints(this.originalRegions);this.regionBoundaryCurves=this._initializeRegionBoundaryCurves(t)}return this}resetWarpState(t=1,e=1){return this.splitPoints=this._initializeSplitPoints(t,e),this.setWarpState(this.splitPoints)}isUnwarped(){const t=this.forEachRegionBoundCoords(((t,e,n,r)=>r.points.map((t=>({x:t.x/this.source.width,y:t.y/this.source.height})))));return 0===this.splitPoints.length&&Object.entries(t[0][0]).every((([t,e])=>{const n=["right","bottom"].includes(t)?1:0,r={top:"y",bottom:"y",left:"x",right:"x"}[t],i={top:"x",bottom:"x",left:"y",right:"y"}[t];return e.every((t=>t[r]===n))&&0===e[0][i]&&Math.abs(e[1][i]-1/3)<Number.EPSILON&&Math.abs(e[2][i]-2/3)<Number.EPSILON&&1===e[3][i]}))}getHitInfo(t){let e=null;try{this.forEachSplitRegion((([n,r,i,o],s,a,c,l,h)=>{const u=B(t,n,r,o),p=B(t,i,r,o);if(u||p)throw e={rowIndex:l,colIndex:h,row:a,col:c,after:[n,r,i,o],before:s,clickPart:u?0:1},Error()}))}catch(t){}return e}splitRegionByPoint(t,e,n,r=.05){const{tl:i,tr:o,bl:s}=this.originalRegions[t][e];let a=(n.x-i.x)/(o.x-i.x),c=(n.y-i.y)/(s.y-i.y);a<r&&(a=0),a>1-r&&(a=1),c<r&&(c=0),c>1-r&&(c=1);const l=[];this.regionBoundaryCurves.forEach(((e,n)=>{if(n!==t||0===c||1===c)return void l.push(e.map((t=>{const e={};for(const n in t)e[n]=t[n].points;return e})));const r=[],i=[];e.forEach((t=>{const{horizontal:e,vertical:n}=this._generateRegionCurves(t,[0,1],[0,c,1]),{left:o,right:s}=n[0].split(c),{left:a,right:l}=n[1].split(c);r.push({left:o.points,right:a.points,top:e[0].points,bottom:e[1].points}),i.push({left:s.points,right:l.points,top:e[1].points,bottom:e[2].points})})),l.push(r,i)})),0!==a&&1!==a&&l.forEach(((t,n)=>{const{horizontal:r,vertical:i}=this._generateRegionCurves({left:new z(t[e].left),right:new z(t[e].right),top:new z(t[e].top),bottom:new z(t[e].bottom)},[0,a,1],[0,1]),{left:o,right:s}=r[0].split(a),{left:c,right:h}=r[1].split(a);l[n].splice(e,1,{left:i[0].points,right:i[1].points,top:o.points,bottom:c.points},{left:i[1].points,right:i[2].points,top:s.points,bottom:h.points})})),this.setWarpState([...this.splitPoints,{x:(0===a?i.x:1===a?o.x:n.x)/this.source.width,y:(0===c?i.y:1===c?s.y:n.y)/this.source.height}],l)}removeRegion(...t){const e=[],{width:n,height:r}=this.source,i=(i,o,s)=>{t.some((t=>t.row===o||t.column===s))||0===o&&0===s||o===this.originalRegions.length&&0===s||0===o&&s===this.originalRegions[0].length||o===this.originalRegions.length&&s===this.originalRegions[0].length||e.some((t=>t.x===i.x||0===s||s===this.originalRegions[0].length))&&e.some((t=>t.y===i.y||0===o||o===this.originalRegions.length))||e.push({x:i.x/n,y:i.y/r})};this.originalRegions.forEach(((t,e)=>{t.forEach(((n,r)=>{const{tl:o,tr:s,bl:a,br:c}=n;i(o,e,r),r===t.length-1&&i(s,e,r+1),e===this.originalRegions.length-1&&(i(a,e+1,r),r===t.length-1&&i(c,e+1,r+1))}))}));const o=this.forEachRegionBoundCoords(),s=[...new Set(t.map((t=>t.row)))];s.sort(((t,e)=>e-t)),s.forEach((t=>{const e=o[t],n=o[t-1];e&&n&&o.splice(t-1,2,n.map(((t,n)=>({top:t.top,left:this._mergeBezierCurves(t.left,e[n].left),right:this._mergeBezierCurves(t.right,e[n].right),bottom:e[n].bottom}))))}));const a=[...new Set(t.map((t=>t.column)))];a.sort(((t,e)=>e-t)),a.forEach((t=>{o.forEach((e=>{const n=e[t],r=e[t-1];n&&r&&e.splice(t-1,2,{top:this._mergeBezierCurves(r.top,n.top),left:r.left,right:n.right,bottom:this._mergeBezierCurves(r.bottom,n.bottom)})}))})),this.setWarpState(e,o)}forEachSplitRegion(t){this.regionPoints.forEach(((e,n)=>{e.forEach(((e,r)=>{const i=e,o=this.regionCurves[n][r].vertical.length-1;i.forEach(((e,s)=>{const a=Math.floor(s/(o+1)),c=s%(o+1),l=i[s],h=i[s+1],u=i[s+o+2],p=i[s+o+1],g=this.originalRegionPoints[n][r][s],f=this.originalRegionPoints[n][r][s+1],x=this.originalRegionPoints[n][r][s+o+2],y=this.originalRegionPoints[n][r][s+o+1];h&&u&&s%(o+1)<o&&t([l,h,u,p],[g,f,x,y],a,c,n,r)}))}))}))}_generateRenderOptions(){var t,e;!this._cacheSourceSize||this._cacheSourceSize.width===this.source.width&&this._cacheSourceSize.height===this.source.height||this.setWarpState(this.splitPoints),this.regionCurves=this._generateAllRegionCurves(this.regionBoundaryCurves),this.regionPoints=this.splitStrategy.execute(this);const{offsetX:n,offsetY:r,width:i,height:o}=this.getBoundingBoxInfo();if(this._inputLimitSize&&!this._cacheInputCanvas){const t=this._inputLimitSize,{width:e,height:n}=this.source,r={width:t.width?Math.min(1,t.width/e):0,height:t.height?Math.min(1,t.height/n):0};r.width=r.width||r.height,r.height=r.height||r.width;const i=Math.min(r.width,r.height),o=document.createElement("canvas");o.width=Math.ceil(e*i),o.height=Math.ceil(n*i);const s=o.getContext("2d");null==s||s.drawImage(this.source,0,0,o.width,o.height),this._inputLimitScale=i,this._cacheInputCanvas=o}if(this._outputLimitSize){let t=1;this._outputLimitSize.width&&(t=Math.min(t,this._outputLimitSize.width/i)),this._outputLimitSize.height&&(t=Math.min(t,this._outputLimitSize.height/o)),this._outputLimitS