"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const M=require("fabric");function p(x){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(x){for(const e in x)if(e!=="default"){const n=Object.getOwnPropertyDescriptor(x,e);Object.defineProperty(t,e,n.get?n:{enumerable:!0,get:()=>x[e]})}}return t.default=x,Object.freeze(t)}const f=p(M),u=x=>Object.keys(x);class z{aligningLineMargin=4;aligningLineWidth=.75;aligningLineColor="#F68066";verticalOffset=5;horizontalOffset=5;ignoreObjTypes=[];pickObjTypes=[];canvas;ctx;viewportTransform;verticalLines=[];horizontalLines=[];activeObj=new f.Object;constructor({canvas:t,aligningOptions:e,ignoreObjTypes:n,pickObjTypes:s}){this.canvas=t,this.ctx=t.getSelectionContext(),this.ignoreObjTypes=n||[],this.pickObjTypes=s||[],e&&(this.aligningLineMargin=e.lineMargin||this.aligningLineMargin,this.aligningLineWidth=e.lineWidth||this.aligningLineWidth,this.aligningLineColor=e.lineColor||this.aligningLineColor,this.verticalOffset=e.verticalOffset||this.verticalOffset,this.horizontalOffset=e.horizontalOffset||this.horizontalOffset)}drawSign(t,e){const n=this.ctx;n.lineWidth=.5,n.strokeStyle=this.aligningLineColor,n.beginPath();const s=2;n.moveTo(t-s,e-s),n.lineTo(t+s,e+s),n.moveTo(t+s,e-s),n.lineTo(t-s,e+s),n.stroke()}drawLine(t,e,n,s){const r=this.ctx,i=f.util.transformPoint(new f.Point(t,e),this.canvas.viewportTransform),a=f.util.transformPoint(new f.Point(n,s),this.canvas.viewportTransform);r.save(),r.lineWidth=this.aligningLineWidth,r.strokeStyle=this.aligningLineColor,r.beginPath(),r.moveTo(i.x,i.y),r.lineTo(a.x,a.y),r.stroke(),this.drawSign(i.x,i.y),this.drawSign(a.x,a.y),r.restore()}centerObjectInCanvas(){const t={x:this.canvas.getWidth()/2,y:this.canvas.getHeight()/2},e=this.activeObj,n=e.getScaledWidth(),s=e.getScaledHeight();e.set({left:t.x-n/2,top:t.y-s/2}),e.setCoords(),this.canvas.renderAll()}drawVerticalLine(t){const e=this.getObjDraggingObjCoords(this.activeObj);u(e).some(n=>Math.abs(e[n].x-t.x)<1e-4)&&this.drawLine(t.x,Math.min(t.y1,t.y2),t.x,Math.max(t.y1,t.y2))}drawHorizontalLine(t){const e=this.getObjDraggingObjCoords(this.activeObj);u(e).some(n=>Math.abs(e[n].y-t.y)<1e-4)&&this.drawLine(Math.min(t.x1,t.x2),t.y,Math.max(t.x1,t.x2),t.y)}isInRange(t,e){return Math.abs(Math.round(t)-Math.round(e))<=this.aligningLineMargin/this.canvas.getZoom()}watchMouseDown(){this.canvas.on("mouse:down",()=>{this.clearLinesMeta(),this.viewportTransform=this.canvas.viewportTransform})}watchMouseUp(){this.canvas.on("mouse:up",()=>{this.clearLinesMeta(),this.canvas.renderAll()})}watchMouseWheel(){this.canvas.on("mouse:wheel",()=>{this.clearLinesMeta()})}clearLinesMeta(){this.verticalLines.length=this.horizontalLines.length=0}watchObjectMoving(){this.canvas.on("object:moving",t=>{this.clearLinesMeta();const e=t.target;this.activeObj=e;const n=this.canvas.getObjects().filter(r=>this.ignoreObjTypes.length?!this.ignoreObjTypes.some(i=>r[i.key]===i.value):this.pickObjTypes.length?this.pickObjTypes.some(i=>r[i.key]===i.value):!0);this.canvas._currentTransform&&this.traversAllObjects(e,n)})}getObjDraggingObjCoords(t){const e=t.aCoords,n=new f.Point((e.tl.x+e.br.x)/2,(e.tl.y+e.br.y)/2),s=n.x-t.getCenterPoint().x,r=n.y-t.getCenterPoint().y;return u(e).reduce((i,a)=>({...i,[a]:{x:e[a].x-s,y:e[a].y-r}}),{c:t.getCenterPoint()})}omitCoords(t,e){let n;if(e==="vertical"){let s=["tl",t.tl],r=["tl",t.tl];u(t).forEach(i=>{t[i].xr[1].x&&(r=[i,t[i]])}),n={[s[0]]:s[1],[r[0]]:r[1],c:t.c}}else{let s=["tl",t.tl],r=["tl",t.tl];u(t).forEach(i=>{t[i].yr[1].y&&(r=[i,t[i]])}),n={[s[0]]:s[1],[r[0]]:r[1],c:t.c}}return n}getObjMaxWidthHeightByCoords(t){const e=Math.max(Math.abs(t.c.y-t.tl.y),Math.abs(t.c.y-t.tr.y))*2,n=Math.max(Math.abs(t.c.x-t.tl.x),Math.abs(t.c.x-t.tr.x))*2;return{objHeight:e,objWidth:n}}calcCenterPointByACoords(t){return new f.Point((t.tl.x+t.br.x)/2,(t.tl.y+t.br.y)/2)}traversAllObjects(t,e){const n=this.getObjDraggingObjCoords(t),s=[],r=[];for(let i=e.length;i--;){if(e[i]===t)continue;const a={...e[i].aCoords,c:e[i].getCenterPoint()},{objHeight:y,objWidth:m}=this.getObjMaxWidthHeightByCoords(a);u(n).forEach(c=>{const v=e[i].angle!==0?this.omitCoords(a,"horizontal"):a;function C(h,o){let l,g;return h==="c"?(l=Math.min(a.c.x-m/2,o[c].x),g=Math.max(a.c.x+m/2,o[c].x)):(l=Math.min(a[h].x,o[c].x),g=Math.max(a[h].x,o[c].x)),{x1:l,x2:g}}u(v).forEach(h=>{if(this.isInRange(n[c].y,a[h].y)){const o=a[h].y;let{x1:l,x2:g}=C(h,n);const d=n[c].y-o;if(r.push(n.c.y-d),t.aCoords){let{x1:w,x2:L}=C(h,{...t.aCoords,c:this.calcCenterPointByACoords(t.aCoords)});this.horizontalLines.push({y:o,x1:w,x2:L})}else this.horizontalLines.push({y:o,x1:l,x2:g})}})}),u(n).forEach(c=>{const v=e[i].angle!==0?this.omitCoords(a,"vertical"):a;function C(h,o){let l,g;return h==="c"?(l=Math.min(v.c.y-y/2,o[c].y),g=Math.max(v.c.y+y/2,o[c].y)):(l=Math.min(a[h].y,o[c].y),g=Math.max(a[h].y,o[c].y)),{y1:l,y2:g}}u(v).forEach(h=>{if(this.isInRange(n[c].x,a[h].x)){const o=a[h].x;let{y1:l,y2:g}=C(h,n);const d=n[c].x-o;if(s.push(n.c.x-d),t.aCoords){let{y1:w,y2:L}=C(h,{...t.aCoords,c:this.calcCenterPointByACoords(t.aCoords)});this.verticalLines.push({x:o,y1:w,y2:L})}else this.verticalLines.push({x:o,y1:l,y2:g})}})}),this.snap({activeObject:t,draggingObjCoords:n,snapXPoints:s,snapYPoints:r})}}snap({activeObject:t,snapXPoints:e,draggingObjCoords:n,snapYPoints:s}){const r=(i,a)=>i.length?i.map(y=>({abs:Math.abs(a-y),val:y})).sort((y,m)=>y.abs-m.abs)[0].val:a;t.setPositionByOrigin(new f.Point(r(e,n.c.x),r(s,n.c.y)),"center","center")}clearGuideline(){this.canvas.clearContext(this.ctx)}watchRender(){this.canvas.on("before:render",()=>{this.clearGuideline()}),this.canvas.on("after:render",()=>{for(let t=this.verticalLines.length;t--;)this.drawVerticalLine(this.verticalLines[t]);for(let t=this.horizontalLines.length;t--;)this.drawHorizontalLine(this.horizontalLines[t]);this.canvas.calcOffset()})}init(){this.watchObjectMoving(),this.watchRender(),this.watchMouseDown(),this.watchMouseUp(),this.watchMouseWheel(),this.centerObjectInCanvas()}}class H{canvas;horizontalOffset;verticalOffset;color;centerLineWidth=1;ctx;isInVerticalCenter=null;isInHorizontalCenter=null;constructor({canvas:t,horizontalOffset:e=4,verticalOffset:n=4,color:s="purple"}){this.canvas=t,this.ctx=t.getSelectionContext(),this.horizontalOffset=e,this.verticalOffset=n,this.color=s}get canvasWidth(){return this.canvas.getWidth()}get canvasHeight(){return this.canvas.getHeight()}get canvasWidthCenter(){return this.canvasWidth/2}get canvasHeightCenter(){return this.canvasHeight/2}get centerLineColor(){return this.color}showCenterLine(t,e,n,s){const r=this.canvas.viewportTransform,i=new f.Point(t,e).transform(r),a=new f.Point(n,s).transform(r);this.ctx.save(),this.ctx.strokeStyle=this.centerLineColor,this.ctx.lineWidth=this.centerLineWidth,this.ctx.beginPath(),this.ctx.moveTo(i.x,i.y),this.ctx.lineTo(a.x,a.y),this.ctx.stroke(),this.ctx.restore()}showHorizontalCenterLine(){this.showCenterLine(0,this.canvasHeightCenter,this.canvasWidth,this.canvasHeightCenter)}showVerticalCenterLine(){this.showCenterLine(this.canvasWidthCenter,0,this.canvasWidthCenter,this.canvasHeight)}init(){this.canvas.on("mouse:down",()=>{this.isInVerticalCenter=null,this.isInHorizontalCenter=null}),this.canvas.on("object:moving",t=>{const e=t.target,n=e.getCenterPoint();this.canvas._currentTransform&&(this.isInVerticalCenter=Math.abs(n.x-this.canvasWidthCenter){this.canvas.clearContext(this.canvas.contextTop)}),this.canvas.on("object:modified",()=>{this.isInVerticalCenter=null,this.isInHorizontalCenter=null,this.canvas.clearContext(this.canvas.contextTop),this.canvas.renderAll()}),this.canvas.on("after:render",()=>{this.isInVerticalCenter&&this.showVerticalCenterLine(),this.isInHorizontalCenter&&this.showHorizontalCenterLine()}),this.canvas.on("mouse:up",()=>{this.canvas.renderAll()})}}exports.AlignGuidelines=z;exports.CenteringGuidelines=H;