canvas-select-plus
Version:
一款基于canvas的2D图像标注工具,在canvas-select的基础上,添加了清空、复制、撤销、重做、隐藏、显示和鼠标样式变化等功能,尚在开发和维护中...
1 lines • 82.3 kB
JavaScript
!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 n(){this.constructor=e}t(e,i),e.prototype=null===i?Object.create(i):(n.prototype=i.prototype,new n)}var i=function(){return i=Object.assign||function(t){for(var e,i=1,n=arguments.length;i<n;i++)for(var r in e=arguments[i])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},i.apply(this,arguments)};function n(t,e,i,n){return new(i||(i=Promise))((function(r,s){function a(t){try{h(n.next(t))}catch(t){s(t)}}function o(t){try{h(n.throw(t))}catch(t){s(t)}}function h(t){var e;t.done?r(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(a,o)}h((n=n.apply(t,e||[])).next())}))}function r(t,e){var i,n,r,s,a={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return s={next:o(0),throw:o(1),return:o(2)},"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function o(o){return function(h){return function(o){if(i)throw new TypeError("Generator is already executing.");for(;s&&(s=0,o[0]&&(a=0)),a;)try{if(i=1,n&&(r=2&o[0]?n.return:o[0]?n.throw||((r=n.return)&&r.call(n),0):n.next)&&!(r=r.call(n,o[1])).done)return r;switch(n=0,r&&(o=[2&o[0],r.value]),o[0]){case 0:case 1:r=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,n=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(!(r=a.trys,(r=r.length>0&&r[r.length-1])||6!==o[0]&&2!==o[0])){a=0;continue}if(3===o[0]&&(!r||o[1]>r[0]&&o[1]<r[3])){a.label=o[1];break}if(6===o[0]&&a.label<r[1]){a.label=r[1],r=o;break}if(r&&a.label<r[2]){a.label=r[2],a.ops.push(o);break}r[2]&&a.ops.pop(),a.trys.pop();continue}o=e.call(t,a)}catch(t){o=[6,t],n=0}finally{i=r=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,h])}}}function s(t){var e="function"==typeof Symbol&&Symbol.iterator,i=e&&t[e],n=0;if(i)return i.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function a(t,e){var i="function"==typeof Symbol&&t[Symbol.iterator];if(!i)return t;var n,r,s=i.call(t),a=[];try{for(;(void 0===e||e-- >0)&&!(n=s.next()).done;)a.push(n.value)}catch(t){r={error:t}}finally{try{n&&!n.done&&(i=s.return)&&i.call(s)}finally{if(r)throw r.error}}return a}function o(t,e,i){if(i||2===arguments.length)for(var n,r=0,s=e.length;r<s;r++)!n&&r in e||(n||(n=Array.prototype.slice.call(e,0,r)),n[r]=e[r]);return t.concat(n||Array.prototype.slice.call(e))}function h(){for(var t=[],e="0123456789abcdef",i=0;i<36;i++){var n=Math.floor(16*Math.random());t[i]=e.slice(n,n+1)}t[14]="4";var r=3&t[19]|8;return t[19]=e.slice(r,r+1),t[8]=t[13]=t[18]=t[23]="-",t.join("")}function l(t,e,i){for(var n=!1,r=i.length,s=0,a=r-1;s<r;a=s++){var o=i[s][0],h=i[s][1],l=i[a][0],c=i[a][1];h>e!=c>e&&t<(l-o)*(e-h)/(c-h)+o&&(n=!n)}return n}function c(t,e){if(void 0===e&&(e=new WeakMap),null===t||"object"!=typeof t)return t;if(e.has(t))return e.get(t);if(t instanceof ImageData){var i=new ImageData(new Uint8ClampedArray(t.data),t.width,t.height);return e.set(t,i),i}if(t instanceof Date)return new Date(t.getTime());if(t instanceof RegExp)return new RegExp(t.source,t.flags);if(t instanceof Map){var n=new Map;return e.set(t,n),t.forEach((function(t,i){n.set(c(i,e),c(t,e))})),n}if(t instanceof Set){var r=new Set;return e.set(t,r),t.forEach((function(t){r.add(c(t,e))})),r}if(Array.isArray(t)){var s=[];return e.set(t,s),t.forEach((function(t,i){s[i]=c(t,e)})),s}var a=Object.create(Object.getPrototypeOf(t));return e.set(t,a),Reflect.ownKeys(t).forEach((function(i){var n=t[i];a[i]=c(n,e)})),a}function u(t,e,i){var n,r,a,o;if(t===e)return!0;if(8===t.type&&8===e.type){try{for(var h=s(["uuid","label","maskBase64"]),l=h.next();!l.done;l=h.next()){if(t[g=l.value]!==e[g])return!1}}catch(t){n={error:t}}finally{try{l&&!l.done&&(r=h.return)&&r.call(h)}finally{if(n)throw n.error}}return!0}if("object"!=typeof t||null===t||"object"!=typeof e||null===e)return!1;if(Array.isArray(t)&&Array.isArray(e)){if(t.length!==e.length)return!1;for(var c=0;c<t.length;c++)if(!u(t[c],e[c],i))return!1;return!0}var d=i||Object.keys(t);try{for(var p=s(d),f=p.next();!f.done;f=p.next()){var g;if(!((g=f.value)in t)&&g in e||g in t&&!(g in e)||!u(t[g],e[g]))return!1}}catch(t){a={error:t}}finally{try{f&&!f.done&&(o=p.return)&&o.call(p)}finally{if(a)throw a.error}}return!0}"function"==typeof SuppressedError&&SuppressedError;var d=function(t,e){this.tagId="",this.label="",this.labelId="",this.labelType=0,this.truncated=0,this.coor=[],this.active=!1,this.creating=!1,this.dragging=!1,this.hiddening=!1,this.locking=!1,this.uuid=h(),this.index=e,Object.assign(this,t)},p=function(t){function i(e,i){var n=t.call(this,e,i)||this;return n.type=1,n.iscontour=!1,n}return e(i,t),Object.defineProperty(i.prototype,"ctrlsData",{get:function(){var t=a(this.coor,2),e=a(t[0],2),i=e[0],n=e[1],r=a(t[1],2),s=r[0],o=r[1];return[[i,n],[i+(s-i)/2,n],[s,n],[s,n+(o-n)/2],[s,o],[i+(s-i)/2,o],[i,o],[i,n+(o-n)/2]]},enumerable:!1,configurable:!0}),i}(d),f=function(t){function i(e,i){var n=t.call(this,e,i)||this;return n.type=2,n}return e(i,t),Object.defineProperty(i.prototype,"ctrlsData",{get:function(){return this.coor.length>2?this.coor:[]},enumerable:!1,configurable:!0}),i}(d),g=function(t){function i(e,i){var n=t.call(this,e,i)||this;return n.type=3,n.color="",n.color=e.color||n.color,n}return e(i,t),i}(d),v=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 n=this._eventTree[t];Array.isArray(n)&&n.forEach((function(t){return t.apply(void 0,o([],a(e),!1))}))},t.prototype.off=function(t,e){var i=this._eventTree[t],n=i.find((function(t){return t===e}));Array.isArray(i)&&n&&i.splice(n,1)},t}(),y=function(t){function i(e,i){var n=t.call(this,e,i)||this;return n.type=4,n}return e(i,t),Object.defineProperty(i.prototype,"ctrlsData",{get:function(){return this.coor.length>1?this.coor:[]},enumerable:!1,configurable:!0}),i}(d),I=function(t){function i(e,i){var n=t.call(this,e,i)||this;return n.type=5,n.radius=0,n.radius=e.radius||n.radius,n}return e(i,t),Object.defineProperty(i.prototype,"ctrlsData",{get:function(){var t=a(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}(d),m=function(t){function i(e,i){var n=t.call(this,e,i)||this;return n.type=6,n.row=1,n.col=1,n.selected=[],n.row=e.row>0?e.row:n.row,n.col=e.col>0?e.col:n.col,n.selected=Array.isArray(e.selected)?e.selected:[],n}return e(i,t),Object.defineProperty(i.prototype,"ctrlsData",{get:function(){var t=a(this.coor,2),e=a(t[0],2),i=e[0],n=e[1],r=a(t[1],2),s=r[0],o=r[1];return[[i,n],[i+(s-i)/2,n],[s,n],[s,n+(o-n)/2],[s,o],[i+(s-i)/2,o],[i,o],[i,n+(o-n)/2]]},enumerable:!1,configurable:!0}),Object.defineProperty(i.prototype,"gridRects",{get:function(){for(var t=a(this.coor,2),e=a(t[0],2),i=e[0],n=e[1],r=a(t[1],2),s=r[0],o=r[1],h=this,l=h.row,c=h.col,u=h.strokeStyle,d=h.fillStyle,f=h.active,g=h.creating,v=h.lineWidth,y=(s-i)/this.col,I=(o-n)/this.row,m=[],S=0;S<l;S++)for(var x=0;x<c;x++){var b=[i+x*y,n+S*I],M=new p({coor:[b,[b[0]+y,b[1]+I]],strokeStyle:u,fillStyle:d,active:f,creating:g,lineWidth:v},S*c+x);m.push(M)}return m},enumerable:!1,configurable:!0}),i}(d),S=function(t){function i(e,i){var n=t.call(this,e,i)||this;return n.type=7,n.brushWidth=1,n.iseraser=!1,n.boundingRect=[],n.iseraser=e.iseraser||n.iseraser,n.boundingRect=e.boundingRect||n.boundingRect,n}return e(i,t),Object.defineProperty(i.prototype,"ctrlsData",{get:function(){return this.coor.length>3?this.coor:[]},enumerable:!1,configurable:!0}),i}(d),x=function(t){function i(e,i){var n=t.call(this,e,i)||this;return n.type=8,n.maskType="",n.maskBase64="",n.pixels=[],n.canvasData=null,n.height=0,n.weight=0,n.maskToPolygon=!1,n.magicPoints=[],n.maskType=e.maskType||"",n.maskBase64=e.maskBase64||"",n.pixels=e.pixels||[],n.canvasData=e.canvasData||null,n.height=e.height||0,n.weight=e.weight||0,n.maskToPolygon=e.maskToPolygon||!1,n.magicPoints=e.magicPoints||[],n}return e(i,t),i}(d),b=function(t){function i(e,i){var n=t.call(this,e,i)||this;return n.type=9,n.boundingRect=[],n.boundingRect=e.boundingRect||n.boundingRect,n}return e(i,t),Object.defineProperty(i.prototype,"ctrlsData",{get:function(){return this.coor.length>3?this.coor:[]},enumerable:!1,configurable:!0}),i}(d),M=function(t){function i(e,i){var n=t.call(this,e,i)||this;return n.type=10,n.encodePixelData=[],n.startPoint=[0,0],n.width=0,n.height=0,n.encodePixelData=e.encodePixelData||[],n.startPoint=e.startPoint||[0,0],n.width=e.width||0,n.height=e.height||0,n}return e(i,t),i}(d),E="2.25.5";function k(t,e){return t>e?1:t<e?-1:0}class w{constructor(t=k,e=!1){this._compare=t,this._root=null,this._size=0,this._noDuplicates=!!e}rotateLeft(t){var e=t.right;e&&(t.right=e.left,e.left&&(e.left.parent=t),e.parent=t.parent),t.parent?t===t.parent.left?t.parent.left=e:t.parent.right=e:this._root=e,e&&(e.left=t),t.parent=e}rotateRight(t){var e=t.left;e&&(t.left=e.right,e.right&&(e.right.parent=t),e.parent=t.parent),t.parent?t===t.parent.left?t.parent.left=e:t.parent.right=e:this._root=e,e&&(e.right=t),t.parent=e}_splay(t){for(;t.parent;){var e=t.parent;e.parent?e.left===t&&e.parent.left===e?(this.rotateRight(e.parent),this.rotateRight(e)):e.right===t&&e.parent.right===e?(this.rotateLeft(e.parent),this.rotateLeft(e)):e.left===t&&e.parent.right===e?(this.rotateRight(e),this.rotateLeft(e)):(this.rotateLeft(e),this.rotateRight(e)):e.left===t?this.rotateRight(e):this.rotateLeft(e)}}splay(t){for(var e,i,n,r,s;t.parent;)(i=(e=t.parent).parent)&&i.parent?((n=i.parent).left===i?n.left=t:n.right=t,t.parent=n):(t.parent=null,this._root=t),r=t.left,s=t.right,t===e.left?(i&&(i.left===e?(e.right?(i.left=e.right,i.left.parent=i):i.left=null,e.right=i,i.parent=e):(r?(i.right=r,r.parent=i):i.right=null,t.left=i,i.parent=t)),s?(e.left=s,s.parent=e):e.left=null,t.right=e,e.parent=t):(i&&(i.right===e?(e.left?(i.right=e.left,i.right.parent=i):i.right=null,e.left=i,i.parent=e):(s?(i.left=s,s.parent=i):i.left=null,t.right=i,i.parent=t)),r?(e.right=r,r.parent=e):e.right=null,t.left=e,e.parent=t)}replace(t,e){t.parent?t===t.parent.left?t.parent.left=e:t.parent.right=e:this._root=e,e&&(e.parent=t.parent)}minNode(t=this._root){if(t)for(;t.left;)t=t.left;return t}maxNode(t=this._root){if(t)for(;t.right;)t=t.right;return t}insert(t,e){var i=this._root,n=null,r=this._compare;if(this._noDuplicates)for(;i;){if(n=i,0===r(i.key,t))return;i=r(i.key,t)<0?i.right:i.left}else for(;i;)n=i,i=r(i.key,t)<0?i.right:i.left;return i={key:t,data:e,left:null,right:null,parent:n},n?r(n.key,i.key)<0?n.right=i:n.left=i:this._root=i,this.splay(i),this._size++,i}find(t){for(var e=this._root,i=this._compare;e;){var n=i(e.key,t);if(n<0)e=e.right;else{if(!(n>0))return e;e=e.left}}return null}contains(t){for(var e=this._root,i=this._compare;e;){var n=i(t,e.key);if(0===n)return!0;e=n<0?e.left:e.right}return!1}remove(t){var e=this.find(t);if(!e)return!1;if(this.splay(e),e.left)if(e.right){var i=this.minNode(e.right);i.parent!==e&&(this.replace(i,i.right),i.right=e.right,i.right.parent=i),this.replace(e,i),i.left=e.left,i.left.parent=i}else this.replace(e,e.left);else this.replace(e,e.right);return this._size--,!0}removeNode(t){if(!t)return!1;if(this.splay(t),t.left)if(t.right){var e=this.minNode(t.right);e.parent!==t&&(this.replace(e,e.right),e.right=t.right,e.right.parent=e),this.replace(t,e),e.left=t.left,e.left.parent=e}else this.replace(t,t.left);else this.replace(t,t.right);return this._size--,!0}erase(t){var e=this.find(t);if(e){this.splay(e);var i=e.left,n=e.right,r=null;i&&(i.parent=null,r=this.maxNode(i),this.splay(r),this._root=r),n&&(i?r.right=n:this._root=n,n.parent=r),this._size--}}pop(){var t=this._root,e=null;if(t){for(;t.left;)t=t.left;e={key:t.key,data:t.data},this.remove(t.key)}return e}next(t){var e=t;if(e)if(e.right)for(e=e.right;e&&e.left;)e=e.left;else for(e=t.parent;e&&e.right===t;)t=e,e=e.parent;return e}prev(t){var e=t;if(e)if(e.left)for(e=e.left;e&&e.right;)e=e.right;else for(e=t.parent;e&&e.left===t;)t=e,e=e.parent;return e}forEach(t){for(var e=this._root,i=[],n=!1,r=0;!n;)e?(i.push(e),e=e.left):i.length>0?(t(e=i.pop(),r++),e=e.right):n=!0;return this}range(t,e,i,n){const r=[],s=this._compare;let a,o=this._root;for(;0!==r.length||o;)if(o)r.push(o),o=o.left;else{if(o=r.pop(),a=s(o.key,e),a>0)break;if(s(o.key,t)>=0&&i.call(n,o))return this;o=o.right}return this}keys(){for(var t=this._root,e=[],i=[],n=!1;!n;)t?(e.push(t),t=t.left):e.length>0?(t=e.pop(),i.push(t.key),t=t.right):n=!0;return i}values(){for(var t=this._root,e=[],i=[],n=!1;!n;)t?(e.push(t),t=t.left):e.length>0?(t=e.pop(),i.push(t.data),t=t.right):n=!0;return i}at(t){for(var e=this._root,i=[],n=!1,r=0;!n;)if(e)i.push(e),e=e.left;else if(i.length>0){if(e=i.pop(),r===t)return e;r++,e=e.right}else n=!0;return null}load(t=[],e=[],i=!1){if(0!==this._size)throw new Error("bulk-load: tree is not empty");const n=t.length;return i&&H(t,e,0,n-1,this._compare),this._root=T(null,t,e,0,n),this._size=n,this}min(){var t=this.minNode(this._root);return t?t.key:null}max(){var t=this.maxNode(this._root);return t?t.key:null}isEmpty(){return null===this._root}get size(){return this._size}static createTree(t,e,i,n,r){return new w(i,r).load(t,e,n)}}function T(t,e,i,n,r){const s=r-n;if(s>0){const a=n+Math.floor(s/2),o={key:e[a],data:i[a],parent:t};return o.left=T(o,e,i,n,a),o.right=T(o,e,i,a+1,r),o}return null}function H(t,e,i,n,r){if(i>=n)return;const s=t[i+n>>1];let a=i-1,o=n+1;for(;;){do{a++}while(r(t[a],s)<0);do{o--}while(r(t[o],s)>0);if(a>=o)break;let i=t[a];t[a]=t[o],t[o]=i,i=e[a],e[a]=e[o],e[o]=i}H(t,e,i,o,r),H(t,e,o+1,n,r)}const _=0,G=1,P=2,D=3,L=0,R=1,A=2,C=3;function O(t,e,i){null===e?(t.inOut=!1,t.otherInOut=!0):(t.isSubject===e.isSubject?(t.inOut=!e.inOut,t.otherInOut=e.otherInOut):(t.inOut=!e.otherInOut,t.otherInOut=e.isVertical()?!e.inOut:e.inOut),e&&(t.prevInResult=!W(e,i)||e.isVertical()?e.prevInResult:e));let n=W(t,i);t.resultTransition=n?function(t,e){let i,n=!t.inOut,r=!t.otherInOut;switch(e){case L:i=n&&r;break;case R:i=n||r;break;case C:i=n^r;break;case A:i=t.isSubject?n&&!r:r&&!n}return i?1:-1}(t,i):0}function W(t,e){switch(t.type){case _:switch(e){case L:return!t.otherInOut;case R:return t.otherInOut;case A:return t.isSubject&&t.otherInOut||!t.isSubject&&!t.otherInOut;case C:return!0}break;case P:return e===L||e===R;case D:return e===A;case G:return!1}return!1}class N{constructor(t,e,i,n,r){this.left=e,this.point=t,this.otherEvent=i,this.isSubject=n,this.type=r||_,this.inOut=!1,this.otherInOut=!1,this.prevInResult=null,this.resultTransition=0,this.otherPos=-1,this.outputContourId=-1,this.isExteriorRing=!0}isBelow(t){const e=this.point,i=this.otherEvent.point;return this.left?(e[0]-t[0])*(i[1]-t[1])-(i[0]-t[0])*(e[1]-t[1])>0:(i[0]-t[0])*(e[1]-t[1])-(e[0]-t[0])*(i[1]-t[1])>0}isAbove(t){return!this.isBelow(t)}isVertical(){return this.point[0]===this.otherEvent.point[0]}get inResult(){return 0!==this.resultTransition}clone(){const t=new N(this.point,this.left,this.otherEvent,this.isSubject,this.type);return t.contourId=this.contourId,t.resultTransition=this.resultTransition,t.prevInResult=this.prevInResult,t.isExteriorRing=this.isExteriorRing,t.inOut=this.inOut,t.otherInOut=this.otherInOut,t}}function B(t,e){return t[0]===e[0]&&t[1]===e[1]}const Y=11102230246251565e-32,X=134217729,F=(3+8*Y)*Y;function j(t,e,i,n,r){let s,a,o,h,l=e[0],c=n[0],u=0,d=0;c>l==c>-l?(s=l,l=e[++u]):(s=c,c=n[++d]);let p=0;if(u<t&&d<i)for(c>l==c>-l?(a=l+s,o=s-(a-l),l=e[++u]):(a=c+s,o=s-(a-c),c=n[++d]),s=a,0!==o&&(r[p++]=o);u<t&&d<i;)c>l==c>-l?(a=s+l,h=a-s,o=s-(a-h)+(l-h),l=e[++u]):(a=s+c,h=a-s,o=s-(a-h)+(c-h),c=n[++d]),s=a,0!==o&&(r[p++]=o);for(;u<t;)a=s+l,h=a-s,o=s-(a-h)+(l-h),l=e[++u],s=a,0!==o&&(r[p++]=o);for(;d<i;)a=s+c,h=a-s,o=s-(a-h)+(c-h),c=n[++d],s=a,0!==o&&(r[p++]=o);return 0===s&&0!==p||(r[p++]=s),p}function z(t){return new Float64Array(t)}const U=33306690738754716e-32,K=22204460492503146e-32,q=11093356479670487e-47,V=z(4),Z=z(8),$=z(12),J=z(16),Q=z(4);function tt(t,e,i,n,r,s){const a=(e-s)*(i-r),o=(t-r)*(n-s),h=a-o;if(0===a||0===o||a>0!=o>0)return h;const l=Math.abs(a+o);return Math.abs(h)>=U*l?h:-function(t,e,i,n,r,s,a){let o,h,l,c,u,d,p,f,g,v,y,I,m,S,x,b,M,E;const k=t-r,w=i-r,T=e-s,H=n-s;S=k*H,d=X*k,p=d-(d-k),f=k-p,d=X*H,g=d-(d-H),v=H-g,x=f*v-(S-p*g-f*g-p*v),b=T*w,d=X*T,p=d-(d-T),f=T-p,d=X*w,g=d-(d-w),v=w-g,M=f*v-(b-p*g-f*g-p*v),y=x-M,u=x-y,V[0]=x-(y+u)+(u-M),I=S+y,u=I-S,m=S-(I-u)+(y-u),y=m-b,u=m-y,V[1]=m-(y+u)+(u-b),E=I+y,u=E-I,V[2]=I-(E-u)+(y-u),V[3]=E;let _=function(t,e){let i=e[0];for(let n=1;n<t;n++)i+=e[n];return i}(4,V),G=K*a;if(_>=G||-_>=G)return _;if(u=t-k,o=t-(k+u)+(u-r),u=i-w,l=i-(w+u)+(u-r),u=e-T,h=e-(T+u)+(u-s),u=n-H,c=n-(H+u)+(u-s),0===o&&0===h&&0===l&&0===c)return _;if(G=q*a+F*Math.abs(_),_+=k*c+H*o-(T*l+w*h),_>=G||-_>=G)return _;S=o*H,d=X*o,p=d-(d-o),f=o-p,d=X*H,g=d-(d-H),v=H-g,x=f*v-(S-p*g-f*g-p*v),b=h*w,d=X*h,p=d-(d-h),f=h-p,d=X*w,g=d-(d-w),v=w-g,M=f*v-(b-p*g-f*g-p*v),y=x-M,u=x-y,Q[0]=x-(y+u)+(u-M),I=S+y,u=I-S,m=S-(I-u)+(y-u),y=m-b,u=m-y,Q[1]=m-(y+u)+(u-b),E=I+y,u=E-I,Q[2]=I-(E-u)+(y-u),Q[3]=E;const P=j(4,V,4,Q,Z);S=k*c,d=X*k,p=d-(d-k),f=k-p,d=X*c,g=d-(d-c),v=c-g,x=f*v-(S-p*g-f*g-p*v),b=T*l,d=X*T,p=d-(d-T),f=T-p,d=X*l,g=d-(d-l),v=l-g,M=f*v-(b-p*g-f*g-p*v),y=x-M,u=x-y,Q[0]=x-(y+u)+(u-M),I=S+y,u=I-S,m=S-(I-u)+(y-u),y=m-b,u=m-y,Q[1]=m-(y+u)+(u-b),E=I+y,u=E-I,Q[2]=I-(E-u)+(y-u),Q[3]=E;const D=j(P,Z,4,Q,$);S=o*c,d=X*o,p=d-(d-o),f=o-p,d=X*c,g=d-(d-c),v=c-g,x=f*v-(S-p*g-f*g-p*v),b=h*l,d=X*h,p=d-(d-h),f=h-p,d=X*l,g=d-(d-l),v=l-g,M=f*v-(b-p*g-f*g-p*v),y=x-M,u=x-y,Q[0]=x-(y+u)+(u-M),I=S+y,u=I-S,m=S-(I-u)+(y-u),y=m-b,u=m-y,Q[1]=m-(y+u)+(u-b),E=I+y,u=E-I,Q[2]=I-(E-u)+(y-u),Q[3]=E;const L=j(D,$,4,Q,J);return J[L-1]}(t,e,i,n,r,s,l)}function et(t,e,i){const n=tt(t[0],t[1],e[0],e[1],i[0],i[1]);return n>0?-1:n<0?1:0}function it(t,e){const i=t.point,n=e.point;return i[0]>n[0]?1:i[0]<n[0]?-1:i[1]!==n[1]?i[1]>n[1]?1:-1:function(t,e,i,n){if(t.left!==e.left)return t.left?1:-1;if(0!==et(i,t.otherEvent.point,e.otherEvent.point))return t.isBelow(e.otherEvent.point)?-1:1;return!t.isSubject&&e.isSubject?1:-1}(t,e,i)}function nt(t,e,i){const n=new N(e,!1,t,t.isSubject),r=new N(e,!0,t.otherEvent,t.isSubject);return B(t.point,t.otherEvent.point)&&console.warn("what is that, a collapsed segment?",t),n.contourId=r.contourId=t.contourId,it(r,t.otherEvent)>0&&(t.otherEvent.left=!0,r.left=!1),t.otherEvent.otherEvent=r,t.otherEvent=n,i.push(r),i.push(n),i}function rt(t,e){return t[0]*e[1]-t[1]*e[0]}function st(t,e){return t[0]*e[0]+t[1]*e[1]}function at(t,e,i){const n=function(t,e,i,n,r){const s=[e[0]-t[0],e[1]-t[1]],a=[n[0]-i[0],n[1]-i[1]];function o(t,e,i){return[t[0]+e*i[0],t[1]+e*i[1]]}const h=[i[0]-t[0],i[1]-t[1]];let l=rt(s,a),c=l*l;const u=st(s,s);if(c>0){const e=rt(h,a)/l;if(e<0||e>1)return null;const n=rt(h,s)/l;return n<0||n>1?null:0===e||1===e?r?null:[o(t,e,s)]:0===n||1===n?r?null:[o(i,n,a)]:[o(t,e,s)]}if(l=rt(h,s),c=l*l,c>0)return null;const d=st(s,h)/u,p=d+st(s,a)/u,f=Math.min(d,p),g=Math.max(d,p);return f<=1&&g>=0?1===f?r?null:[o(t,f>0?f:0,s)]:0===g?r?null:[o(t,g<1?g:1,s)]:r&&0===f&&1===g?null:[o(t,f>0?f:0,s),o(t,g<1?g:1,s)]:null}(t.point,t.otherEvent.point,e.point,e.otherEvent.point),r=n?n.length:0;if(0===r)return 0;if(1===r&&(B(t.point,e.point)||B(t.otherEvent.point,e.otherEvent.point)))return 0;if(2===r&&t.isSubject===e.isSubject)return 0;if(1===r)return B(t.point,n[0])||B(t.otherEvent.point,n[0])||nt(t,n[0],i),B(e.point,n[0])||B(e.otherEvent.point,n[0])||nt(e,n[0],i),1;const s=[];let a=!1,o=!1;return B(t.point,e.point)?a=!0:1===it(t,e)?s.push(e,t):s.push(t,e),B(t.otherEvent.point,e.otherEvent.point)?o=!0:1===it(t.otherEvent,e.otherEvent)?s.push(e.otherEvent,t.otherEvent):s.push(t.otherEvent,e.otherEvent),a&&o||a?(e.type=G,t.type=e.inOut===t.inOut?P:D,a&&!o&&nt(s[1].otherEvent,s[0].point,i),2):o?(nt(s[0],s[1].point,i),3):s[0]!==s[3].otherEvent?(nt(s[0],s[1].point,i),nt(s[1],s[2].point,i),3):(nt(s[0],s[1].point,i),nt(s[3].otherEvent,s[2].point,i),3)}function ot(t,e){if(t===e)return 0;if(0!==et(t.point,t.otherEvent.point,e.point)||0!==et(t.point,t.otherEvent.point,e.otherEvent.point))return B(t.point,e.point)?t.isBelow(e.otherEvent.point)?-1:1:t.point[0]===e.point[0]?t.point[1]<e.point[1]?-1:1:1===it(t,e)?e.isAbove(t.point)?-1:1:t.isBelow(e.point)?-1:1;if(t.isSubject!==e.isSubject)return t.isSubject?-1:1;{let i=t.point,n=e.point;if(i[0]===n[0]&&i[1]===n[1])return i=t.otherEvent.point,n=e.otherEvent.point,i[0]===n[0]&&i[1]===n[1]?0:t.contourId>e.contourId?1:-1}return 1===it(t,e)?1:-1}class ht{constructor(){this.points=[],this.holeIds=[],this.holeOf=null,this.depth=null}isExterior(){return null==this.holeOf}}function lt(t,e,i,n){let r,s=t+1,a=e[t].point;const o=e.length;for(s<o&&(r=e[s].point);s<o&&r[0]===a[0]&&r[1]===a[1];){if(!i[s])return s;s++,s<o&&(r=e[s].point)}for(s=t-1;i[s]&&s>n;)s--;return s}function ct(t,e,i){const n=new ht;if(null!=t.prevInResult){const r=t.prevInResult,s=r.outputContourId;if(r.resultTransition>0){const t=e[s];if(null!=t.holeOf){const r=t.holeOf;e[r].holeIds.push(i),n.holeOf=r,n.depth=e[s].depth}else e[s].holeIds.push(i),n.holeOf=s,n.depth=e[s].depth+1}else n.holeOf=null,n.depth=e[s].depth}else n.holeOf=null,n.depth=0;return n}function ut(t){let e,i;const n=function(t){let e,i,n,r;const s=[];for(i=0,n=t.length;i<n;i++)e=t[i],(e.left&&e.inResult||!e.left&&e.otherEvent.inResult)&&s.push(e);let a=!1;for(;!a;)for(a=!0,i=0,n=s.length;i<n;i++)i+1<n&&1===it(s[i],s[i+1])&&(r=s[i],s[i]=s[i+1],s[i+1]=r,a=!1);for(i=0,n=s.length;i<n;i++)e=s[i],e.otherPos=i;for(i=0,n=s.length;i<n;i++)e=s[i],e.left||(r=e.otherPos,e.otherPos=e.otherEvent.otherPos,e.otherEvent.otherPos=r);return s}(t),r={},s=[];for(e=0,i=n.length;e<i;e++){if(r[e])continue;const t=s.length,i=ct(n[e],s,t),a=e=>{r[e]=!0,e<n.length&&n[e]&&(n[e].outputContourId=t)};let o=e,h=e;const l=n[e].point;for(i.points.push(l);a(o),o=n[o].otherPos,a(o),i.points.push(n[o].point),o=lt(o,n,r,h),!(o==h||o>=n.length)&&n[o];);s.push(i)}return s}function dt(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var pt,ft={exports:{}};var gt=dt(function(){if(pt)return ft.exports;function t(i,n){if(!(this instanceof t))return new t(i,n);if(this.data=i||[],this.length=this.data.length,this.compare=n||e,this.length>0)for(var r=(this.length>>1)-1;r>=0;r--)this._down(r)}function e(t,e){return t<e?-1:t>e?1:0}return pt=1,ft.exports=t,ft.exports.default=t,t.prototype={push:function(t){this.data.push(t),this.length++,this._up(this.length-1)},pop:function(){if(0!==this.length){var t=this.data[0];return this.length--,this.length>0&&(this.data[0]=this.data[this.length],this._down(0)),this.data.pop(),t}},peek:function(){return this.data[0]},_up:function(t){for(var e=this.data,i=this.compare,n=e[t];t>0;){var r=t-1>>1,s=e[r];if(i(n,s)>=0)break;e[t]=s,t=r}e[t]=n},_down:function(t){for(var e=this.data,i=this.compare,n=this.length>>1,r=e[t];t<n;){var s=1+(t<<1),a=s+1,o=e[s];if(a<this.length&&i(e[a],o)<0&&(s=a,o=e[a]),i(o,r)>=0)break;e[t]=o,t=s}e[t]=r}},ft.exports}());const vt=Math.max,yt=Math.min;let It=0;function mt(t,e,i,n,r,s){let a,o,h,l,c,u;for(a=0,o=t.length-1;a<o;a++){if(h=t[a],l=t[a+1],c=new N(h,!1,void 0,e),u=new N(l,!1,c,e),c.otherEvent=u,h[0]===l[0]&&h[1]===l[1])continue;c.contourId=u.contourId=i,s||(c.isExteriorRing=!1,u.isExteriorRing=!1),it(c,u)>0?u.left=!0:c.left=!0;const o=h[0],d=h[1];r[0]=yt(r[0],o),r[1]=yt(r[1],d),r[2]=vt(r[2],o),r[3]=vt(r[3],d),n.push(c),n.push(u)}}const St=[];function xt(t,e,i){"number"==typeof t[0][0][0]&&(t=[t]),"number"==typeof e[0][0][0]&&(e=[e]);let n=function(t,e,i){let n=null;return t.length*e.length==0&&(i===L?n=St:i===A?n=t:i!==R&&i!==C||(n=0===t.length?e:t)),n}(t,e,i);if(n)return n===St?null:n;const r=[1/0,1/0,-1/0,-1/0],s=[1/0,1/0,-1/0,-1/0],a=function(t,e,i,n,r){const s=new gt(null,it);let a,o,h,l,c,u;for(h=0,l=t.length;h<l;h++)for(a=t[h],c=0,u=a.length;c<u;c++)o=0===c,o&&It++,mt(a[c],!0,It,s,i,o);for(h=0,l=e.length;h<l;h++)for(a=e[h],c=0,u=a.length;c<u;c++)o=0===c,r===A&&(o=!1),o&&It++,mt(a[c],!1,It,s,n,o);return s}(t,e,r,s,i);if(n=function(t,e,i,n,r){let s=null;return(i[0]>n[2]||n[0]>i[2]||i[1]>n[3]||n[1]>i[3])&&(r===L?s=St:r===A?s=t:r!==R&&r!==C||(s=t.concat(e))),s}(t,e,r,s,i),n)return n===St?null:n;const o=function(t,e,i,n,r,s){const a=new w(ot),o=[],h=Math.min(n[2],r[2]);let l,c,u;for(;0!==t.length;){let e=t.pop();if(o.push(e),s===L&&e.point[0]>h||s===A&&e.point[0]>n[2])break;if(e.left){c=l=a.insert(e),u=a.minNode(),l=l!==u?a.prev(l):null,c=a.next(c);const i=l?l.key:null;let n;if(O(e,i,s),c&&2===at(e,c.key,t)&&(O(e,i,s),O(c.key,e,s)),l&&2===at(l.key,e,t)){let t=l;t=t!==u?a.prev(t):null,n=t?t.key:null,O(i,n,s),O(e,i,s)}}else e=e.otherEvent,c=l=a.find(e),l&&c&&(l=l!==u?a.prev(l):null,c=a.next(c),a.remove(e),c&&l&&at(l.key,c.key,t))}return o}(a,0,0,r,s,i),h=ut(o),l=[];for(let t=0;t<h.length;t++){let e=h[t];if(e.isExterior()){let t=[e.points];for(let i=0;i<e.holeIds.length;i++){let n=e.holeIds[i];t.push(h[n].points)}l.push(t)}}return l}function bt(t,e){return xt(t,e,R)}function Mt(t,e){return xt(t,e,A)}var Et;!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.Mask=8]="Mask",t[t.Pencil=9]="Pencil",t[t.BrushMask=10]="BrushMask"}(Et||(Et={}));var kt=function(t){function d(e,i){var n=t.call(this)||this;n.version=E,n.lock=!1,n.readonly=!1,n.MIN_WIDTH=10,n.MIN_HEIGHT=10,n.MIN_RADIUS=5,n.MIN_POINTNUM=3,n.MIN_LENGTH=140,n.strokeStyle="#000",n.fillStyle="rgba(0, 0, 255, 0.1)",n.lineWidth=2,n.activeStrokeStyle="#000",n.activeFillStyle="#000",n.ctrlStrokeStyle="#000",n.ctrlFillStyle="#fff",n.ctrlRadius=3,n.hideLabel=!1,n.labelFillStyle="rgba(255, 255, 255, 0.5)",n.labelFontFamily="sans-serif",n.labelFontSize=18,n.textFillStyle="#FFFFFF",n.labelMaxLen=10,n.WIDTH=0,n.HEIGHT=0,n.imagesrc="",n.imagealpha=1,n.olddataset=[],n.dataset=[],n.MAX_LENGTH=10,n.doneList=[],n.undoList=[],n.hideList=[],n.remmberOrigin=[0,0],n.createType=Et.None,n.ctrlIndex=-1,n.clickIndex=-1,n.image=new Image,n.IMAGE_WIDTH=0,n.IMAGE_ORIGIN_HEIGHT=0,n.IMAGE_HEIGHT=0,n.originX=0,n.originY=0,n.scaleStep=0,n.textscaleStep=0,n.scrollZoom=!0,n.dblTouch=300,n.dblTouchStore=0,n.alpha=!0,n.focusMode=!1,n.scaleTouchStore=0,n.isTouch2=!1,n.isMobile=navigator.userAgent.includes("Mobile"),n.labelUp=!1,n.isCtrlKey=!1,n.ctrlCode="ControlLeft",n.gridMenuEnable=!0,n.gridSelectedFillStyle="rgba(255, 255, 0, 0.8)",n.ispainting=!1,n.brushlineWidth=1,n.brushstrokeStyle="rgba(255, 0, 0, 0.8)",n.pencillineWidth=.5,n.pencilstrokeStyle="rgba(255, 0, 0, 0.8)",n.mask_alpha=96,n.densityFactor=1,n.activeCanvasData=null,n.activePolygon="",n.isEraser=!1,n.isErasing=!1,n.eraserPoints=[],n.tempBrushPoints=[],n.eraserSize=8,n.random_color=[{r:255,g:0,b:0},{r:0,g:255,b:0},{r:0,b:255,g:0}],n.isMagicToolActive=!1,n.magicPoints=[],n.maxLinePointCount=2,n.closePolygon=function(t){var e=t.filter((function(t){return-1!==t[0]||-1!==t[1]}));return e.length>0&&e.push(e[0]),[e]},n.getImagedataFromImageClass=function(t,e){var i=document.createElement("canvas"),r=i.getContext("2d",{willReadFrequently:!0});if(!i||!r)return console.error("Canvas or context is not initialized"),null;i.width=n.WIDTH,i.height=n.HEIGHT;var s=document.createElement("canvas"),a=s.getContext("2d",{willReadFrequently:!0});if(!a)return console.error("Temporary canvas context is not initialized"),null;s.width=n.WIDTH,s.height=n.HEIGHT,a.drawImage(t,0,0);var o=null==a?void 0:a.getImageData(0,0,s.width,s.height),h=null==o?void 0:o.data;if(!h)return console.error("Failed to retrieve pixel data"),null;var l=r.getImageData(0,0,n.WIDTH,n.HEIGHT),c=l.data;if("everything"===e){for(var u=0;u<h.length;u+=4)if(h[u]>0){var d=h[u]%n.random_color.length;c[u]=n.random_color[d].r,c[u+1]=n.random_color[d].g,c[u+2]=n.random_color[d].b,c[u+3]=n.mask_alpha}r.putImageData(l,0,0)}else if("rect"===e);else{if("magic"!==e)return console.error("Unknown mask type"),null;var p=[];for(u=0;u<h.length;u+=4)255==h[u]&&255==h[u+1]&&255==h[u+2]&&p.push(u)}var f=document.createElement("canvas"),g=f.getContext("2d",{willReadFrequently:!0});return g?(f.width=n.IMAGE_WIDTH,f.height=n.IMAGE_HEIGHT,g.drawImage(t,0,0,n.IMAGE_WIDTH,n.IMAGE_HEIGHT),g.getImageData(0,0,n.IMAGE_WIDTH,n.IMAGE_HEIGHT).data):(console.error("Scaled canvas context is not initialized"),null)},n.drawPromptPointOnClick=function(t,e){var i=t.coor[0]*n.scale,r=t.coor[1]*n.scale,s=t.color,a=e.getContext("2d",{willReadFrequently:!0});a&&(a.beginPath(),a.arc(i,r,3,0,2*Math.PI),a.fillStyle="rgba(255, 255, 255, 0.75)",a.fill(),a.strokeStyle=s,a.stroke())},n.handleLoad=n.handleLoad.bind(n),n.handleContextmenu=n.handleContextmenu.bind(n),n.handleMousewheel=n.handleMousewheel.bind(n),n.handleMouseDown=n.handleMouseDown.bind(n),n.handleMouseMove=n.handleMouseMove.bind(n),n.handleMouseUp=n.handleMouseUp.bind(n),n.handleDblclick=n.handleDblclick.bind(n),n.handleKeyup=n.handleKeyup.bind(n),n.handleKeydown=n.handleKeydown.bind(n);var r="string"==typeof e?document.querySelector(e):e;if(r instanceof HTMLCanvasElement){n.canvas=r,n.offScreen=document.createElement("canvas"),n.imagesrc=i,n.initSetting(),n.initEvents(),i&&n.setImage(i);for(var s=1;s<=255;s++){var a=Math.floor(256*Math.random()),o=Math.floor(256*Math.random()),h=Math.floor(256*Math.random());n.random_color[s]={r:a,g:o,b:h}}}else console.warn("HTMLCanvasElement is required!");return n}return e(d,t),Object.defineProperty(d.prototype,"activeShape",{get:function(){return this.dataset.find((function(t){return t.active}))||{}},enumerable:!1,configurable:!0}),Object.defineProperty(d.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(d.prototype,"imageMin",{get:function(){return Math.min(this.IMAGE_WIDTH,this.IMAGE_HEIGHT)},enumerable:!1,configurable:!0}),Object.defineProperty(d.prototype,"imageOriginMax",{get:function(){return Math.max(this.IMAGE_ORIGIN_WIDTH,this.IMAGE_ORIGIN_HEIGHT)},enumerable:!1,configurable:!0}),d.prototype.mergeEvent=function(t){var e=0,n=0,r=0,s=0;if(this.isMobile){var a=t.touches[0],o=a.clientX,h=a.clientY,l=t.target.getBoundingClientRect(),c=l.left,u=l.top;if(e=Math.round(o-c),n=Math.round(h-u),2===t.touches.length){var d=t.touches[1]||{},p=d.clientX,f=void 0===p?0:p,g=d.clientY,v=void 0===g?0:g;r=Math.round(Math.abs((f-o)/2+o)-c),s=Math.round(Math.abs((v-h)/2+h)-u)}}else e=t.offsetX,n=t.offsetY;return i(i({},t),{mouseX:e,mouseY:n,mouseCX:r,mouseCY:s})},d.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()},d.prototype.handleContextmenu=function(t){t.preventDefault(),this.evt=t,this.lock},d.prototype.handleMousewheel=function(t){if(t.stopPropagation(),this.evt=t,!this.lock&&this.scrollZoom){var e=this.mergeEvent(t),i=e.mouseX,n=e.mouseY;this.mouse=[i,n],t.deltaY>0&&this.imageMin<this.MIN_LENGTH||t.deltaY<0&&this.IMAGE_WIDTH>10*this.imageOriginMax||(t.deltaY<0?this.textscaleStep++:this.textscaleStep--,this.setScale(t.deltaY<0,!0))}},d.prototype.handleMouseDown=function(t){var e=this;if(t.stopPropagation(),this.evt=t,!this.lock){var i=this.mergeEvent(t),n=i.mouseX,r=i.mouseY,s=i.mouseCX,o=i.mouseCY,h=Math.round(n/this.scale),l=Math.round(r/this.scale);if(this.mouse=this.isMobile&&2===t.touches.length?[s,o]:[n,r],this.remmberOrigin=[n-this.originX,r-this.originY],this.olddataset=c(this.dataset),!this.isMobile&&1===t.buttons||this.isMobile&&1===t.touches.length){var u=this.activeShape.ctrlsData||[];if(this.ctrlIndex=u.findIndex((function(t){return e.isPointInCircle(e.mouse,t,e.ctrlRadius)})),this.clickIndex=this.ctrlIndex,this.ctrlIndex>-1&&!this.readonly){console.log("this.ctrlIndex",this.ctrlIndex);var d=a(u[this.ctrlIndex],2),v=d[0],x=d[1];this.activeShape.type===Et.Polygon&&this.activeShape.coor.length>2&&0===this.ctrlIndex?this.handleDblclick(t):this.update(),this.remmber=[[h-v,l-x]]}else if(this.isInBackground(t)){if(this.activeShape.creating&&!this.readonly){if([Et.Polygon,Et.Line].includes(this.activeShape.type)){var M=a(this.activeShape.coor[this.activeShape.coor.length-1],2),E=M[0],k=M[1];if(E!==h&&k!==l){var w=Math.round(h-this.originX/this.scale),T=Math.round(l-this.originY/this.scale);this.activeShape.coor.push([w,T])}}}else if(this.createType===Et.None||this.readonly||this.isCtrlKey){var H=a(this.hitOnShape(this.mouse),2),_=H[0],G=H[1];if(_>-1&&!G.locking&&!this.readonly){if(G.type===Et.Dot&&"color"in G&&""!==G.color)return;if(G.type===Et.Brush)return void("iseraser"in G&&!G.iseraser&&(this.dataset.forEach((function(t,e){return t.active=e===_})),0===this.activeShape.boundingRect.length&&(this.activeShape.boundingRect=this.removeDuplicatePoints(G.coor,!0,!1).resultRect),this.emit("select",G)));if(G.type===Et.Pencil)return this.dataset.forEach((function(t,e){return t.active=e===_})),this.emit("select",G),void this.update();if(G.dragging=!0,this.dataset.forEach((function(t,e){return t.active=e===_})),this.dataset.splice(_,1,G),!this.readonly)if(this.remmber=[],[Et.Dot,Et.Circle].includes(G.type)){var P=a(G.coor,2);E=P[0],k=P[1];this.remmber=[[h-E,l-k]]}else G.coor.forEach((function(t){e.remmber.push([h-t[0],l-t[1]])}));this.emit("select",G)}else this.activeShape.active=!1,this.dataset.sort((function(t,e){return t.index-e.index})),this.emit("select",null)}else{var D=void 0,L=[w=Math.round(h-this.originX/this.scale),T=Math.round(l-this.originY/this.scale)];switch(this.createType){case Et.Rect:(D=new p({coor:[L,L]},this.dataset.length)).creating=!0;break;case Et.Polygon:(D=new f({coor:[L]},this.dataset.length)).creating=!0;break;case Et.Dot:D=new g({coor:L},this.dataset.length),this.emit("add",D);break;case Et.Line:(D=new y({coor:[L]},this.dataset.length)).creating=!0;break;case Et.Circle:(D=new I({coor:L},this.dataset.length)).creating=!0;break;case Et.Grid:(D=new m({coor:[L,L]},this.dataset.length)).creating=!0;break;case Et.Brush:(D=new S({coor:[L]},this.dataset.length)).creating=!0,D.lineWidth=this.brushlineWidth,D.strokeStyle=this.brushstrokeStyle,this.ispainting=!0,this.isEraser&&(D.iseraser=!0);break;case Et.Pencil:(D=new b({coor:[L]},this.dataset.length)).creating=!0,D.lineWidth=this.pencillineWidth,D.strokeStyle=this.pencilstrokeStyle,this.ispainting=!0}this.dataset.forEach((function(t){t.active=!1})),D.active=!0,this.dataset.push(D)}this.update()}}else if(!this.isMobile&&2===t.buttons||this.isMobile&&3===t.touches.length&&!this.readonly){if([Et.Grid].includes(this.activeShape.type)&&this.gridMenuEnable){var R=prompt("x 行 y 列 x,y",[this.activeShape.row,this.activeShape.col].join(","));if("string"==typeof R){var A=a(R.split(","),2),C=A[0],O=A[1];/^[1-9]\d*$/.test(C)&&/^[1-9]\d*$/.test(O)&&(this.activeShape.row=Number(C),this.activeShape.col=Number(O),this.update())}}this.emit("contextmenu",t)}}},d.prototype.handleMouseMove=function(t){if(t.stopPropagation(),this.evt=t,!this.lock){var e=this.mergeEvent(t),i=e.mouseX,n=e.mouseY,r=e.mouseCX,s=e.mouseCY,o=Math.round(i/this.scale),h=Math.round(n/this.scale);if(this.mouse=this.isMobile&&2===t.touches.length?[r,s]:[i,n],(!this.isMobile&&1===t.buttons||this.isMobile&&1===t.touches.length)&&this.activeShape.type){if(this.ctrlIndex>-1&&this.remmber.length&&(this.isInBackground(t)||this.activeShape.type===Et.Circle)){var l=a(this.remmber,1),c=a(l[0],2),u=c[0],d=c[1];if([Et.Rect,Et.Grid].includes(this.activeShape.type)){var p=a(this.activeShape.coor,2),f=a(p[0],2),g=f[0],v=f[1],y=a(p[1],2),I=y[0],m=y[1],S=[];switch(this.ctrlIndex){case 0:S=[[o-u,h-d],[I,m]];break;case 1:S=[[g,h-d],[I,m]];break;case 2:S=[[g,h-d],[o-u,m]];break;case 3:S=[[g,v],[o-u,m]];break;case 4:S=[[g,v],[o-u,h-d]];break;case 5:S=[[g,v],[I,h-d]];break;case 6:S=[[o-u,v],[I,h-d]];break;case 7:S=[[o-u,v],[I,m]]}var x=a(S,2),b=a(x[0],2),M=b[0],E=b[1],k=a(x[1],2),w=k[0],T=k[1];(M<0||w<0||E<0||T<0||w>this.IMAGE_ORIGIN_WIDTH||T>this.IMAGE_ORIGIN_HEIGHT)&&(M<0&&(M=0),w<0&&(w=0),E<0&&(E=0),T<0&&(T=0),w>this.IMAGE_ORIGIN_WIDTH&&(w=this.IMAGE_ORIGIN_WIDTH),T>this.IMAGE_ORIGIN_HEIGHT&&(T=this.IMAGE_ORIGIN_HEIGHT)),w-M>=this.MIN_WIDTH&&T-E>=this.MIN_HEIGHT?this.activeShape.coor=[[M,E],[w,T]]:this.emit("warn","Width cannot be less than ".concat(this.MIN_WIDTH,",Height cannot be less than").concat(this.MIN_HEIGHT,"。"))}else if([Et.Polygon,Et.Line].includes(this.activeShape.type)){var H=[Math.round(o-this.originX/this.scale),Math.round(h-this.originY/this.scale)];this.activeShape.coor.splice(this.ctrlIndex,1,H)}else if(this.activeShape.type===Et.Circle){var _=Math.round(o-this.originX/this.scale)-this.activeShape.coor[0];_>=this.MIN_RADIUS&&(this.activeShape.radius=_)}}else if(this.activeShape.dragging&&!this.readonly){S=[];var G=!0,P=this.IMAGE_ORIGIN_WIDTH||this.WIDTH,D=this.IMAGE_ORIGIN_HEIGHT||this.HEIGHT;if([Et.Dot,Et.Circle].includes(this.activeShape.type)){var L=a(this.remmber[0],2),R=L[0];d=h-L[1];((u=o-R)<0||u>P||d<0||d>D)&&(G=!1),S=[u,d]}else for(var A=0;A<this.activeShape.coor.length;A++){var C=this.remmber[A];u=o-C[0],d=h-C[1];(u<0||u>P||d<0||d>D)&&(G=!1),S.push([u,d])}G&&(this.activeShape.coor=S)}else if(this.activeShape.creating&&this.isInBackground(t)){u=Math.round(o-this.originX/this.scale),d=Math.round(h-this.originY/this.scale);if([Et.Rect,Et.Grid].includes(this.activeShape.type))this.activeShape.coor.splice(1,1,[u,d]);else if(this.activeShape.type===Et.Circle){var O=a(this.activeShape.coor,2),W=(g=O[0],v=O[1],Math.sqrt(Math.pow(g-u,2)+Math.pow(v-d,2)));this.activeShape.radius=W}else if(this.ispainting&&this.createType===Et.Brush){H=[Math.round(o-this.originX/this.scale),Math.round(h-this.originY/this.scale)];this.activeShape.coor.push(H)}else if(this.ispainting&&this.createType===Et.Pencil){H=[Math.round(o-this.originX/this.scale),Math.round(h-this.originY/this.scale)];this.activeShape.coor.push(H)}}this.update()}else if([Et.Polygon,Et.Line,Et.Brush,Et.Pencil].includes(this.activeShape.type)&&this.activeShape.creating)this.update();else if(!this.isMobile&&2===t.buttons&&3===t.which||this.isMobile&&1===t.touches.length&&!this.isTouch2)this.originX=Math.round(i-this.remmberOrigin[0]),this.originY=Math.round(n-this.remmberOrigin[1]),this.emit("dragimg"),this.update();else if(this.isMobile&&2===t.touches.length){this.isTouch2=!0;var N=t.touches[0],B=t.touches[1],Y=this.scaleTouchStore;this.scaleTouchStore=Math.abs((B.clientX-N.clientX)*(B.clientY-N.clientY)),this.setScale(this.scaleTouchStore>Y,!0)}}},d.prototype.handleMouseUp=function(t){var e;if(console.log("handleMouseUp"),t.stopPropagation(),this.evt=t,!this.lock){if(this.isMobile){if(0===t.touches.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!==Et.None&&!this.isCtrlKey){if(this.activeShape.dragging=!1,this.activeShape.creating){if([Et.Rect,Et.Grid].includes(this.activeShape.type)){var i=a(this.activeShape.coor,2),n=a(i[0],2),r=n[0],s=n[1],o=a(i[1],2),h=o[0],l=o[1];Math.abs(r-h)<this.MIN_WIDTH||Math.abs(s-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(r,h),Math.min(s,l)],[Math.max(r,h),Math.max(s,l)]],this.activeShape.creating=!1,this.activeShape.truncated=0,this.emit("add",this.activeShape))}else if(this.activeShape.type===Et.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));else if(this.createType===Et.Brush)if(this.activeShape.coor.length<this.MIN_POINTNUM)this.dataset.pop(),this.emit("warn","Path points cannot be less than ".concat(this.MIN_POINTNUM));else{this.ispainting=!1,this.activeShape.creating=!1;var d=this.removeDuplicatePoints(this.activeShape.coor,!0),p=d.resultCoor,f=d.resultRect;this.activeShape.coor=p,this.activeShape.boundingRect=f,this.emit("add",this.activeShape)}else if(this.createType===Et.Pencil)if(this.activeShape.coor.length<this.MIN_POINTNUM)this.dataset.pop(),this.emit("warn","Path points cannot be less than ".concat(this.MIN_POINTNUM));else{this.ispainting=!1,this.activeShape.creating=!1;for(var g=this.dataset.length-1;g>=0;g--){var v=this.dataset[g];if(v.type===Et.Pencil&&v.uuid!==this.activeShape.uuid){var y=v,I=this.closePolygon(y.coor),m=this.closePolygon(this.activeShape.coor),S=xt(I,m,L);if(S&&S.length>0)if(v.strokeStyle===this.activeShape.strokeStyle){var x=bt(I,m);(null===(e=null==x?void 0:x[0])||void 0===e?void 0:e[0])&&(this.activeShape.coor=x[0][0],m=this.closePolygon(this.activeShape.coor)),this.dataset.splice(g,1)}else{var b=Mt(m,I);if(!b||0===b.length)continue;var M=Mt(I,m);M&&0!==M.length?(y.coor=M[0][0],y.coor[y.coor.length-1]=[-1,-1]):this.dataset.splice(g,1)}}}this.activeShape.coor.push([-1,-1]);var E=this.removeDuplicatePoints(this.activeShape.coor,!1);p=E.resultCoor,f=E.resultRect;this.activeShape.coor=p,this.emit("add",this.activeShape)}else if(this.createType===Et.Line){if(this.activeShape.coor.length===this.maxLinePointCount)this.activeShape.type===Et.Line&&this.activeShape.coor.length>1&&(this.emit("add",this.activeShape),this.activeShape.creating=!1)}this.update()}u(this.olddataset,this.dataset,["coor","label","labelUp","lineWidth","strokeStyle","textFillStyle","uuid","remark","length"])||(this.manageDoneList(c(this.dataset)),console.log("this.doneList",this.doneList))}}},d.prototype.handleDblclick=function(t){var e=this;if(t.stopPropagation(),this.evt=t,!this.lock)if([Et.Polygon,Et.Line].includes(this.activeShape.type)){var i=this.activeShape.type===Et.Polygon&&this.activeShape.coor.length>2,n=this.activeShape.type===Et.Line&&this.activeShape.coor.length>1;(i||n)&&(this.emit("add",this.activeShape),this.activeShape.creating=!1,this.update())}else[Et.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())},d.prototype.handleKeydown=function(t){t.code===this.ctrlCode&&(this.isCtrlKey=!0)},d.prototype.handleKeyup=function(t){t.code===this.ctrlCode&&(this.isCtrlKey=!1),this.evt=t,!this.isCtrlKey||"v"!==t.key||this.readonly?this.lock||document.activeElement!==document.body||this.readonly||this.activeShape.type&&([Et.Polygon,Et.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&&"Delete"!==t.key||this.deleteByIndex(this.activeShape.index)):this.copyByIndex(this.activeShape.index)},d.prototype.initSetting=function(){var t=window.devicePixelRatio||1;this.canvas.style.userSelect="none",this.ctx=this.ctx||this.canvas.getContext("2d",{alpha:this.alpha,willReadFrequently:!0}),this.WIDTH=Math.round(this.canvas.clientWidth),this.HEIGHT=Math.round(this.canvas.clientHeight),this.canvas.width=this.WIDTH*t,this.canvas.height=this.HEIGHT*t,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}),this.ctx.scale(t,t)},d.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.removeEventListener("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))},d.prototype.getscaledPoint=function(t){var e=this.mergeEvent(t),i=e.mouseX,n=e.mouseY,r=Math.round(i/this.scale),s=Math.round(n/this.scale);return[Math.round(r-this.originX/this.scale),Math.round(s-this.originY/this.scale)]},d.prototype.setImage=function(t,e){void 0===e&&(e=1),this.image.crossOrigin="Anonymous",this.image.src=t,this.imagealpha=e,this.scaleStep=0,this.textscaleStep=0},d.prototype.handleMaskShape=function(t,e){return n(this,void 0,void 0,(function(){var i,n,s,a=this;return r(this,(function(r){return i=new x(t,e),n=i.maskBase64,(s=new Image).crossOrigin="Anonymous",s.src="data:image/png;base64,".concat(n),[2,new Promise((function(n,r){s.onload=function(){var o=[],h=a.getImagedataFromImageClass(s,"magic");if(h){for(var l=0;l<h.length;l+=4)255===h[l]&&255===h[l+1]&&255===h[l+2]&&o.push(l);if(i.pixels=o,i.height=a.IMAGE_HEIGHT,i.weight=a.IMAGE_WIDTH,i.fillStyle=t.fillStyle,i.strokeStyle=t.strokeStyle,"maskToPolygon"in t&&t.maskToPolygon&&"click"===i.maskType){a.activeCanvasData=a.putDataOnCanvas(a.canvas,o,i.fillStyle,!1);var c=new f({coor:a.getContourPointsOfColoredRegion(a.activeCanvasData,.5)},e);c.tagId=t.tagId,c.label=t.label,c.strokeStyle=t.strokeStyle,a.activePolygon=c.uuid,n(c)}else i.canvasData=a.putDataOnCanvas(a.canvas,o,i.fillStyle,!0),i.tagId=t.tagId,i.label=t.label,n(i);a.magicPoints=i.magicPoints}else console.error("Failed to get pixel data from mask image"),r(null)},s.onerror=function(t){console.error("Error loading mask image",t),r(null)}}))]}))}))},d.prototype.setData=function(t,e,i,a){var o=this;return void 0===e&&(e=!0),void 0===i&&(i=!1),void 0===a&&(a=!1),new Promise((function(h){setTimeout((function(){return n(o,void 0,void 0,(function(){var n,o,l,u,d,v,x,E,k,w;return r(this,(function(r){switch(r.label){case 0:if(!e)return[3,22];n=[],o=new Map,t.forEach((function(t,e){o.set(t,e)})),r.label=1;case 1:r.trys.push([1,19,20,21]),l=s(t),u=l.next(),r.label=2;case 2:if(u.done)return[3,18];if(d=u.value,!Object.prototype.toString.call(d).includes("Object"))return[3,16];switch(v=void 0,x=o.get(d),d.type){case Et.Rect:return[3,3];case Et.Polygon:return[3,4];case Et.Dot:return[3,5];case Et.Line:return[3,6];case Et.Circle:return[3,7];case Et.Grid:return[3,8];case Et.Brush:return[3,9];case Et.BrushMask:return[3,10];case Et.Mask:return[3,11];case Et.Pencil:return[3,13]}return[3,14];case 3:return v=new p(d,x),[3,15];case 4:return v=new f(d,x),[3,15];case 5:return v=new g(d,x),[3,15];case 6:return v=new y(d,x),[3,15];case 7:return v=new I(d,x),[3,15];case 8:return v=new m(d,x),[3,15];case 9:return v=new S(d,x),[3,15];case 10:return v=new M(d,x),[3,15];case 11:return[4,this.handleMaskShape(d,x)];case 12:return v=r.sent(),[3,15];case 13:return v=new b(d,x),[3,15];case 14:return console.warn("Invalid shape",d),[3,15];case 15:return[Et.Rect,Et.Polygon,Et.Dot,Et.Line,Et.Circle,Et.Grid,Et.Brush,Et.BrushMask,Et.Mask,Et.Pencil].includes(d.type)&&n.push(v),[3,17];case 16:console.warn("Shape must be an enumerable Object.",d),r.label=17;case 17:return u=l.next(),[3,2];case 18:return[3,21];case 19:return E=r.sent(),k={error:E},[3,21];case 20:try{u&&!u.done&&(w=l.return)&&w.call(l)}finally{if(k)throw k.error}return[7];case 21:return this.dataset=n,[3,23];case 22:this.dataset=t,r.label=23;case 23:return this.update(i,a),0===this.doneList.length&&void 0!==this.dataset&&this.manageDoneList(c(this.dataset)),h(),[2]}}))}))}),0)}))},d.prototype.hitOnShape=function(t){for(var e,i=-1,n=this.dataset.length-1;n>-1;n--){var r=this.dataset[n];if(this.isPointInBackground(t)&&(r.type===Et.Dot&&this.isPointInCircle(t,r.coor,this.ctrlRadius)||r.type===Et.Circle&&this.isPointInCircle(t,r.coor,r.radius*this.scale)||r.type===Et.Rect&&this.isPointInRect(t,r.coor)||r.type===Et.Polygon&&this.isPointInPolygon(t,r.coor)||r.type===Et.Line&&this.isPointInLine(t,r.coor)||r.type===Et.Grid&&this.isPointInRect(t,r.coor)||r.type===Et.Brush&&this.isPointInLine(t,r.coor)||r.type===Et.Pencil&&this.isPointInPolygon(t,r.coor)||r.type===Et.Mask&&this.isMouseInPixelsRegion(t,r.canvasData))){if(this.focusMode&&!r.active||r.hiddening)continue;i=n,e=r;break}}return[i,e]},d.prototype.hitOnShapeVertex=function(){var t=this,e=this.activeShape,i=this.activeShape.ctrlsData||[];return this.ctrlIndex=i.findIndex((function(e){return t.isPointInCircle(t.mouse,e,t.ctrlRadius)})),this.ctrlIndex>-1&&!this.readonly&&!e.hiddening?e.type===Et.Rect?0===this.ctrlIndex?"nw-resize":1===this.ctrlIndex?"ns-resize":2===this.ctrlIndex?"ne-resize":3===this.ctrlIndex?"ew-resize":4===this.ctrlIndex?"se-resize":5===this.ctrlIndex?"ns-resize":6===this.ctrlIndex?"sw-resize":"ew-resize":e.type===Et.Brush||e.type===Et.Pencil||e.type===Et.Polygon||e.type===Et.Line||e.type===Et.Circle?"pointer":"move":""},d.prototype.isInBackground=function(t){var e=this.mergeEvent(t),i=e.mouseX,n=e.mouseY;return i>=this.originX&&n>=this.originY&&i<=this.o