UNPKG

canvas-select

Version:

一个用于图片标注的javascript库,基于canvas,简单轻量,支持矩形、多边形、点、折线、圆形、网格、画笔、橡皮擦

9 lines (8 loc) 39.4 kB
/*! * canvas-select v2.32.3 * 一个用于图片标注的javascript库,基于canvas,简单轻量,支持矩形、多边形、点、折线、圆形、网格、画笔、橡皮擦 * (c) 2025 heylight * Released under the MIT License. */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).CanvasSelect=e()}(this,(function(){"use strict";var t=function(e,i){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i])},t(e,i)};function e(e,i){if("function"!=typeof i&&null!==i)throw new TypeError("Class extends value "+String(i)+" is not a constructor or null");function s(){this.constructor=e}t(e,i),e.prototype=null===i?Object.create(i):(s.prototype=i.prototype,new s)}var i=function(){return i=Object.assign||function(t){for(var e,i=1,s=arguments.length;i<s;i++)for(var a in e=arguments[i])Object.prototype.hasOwnProperty.call(e,a)&&(t[a]=e[a]);return t},i.apply(this,arguments)};function s(t,e){var i="function"==typeof Symbol&&t[Symbol.iterator];if(!i)return t;var s,a,n=i.call(t),o=[];try{for(;(void 0===e||e-- >0)&&!(s=n.next()).done;)o.push(s.value)}catch(t){a={error:t}}finally{try{s&&!s.done&&(i=n.return)&&i.call(n)}finally{if(a)throw a.error}}return o}"function"==typeof SuppressedError&&SuppressedError;var a,n=function(t,e){this.label="",this.coor=[],this.active=!1,this.creating=!1,this.dragging=!1,this.uuid=function(){for(var t=[],e="0123456789abcdef",i=0;i<36;i++){var s=Math.floor(16*Math.random());t[i]=e.slice(s,s+1)}t[14]="4";var a=3&t[19]|8;return t[19]=e.slice(a,a+1),t[8]=t[13]=t[18]=t[23]="-",t.join("")}(),this.index=e,Object.assign(this,t)},o=function(t){function i(e,i,s){var a,n,o,r,h=t.call(this,e,i)||this;return h.type=1,h.rotation=0,h.lineWidth=null!==(a=e.lineWidth)&&void 0!==a?a:s.lineWidth,h.fillStyle=null!==(n=e.fillStyle)&&void 0!==n?n:s.fillStyle,h.strokeStyle=null!==(o=e.strokeStyle)&&void 0!==o?o:s.strokeStyle,h.showRotation=null!==(r=e.showRotation)&&void 0!==r?r:s.showRotation,h}return e(i,t),Object.defineProperty(i.prototype,"ctrlsData",{get:function(){var t=this,e=s(this.coor,2),i=s(e[0],2),a=i[0],n=i[1],o=s(e[1],2),r=o[0],h=o[1],c=(a+r)/2,l=(n+h)/2,d=r-a,u=h-n,v=[[a,n],[a+d/2,n],[r,n],[r,n+u/2],[r,h],[a+d/2,h],[a,h],[a,n+u/2]];return this.showRotation&&v.push([c,n-20,"green"]),0===this.rotation?v:v.map((function(e){var i=s(e,2),a=i[0],n=i[1],o=a-c,r=n-l,h=o*Math.cos(t.rotation)-r*Math.sin(t.rotation),d=o*Math.sin(t.rotation)+r*Math.cos(t.rotation);return[h+c,d+l]}))},enumerable:!1,configurable:!0}),Object.defineProperty(i.prototype,"center",{get:function(){var t=s(this.coor,2),e=s(t[0],2),i=e[0],a=e[1],n=s(t[1],2);return[(i+n[0])/2,(a+n[1])/2]},enumerable:!1,configurable:!0}),i}(n),r=function(t){function i(e,i,s){var a,n,o=t.call(this,e,i)||this;return o.type=2,o.fillStyle=null!==(a=e.fillStyle)&&void 0!==a?a:s.fillStyle,o.strokeStyle=null!==(n=e.strokeStyle)&&void 0!==n?n:s.strokeStyle,o}return e(i,t),Object.defineProperty(i.prototype,"ctrlsData",{get:function(){return this.coor.length>2?this.coor:[]},enumerable:!1,configurable:!0}),i}(n),h=function(t){function i(e,i,s){var a,n,o=t.call(this,e,i)||this;return o.type=3,o.fillStyle=null!==(a=e.fillStyle)&&void 0!==a?a:s.fillStyle,o.strokeStyle=null!==(n=e.strokeStyle)&&void 0!==n?n:s.strokeStyle,o}return e(i,t),i}(n),c=function(){function t(){this._eventTree={}}return t.prototype.on=function(t,e){var i=this._eventTree[t];Array.isArray(i)?i.push(e):this._eventTree[t]=[e]},t.prototype.emit=function(t){for(var e=[],i=1;i<arguments.length;i++)e[i-1]=arguments[i];var a=this._eventTree[t];Array.isArray(a)&&a.forEach((function(t){return t.apply(void 0,function(t,e,i){if(i||2===arguments.length)for(var s,a=0,n=e.length;a<n;a++)!s&&a in e||(s||(s=Array.prototype.slice.call(e,0,a)),s[a]=e[a]);return t.concat(s||Array.prototype.slice.call(e))}([],s(e),!1))}))},t.prototype.off=function(t,e){var i=this._eventTree[t],s=i.find((function(t){return t===e}));Array.isArray(i)&&s&&i.splice(s,1)},t}(),l=function(t){function i(e,i,s){var a,n=t.call(this,e,i)||this;return n.type=4,n.strokeStyle=null!==(a=e.strokeStyle)&&void 0!==a?a:s.strokeStyle,n}return e(i,t),Object.defineProperty(i.prototype,"ctrlsData",{get:function(){return this.coor.length>1?this.coor:[]},enumerable:!1,configurable:!0}),i}(n),d=function(t){function i(e,i,s){var a,n,o=t.call(this,e,i)||this;return o.type=5,o.radius=0,o.radius=e.radius||o.radius,o.fillStyle=null!==(a=e.fillStyle)&&void 0!==a?a:s.fillStyle,o.strokeStyle=null!==(n=e.strokeStyle)&&void 0!==n?n:s.strokeStyle,o}return e(i,t),Object.defineProperty(i.prototype,"ctrlsData",{get:function(){var t=s(this.coor,2),e=t[0],i=t[1];return[[e,i-this.radius],[e+this.radius,i],[e,i+this.radius],[e-this.radius,i]]},enumerable:!1,configurable:!0}),i}(n),u=function(t){function i(e,i,s){var a,n,o=t.call(this,e,i)||this;return o.type=6,o.row=1,o.col=1,o.selected=[],o.row=e.row>0?e.row:o.row,o.col=e.col>0?e.col:o.col,o.selected=Array.isArray(e.selected)?e.selected:[],o.fillStyle=null!==(a=e.fillStyle)&&void 0!==a?a:s.fillStyle,o.strokeStyle=null!==(n=e.strokeStyle)&&void 0!==n?n:s.strokeStyle,o}return e(i,t),Object.defineProperty(i.prototype,"ctrlsData",{get:function(){var t=s(this.coor,2),e=s(t[0],2),i=e[0],a=e[1],n=s(t[1],2),o=n[0],r=n[1];return[[i,a],[i+(o-i)/2,a],[o,a],[o,a+(r-a)/2],[o,r],[i+(o-i)/2,r],[i,r],[i,a+(r-a)/2]]},enumerable:!1,configurable:!0}),Object.defineProperty(i.prototype,"gridRects",{get:function(){for(var t=s(this.coor,2),e=s(t[0],2),i=e[0],a=e[1],n=s(t[1],2),r=n[0],h=n[1],c=this,l=c.row,d=c.col,u=c.strokeStyle,v=c.fillStyle,f=c.active,p=c.creating,y=c.lineWidth,g=(r-i)/this.col,S=(h-a)/this.row,I=[],m=0;m<l;m++)for(var b=0;b<d;b++){var x=[i+b*g,a+m*S],M=new o({coor:[x,[x[0]+g,x[1]+S]],strokeStyle:u,fillStyle:v,active:f,creating:p,lineWidth:y},m*d+b,{});I.push(M)}return I},enumerable:!1,configurable:!0}),i}(n),v="2.32.3",f=function(t){function i(e,i,s){var a,n,o=t.call(this,e,i)||this;return o.type=7,o.coor=[],o.brushSize=null!==(a=e.brushSize)&&void 0!==a?a:s.brushSize,o.brushStokeStyle=null!==(n=e.brushStokeStyle)&&void 0!==n?n:s.brushStokeStyle,o.coor=e.coor,o}return e(i,t),i}(n),p=function(t){function i(e,i,s){var a,n=t.call(this,e,i)||this;return n.type=8,n.coor=[],n.eraserSize=null!==(a=e.eraserSize)&&void 0!==a?a:s.eraserSize,n.coor=e.coor,n}return e(i,t),i}(n);return function(t){t[t.None=0]="None",t[t.Rect=1]="Rect",t[t.Polygon=2]="Polygon",t[t.Dot=3]="Dot",t[t.Line=4]="Line",t[t.Circle=5]="Circle",t[t.Grid=6]="Grid",t[t.Brush=7]="Brush",t[t.Eraser=8]="Eraser"}(a||(a={})),function(t){function n(e,i){var s=t.call(this)||this;s.version=v,s.lock=!1,s.readonly=!1,s.MIN_WIDTH=10,s.MIN_HEIGHT=10,s.MIN_RADIUS=5,s.strokeStyle="#0f0",s.fillStyle="rgba(0, 0, 255, 0.1)",s.lineWidth=1,s.activeStrokeStyle="#f00",s.activeFillStyle="rgba(0, 0, 255, 0.1)",s.ctrlStrokeStyle="#000",s.ctrlFillStyle="#fff",s.ctrlRadius=3,s.hideLabel=!1,s.labelFillStyle="#fff",s.labelFont="10px sans-serif",s.textFillStyle="#000",s.labelMaxLen=10,s.WIDTH=0,s.HEIGHT=0,s.crossX=new l({},0,{}),s.crossY=new l({},1,{}),s.crossStroke="#ff0",s.showCross=!1,s.showRotation=!1,s.dataset=[],s.isMagnifierVisible=!1,s.magnifierPosition="auto",s.remmber=[],s.mouse=[0,0],s.remmberOrigin=[0,0],s.createType=a.None,s.ctrlIndex=-1,s.image=new Image,s.IMAGE_ORIGIN_WIDTH=0,s.IMAGE_WIDTH=0,s.IMAGE_ORIGIN_HEIGHT=0,s.IMAGE_HEIGHT=0,s.originX=0,s.originY=0,s.scaleStep=0,s.scrollZoom=!0,s.timer=null,s.dblTouch=300,s.dblTouchStore=0,s.alpha=!0,s.focusMode=!1,s.scaleTouchStore=0,s.isTouch2=!1,s.isMobile=navigator.userAgent.includes("Mobile"),s.labelUp=!1,s.isCtrlKey=!1,s.ctrlCode="ControlLeft",s.gridMenuEnable=!0,s.gridSelectedFillStyle="rgba(255, 255, 0, 0.6)",s.brushSize=20,s.brushStokeStyle="#f00",s.eraserSize=20,s.position=[0,0],s.handleLoad=s.handleLoad.bind(s),s.handleContextmenu=s.handleContextmenu.bind(s),s.handleMousewheel=s.handleMousewheel.bind(s),s.handleMouseDown=s.handleMouseDown.bind(s),s.handleMouseMove=s.handleMouseMove.bind(s),s.handleMouseUp=s.handleMouseUp.bind(s),s.handleDblclick=s.handleDblclick.bind(s),s.handleKeyup=s.handleKeyup.bind(s),s.handleKeydown=s.handleKeydown.bind(s);var n="string"==typeof e?document.querySelector(e):e;return n instanceof HTMLCanvasElement?(s.canvas=n,s.offScreen=document.createElement("canvas"),s.initSetting(),s.initEvents(),i&&s.setImage(i)):console.warn("HTMLCanvasElement is required!"),s.crossX.strokeStyle=s.crossStroke,s.crossY.strokeStyle=s.crossStroke,s}return e(n,t),Object.defineProperty(n.prototype,"activeShape",{get:function(){return this.dataset.find((function(t){return t.active}))||{}},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"scale",{get:function(){return this.IMAGE_ORIGIN_WIDTH&&this.IMAGE_WIDTH?this.IMAGE_WIDTH/this.IMAGE_ORIGIN_WIDTH:1},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"imageMin",{get:function(){return Math.min(this.IMAGE_WIDTH,this.IMAGE_HEIGHT)},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"imageOriginMax",{get:function(){return Math.max(this.IMAGE_ORIGIN_WIDTH,this.IMAGE_ORIGIN_HEIGHT)},enumerable:!1,configurable:!0}),n.prototype.createMagnifierCanvas=function(){this.isMagnifierVisible&&(this.magnifierCanvas=this.magnifierCanvas||document.createElement("canvas"),this.magnifierCtx=this.magnifierCanvas&&this.magnifierCanvas.getContext("2d",{willReadFrequently:!0}),this.magnifierCanvas.style.position="fixed",this.magnifierCanvas.style.pointerEvents="none",this.magnifierCanvas.style.zIndex="1000",this.magnifierCanvas.style.border="1px solid black",this.magnifierCanvas.style.borderRadius="50%",this.magnifierCanvas.style.width="100px",this.magnifierCanvas.style.height="100px",document.body.appendChild(this.magnifierCanvas))},n.prototype.createMagnifier=function(t,e){this.magnifierCanvas?this.updateMagnifier(t,e):this.createMagnifierCanvas()},n.prototype.updateMagnifier=function(t,e){var i,a;if(this.canvas&&this.magnifierCanvas&&this.magnifierCtx){var n=100,o=window.devicePixelRatio||1;if(this.magnifierCanvas.width=n,this.magnifierCanvas.height=n,this.magnifierCtx.clearRect(0,0,n,n),this.magnifierPosition&&2===this.magnifierPosition.length){var r=s(this.magnifierPosition,2),h=r[0],c=r[1];this.magnifierCanvas.style.left="".concat(h,"px"),this.magnifierCanvas.style.top="".concat(c,"px")}else this.magnifierCanvas.style.left="".concat(t+10,"px"),this.magnifierCanvas.style.top="".concat(e+10,"px");var l=this.getImageDataFromCanvas(this.canvas,[t*o-50,e*o-50,n,n]),d=null===(i=this.magnifierCanvas.getContext("2d",{willReadFrequently:!0}))||void 0===i?void 0:i.createImageData(this.magnifierCanvas.width,this.magnifierCanvas.height);if(d&&l){for(var u=0,v=0;v<n;v+=1)for(var f=0;f<n;f+=1){for(var p=v;p<v+1;p++)for(var y=f;y<f+1;y++){var g=4*(p*n+y);d.data[g]=l.data[u],d.data[g+1]=l.data[u+1],d.data[g+2]=l.data[u+2],d.data[g+3]=l.data[u+3]}u+=4}null===(a=this.magnifierCanvas.getContext("2d",{willReadFrequently:!0}))||void 0===a||a.putImageData(d,0,0),this.magnifierCtx.strokeStyle="rgba(0, 0, 0, 0.5)",this.magnifierCtx.lineWidth=2,this.magnifierCtx.strokeRect(0,0,n,n)}}},n.prototype.destroyMagnifier=function(){this.magnifierCanvas&&(this.magnifierCanvas.remove(),this.magnifierCanvas=void 0,this.magnifierCtx=void 0)},n.prototype.getImageDataFromCanvas=function(t,e){var i=s(e,4),a=i[0],n=i[1],o=i[2],r=i[3],h=t.getContext("2d",{willReadFrequently:!0});return null==h?void 0:h.getImageData(a,n,o,r)},n.prototype.mergeEvent=function(t){var e,s,a=0,n=0,o=0,r=0;if(this.isMobile&&t.touches){var h=t.touches[0],c=h.clientX,l=h.clientY,d=t.target.getBoundingClientRect(),u=d.left,v=d.top;if(a=Math.round(c-u),n=Math.round(l-v),2===(null===(e=t.touches)||void 0===e?void 0:e.length)){var f=t.touches[1]||{},p=f.clientX,y=void 0===p?0:p,g=f.clientY,S=void 0===g?0:g;o=Math.round(Math.abs((y-c)/2+c)-u),r=Math.round(Math.abs((S-l)/2+l)-v)}else 1===(null===(s=t.touches)||void 0===s?void 0:s.length)&&(o=Math.round(c-u),r=Math.round(l-v))}else a=t.offsetX,n=t.offsetY;return i(i({},t),{mouseX:a,mouseY:n,mouseCX:o,mouseCY:r})},n.prototype.handleLoad=function(){this.emit("load",this.image.src),this.IMAGE_ORIGIN_WIDTH=this.IMAGE_WIDTH=this.image.width,this.IMAGE_ORIGIN_HEIGHT=this.IMAGE_HEIGHT=this.image.height,this.fitZoom()},n.prototype.handleContextmenu=function(t){t.preventDefault(),this.lock},n.prototype.handleMousewheel=function(t){if(t.preventDefault(),t.stopPropagation(),!this.lock&&this.scrollZoom){var e=this.mergeEvent(t),i=e.mouseX,s=e.mouseY;this.mouse=[i,s],this.setScale(t.deltaY<0,!0)}},n.prototype.handleMouseDown=function(t){var e,i,n,c=this;if(t.stopPropagation(),!this.lock){var v=this.mergeEvent(t),y=v.mouseX,g=v.mouseY,S=v.mouseCX,I=v.mouseCY,m=Math.round(y/this.scale),b=Math.round(g/this.scale);if(this.mouse=this.isMobile&&2===(null===(e=t.touches)||void 0===e?void 0:e.length)?[S,I]:[y,g],this.remmberOrigin=[y-this.originX,g-this.originY],!this.isMobile&&1===t.buttons||this.isMobile&&1===(null===(i=t.touches)||void 0===i?void 0:i.length)){var x=this.activeShape.ctrlsData||[];if(this.ctrlIndex=x.findIndex((function(t){return c.isPointInCircle(c.mouse,t,c.ctrlRadius)})),this.ctrlIndex>-1&&!this.readonly){var M=s(x[this.ctrlIndex],2),E=M[0],H=M[1];this.activeShape.type===a.Polygon&&this.activeShape.coor.length>2&&0===this.ctrlIndex&&this.handleDblclick(t),this.remmber=[[m-E,b-H]]}else if(this.isInBackground(t)){var T=Math.round(m-this.originX/this.scale),w=Math.round(b-this.originY/this.scale);if(this.activeShape.creating&&!this.readonly){if([a.Polygon,a.Line].includes(this.activeShape.type)){var G=s(this.activeShape.coor[this.activeShape.coor.length-1],2),C=G[0],k=G[1];C!==m&&k!==b&&this.activeShape.coor.push([T,w])}}else if(this.createType===a.None||this.readonly||this.isCtrlKey){var D=s(this.hitOnShape(this.mouse),2),_=D[0],W=D[1];if(_>-1&&W){if(W.dragging=!0,this.dataset.forEach((function(t,e){return t.active=e===_})),this.dataset.splice(_,1),this.dataset.push(W),!this.readonly)if(this.remmber=[],[a.Dot,a.Circle].includes(W.type)){var R=s(W.coor,2);C=R[0],k=R[1];this.remmber=[[m-C,b-k]]}else W.coor.forEach((function(t){c.remmber.push([m-t[0],b-t[1]])}));this.emit("select",W)}else this.activeShape.active=!1,this.dataset.sort((function(t,e){return t.index-e.index})),this.emit("select",null)}else{var P=void 0,A=[T,w];switch(this.createType){case a.Rect:(P=new o({coor:[A,A]},this.dataset.length,this)).creating=!0;break;case a.Polygon:(P=new r({coor:[A]},this.dataset.length,this)).creating=!0;break;case a.Dot:P=new h({coor:A},this.dataset.length,this),this.emit("add",P);break;case a.Line:(P=new l({coor:[A]},this.dataset.length,this)).creating=!0;break;case a.Circle:P=new d({coor:A},this.dataset.length,this),console.log(P),P.creating=!0;break;case a.Grid:(P=new u({coor:[A,A]},this.dataset.length,this)).creating=!0;break;case a.Brush:(P=new f({coor:[A]},this.dataset.length,this)).creating=!0;break;case a.Eraser:(P=new p({coor:[A]},this.dataset.length,this)).creating=!0}P&&(this.dataset.forEach((function(t){t.active=!1})),P.active=!0,this.dataset.push(P))}this.update()}}else if(!this.isMobile&&2===t.buttons||this.isMobile&&3===(null===(n=t.touches)||void 0===n?void 0:n.length)&&!this.readonly){if([a.Grid].includes(this.activeShape.type)&&this.gridMenuEnable){var L=prompt("x 行 y 列 x,y",[this.activeShape.row,this.activeShape.col].join(","));if("string"==typeof L){var O=s(L.split(","),2),N=O[0],X=O[1];/^[1-9]\d*$/.test(N)&&/^[1-9]\d*$/.test(X)&&(this.activeShape.row=Number(N),this.activeShape.col=Number(X),this.update())}}this.emit("contextmenu",t)}}},n.prototype.handleMouseMove=function(t){var e,i,n,o;if(t.stopPropagation(),!this.lock){var r=this.mergeEvent(t),h=r.mouseX,c=r.mouseY,l=r.mouseCX,d=r.mouseCY,u=Math.round(h/this.scale),v=Math.round(c/this.scale);!this.isCtrlKey&&this.isInBackground(t)?(this.crossX.coor=[[u-this.originX/this.scale,0],[u-this.originX/this.scale,this.image.height]],this.crossY.coor=[[0,v-this.originY/this.scale],[this.image.width,v-this.originY/this.scale]]):(this.crossX.coor=[],this.crossY.coor=[]),this.mouse=this.isMobile&&2===(null===(e=t.touches)||void 0===e?void 0:e.length)?[l,d]:[h,c];var f=Math.round(u-this.originX/this.scale),p=Math.round(v-this.originY/this.scale);if(this.position=[f,p],(!this.isMobile&&1===t.buttons||this.isMobile&&1===(null===(i=t.touches)||void 0===i?void 0:i.length))&&this.activeShape.type){if(this.ctrlIndex>-1&&this.remmber.length&&(this.isInBackground(t)||this.activeShape.type===a.Circle)){var y=s(this.remmber,1),g=s(y[0],2),S=g[0],I=g[1];if([a.Rect,a.Grid].includes(this.activeShape.type))if(8===this.ctrlIndex){var m=s(this.activeShape.center,2),b=u-S-m[0],x=v-I-m[1];this.activeShape.rotation=Math.atan2(x,b)+Math.PI/2}else{var M=s(this.activeShape.coor,2),E=s(M[0],2),H=E[0],T=E[1],w=s(M[1],2),G=w[0],C=w[1],k=[];switch(this.ctrlIndex){case 0:k=[[u-S,v-I],[G,C]];break;case 1:k=[[H,v-I],[G,C]];break;case 2:k=[[H,v-I],[u-S,C]];break;case 3:k=[[H,T],[u-S,C]];break;case 4:k=[[H,T],[u-S,v-I]];break;case 5:k=[[H,T],[G,v-I]];break;case 6:k=[[u-S,T],[G,v-I]];break;case 7:k=[[u-S,T],[G,C]]}var D=s(k,2),_=s(D[0],2),W=_[0],R=_[1],P=s(D[1],2),A=P[0],L=P[1];(W<0||A<0||R<0||L<0||A>this.IMAGE_ORIGIN_WIDTH||L>this.IMAGE_ORIGIN_HEIGHT)&&(W<0&&(W=0),A<0&&(A=0),R<0&&(R=0),L<0&&(L=0),A>this.IMAGE_ORIGIN_WIDTH&&(A=this.IMAGE_ORIGIN_WIDTH),L>this.IMAGE_ORIGIN_HEIGHT&&(L=this.IMAGE_ORIGIN_HEIGHT)),A-W>=this.MIN_WIDTH&&L-R>=this.MIN_HEIGHT?this.activeShape.coor=[[W,R],[A,L]]:this.emit("warn","Width cannot be less than ".concat(this.MIN_WIDTH,",Height cannot be less than").concat(this.MIN_HEIGHT,"。"))}else if([a.Polygon,a.Line].includes(this.activeShape.type)){var O=[f,p];this.activeShape.coor.splice(this.ctrlIndex,1,O)}else if(this.activeShape.type===a.Circle){var N=Math.round(u-this.originX/this.scale)-this.activeShape.coor[0];N>=this.MIN_RADIUS&&(this.activeShape.radius=N)}if(this.isMagnifierVisible){var X=s(this.isMobile?[l,d]:[h,c],2),Y=X[0],F=X[1];this.createMagnifier(Y,F)}}else if(this.activeShape.dragging&&!this.readonly){if(this.isMagnifierVisible&&3===this.activeShape.type){var B=s(this.isMobile?[l,d]:[h,c],2);Y=B[0],F=B[1];this.createMagnifier(Y,F)}k=[];var j=!0,z=this.IMAGE_ORIGIN_WIDTH||this.WIDTH,K=this.IMAGE_ORIGIN_HEIGHT||this.HEIGHT;if([a.Dot,a.Circle].includes(this.activeShape.type)){var U=s(this.remmber[0],2),q=U[0];I=v-U[1];((S=u-q)<0||S>z||I<0||I>K)&&(j=!1),k=[S,I]}else for(var J=0;J<this.activeShape.coor.length;J++){var V=this.remmber[J];S=u-V[0],I=v-V[1];(S<0||S>z||I<0||I>K)&&(j=!1),k.push([S,I])}j&&(this.activeShape.coor=k)}else if(this.activeShape.creating&&this.isInBackground(t))if([a.Rect,a.Grid].includes(this.activeShape.type))this.activeShape.coor.splice(1,1,[f,p]);else if(this.activeShape.type===a.Circle){var Z=s(this.activeShape.coor,2),$=(H=Z[0],T=Z[1],Math.sqrt(Math.pow(H-f,2)+Math.pow(T-p,2)));this.activeShape.radius=$}else[a.Brush,a.Eraser].includes(this.activeShape.type)&&this.activeShape.coor.push([f,p]);this.update()}else if([a.Polygon,a.Line].includes(this.activeShape.type)&&this.activeShape.creating)this.update();else if(!this.isMobile&&2===t.buttons&&3===t.which||this.isMobile&&1===(null===(n=t.touches)||void 0===n?void 0:n.length)&&!this.isTouch2)this.originX=Math.round(h-this.remmberOrigin[0]),this.originY=Math.round(c-this.remmberOrigin[1]),this.update();else if(this.isMobile&&2===(null===(o=t.touches)||void 0===o?void 0:o.length)){this.isTouch2=!0;var Q=t.touches[0],tt=t.touches[1],et=this.scaleTouchStore;this.scaleTouchStore=Math.abs((tt.clientX-Q.clientX)*(tt.clientY-Q.clientY)),this.setScale(this.scaleTouchStore>et,!0)}else this.isCtrlKey||this.update()}},n.prototype.handleMouseUp=function(t){var e;if(t.stopPropagation(),!this.lock){if(this.destroyMagnifier(),this.isMobile){if(0===(null===(e=t.touches)||void 0===e?void 0:e.length)&&(this.isTouch2=!1),Date.now()-this.dblTouchStore<this.dblTouch)return void this.handleDblclick(t);this.dblTouchStore=Date.now()}if(this.remmber=[],this.activeShape.type!==a.None&&!this.isCtrlKey&&(this.activeShape.dragging=!1,this.activeShape.creating)){if([a.Rect,a.Grid].includes(this.activeShape.type)){var i=s(this.activeShape.coor,2),n=s(i[0],2),o=n[0],r=n[1],h=s(i[1],2),c=h[0],l=h[1];Math.abs(o-c)<this.MIN_WIDTH||Math.abs(r-l)<this.MIN_HEIGHT?(this.dataset.pop(),this.emit("warn","Width cannot be less than ".concat(this.MIN_WIDTH,",Height cannot be less than ").concat(this.MIN_HEIGHT))):(this.activeShape.coor=[[Math.min(o,c),Math.min(r,l)],[Math.max(o,c),Math.max(r,l)]],this.activeShape.creating=!1,this.emit("add",this.activeShape))}else this.activeShape.type===a.Circle?this.activeShape.radius<this.MIN_RADIUS?(this.dataset.pop(),this.emit("warn","Radius cannot be less than ".concat(this.MIN_WIDTH))):(this.activeShape.creating=!1,this.emit("add",this.activeShape)):[a.Brush,a.Eraser].includes(this.activeShape.type)&&(this.activeShape.creating=!1,this.emit("add",this.activeShape));this.update()}}},n.prototype.handleDblclick=function(t){var e=this;if(t.stopPropagation(),!this.lock)if([a.Polygon,a.Line].includes(this.activeShape.type)){var i=this.activeShape.type===a.Polygon&&this.activeShape.coor.length>2,s=this.activeShape.type===a.Line&&this.activeShape.coor.length>1;(i||s)&&(this.emit("add",this.activeShape),this.activeShape.creating=!1,this.update())}else[a.Grid].includes(this.activeShape.type)&&this.activeShape.active&&(this.activeShape.gridRects.forEach((function(t){if(e.isPointInRect(e.mouse,t.coor)){var i=e.activeShape.selected.findIndex((function(e){return t.index===e}));i>-1?e.activeShape.selected.splice(i,1):e.activeShape.selected.push(t.index)}})),this.update())},n.prototype.handleKeydown=function(t){t.code===this.ctrlCode&&(this.isCtrlKey=!0)},n.prototype.handleKeyup=function(t){t.code===this.ctrlCode&&(this.isCtrlKey=!1),this.lock||document.activeElement!==document.body||this.readonly||this.activeShape.type&&([a.Polygon,a.Line].includes(this.activeShape.type)&&"Escape"===t.key?(this.activeShape.coor.length>1&&this.activeShape.creating?this.activeShape.coor.pop():this.deleteByIndex(this.activeShape.index),this.update()):"Backspace"===t.key&&this.deleteByIndex(this.activeShape.index))},n.prototype.initSetting=function(){var t;if(this.canvas&&this.offScreen){var e=window.devicePixelRatio||1;this.image.crossOrigin="anonymous",this.canvas.style.userSelect="none",this.ctx=this.ctx||this.canvas.getContext("2d",{alpha:this.alpha}),this.WIDTH=Math.round(this.canvas.clientWidth),this.HEIGHT=Math.round(this.canvas.clientHeight),this.canvas.width=this.WIDTH*e,this.canvas.height=this.HEIGHT*e,this.canvas.style.width=this.WIDTH+"px",this.canvas.style.height=this.HEIGHT+"px",this.offScreen.width=this.WIDTH,this.offScreen.height=this.HEIGHT,this.offScreenCtx=this.offScreenCtx||this.offScreen.getContext("2d",{willReadFrequently:!0}),null===(t=this.ctx)||void 0===t||t.scale(e,e)}},n.prototype.initEvents=function(){this.canvas&&(this.image.addEventListener("load",this.handleLoad),this.canvas.addEventListener("touchstart",this.handleMouseDown),this.canvas.addEventListener("touchmove",this.handleMouseMove),this.canvas.addEventListener("touchend",this.handleMouseUp),this.canvas.addEventListener("contextmenu",this.handleContextmenu),this.canvas.addEventListener("mousewheel",this.handleMousewheel),this.canvas.addEventListener("wheel",this.handleMousewheel),this.canvas.addEventListener("mousedown",this.handleMouseDown),this.canvas.addEventListener("mousemove",this.handleMouseMove),this.canvas.addEventListener("mouseup",this.handleMouseUp),this.canvas.addEventListener("dblclick",this.handleDblclick),document.body.addEventListener("keydown",this.handleKeydown,!0),document.body.addEventListener("keyup",this.handleKeyup,!0))},n.prototype.setImage=function(t){"string"==typeof t?this.image.src=t:(this.image=t,this.image.crossOrigin="anonymous",this.image.complete?this.handleLoad():this.image.addEventListener("load",this.handleLoad))},n.prototype.setData=function(t){var e=this;setTimeout((function(){var i=[];t.forEach((function(t,s){if(Object.prototype.toString.call(t).includes("Object")){var n=void 0;switch(t.type){case a.Rect:n=new o(t,s,e);break;case a.Polygon:n=new r(t,s,e);break;case a.Dot:n=new h(t,s,e);break;case a.Line:n=new l(t,s,e);break;case a.Circle:n=new d(t,s,e);break;case a.Grid:n=new u(t,s,e);break;case a.Brush:n=new f(t,s,e);break;case a.Eraser:n=new p(t,s,e);break;default:console.warn("Invalid shape",t)}[a.Rect,a.Polygon,a.Dot,a.Line,a.Circle,a.Grid,a.Brush,a.Eraser].includes(t.type)&&n&&i.push(n)}else console.warn("Shape must be an enumerable Object.",t)})),e.dataset=i,e.update()}))},n.prototype.hitOnShape=function(t){for(var e,i=-1,s=this.dataset.length-1;s>-1;s--){var n=this.dataset[s];if(!n.hide&&(n.type===a.Dot&&this.isPointInCircle(t,n.coor,this.ctrlRadius)||n.type===a.Circle&&this.isPointInCircle(t,n.coor,n.radius*this.scale)||n.type===a.Rect&&this.isPointInRect(t,n.coor)||n.type===a.Polygon&&this.isPointInPolygon(t,n.coor)||n.type===a.Line&&this.isPointInLine(t,n.coor)||n.type===a.Grid&&this.isPointInRect(t,n.coor))){if(this.focusMode&&!n.active)continue;i=s,e=n;break}}return[i,e]},n.prototype.isInBackground=function(t){var e=this.mergeEvent(t),i=e.mouseX,s=e.mouseY;return i>=this.originX&&s>=this.originY&&i<=this.originX+this.IMAGE_ORIGIN_WIDTH*this.scale&&s<=this.originY+this.IMAGE_ORIGIN_HEIGHT*this.scale},n.prototype.isPointInRect=function(t,e){var i=this,a=s(t,2),n=a[0],o=a[1],r=s(e.map((function(t){return t.map((function(t){return t*i.scale}))})),2),h=s(r[0],2),c=h[0],l=h[1],d=s(r[1],2),u=d[0],v=d[1];return c+this.originX<=n&&n<=u+this.originX&&l+this.originY<=o&&o<=v+this.originY},n.prototype.isPointInPolygon=function(t,e){var i=this;if(!this.offScreenCtx)return!1;this.offScreenCtx.save(),this.offScreenCtx.clearRect(0,0,this.WIDTH,this.HEIGHT),this.offScreenCtx.translate(this.originX,this.originY),this.offScreenCtx.beginPath(),e.forEach((function(t,e){var a,n,o=s(t.map((function(t){return Math.round(t*i.scale)})),2),r=o[0],h=o[1];0===e?null===(a=i.offScreenCtx)||void 0===a||a.moveTo(r,h):null===(n=i.offScreenCtx)||void 0===n||n.lineTo(r,h)})),this.offScreenCtx.closePath(),this.offScreenCtx.fill();var a=this.offScreenCtx.getImageData(0,0,this.WIDTH,this.HEIGHT),n=(t[1]-1)*this.WIDTH*4+4*t[0];return this.offScreenCtx.restore(),0!==a.data[n+3]},n.prototype.isPointInCircle=function(t,e,i){var a=this,n=s(t,2),o=n[0],r=n[1],h=s(e.map((function(t){return t*a.scale})),2),c=h[0],l=h[1];return Math.sqrt(Math.pow(c+this.originX-o,2)+Math.pow(l+this.originY-r,2))<=i},n.prototype.isPointInLine=function(t,e){var i=this;if(!this.offScreenCtx)return!1;this.offScreenCtx.save(),this.offScreenCtx.clearRect(0,0,this.WIDTH,this.HEIGHT),this.offScreenCtx.translate(this.originX,this.originY),this.offScreenCtx.lineWidth=this.lineWidth>5?this.lineWidth:5,this.offScreenCtx.beginPath(),e.forEach((function(t,e){var a,n,o=s(t.map((function(t){return Math.round(t*i.scale)})),2),r=o[0],h=o[1];0===e?null===(a=i.offScreenCtx)||void 0===a||a.moveTo(r,h):null===(n=i.offScreenCtx)||void 0===n||n.lineTo(r,h)})),this.offScreenCtx.stroke();var a=this.offScreenCtx.getImageData(0,0,this.WIDTH,this.HEIGHT),n=(t[1]-1)*this.WIDTH*4+4*t[0];return this.offScreenCtx.restore(),0!==a.data[n+3]},n.prototype.drawRect=function(t,e){var i=this;if(this.ctx&&2===t.coor.length){var a=t.strokeStyle,n=void 0===a?"":a,o=t.fillStyle,r=void 0===o?"":o,h=t.active,c=t.creating,l=t.coor,d=t.lineWidth,u=t.rotation,v=s(l.map((function(t){return t.map((function(t){return Math.round(t*i.scale)}))})),2),f=s(v[0],2),p=f[0],y=f[1],g=s(v[1],2),S=g[0],I=g[1],m=(p+S)/2,b=(y+I)/2,x=S-p,M=I-y;this.ctx.save(),this.ctx.lineWidth=d||this.lineWidth,(null==e?void 0:e.isSelected)?this.ctx.fillStyle=(null==e?void 0:e.selectedFillStyle)||r:this.ctx.fillStyle=h||c?this.activeFillStyle:r,this.ctx.strokeStyle=h||c?this.activeStrokeStyle:n,this.ctx.translate(m,b),this.ctx.rotate(u),this.ctx.translate(-m,-b),c||this.ctx.fillRect(p,y,x,M),this.ctx.strokeRect(p,y,x,M),this.ctx.restore(),this.drawLabel(l[0],t)}},n.prototype.drawPolygon=function(t){var e=this;if(this.ctx){var i=t.strokeStyle,a=void 0===i?"":i,n=t.fillStyle,o=void 0===n?"":n,r=t.active,h=t.creating,c=t.coor,l=t.lineWidth;if(this.ctx.save(),this.ctx.lineJoin="round",this.ctx.lineWidth=l||this.lineWidth,this.ctx.fillStyle=r||h?this.activeFillStyle:o,this.ctx.strokeStyle=r||h?this.activeStrokeStyle:a,this.ctx.beginPath(),c.forEach((function(t,i){var a,n,o=s(t.map((function(t){return Math.round(t*e.scale)})),2),r=o[0],h=o[1];0===i?null===(a=e.ctx)||void 0===a||a.moveTo(r,h):null===(n=e.ctx)||void 0===n||n.lineTo(r,h)})),h){var d=s(this.mouse||[],2),u=d[0],v=d[1];this.ctx.lineTo(u-this.originX,v-this.originY)}else c.length>2&&this.ctx.closePath();this.ctx.fill(),this.ctx.stroke(),this.ctx.restore(),this.drawLabel(c[0],t)}},n.prototype.drawDot=function(t){var e=this;if(this.ctx){var i=t.strokeStyle,a=void 0===i?"":i,n=t.creating,o=t.fillStyle,r=void 0===o?"":o,h=t.active,c=t.coor,l=t.lineWidth,d=s(c.map((function(t){return t*e.scale})),2),u=d[0],v=d[1];this.ctx.save(),this.ctx.lineWidth=l||this.lineWidth,this.ctx.fillStyle=h||n?this.activeFillStyle:r,this.ctx.strokeStyle=h?this.activeStrokeStyle:a,this.ctx.beginPath(),this.ctx.arc(u,v,this.ctrlRadius,0,2*Math.PI,!0),this.ctx.fill(),this.ctx.arc(u,v,this.ctrlRadius,0,2*Math.PI,!0),this.ctx.stroke(),this.ctx.restore(),this.drawLabel(c,t)}},n.prototype.drawCirle=function(t){var e=this;if(this.ctx){var i=t.strokeStyle,a=void 0===i?"":i,n=t.fillStyle,o=void 0===n?"":n,r=t.active,h=t.coor,c=t.creating,l=t.radius,d=t.ctrlsData,u=t.lineWidth,v=s(h.map((function(t){return t*e.scale})),2),f=v[0],p=v[1];this.ctx.save(),this.ctx.lineWidth=u||this.lineWidth,this.ctx.fillStyle=r||c?this.activeFillStyle:o,this.ctx.strokeStyle=r||c?this.activeStrokeStyle:a,this.ctx.beginPath(),this.ctx.arc(f,p,l*this.scale,0,2*Math.PI,!0),this.ctx.fill(),this.ctx.arc(f,p,l*this.scale,0,2*Math.PI,!0),this.ctx.stroke(),this.ctx.restore(),this.drawLabel(d[0],t)}},n.prototype.drawLine=function(t){var e=this;if(this.ctx){var i=t.strokeStyle,a=void 0===i?"":i,n=t.active,o=t.creating,r=t.coor,h=t.lineWidth;if(this.ctx.save(),this.ctx.lineJoin="round",this.ctx.lineWidth=h||this.lineWidth,this.ctx.strokeStyle=n||o?this.activeStrokeStyle:a,this.ctx.beginPath(),r.forEach((function(t,i){var a,n,o=s(t.map((function(t){return Math.round(t*e.scale)})),2),r=o[0],h=o[1];0===i?null===(a=e.ctx)||void 0===a||a.moveTo(r,h):null===(n=e.ctx)||void 0===n||n.lineTo(r,h)})),o){var c=s(this.mouse||[],2),l=c[0],d=c[1];this.ctx.lineTo(l-this.originX,d-this.originY)}this.ctx.stroke(),this.ctx.restore(),this.drawLabel(r[0],t)}},n.prototype.drawGrid=function(t){var e=this;if(this.ctx&&2===t.coor.length){var i=t.strokeStyle,a=void 0===i?"":i,n=t.fillStyle,o=void 0===n?"":n,r=t.active,h=t.creating,c=t.coor,l=t.lineWidth,d=s(c.map((function(t){return t.map((function(t){return Math.round(t*e.scale)}))})),2),u=s(d[0],2),v=u[0],f=u[1],p=s(d[1],2),y=p[0],g=p[1];this.ctx.save(),this.ctx.lineWidth=l||this.lineWidth,this.ctx.fillStyle=r||h?this.activeFillStyle:o,this.ctx.strokeStyle=r||h?this.activeStrokeStyle:a,t.gridRects.forEach((function(i,s){var a;e.drawRect(i,{selectedFillStyle:t.selectedFillStyle||e.gridSelectedFillStyle,isSelected:null===(a=t.selected)||void 0===a?void 0:a.includes(s)})}));var S=y-v,I=g-f;h||this.ctx.fillRect(v,f,S,I),this.ctx.strokeRect(v,f,S,I),this.ctx.restore(),this.drawLabel(c[0],t)}},n.prototype.drawBrushPath=function(t){var e=this;if(this.ctx){var i=t.coor,a=t.brushSize,n=void 0===a?1:a,o=t.brushStokeStyle,r=void 0===o?"":o;this.ctx.save(),this.ctx.globalCompositeOperation="source-over",this.ctx.lineCap="round",this.ctx.lineJoin="round",this.ctx.lineWidth=Math.round(n*this.scale),this.ctx.strokeStyle=r,this.ctx.beginPath(),i.forEach((function(t,i){var a,n,o=s(t.map((function(t){return Math.round(t*e.scale)})),2),r=o[0],h=o[1];0===i?null===(a=e.ctx)||void 0===a||a.moveTo(r,h):null===(n=e.ctx)||void 0===n||n.lineTo(r,h)})),this.ctx.stroke(),this.ctx.restore()}},n.prototype.drawEraserPath=function(t){var e=this;if(this.ctx){var i=t.coor,a=t.eraserSize,n=void 0===a?1:a;this.ctx.save(),this.ctx.globalCompositeOperation="destination-out",this.ctx.lineCap="round",this.ctx.lineJoin="round",this.ctx.lineWidth=Math.round(n*this.scale),this.ctx.beginPath(),i.forEach((function(t,i){var a,n,o=s(t.map((function(t){return Math.round(t*e.scale)})),2),r=o[0],h=o[1];0===i?null===(a=e.ctx)||void 0===a||a.moveTo(r,h):null===(n=e.ctx)||void 0===n||n.lineTo(r,h)})),this.ctx.stroke(),this.ctx.restore()}},n.prototype.clearBrush=function(){var t=this.dataset.filter((function(t){return![a.Brush,a.Eraser].includes(t.type)}));this.setData(t)},n.prototype.drawCtrl=function(t,e){var i=this;if(this.ctx){var a=s(t.map((function(t){return t*i.scale})),2),n=a[0],o=a[1];this.ctx.save(),this.ctx.beginPath(),this.ctx.fillStyle=e||this.ctrlFillStyle,this.ctx.strokeStyle=this.ctrlStrokeStyle,this.ctx.arc(n,o,this.ctrlRadius,0,2*Math.PI,!0),this.ctx.fill(),this.ctx.arc(n,o,this.ctrlRadius,0,2*Math.PI,!0),this.ctx.stroke(),this.ctx.restore()}},n.prototype.drawCtrlList=function(t){var e=this;t.ctrlsData.forEach((function(i,s){t.type===a.Circle?1===s&&e.drawCtrl(i):e.drawCtrl(i,t.type===a.Rect&&8===s?"yellow":void 0)}))},n.prototype.drawLabel=function(t,e){var i=this,n=e.label,o=void 0===n?"":n,r=e.labelFillStyle,h=void 0===r?"":r,c=e.labelFont,l=void 0===c?"":c,d=e.textFillStyle,u=void 0===d?"":d,v=e.hideLabel,f=e.labelUp,p=e.lineWidth,y="boolean"==typeof v?v:this.hideLabel,g="boolean"==typeof f?f:this.labelUp,S=p||this.lineWidth;if(this.ctx&&o.length&&!y){this.ctx.font=l||this.labelFont;var I=o.length<this.labelMaxLen+1?o:"".concat(o.slice(0,this.labelMaxLen),"..."),m=this.ctx.measureText(I),b=parseInt(this.ctx.font)-4,x=m.width+8,M=b+8,E=s(t.map((function(t){return t*i.scale})),2),H=E[0],T=E[1];if(e.type===a.Rect&&0!==e.rotation){var w=e,G=s(w.coor,2),C=s(G[0],2),k=C[0],D=C[1],_=s(G[1],2),W=_[0],R=_[1],P=(k+W)/2*this.scale,A=(D+R)/2*this.scale,L=H-P,O=T-A;H=L*Math.cos(w.rotation)-O*Math.sin(w.rotation)+P,T=L*Math.sin(w.rotation)+O*Math.cos(w.rotation)+A}var N=this.IMAGE_ORIGIN_WIDTH-t[0]<x/this.scale,X=this.IMAGE_ORIGIN_HEIGHT-t[1]<M/this.scale,Y=t[1]>M/this.scale,F=g?Y:X;this.ctx.save(),this.ctx.fillStyle=h||this.labelFillStyle,this.ctx.fillRect(N?H-m.width-4-S/2:H+S/2,F?T-M-S/2:T+S/2,x,M),this.ctx.fillStyle=u||this.textFillStyle,this.ctx.fillText(I,N?H-m.width:H+4+S/2,F?T-M+b+4:T+b+4+S/2,180),this.ctx.restore()}},n.prototype.update=function(){var t=this;window.cancelAnimationFrame(this.timer),this.timer=window.requestAnimationFrame((function(){var e,i,s;if(t.ctx){t.ctx.save(),t.ctx.clearRect(0,0,t.WIDTH,t.HEIGHT),t.ctx.translate(t.originX,t.originY);for(var n=t.focusMode?t.activeShape.type?[t.activeShape]:[]:t.dataset,o=n.filter((function(t){return t.type===a.Brush||t.type===a.Eraser})),r=0;r<o.length;r++){if(!(c=o[r]).hide)switch(c.type){case a.Brush:c.brushSize=null!==(e=c.brushSize)&&void 0!==e?e:t.brushSize,c.brushStokeStyle=null!==(i=c.brushStokeStyle)&&void 0!==i?i:t.brushStokeStyle,t.drawBrushPath(c);break;case a.Eraser:c.eraserSize=null!==(s=c.eraserSize)&&void 0!==s?s:t.eraserSize,t.drawEraserPath(c)}}var h=n.filter((function(t){return t.type!==a.Brush&&t.type!==a.Eraser}));for(r=0;r<h.length;r++){var c;if(!(c=h[r]).hide)switch(c.type){case a.Rect:t.drawRect(c);break;case a.Polygon:t.drawPolygon(c);break;case a.Dot:t.drawDot(c);break;case a.Line:t.drawLine(c);break;case a.Circle:t.drawCirle(c);break;case a.Grid:t.drawGrid(c)}}!t.isCtrlKey&&t.showCross&&(t.drawLine(t.crossX),t.drawLine(t.crossY)),[a.Rect,a.Polygon,a.Line,a.Circle,a.Grid].includes(t.activeShape.type)&&!t.activeShape.hide&&t.drawCtrlList(t.activeShape),t.ctx.globalCompositeOperation="destination-over",t.IMAGE_WIDTH&&t.IMAGE_HEIGHT&&t.ctx.drawImage(t.image,0,0,t.IMAGE_WIDTH,t.IMAGE_HEIGHT),t.ctx.globalCompositeOperation="source-over",t.ctx.restore(),t.emit("updated",t.dataset)}}))},n.prototype.deleteByIndex=function(t){var e=this.dataset.findIndex((function(e){return e.index===t}));e>-1&&(this.emit("delete",this.dataset[e]),this.dataset.splice(e,1),this.dataset.forEach((function(t,e){t.index=e})),this.update())},n.prototype.deleteByUuid=function(t){var e=this.dataset.find((function(e){return e.uuid===t}));e&&(this.emit("delete",e),this.dataset=this.dataset.filter((function(e){return e.uuid!==t})),this.update())},n.prototype.calcStep=function(t){void 0===t&&(t=""),this.IMAGE_WIDTH<this.WIDTH&&this.IMAGE_HEIGHT<this.HEIGHT&&(""!==t&&"b"!==t||(this.setScale(!0,!1,!0),this.calcStep("b"))),(this.IMAGE_WIDTH>this.WIDTH||this.IMAGE_HEIGHT>this.HEIGHT)&&(""!==t&&"s"!==t||(this.setScale(!1,!1,!0),this.calcStep("s")))},n.prototype.setScale=function(t,e,i){if(void 0===e&&(e=!1),void 0===i&&(i=!1),!this.lock&&!(!t&&this.imageMin<20||t&&this.IMAGE_WIDTH>100*this.imageOriginMax)){t?this.scaleStep++:this.scaleStep--;var a=0,n=0,o=s(this.mouse||[],2),r=o[0],h=o[1];e&&(a=(r-this.originX)/this.scale,n=(h-this.originY)/this.scale);var c=Math.abs(this.scaleStep),l=this.IMAGE_WIDTH;if(this.IMAGE_WIDTH=Math.round(this.IMAGE_ORIGIN_WIDTH*Math.pow(this.scaleStep>=0?1.05:.95,c)),this.IMAGE_HEIGHT=Math.round(this.IMAGE_ORIGIN_HEIGHT*Math.pow(this.scaleStep>=0?1.05:.95,c)),e)this.originX=r-a*this.scale,this.originY=h-n*this.scale;else{var d=this.IMAGE_WIDTH/l;this.originX=this.WIDTH/2-(this.WIDTH/2-this.originX)*d,this.originY=this.HEIGHT/2-(this.HEIGHT/2-this.originY)*d}i||this.update()}},n.prototype.fitZoom=function(){this.calcStep(),this.IMAGE_HEIGHT/this.IMAGE_WIDTH>=this.HEIGHT/this.WIDTH?(this.IMAGE_WIDTH=this.IMAGE_ORIGIN_WIDTH/(this.IMAGE_ORIGIN_HEIGHT/this.HEIGHT),this.IMAGE_HEIGHT=this.HEIGHT):(this.IMAGE_WIDTH=this.WIDTH,this.IMAGE_HEIGHT=this.IMAGE_ORIGIN_HEIGHT/(this.IMAGE_ORIGIN_WIDTH/this.WIDTH)),this.originX=(this.WIDTH-this.IMAGE_WIDTH)/2,this.originY=(this.HEIGHT-this.IMAGE_HEIGHT)/2,this.update()},n.prototype.setFocusMode=function(t){this.focusMode=t,this.update()},n.prototype.destroy=function(){this.canvas&&(this.image.removeEventListener("load",this.handleLoad),this.canvas.removeEventListener("contextmenu",this.handleContextmenu),this.canvas.removeEventListener("mousewheel",this.handleMousewheel),this.canvas.removeEventListener("wheel",this.handleMousewheel),this.canvas.removeEventListener("mousedown",this.handleMouseDown),this.canvas.removeEventListener("touchend",this.handleMouseDown),this.canvas.removeEventListener("mousemove",this.handleMouseMove),this.canvas.removeEventListener("touchmove",this.handleMouseMove),this.canvas.removeEventListener("mouseup",this.handleMouseUp),this.canvas.removeEventListener("touchend",this.handleMouseUp),this.canvas.removeEventListener("dblclick",this.handleDblclick),document.body.removeEventListener("keydown",this.handleKeydown,!0),document.body.removeEventListener("keyup",this.handleKeyup,!0),this.canvas.width=this.WIDTH,this.canvas.height=this.HEIGHT,this.canvas.style.width="",this.canvas.style.height="",this.canvas.style.userSelect="")},n.prototype.resize=function(){this.canvas&&(this.canvas.removeAttribute("width"),this.canvas.removeAttribute("height"),this.canvas.style.width="",this.canvas.style.height="",this.initSetting(),this.update())},n}(c)})); //# sourceMappingURL=canvas-select.min.js.map