z-polygon-editor
Version:
一个基于MarkJs二次开发的Canvas标注库, 用于电子围栏多边形标注
3 lines (2 loc) • 23.3 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).ZMark=t()}(this,(function(){"use strict";function e(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function t(e,t){for(var i=0;i<t.length;i++){var n=t[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function i(e,i,n){return i&&t(e.prototype,i),n&&t(e,n),e}function n(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}var r=function(){function t(){e(this,t),this.observerListeners={},this.observerToken=0}return i(t,[{key:"publish",value:function(e){if(!e||!this.observerListeners[e])return!1;for(var t=this.observerListeners[e],i=arguments.length,n=new Array(i>1?i-1:0),r=1;r<i;r++)n[r-1]=arguments[r];for(var s=0;s<t.length;s++)t[s].context?t[s].fn.apply(t[s].context,n):t[s].fn.apply(t[s].fn,n)}},{key:"subscribe",value:function(e,t){var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"";return this.observerListeners[e]||(this.observerListeners[e]=[]),this.observerToken++,this.observerListeners[e].push({fn:t,context:i,token:this.observerToken}),this.observerToken}},{key:"unsubscribe",value:function(e){if(!e)return!1;for(var t in this.observerListeners)if(this.observerListeners.hasOwnProperty(t))for(var i=0;i<this.observerListeners[t].length;i++)this.observerListeners[t][i].token===e&&this.observerListeners[t].splice(i,1)}},{key:"clearTopic",value:function(e){if(!e)return!1;for(var t in this.observerListeners)this.observerListeners.hasOwnProperty(t)&&t===e&&delete this.observerListeners[t]}},{key:"clearAll",value:function(){this.observerListeners={},this.observerToken=0}}]),t}();function s(e,t,i,n){var r,s=[];if(e===i)for(var o=Math.abs(n-t),a=Math.min(t,n),h=0;h<=o;h++)s.push([e,a+h]);else{var c,l;l=0-1*t-(c=(t-n)/(i-e))*e;for(var u=Math.abs(i-e),v=Math.min(e,i),f=0;f<=u;f++)s.push([v+f,(r=v+f,(0-l-c*r)/1)])}return s}function o(e,t,i,n){return Math.sqrt(Math.pow(e-i,2)+Math.pow(t-n,2))}var a={checkLineSegmentCross:function(e,t,i,n){var r=!1,s=[t.x-e.x,t.y-e.y],o=[i.x-e.x,i.y-e.y],a=[n.x-e.x,n.y-e.y],h=s[0]*o[1]-s[1]*o[0],c=s[0]*a[1]-s[1]*a[0],l=[i.x-n.x,i.y-n.y],u=[e.x-n.x,e.y-n.y],v=[t.x-n.x,t.y-n.y];return h*c<0&&(l[0]*u[1]-l[1]*u[0])*(l[0]*v[1]-l[1]*v[0])<0&&(r=!0),r},loadImage:function(e){return new Promise((function(t,i){var n=new Image;n.onload=function(){t(n)},n.onerror=function(e){i(e)},n.src=e}))},getTwoPointDistance:o,getLinePointYByX:function(e,t,i,n,r){return e===i?Math.min(t,n):(o=0-1*t-(s=(t-n)/(i-e))*e,function(e){return(0-o-s*e)/1}(r));var s,o},getLineAllPoint:s,getLinePointDistance:function(e,t,i,n,r,s){return e===i?Math.abs(r-e):(a=0-1*t-(o=(t-n)/(i-e))*e,Math.abs((o*r+1*s+a)/Math.sqrt(o*o+1)));var o,a},getAngle:function(e,t,i,n){var r=180*Math.atan2(n-t,i-e)/Math.PI;return r>0?r-360:r},getNearestPointFromLine:function(e,t,i,n,r,a){var h=s(e,t,i,n),c=1/0,l=null;return h.forEach((function(e){var t=o(e[0],e[1],r,a);t<c&&(c=t,l=e)})),l},noop:function(e,t,i){}};function h(e,t){(null==t||t>e.length)&&(t=e.length);for(var i=0,n=new Array(t);i<t;i++)n[i]=e[i];return n}function c(e){return function(e){if(Array.isArray(e))return h(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||function(e,t){if(e){if("string"==typeof e)return h(e,t);var i=Object.prototype.toString.call(e).slice(8,-1);return"Object"===i&&e.constructor&&(i=e.constructor.name),"Map"===i||"Set"===i?Array.from(e):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?h(e,t):void 0}}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function l(e,t){var i=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),i.push.apply(i,n)}return i}function u(e){for(var t=1;t<arguments.length;t++){var i=null!=arguments[t]?arguments[t]:{};t%2?l(Object(i),!0).forEach((function(t){n(e,t,i[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(i)):l(Object(i)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(i,t))}))}return e}var v={lineWidth:3,strokeColor:"rgba(0, 136, 255, 1)",lineJoin:"round",frontLineWidth:3,frontStrokeColor:"rgba(0, 136, 255, 1)",frontLineJoin:"round",dashOffset:[5,10]},f="rgba(0, 136, 255, 0.5)",d={lineWidth:3,strokeColor:"rgba(0, 136, 255, 1)",fillColor:"rgba(0, 136, 255, 0.5)"},p={showPoint:!0,pointType:"square",pointWidth:3,lineType:"line",isDash:!1},y=function(){function t(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};e(this,t),this.ctx=i,this.opt=u(u({},p),n),this.data=n.data||null,this.strokeStyle=n.strokeStyle?u(u({},v),n.strokeStyle):v,this.fillColor=n.fillColor||f,this.pointStyle=n.pointStyle?u(u({},d),n.pointStyle):d,this.pointArr=n.pointArr||[],this.updatePointFn=n.updatePoint,this.isClosePath=!1,this.isEditing=!1,this.isDragging=!1,this.dragPointIndex=-1,this.dragCachePointArr=[],this.hoverActive=!1,this.area=n.area||!1,this.areaToPointPos=null,this.enableAddPoint=n.enableAddPoint||!1}return i(t,[{key:"enable",value:function(){this.isEditing=!0,this.insertFictitiousPoints()}},{key:"disable",value:function(){this.isEditing=!1,this.removeFictitiousPoints()}},{key:"getTruePointArr",value:function(){return this.pointArr.filter((function(e){return!e.fictitious}))}},{key:"getPointLength",value:function(){return this.getTruePointArr().length}},{key:"pushPoint",value:function(e,t){this.isEditing&&!this.isClosePath&&this.pointArr.push({x:e,y:t})}},{key:"removePoint",value:function(e){this.pointArr[e].fictitious||(this.pointArr.splice(e,1),this.removeFictitiousPoints(),this.insertFictitiousPoints())}},{key:"areaToPoint",value:function(e,t){this.areaToPointPos={x:e,y:t}}},{key:"render",value:function(){(this.isClosePath||this.area)&&this.renderArea(),"custom"===this.opt.lineType?this.opt.customRenderLine&&this.opt.customRenderLine(this):"borderLine"===this.opt.lineType?(this.renderLines(this.strokeStyle),this.renderLines(u(u({},this.strokeStyle),{},{lineWidth:this.strokeStyle.frontLineWidth,strokeColor:this.strokeStyle.frontStrokeColor,lineJoin:this.strokeStyle.frontLineJoin}))):this.renderLines(this.strokeStyle),this.renderPoints()}},{key:"renderArea",value:function(){this.ctx.save(),this.ctx.fillStyle=this.fillColor,this.ctx.beginPath();for(var e=this.pointArr.concat(this.area&&this.areaToPointPos?[this.areaToPointPos]:[]),t=0;t<e.length;t++){var i=e[t].x,n=e[t].y;0===t?this.ctx.moveTo(i,n):this.ctx.lineTo(i,n)}this.ctx.closePath(),this.ctx.fill(),this.ctx.restore()}},{key:"renderLines",value:function(e,t){var i=e.lineWidth,n=e.strokeColor,r=e.lineJoin,s=e.dashOffset;this.ctx.save(),this.ctx.lineWidth=i,this.ctx.strokeStyle=n,this.ctx.lineJoin=r,this.ctx.beginPath(),this.opt.isDash?this.ctx.setLineDash(s):a.noop();for(var o=this.pointArr.concat(this.area&&this.areaToPointPos?[this.areaToPointPos]:[]),h=0;h<o.length;h++){var c=o[h].x,l=o[h].y;0===h?this.ctx.moveTo(c,l):this.ctx.lineTo(c,l)}(this.isClosePath||this.area)&&this.ctx.closePath(),t||this.ctx.stroke(),this.ctx.restore()}},{key:"renderPoints",value:function(e,t){for(var i=0;i<this.pointArr.length;i++){this.ctx.beginPath();var n=this.pointArr[i].x,r=this.pointArr[i].y;(this.isEditing||e||this.hoverActive)&&(this.drawPoint(n,r,e,!1,this.pointArr[i].fictitious),t&&t(i))}}},{key:"insertFictitiousPoints",value:function(){if(this.isEditing&&this.isClosePath&&this.enableAddPoint){this.removeFictitiousPoints();for(var e=[],t=this.pointArr,i=t.length,n=0;n<i-1;n++){var r=t[n],s=t[n+1];e.push({x:(r.x+s.x)/2,y:(r.y+s.y)/2,fictitious:!0})}e.push({x:(t[i-1].x+t[0].x)/2,y:(t[i-1].y+t[0].y)/2,fictitious:!0});for(var o=[],a=0;a<this.pointArr.length;a++)o.push(this.pointArr[a]),o.push(e.shift());this.pointArr=o}}},{key:"removeFictitiousPoints",value:function(){this.pointArr=this.getTruePointArr()}},{key:"drawPoint",value:function(e,t,i,n,r){var s=this.opt,o=s.customRenderPoint,a=s.showPoint,h=s.pointType,c=s.pointWidth;if(n&&this.ctx.beginPath(),o)o(this.ctx,e,t,i,this.pointStyle);else{switch(this.ctx.save(),this.ctx.lineWidth=this.pointStyle.lineWidth,this.ctx.strokeStyle=this.pointStyle.strokeColor,this.ctx.fillStyle=this.pointStyle.fillColor,r&&(this.ctx.strokeStyle=this.pointStyle.fillColor,this.ctx.fillStyle=this.pointStyle.strokeColor),h){case"square":this.ctx.rect(e-c,t-c,2*c,2*c);break;case"circle":this.ctx.arc(e,t,2*c,0,2*Math.PI)}i||a&&(this.ctx.fill(),this.ctx.stroke()),this.ctx.restore()}}},{key:"checkInPath",value:function(e,t){return this.renderLines(this.strokeStyle,!0),this.ctx.isPointInPath(e,t)}},{key:"checkInPoints",value:function(e,t){var i=this,n=-1;return this.renderPoints(!0,(function(r){i.ctx.isPointInPath(e,t)&&(n=r)})),n}},{key:"closePath",value:function(){this.areaToPointPos=null,this.isClosePath=!0}},{key:"enableDrag",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:-1;this.isDragging=!0,this.dragPointIndex=e,this.dragCachePointArr=JSON.parse(JSON.stringify(this.pointArr))}},{key:"getTruePointIndex",value:function(e){if(-1===e||this.pointArr[e].fictitious)return e;for(var t=0,i=0;i<e;i++)this.pointArr[i].fictitious&&t++;return e-t}},{key:"disableDrag",value:function(){this.isDragging=!1,this.dragPointIndex=-1,this.dragCachePointArr=[],this.insertFictitiousPoints()}},{key:"dragPoint",value:function(e,t){this.isDragging&&-1!==this.dragPointIndex&&(this.pointArr[this.dragPointIndex].fictitious&&delete this.pointArr[this.dragPointIndex].fictitious,this.dragPointIndex=this.getTruePointIndex(this.dragPointIndex),this.removeFictitiousPoints(),this.updatePointFn?this.updatePointFn(this,e,t):this.pointArr.splice(this.dragPointIndex,1,u(u({},this.pointArr[this.dragPointIndex]),{},{x:e,y:t})))}},{key:"dragAll",value:function(e,t){this.isDragging&&(this.pointArr=this.dragCachePointArr.map((function(i){return u(u({},i),{},{x:i.x+e,y:i.y+t})})))}},{key:"enableHoverActive",value:function(){this.hoverActive=!0}},{key:"disableHoverActive",value:function(){this.hoverActive=!1}},{key:"checkLineSegmentCross",value:function(){if(!this.checkCrossPrevCheck())return!1;for(var e=this.createLineSegments(!0),t=e.length,i=!1,n=0;n<t;n++){var r=e[n];this.checkCrossWithLineSegments(r[0],r[1],!0)&&(i=!0)}return i}},{key:"checkNextLineSegmentCross",value:function(e,t){if(!this.checkCrossPrevCheck())return!1;var i=this.getTruePointArr(),n={x:e,y:t},r=i[i.length-1];return this.checkCrossWithLineSegments(n,r)}},{key:"checkEndLineSegmentCross",value:function(){if(!this.checkCrossPrevCheck())return!1;var e=this.getTruePointArr(),t=e[e.length-1],i=e[0];return this.checkCrossWithLineSegments(t,i)}},{key:"checkCrossWithLineSegments",value:function(e,t,i){for(var n=this.createLineSegments(i),r=!1,s=0;s<n.length;s++){var o=n[s],h=o[0],c=o[1];a.checkLineSegmentCross(h,c,e,t)&&(r=!0)}return r}},{key:"createLineSegments",value:function(e){for(var t=this.getTruePointArr(),i=t.length,n=[],r=0;r<i-1;r++)n.push([t[r],t[r+1]]);return e&&n.push([t[i-1],t[0]]),n}},{key:"checkCrossPrevCheck",value:function(){return!(this.getTruePointArr().length<=2)}},{key:"getPintNearestLine",value:function(e,t){var i=this.createLineSegments(this.isClosePath);if(i.length<=0)return null;-1!==this.dragPointIndex&&(0===this.dragPointIndex?(i.splice(0,1),i.splice(-1,1)):i.splice(this.dragPointIndex-1,2));for(var n,r=1/0,s=0;s<i.length;s++){var o=i[s],h=o[0],c=o[1],l=a.getLinePointDistance(h.x,h.y,c.x,c.y,e,t);l<r&&(r=l,n=o)}return[r,n]}}]),t}();function b(e,t){var i=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),i.push.apply(i,n)}return i}function g(e){for(var t=1;t<arguments.length;t++){var i=null!=arguments[t]?arguments[t]:{};t%2?b(Object(i),!0).forEach((function(t){n(e,t,i[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(i)):b(Object(i)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(i,t))}))}return e}var P={value:[],max:-1,hoverActive:!1,readonly:!1,single:!1,noCrossing:!1,dbClickRemovePoint:!1,area:!1,adsorbent:!0,adsorbentNum:5,adsorbentLine:!0,dbClickActive:!1,singleClickComplete:!0,enableAddPoint:!1};function k(e,t){var i=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),i.push.apply(i,n)}return i}function m(e){for(var t=1;t<arguments.length;t++){var i=null!=arguments[t]?arguments[t]:{};t%2?k(Object(i),!0).forEach((function(t){n(e,t,i[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(i)):k(Object(i)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(i,t))}))}return e}var x={dbClickTime:200},E=function(){function t(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};if(e(this,t),!i.el)throw new Error("el属性为空");if(this.opt=m(m({},x),i),this.el="string"==typeof i.el?document.querySelector(i.el):i.el,!this.el)throw new Error("容器元素获取失败");this.elRectInfo=null,this.canvasEle=null,this.canvasEleRectInfo=null,this.ctx=null,this.clickTimer=null,this.observer=new r,this.mousedownPos={x:0,y:0},this.mouseupPos={x:0,y:0},this.lastClickTime=0,this.bindEventCallback(),this.init(),this.usePlugins()}return i(t,[{key:"usePlugins",value:function(){var e=this,i=0,n=t.pluginList.length;!function r(){i>=n||(0,t.pluginList[i])(e,a).then((function(){i++,r()}))}()}},{key:"on",value:function(e,t){return this.observer.subscribe(e,t)}},{key:"off",value:function(e){this.observer.unsubscribe(e)}},{key:"init",value:function(){this.createElement(),this.ctx=this.canvasEle.getContext("2d"),this.bindEvent()}},{key:"destroy",value:function(){this.unbindEvent(),this.el.removeChild(this.canvasEle),this.observer.publish("DESTORY"),this.observer.clearAll()}},{key:"createElement",value:function(){this.elRectInfo=this.el.getBoundingClientRect();var e=this.elRectInfo,t=e.width,i=e.height;this.canvasEle=document.createElement("canvas"),this.canvasEle.width=t,this.canvasEle.height=i,this.canvasEleRectInfo={width:t,height:i},this.el.appendChild(this.canvasEle)}},{key:"bindEventCallback",value:function(){this.onclick=this.onclick.bind(this),this.onmousedown=this.onmousedown.bind(this),this.onmousemove=this.onmousemove.bind(this),this.onmouseup=this.onmouseup.bind(this),this.onmouseenter=this.onmouseenter.bind(this),this.onmouseleave=this.onmouseleave.bind(this),this.onWindowClick=this.onWindowClick.bind(this)}},{key:"bindEvent",value:function(){this.canvasEle.addEventListener("click",this.onclick),this.canvasEle.addEventListener("mousedown",this.onmousedown),this.canvasEle.addEventListener("mousemove",this.onmousemove),window.addEventListener("mouseup",this.onmouseup),this.canvasEle.addEventListener("mouseenter",this.onmouseenter),this.canvasEle.addEventListener("mouseleave",this.onmouseleave),window.addEventListener("click",this.onWindowClick)}},{key:"unbindEvent",value:function(){this.canvasEle.removeEventListener("click",this.onclick),this.canvasEle.removeEventListener("mousedown",this.onmousedown),this.canvasEle.removeEventListener("mousemove",this.onmousemove),window.removeEventListener("mouseup",this.onmouseup),this.canvasEle.removeEventListener("mouseenter",this.onmouseenter),this.canvasEle.removeEventListener("mouseleave",this.onmouseleave),window.removeEventListener("click",this.onWindowClick)}},{key:"onclick",value:function(e){var t=this;this.clickTimer&&(clearTimeout(this.clickTimer),this.clickTimer=null),this.clickTimer=setTimeout((function(){t.observer.publish("CLICK",e)}),this.opt.dbClickTime),Date.now()-this.lastClickTime<=this.opt.dbClickTime&&(clearTimeout(this.clickTimer),this.clickTimer=null,this.lastClickTime=0,this.observer.publish("DOUBLE-CLICK",e)),this.lastClickTime=Date.now()}},{key:"onmousedown",value:function(e){this.mousedownPos={x:e.clientX,y:e.clientY},this.observer.publish("MOUSEDOWN",e)}},{key:"onmousemove",value:function(e){this.observer.publish("MOUSEMOVE",e)}},{key:"onmouseup",value:function(e){this.mouseupPos={x:e.clientX,y:e.clientY},this.observer.publish("MOUSEUP",e)}},{key:"onmouseenter",value:function(e){this.observer.publish("MOUSEENTER",e)}},{key:"onmouseleave",value:function(e){this.observer.publish("MOUSELEAVE",e)}},{key:"onWindowClick",value:function(e){this.observer.publish("WINDOW-CLICK",e)}},{key:"clearCanvas",value:function(){this.ctx.clearRect(0,0,this.canvasEle.width,this.canvasEle.height)}},{key:"toCanvasPos",value:function(e){var t=e.clientX,i=e.clientY,n=this.canvasEle.getBoundingClientRect();return{x:t-n.left,y:i-n.top}}}],[{key:"use",value:function(e){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:-1;if(e){if(e.used)return this;e.used=!0,-1===i?t.pluginList.push(e):t.pluginList.splice(i,0,e)}}}]),t}();return n(E,"pluginList",[]),E.use((function(e,t){var i=null,n=new Promise((function(e){i=e})),r=g(g({},P),e.opt);r.dbClickActive&&(e.opt.cursorTips?e.opt.cursorTips.HOVER||(e.opt.cursorTips.HOVER="双击激活该区域并进入编辑状态"):e.opt.cursorTips={HOVER:"双击激活该区域并进入编辑状态"});var s=[],o=null,a={x:0,y:0},h={x:0,y:0},l=r.readonly,u=!1,v=null,f=null,d=0,p=null,b=[0,0],k={x:0,y:0},m=!1,x=E();function E(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return new y(e.ctx,g(g(g(g({id:d++},r),v),t),{},{area:r.area}))}function C(){e.clearCanvas(),r.single&&(o||u)?o&&o.render():s.forEach((function(e){e.render()})),p&&x.drawPoint(p[0],p[1],!1,!0)}function O(){s.forEach((function(e){e.disable()}))}function L(){s.forEach((function(e){e.disableHoverActive()}))}function A(e,t){for(var i=s.length-1;i>=0;i--){var n=s[i];if(n.checkInPath(e,t)||-1!==n.checkInPoints(e,t))return n}}function I(e,t){for(var i=[],n=s.length-1;n>=0;n--){var r=s[n];(r.checkInPath(e,t)||-1!==r.checkInPoints(e,t))&&i.push(r)}return i}function T(){return o&&!o.isClosePath}function w(){L(),O(),M(null),C()}function S(t){return!l&&(u=t,e.observer.publish("IS-CREATE-MARKING-CHANGE",t),!0)}function M(t){return!l&&(o=t,e.observer.publish("CURRENT-MARK-ITEM-CHANGE",t),!0)}function D(e,i){if(!r.adsorbent)return[e,i];var n=1/0,a=e,h=i,c=null;return s.forEach((function(s){if(s.pointArr.forEach((function(l,u){if(!o||s!==o||s.dragPointIndex!==u){var v=t.getTwoPointDistance(l.x,l.y,e,i);v<=r.adsorbentNum&&v<n&&(n=v,a=l.x,h=l.y,c=[a,h])}})),r.adsorbentLine){var l=s.getPintNearestLine(e,i);if(l&&l[0]<=r.adsorbentNum){var u=l[1],v=u[0],f=u[1],d=Math.min(v.x,f.x),p=Math.max(v.x,f.x);if(e>=d&&e<=p){var y=t.getNearestPointFromLine(v.x,v.y,f.x,f.y,e,i);a=y[0],h=y[1],c=[a,h]}}}})),p=c,[a,h]}return r.value.length>0&&(r.value.forEach((function(t){var i=new y(e.ctx,g(g(g({id:d++},r),t),{},{pointArr:t.pointArr.map((function(t){return{x:t.x*e.canvasEleRectInfo.width,y:t.y*e.canvasEleRectInfo.height}}))}));i.closePath(),s.push(i)})),C()),e.on("CLICK",(function(t){if(!l)if(m)m=!1;else{var i=e.toCanvasPos(t),n=i.x,a=i.y,h=null;if(u){var c=n,v=a;if(p&&(c=p[0],v=p[1],p=null),o)if(r.noCrossing)o.checkNextLineSegmentCross(c,v)?e.observer.publish("LINE-CROSS",o):o.pushPoint(c,v);else o.pushPoint(c,v);else-1===r.max||s.length<r.max?(O(),M(E()),o.enable(),o.pushPoint(c,v),s.push(o)):(e.observer.publish("COUNT-LIMIT",o),S(!1))}else(h=A(n,a))?r.dbClickActive||I(n,a).includes(o)||(!r.single||r.single&&!o)&&(O(),h.enable(),M(h)):!r.single&&r.singleClickComplete&&w();C()}})),e.on("DOUBLE-CLICK",(function(t){if(!l){var i=e.toCanvasPos(t),n=i.x,s=i.y,a=!0,h=A(n,s),c=!(!h||!o)&&h===o;if(o){var v=o.checkInPoints(n,s);r.dbClickRemovePoint&&-1!==v?(a=!1,o.getPointLength()>3?(o.removePoint(v),C()):e.observer.publish("NOT-ENOUGH-POINTS-REMOVE",o)):o.getPointLength()<3?(a=!1,e.observer.publish("NOT-ENOUGH-END-POINTS",o)):r.noCrossing&&o.checkEndLineSegmentCross()?(a=!1,e.observer.publish("LINE-CROSS",o)):(u&&e.observer.publish("COMPLETE-CREATE-ITEM",o,t),S(!1),o.closePath(),o.disable(),p=null,M(null),C(),e.observer.publish("COMPLETE-EDIT-ITEM",o,t))}r.dbClickActive&&!u&&a&&h&&!c&&(O(),h.enable(),M(h),C())}})),e.on("MOUSEDOWN",(function(t){if(!l){var i=e.toCanvasPos(t),n=i.x,s=i.y;if(o&&o.isEditing&&o.isClosePath){var c=o.checkInPoints(n,s);(o.checkInPath(n,s)||-1!==c)&&(r.noCrossing&&(f=JSON.parse(JSON.stringify(o.pointArr))),a.x=n,a.y=s,h.x=n,h.y=s,o.enableDrag(c))}}})),e.on("MOUSEMOVE",(function(i){if(!l){var n=e.toCanvasPos(i),v=n.x,f=n.y;if(o&&o.isDragging){if(-1!==o.dragPointIndex){var d;(d=o).dragPoint.apply(d,c(D(v,f)))}else{!function(e,i){if(!r.adsorbent)return[e,i];var n=1/0,h=null,c=null;o.pointArr.forEach((function(e){s.forEach((function(i){i!==o&&i.pointArr.forEach((function(i){var r=t.getTwoPointDistance(e.x,e.y,i.x,i.y);r<n&&(n=r,h=e,c=i)}))}))})),n<=r.adsorbentNum&&(b=[c.x-h.x,c.y-h.y],a.x-=b[0],a.y-=b[1])}(),0!==b[0]&&0!==b[1]&&0===k.x&&0===k.y&&(k.x=v,k.y=f),0!==k.x&&0!==k.y&&t.getTwoPointDistance(k.x,k.y,v,f)>r.adsorbentNum&&(b=[0,0],a.x=h.x,a.y=h.y,k.x=0,k.y=0);var p=v-a.x,y=f-a.y;o.dragAll(p,y)}C();var g=o.checkInPoints(v,f);e.observer.publish("HOVER-ITEM",o,o,I(v,f),i,g)}else if(u){var P,m=D(v-a.x,f-a.y);if(r.area&&o)(P=o).areaToPoint.apply(P,c(m));C()}else if(!u){var x=A(v,f);if(!r.hoverActive||o&&!o.isClosePath||(L(),x&&x.enableHoverActive(),C()),x&&x.isClosePath){var E=x.checkInPoints(v,f);e.observer.publish("HOVER-ITEM",x,o,I(v,f),i,E)}}}})),e.on("MOUSEUP",(function(t){l||o&&o.isDragging&&(m=!0,o.disableDrag(),a.x=0,a.y=0,h.x=0,h.y=0,r.noCrossing&&o.checkLineSegmentCross()&&(e.observer.publish("LINE-CROSS",o),o.pointArr=f,f=null),C())})),e._disableAllItemsEdit=O,e._setMarkEditItem=M,e._createNewMarkItem=E,e._setIsCreateMarking=S,e._render=C,e._disableAllItemsHoverActive=L,e._checkInPathItem=A,e._checkInPathAllItems=I,e._getIsCreateIngMarkItem=T,e.getState=function(){return{markItemList:s,curEditingMarkItem:o,isReadonly:l,isCreateMarking:u}},e.getMarkData=function(){return s.map((function(t){var i=t.pointArr.map((function(t){return{x:t.x/e.canvasEleRectInfo.width,y:t.y/e.canvasEleRectInfo.height}}));return{data:t.data,pointArr:i}}))},e.enableEdit=function(){l=!1},e.disableEdit=function(){return!T()&&(w(),l=!0,!0)},e.deleteMarkItem=function(t){if(!t)return!1;var i=s.findIndex((function(e){return e===t}));if(-1!==i){o===t&&M(null);var n=s.splice(i,1);return C(),e.observer.publish("DELETE-MARKING-ITEM",n[0],i),!0}return!1},e.deleteAllMarkItem=function(){s=[],M(null),C(),e.observer.publish("DELETE-ALL-MARKING-ITEM")},e.createMarkItem=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;return!T()&&!l&&(w(),v=t,S(!0),r.single&&e.clearCanvas(),!0)},e.exitCreate=function(){if(!u)return!1;if(S(!1),T()){var e=s.findIndex((function(e){return e===o}));s.splice(e,1)}w()},e.reset=w,i(),n})),E}));
//# sourceMappingURL=z-polygon-editor.js.map