@labelbee/lb-annotation 1.28.0-alpha.8 → 1.28.0-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/toolOperation/basicToolOperation.js +1 -1
- package/dist/core/toolOperation/pointCloud2dOperation.js +1 -1
- package/dist/core/toolOperation/polygonOperation.js +1 -1
- package/dist/core/toolOperation/rectOperation.js +2 -2
- package/dist/types/core/toolOperation/ScribbleTool.d.ts +1 -9
- package/dist/types/core/toolOperation/basicToolOperation.d.ts +6 -1
- package/dist/types/core/toolOperation/polygonOperation.d.ts +6 -11
- package/dist/types/core/toolOperation/rectOperation.d.ts +3 -1
- package/dist/types/utils/tool/AxisUtils.d.ts +3 -27
- package/dist/utils/tool/AxisUtils.js +1 -1
- package/dist/utils/tool/DrawUtils.js +1 -1
- package/dist/utils/tool/PolygonUtils.js +1 -1
- package/dist/utils/tool/polygonTool.js +1 -1
- package/es/core/toolOperation/basicToolOperation.js +1 -1
- package/es/core/toolOperation/pointCloud2dOperation.js +1 -1
- package/es/core/toolOperation/polygonOperation.js +1 -1
- package/es/core/toolOperation/rectOperation.js +2 -2
- package/es/utils/tool/AxisUtils.js +1 -1
- package/es/utils/tool/DrawUtils.js +3 -3
- package/es/utils/tool/PolygonUtils.js +1 -1
- package/es/utils/tool/polygonTool.js +1 -1
- package/package.json +1 -1
|
@@ -47,15 +47,7 @@ export default class AxisUtils {
|
|
|
47
47
|
* @param currentPos
|
|
48
48
|
* @returns
|
|
49
49
|
*/
|
|
50
|
-
static changePointByZoom(point: IPoint | IPolygonPoint, zoom: number, currentPos?: ICoordinate):
|
|
51
|
-
x: number;
|
|
52
|
-
y: number;
|
|
53
|
-
specialPoint?: boolean | undefined;
|
|
54
|
-
specialEdge?: boolean | undefined;
|
|
55
|
-
} | {
|
|
56
|
-
x: number;
|
|
57
|
-
y: number;
|
|
58
|
-
};
|
|
50
|
+
static changePointByZoom(point: IPoint | IPolygonPoint, zoom: number, currentPos?: ICoordinate): IPoint;
|
|
59
51
|
/**
|
|
60
52
|
* 计算点集在 zoom 和 currentPos 的转换
|
|
61
53
|
* @param pointList
|
|
@@ -63,15 +55,7 @@ export default class AxisUtils {
|
|
|
63
55
|
* @param currentPos
|
|
64
56
|
* @returns
|
|
65
57
|
*/
|
|
66
|
-
static changePointListByZoom(pointList: IPoint[] | IPolygonPoint[], zoom: number, currentPos?: ICoordinate):
|
|
67
|
-
x: number;
|
|
68
|
-
y: number;
|
|
69
|
-
specialPoint?: boolean | undefined;
|
|
70
|
-
specialEdge?: boolean | undefined;
|
|
71
|
-
} | {
|
|
72
|
-
x: number;
|
|
73
|
-
y: number;
|
|
74
|
-
})[];
|
|
58
|
+
static changePointListByZoom(pointList: IPoint[] | IPolygonPoint[], zoom: number, currentPos?: ICoordinate): IPoint[];
|
|
75
59
|
static changePlanePointByZoom(planePoints: IPlanePoints, zoom: number, currentPos?: ICoordinate): IPlanePoints;
|
|
76
60
|
static changeCuboidByZoom(cuboid: ICuboid | IDrawingCuboid, zoom: number, currentPos?: ICoordinate): ICuboid | IDrawingCuboid;
|
|
77
61
|
static transformPlain2PointList({ tl, tr, br, bl }: IPlanePoints): any[];
|
|
@@ -140,15 +124,7 @@ export default class AxisUtils {
|
|
|
140
124
|
zoom: number;
|
|
141
125
|
}>): {
|
|
142
126
|
type: string;
|
|
143
|
-
points:
|
|
144
|
-
x: number;
|
|
145
|
-
y: number;
|
|
146
|
-
specialPoint?: boolean | undefined;
|
|
147
|
-
specialEdge?: boolean | undefined;
|
|
148
|
-
} | {
|
|
149
|
-
x: number;
|
|
150
|
-
y: number;
|
|
151
|
-
})[];
|
|
127
|
+
points: IPoint[];
|
|
152
128
|
originCuboid: ICuboid;
|
|
153
129
|
positions: import("@/types/tool/cuboid").ICuboidPosition[];
|
|
154
130
|
}[] | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var tool=require("../../constant/tool.js"),MathUtils=require("../MathUtils.js"),PolygonUtils=require("./PolygonUtils.js"),LineToolUtils=require("./LineToolUtils.js"),CuboidUtils=require("./CuboidUtils.js"),__defProp=Object.defineProperty,__defProps=Object.defineProperties,__getOwnPropDescs=Object.getOwnPropertyDescriptors,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(h,t,e)=>t in h?__defProp(h,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[t]=e,__spreadValues=(h,t)=>{for(var e in t||(t={}))__hasOwnProp.call(t,e)&&__defNormalProp(h,e,t[e]);if(__getOwnPropSymbols)for(var e of __getOwnPropSymbols(t))__propIsEnum.call(t,e)&&__defNormalProp(h,e,t[e]);return h},__spreadProps=(h,t)=>__defProps(h,__getOwnPropDescs(t));class AxisUtils{static getOffsetCoordinate(t,e,i){return{x:t.x*i+e.x,y:t.y*i+e.y}}static changeDrawOutsideTarget(t,e,i,s,n,o){return typeof s=="boolean"&&!s&&(n&&o?(t.x-e.x>(n.x+n.width)*o&&(t.x=(n.x+n.width)*o+e.x),t.x-e.x<n.x*o&&(t.x=n.x*o+e.x),t.y-e.y>(n.y+n.height)*o&&(t.y=(n.y+n.height)*o+e.y),t.y-e.y<n.y*o&&(t.y=n.y*o+e.y)):(t.x-e.x>i.width&&(t.x=i.width+e.x),t.x-e.x<0&&(t.x=e.x),t.y-e.y>i.height&&(t.y=i.height+e.y),t.y-e.y<0&&(t.y=e.y))),t}static changeCoordinateByRotate(t,e,i){const{width:s,height:n}=i,{x:o,y:r}=t;switch(e%360){case 90:return{x:n-r,y:o};case 180:return{x:s-o,y:n-r};case 270:return{x:r,y:s-o};default:return t}}static changeRectByZoom(t,e,i={x:0,y:0}){return __spreadProps(__spreadValues({},t),{x:t.x*e+i.x,y:t.y*e+i.y,width:t.width*e,height:t.height*e})}static changePointByZoom(t,e,i={x:0,y:0}){return __spreadProps(__spreadValues({},t),{x:t.x*e+i.x,y:t.y*e+i.y})}static changePointListByZoom(t,e,i={x:0,y:0}){return t==null?void 0:t.map(s=>this.changePointByZoom(s,e,i))}static changePlanePointByZoom(t,e,i={x:0,y:0}){return Object.entries(t).reduce((s,[n,o])=>{const r=AxisUtils.changePointByZoom(o,e,i);return __spreadProps(__spreadValues({},s),{[n]:r})},{})}static changeCuboidByZoom(t,e,i={x:0,y:0}){return __spreadProps(__spreadValues({},t),{frontPoints:this.changePlanePointByZoom(t.frontPoints,e,i),backPoints:t.backPoints?this.changePlanePointByZoom(t.backPoints,e,i):t.backPoints})}static transformPlain2PointList({tl:t,tr:e,br:i,bl:s}){return[t,e,i,s]}static axisArea(t,e=3){const{x:i,y:s}=t,n=[];for(let o=i-e;o<i+e;o+=e/3)for(let r=s-e;r<s+e;r+=e/3)n.push({x:o,y:r});return n}static getOriginCoordinateWithOffsetCoordinate(t,e=1,i={x:0,y:0}){return{x:(t.x-i.x)/e,y:(t.y-i.y)/e}}static returnClosePointIndex(t,e,i=3){let s=-1;for(let n=0;n<e.length;n++){const o=e[n];this.getIsInScope(t,o,i)&&(s=n)}return s}static isCloseCuboid(t,e,i={scope:5,zoom:1}){const s=CuboidUtils.getHighlightPoints(e),{scope:n=5}=i;for(let r=0;r<s.length;r++){const g=s[r];if(this.getIsInScope(t,g.point,n))return!0}const o=CuboidUtils.getHighlightLines(e);for(let r=0;r<o.length;r++){const g=o[r],{length:y}=MathUtils.default.getFootOfPerpendicular(t,g.p1,g.p2,!0);if(y<n)return!0}return!!PolygonUtils.isInPolygon(t,CuboidUtils.getCuboidHoverRange(e))}static isCloseCuboidList(t,e,i={scope:5,zoom:1}){return e.some(s=>this.isCloseCuboid(t,s,i))}static returnClosePointOrLineInCuboid(t,e,i={scope:5,zoom:1}){const s=CuboidUtils.getHighlightPoints(e),{scope:n=5,zoom:o=1}=i;for(let a=0;a<s.length;a++){const l=s[a];if(this.getIsInScope(t,l.point,n))return[{type:"point",points:[this.changePointByZoom(s[a].point,o)],originCuboid:e,positions:l.positions}]}let r=n;const g=CuboidUtils.getHighlightLines(e);let y;for(let a=0;a<g.length;a++){const l=g[a],{length:p}=MathUtils.default.getFootOfPerpendicular(t,l.p1,l.p2,!0);p<r&&(r=p,y=[{type:"line",points:this.changePointListByZoom([l.p1,l.p2],o),originCuboid:e,positions:l.positions}])}if(y)return y}static getIsInScope(t,e,i){return Math.abs(t.x-e.x)<i&&Math.abs(t.y-e.y)<i}}class CoordinateUtils{constructor(t){this.currentPos=t.currentPos,this.zoom=t.zoom,this.basicImgInfo=t.basicImgInfo,this.dependToolName=""}get isDependPolygon(){return this.dependToolName===tool.EToolName.Polygon}get isDependRect(){return this.dependToolName===tool.EToolName.Rect}get isDependOriginalImage(){return this.dependToolName===""}getAbsCoord(t){return{x:(t.x-this.currentPos.x)/this.zoom,y:(t.y-this.currentPos.y)/this.zoom}}getRenderCoord(t){return{x:t.x*this.zoom+this.currentPos.x,y:t.y*this.zoom+this.currentPos.y}}coordInsideRect(t,e){const{x:i,y:s,width:n,height:o}=e;return{x:MathUtils.default.withinRange(t.x,[i,i+n]),y:MathUtils.default.withinRange(t.y,[s,s+o])}}getPolygonPointList(t,e){return t===tool.ELineTypes.Curve?PolygonUtils.createSmoothCurvePointsFromPointList(e):e}getIntersection(t,e,i){const s=this.getRenderCoord(e),n=this.getRenderCoord(t),o={pointA:s,pointB:n};return LineToolUtils.default.calcOptimalIntersection(i,o,s,LineToolUtils.POINT_RADIUS,this.zoom)}coordInsidePolygon(t,e,i,s){const{pointList:n}=i,o=s==null?void 0:s.lineType;if(n.length===0)return t;const r=this.getPolygonPointList(o,n);if(PolygonUtils.isInPolygon(t,r))return t;const y=r.concat(r[0]).map(l=>this.getRenderCoord(l)),a=this.getIntersection(t,e,y);return a?this.getAbsCoord(a==null?void 0:a.point):t}coordInsideImage(t){return this.coordInsideRect(t,__spreadProps(__spreadValues({},this.basicImgInfo),{x:0,y:0}))}getNextCoordByDependTool(t,e){if(this.isDependRect)return this.coordInsideRect(t,this.basicResult);if(this.isDependPolygon)return this.coordInsidePolygon(t,e,this.basicResult,this.dependToolConfig);if(this.isDependOriginalImage)return this.coordInsideImage(t)}setDependInfo(t,e){this.dependToolName=t!=null?t:"",this.dependToolConfig=t?e:void 0}setBasicImgInfo(t){this.basicImgInfo=t}setBasicResult(t){this.basicResult=t}setZoomAndCurrentPos(t,e){this.zoom=t,this.currentPos=e}isCoordInsideTarget(t){if(this.isDependPolygon)return this.isInBasicPolygon(t);if(this.isDependRect){const{x:e,y:i,width:s,height:n}=this.basicResult,o=[e,e+s],r=[i,i+n];return MathUtils.default.isInRange(t.x,o)&&MathUtils.default.isInRange(t.y,r)}return MathUtils.default.isInRange(t.x,[0,this.basicImgInfo.width])&&MathUtils.default.isInRange(t.y,[0,this.basicImgInfo.height])}isInBasicPolygon(t){var e,i;return PolygonUtils.isInPolygon(t,((e=this.basicResult)==null?void 0:e.pointList)||[],(i=this.dependToolConfig)==null?void 0:i.lineType)}}exports.CoordinateUtils=CoordinateUtils,exports.default=AxisUtils;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var tool=require("../../constant/tool.js"),MathUtils=require("../MathUtils.js"),PolygonUtils=require("./PolygonUtils.js"),LineToolUtils=require("./LineToolUtils.js"),CuboidUtils=require("./CuboidUtils.js"),__defProp=Object.defineProperty,__defProps=Object.defineProperties,__getOwnPropDescs=Object.getOwnPropertyDescriptors,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(h,t,e)=>t in h?__defProp(h,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[t]=e,__spreadValues=(h,t)=>{for(var e in t||(t={}))__hasOwnProp.call(t,e)&&__defNormalProp(h,e,t[e]);if(__getOwnPropSymbols)for(var e of __getOwnPropSymbols(t))__propIsEnum.call(t,e)&&__defNormalProp(h,e,t[e]);return h},__spreadProps=(h,t)=>__defProps(h,__getOwnPropDescs(t));class AxisUtils{static getOffsetCoordinate(t,e,i){return{x:t.x*i+e.x,y:t.y*i+e.y}}static changeDrawOutsideTarget(t,e,i,s,n,o){return typeof s=="boolean"&&!s&&(n&&o?(t.x-e.x>(n.x+n.width)*o&&(t.x=(n.x+n.width)*o+e.x),t.x-e.x<n.x*o&&(t.x=n.x*o+e.x),t.y-e.y>(n.y+n.height)*o&&(t.y=(n.y+n.height)*o+e.y),t.y-e.y<n.y*o&&(t.y=n.y*o+e.y)):(t.x-e.x>i.width&&(t.x=i.width+e.x),t.x-e.x<0&&(t.x=e.x),t.y-e.y>i.height&&(t.y=i.height+e.y),t.y-e.y<0&&(t.y=e.y))),t}static changeCoordinateByRotate(t,e,i){const{width:s,height:n}=i,{x:o,y:r}=t;switch(e%360){case 90:return{x:n-r,y:o};case 180:return{x:s-o,y:n-r};case 270:return{x:r,y:s-o};default:return t}}static changeRectByZoom(t,e,i={x:0,y:0}){return __spreadProps(__spreadValues({},t),{x:t.x*e+i.x,y:t.y*e+i.y,width:t.width*e,height:t.height*e})}static changePointByZoom(t,e,i={x:0,y:0}){return e===1&&i.x===0&&i.y===0?t:__spreadProps(__spreadValues({},t),{x:t.x*e+i.x,y:t.y*e+i.y})}static changePointListByZoom(t,e,i={x:0,y:0}){return t==null?void 0:t.map(s=>this.changePointByZoom(s,e,i))}static changePlanePointByZoom(t,e,i={x:0,y:0}){return Object.entries(t).reduce((s,[n,o])=>{const r=AxisUtils.changePointByZoom(o,e,i);return __spreadProps(__spreadValues({},s),{[n]:r})},{})}static changeCuboidByZoom(t,e,i={x:0,y:0}){return __spreadProps(__spreadValues({},t),{frontPoints:this.changePlanePointByZoom(t.frontPoints,e,i),backPoints:t.backPoints?this.changePlanePointByZoom(t.backPoints,e,i):t.backPoints})}static transformPlain2PointList({tl:t,tr:e,br:i,bl:s}){return[t,e,i,s]}static axisArea(t,e=3){const{x:i,y:s}=t,n=[];for(let o=i-e;o<i+e;o+=e/3)for(let r=s-e;r<s+e;r+=e/3)n.push({x:o,y:r});return n}static getOriginCoordinateWithOffsetCoordinate(t,e=1,i={x:0,y:0}){return{x:(t.x-i.x)/e,y:(t.y-i.y)/e}}static returnClosePointIndex(t,e,i=3){let s=-1;for(let n=0;n<e.length;n++){const o=e[n];this.getIsInScope(t,o,i)&&(s=n)}return s}static isCloseCuboid(t,e,i={scope:5,zoom:1}){const s=CuboidUtils.getHighlightPoints(e),{scope:n=5}=i;for(let r=0;r<s.length;r++){const g=s[r];if(this.getIsInScope(t,g.point,n))return!0}const o=CuboidUtils.getHighlightLines(e);for(let r=0;r<o.length;r++){const g=o[r],{length:y}=MathUtils.default.getFootOfPerpendicular(t,g.p1,g.p2,!0);if(y<n)return!0}return!!PolygonUtils.isInPolygon(t,CuboidUtils.getCuboidHoverRange(e))}static isCloseCuboidList(t,e,i={scope:5,zoom:1}){return e.some(s=>this.isCloseCuboid(t,s,i))}static returnClosePointOrLineInCuboid(t,e,i={scope:5,zoom:1}){const s=CuboidUtils.getHighlightPoints(e),{scope:n=5,zoom:o=1}=i;for(let a=0;a<s.length;a++){const l=s[a];if(this.getIsInScope(t,l.point,n))return[{type:"point",points:[this.changePointByZoom(s[a].point,o)],originCuboid:e,positions:l.positions}]}let r=n;const g=CuboidUtils.getHighlightLines(e);let y;for(let a=0;a<g.length;a++){const l=g[a],{length:p}=MathUtils.default.getFootOfPerpendicular(t,l.p1,l.p2,!0);p<r&&(r=p,y=[{type:"line",points:this.changePointListByZoom([l.p1,l.p2],o),originCuboid:e,positions:l.positions}])}if(y)return y}static getIsInScope(t,e,i){return Math.abs(t.x-e.x)<i&&Math.abs(t.y-e.y)<i}}class CoordinateUtils{constructor(t){this.currentPos=t.currentPos,this.zoom=t.zoom,this.basicImgInfo=t.basicImgInfo,this.dependToolName=""}get isDependPolygon(){return this.dependToolName===tool.EToolName.Polygon}get isDependRect(){return this.dependToolName===tool.EToolName.Rect}get isDependOriginalImage(){return this.dependToolName===""}getAbsCoord(t){return{x:(t.x-this.currentPos.x)/this.zoom,y:(t.y-this.currentPos.y)/this.zoom}}getRenderCoord(t){return{x:t.x*this.zoom+this.currentPos.x,y:t.y*this.zoom+this.currentPos.y}}coordInsideRect(t,e){const{x:i,y:s,width:n,height:o}=e;return{x:MathUtils.default.withinRange(t.x,[i,i+n]),y:MathUtils.default.withinRange(t.y,[s,s+o])}}getPolygonPointList(t,e){return t===tool.ELineTypes.Curve?PolygonUtils.createSmoothCurvePointsFromPointList(e):e}getIntersection(t,e,i){const s=this.getRenderCoord(e),n=this.getRenderCoord(t),o={pointA:s,pointB:n};return LineToolUtils.default.calcOptimalIntersection(i,o,s,LineToolUtils.POINT_RADIUS,this.zoom)}coordInsidePolygon(t,e,i,s){const{pointList:n}=i,o=s==null?void 0:s.lineType;if(n.length===0)return t;const r=this.getPolygonPointList(o,n);if(PolygonUtils.isInPolygon(t,r))return t;const y=r.concat(r[0]).map(l=>this.getRenderCoord(l)),a=this.getIntersection(t,e,y);return a?this.getAbsCoord(a==null?void 0:a.point):t}coordInsideImage(t){return this.coordInsideRect(t,__spreadProps(__spreadValues({},this.basicImgInfo),{x:0,y:0}))}getNextCoordByDependTool(t,e){if(this.isDependRect)return this.coordInsideRect(t,this.basicResult);if(this.isDependPolygon)return this.coordInsidePolygon(t,e,this.basicResult,this.dependToolConfig);if(this.isDependOriginalImage)return this.coordInsideImage(t)}setDependInfo(t,e){this.dependToolName=t!=null?t:"",this.dependToolConfig=t?e:void 0}setBasicImgInfo(t){this.basicImgInfo=t}setBasicResult(t){this.basicResult=t}setZoomAndCurrentPos(t,e){this.zoom=t,this.currentPos=e}isCoordInsideTarget(t){if(this.isDependPolygon)return this.isInBasicPolygon(t);if(this.isDependRect){const{x:e,y:i,width:s,height:n}=this.basicResult,o=[e,e+s],r=[i,i+n];return MathUtils.default.isInRange(t.x,o)&&MathUtils.default.isInRange(t.y,r)}return MathUtils.default.isInRange(t.x,[0,this.basicImgInfo.width])&&MathUtils.default.isInRange(t.y,[0,this.basicImgInfo.height])}isInBasicPolygon(t){var e,i;return PolygonUtils.isInPolygon(t,((e=this.basicResult)==null?void 0:e.pointList)||[],(i=this.dependToolConfig)==null?void 0:i.lineType)}}exports.CoordinateUtils=CoordinateUtils,exports.default=AxisUtils;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
"use strict";var rgba=require("color-rgba"),lbUtils=require("@labelbee/lb-utils"),tool=require("../../constant/tool.js"),PolygonUtils=require("./PolygonUtils.js"),UnitUtils=require("./UnitUtils.js"),AxisUtils=require("./AxisUtils.js"),CuboidUtils=require("./CuboidUtils.js"),AttributeUtils=require("./AttributeUtils.js");function _interopDefaultLegacy(r){return r&&typeof r=="object"&&"default"in r?r:{default:r}}var rgba__default=_interopDefaultLegacy(rgba),__defProp=Object.defineProperty,__defProps=Object.defineProperties,__getOwnPropDescs=Object.getOwnPropertyDescriptors,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(r,t,i)=>t in r?__defProp(r,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):r[t]=i,__spreadValues=(r,t)=>{for(var i in t||(t={}))__hasOwnProp.call(t,i)&&__defNormalProp(r,i,t[i]);if(__getOwnPropSymbols)for(var i of __getOwnPropSymbols(t))__propIsEnum.call(t,i)&&__defNormalProp(r,i,t[i]);return r},__spreadProps=(r,t)=>__defProps(r,__getOwnPropDescs(t));const DEFAULT_ZOOM=1,DEFAULT_CURRENT_POS={x:0,y:0},DEFAULT_ROTATE=0,DEFAULT_COLOR="",HIGHLIGHT_ICON_SVG_PATHS=[{d:"M0.423514 10.1595C-0.362666 7.22543 1.37854 4.20957 4.3126 3.42339C7.24666 2.63721 10.2625 4.37842 11.0487 7.31248L49.8716 152.201C50.6577 155.135 48.9165 158.151 45.9825 158.937C43.0484 159.724 40.0325 157.982 39.2464 155.048L0.423514 10.1595Z"},{d:"M14.0774 9.47294C28.5 -16.5001 91.5 25.5001 113.138 0.529419L131.773 70.076C112 96.9999 50.5 54 32.7124 79.0196L14.0774 9.47294Z"}],_DrawUtils=class{static drawLine(r,t,i,e={}){const o=r.getContext("2d"),{color:s=DEFAULT_COLOR,thickness:l=1,lineCap:c="round",lineDash:n}=e;o.save(),o.strokeStyle=s,o.lineWidth=l,o.lineCap=c,n&&o.setLineDash(n),o.beginPath(),o.moveTo(t.x,t.y),o.lineTo(i.x+1,i.y+1),o.stroke(),o.restore()}static drawRect(r,t,i={}){const e=r.getContext("2d"),{color:o=DEFAULT_COLOR,thickness:s=1,lineCap:l="round",hiddenText:c=!1,lineDash:n}=i;if(e.save(),e.strokeStyle=o,e.lineWidth=s,e.lineCap=l,Array.isArray(n)&&e.setLineDash(n),e.fillStyle=o,e.strokeRect(t.x,t.y,t.width,t.height),c===!1){let a="";if(t.attribute&&(a=`${a} ${t.attribute}`),this.drawText(r,{x:t.x,y:t.y-5},a),t.textAttribute){const h=`${~~t.width} * ${~~t.height}`.length*7,u=0,y=Math.max(20,t.width-h);this.drawText(r,{x:t.x,y:t.y+t.height+20+u},t.textAttribute,{textMaxWidth:y})}}e.restore()}static drawRectWithFill(r,t,i={}){const e=r.getContext("2d"),{color:o=DEFAULT_COLOR}=i;e.save(),e.fillStyle=o,e.fillRect(t.x,t.y,t.width,t.height),e.restore()}static drawTagByDom(r,t,i){const e=r;if(!((t==null?void 0:t.length)>0))return;const o=document.createElement("div");return o.innerHTML=t,o.setAttribute("id",i),e==null||e.appendChild(o),o}static drawTag(r,t){var i;const e=r==null?void 0:r.parentNode,o=window.self.document.getElementById("tagToolTag");if(o&&e&&e.contains(o)&&(e==null||e.removeChild(o)),!((t==null?void 0:t.length)>0))return;const s=document.createElement("div");return s.innerHTML=(i=t.reduce((l,c)=>`${l}${c.keyName}: ${c.value.join(" \u3001 ")}
|
|
2
|
-
`,""))!=null?i:"",s.setAttribute("id","tagToolTag"),e==null||e.appendChild(s),s}static drawLineWithPointList(r,t,i={}){if(t.length<2)return;const e=r.getContext("2d"),{color:o=DEFAULT_COLOR,thickness:s=1,lineCap:l="round",lineType:c=tool.ELineTypes.Line,lineDash:n,hoverEdgeIndex:a}=i;e.save(),(()=>{e.strokeStyle=o,e.lineWidth=s,e.lineCap=l,Array.isArray(n)?e.setLineDash(n):e.setLineDash([])})(),c===tool.ELineTypes.Curve?(a!==void 0&&a>-1&&t.push(t[0]),t=PolygonUtils.createSmoothCurvePointsFromPointList([...t],tool.SEGMENT_NUMBER),a!==void 0&&a>-1&&(t=t.slice((tool.SEGMENT_NUMBER+1)*a,(tool.SEGMENT_NUMBER+1)*(a+1)))):a!==void 0&&a>-1&&(t=[...t,t[0]],t=t.slice(a,a+2));const h=[];e.beginPath(),e.moveTo(t[0].x,t[0].y);for(let f=0;f<t.length-1;f++)t[f].specialEdge&&h.push({i1:f,i2:f+1}),e.lineTo(t[f+1].x,t[f+1].y);e.stroke(),e.save(),e.lineWidth=s*.8,e.lineCap="butt",e.strokeStyle="white",e.setLineDash([3,3]),h.forEach(f=>{const g=t[f.i1],w=t[f.i2];e.beginPath(),e.moveTo(g.x,g.y),e.lineTo(w.x,w.y),e.stroke()}),e.restore();const u=4,y=2;return t.forEach(f=>{f.specialPoint&&(this.drawSpecialPoint(r,f,u+y,o),this.drawSpecialPoint(r,f,u,"white"))}),e.restore(),t}static drawCircle(r,t,i,e={}){const o=r.getContext("2d"),{startAngleDeg:s=0,endAngleDeg:l=360,thickness:c=1,color:n=DEFAULT_COLOR,fill:a=DEFAULT_COLOR}=e,d=UnitUtils.deg2rad(s),h=UnitUtils.deg2rad(l);o.save(),o.beginPath(),o.strokeStyle=n,o.fillStyle=a,o.lineWidth=c,o.arc(t.x,t.y,i,d,h,!1),o.stroke(),a&&o.fill(),o.closePath(),o.restore()}static drawCircleWithFill(r,t,i=3,e={}){const o=r.getContext("2d"),{color:s=DEFAULT_COLOR}=e;o.save();const l=UnitUtils.deg2rad(0),c=UnitUtils.deg2rad(360);o.fillStyle=s,o.beginPath(),o.arc(t.x,t.y,i,l,c,!1),o.fill(),o.restore()}static drawSpecialPoint(r,t,i=6,e){const o=r.getContext("2d"),{x:s,y:l}=t;o.save(),o.beginPath(),o.fillStyle=e;const c=i*1.5,n=c*Math.sqrt(3)/2,a=c/2;o.moveTo(s,l-c),o.lineTo(s-n,l+a),o.lineTo(s+n,l+a),o.closePath(),o.fill(),o.restore()}static drawPolygon(r,t,i={}){const{isClose:e=!1,lineType:o=tool.ELineTypes.Line}=i;return e===!0&&(t=[...t,t[0]]),o===tool.ELineTypes.Curve&&(t=PolygonUtils.createSmoothCurvePointsFromPointList([...t])),this.drawLineWithPointList(r,t,__spreadProps(__spreadValues({},i),{lineType:tool.ELineTypes.Line})),t}static drawPolygonWithFill(r,t,i={}){if(t.length<2)return;const e=r.getContext("2d"),{color:o=DEFAULT_COLOR,lineType:s=tool.ELineTypes.Line}=i;e.save(),e.fillStyle=o,e.beginPath(),s===tool.ELineTypes.Curve&&(t=PolygonUtils.createSmoothCurvePointsFromPointList([...t,t[0]]));const[l,...c]=t;return e.moveTo(l.x,l.y),c.forEach(n=>{e.lineTo(n.x,n.y)}),e.fill(),e.restore(),t}static drawPolygonWithFillAndLine(r,t,i={}){const{strokeColor:e,fillColor:o,thickness:s,lineCap:l,isClose:c,lineType:n}=i,a=this.drawPolygon(r,t,{color:e,thickness:s,lineCap:l,isClose:c,lineType:n});return this.drawPolygonWithFill(r,t,{color:o,lineType:n}),a}static drawPolygonWithKeyPoint(r,t,i={}){const{pointColor:e="white",strokeColor:o}=i,s=this.drawPolygon(r,t,i);return s.forEach(l=>{this.drawCircleWithFill(r,l,4,{color:o}),this.drawCircleWithFill(r,l,3,{color:e})}),s}static drawSelectedPolygonWithFillAndLine(r,t,i={}){const{pointColor:e="white",strokeColor:o}=i,s=this.drawPolygonWithFillAndLine(r,t,i);return s.forEach(l=>{this.drawCircleWithFill(r,l,4,{color:o}),this.drawCircleWithFill(r,l,3,{color:e})}),s}static drawText(r,t,i,e={}){if(!i)return;const o=r.getContext("2d"),{color:s=DEFAULT_COLOR,font:l=tool.DEFAULT_FONT,shadowColor:c="",shadowBlur:n=0,shadowOffsetX:a=0,shadowOffsetY:d=0,textMaxWidth:h=164,offsetX:u=0,offsetY:y=0,textAlign:f="start",lineHeight:g}=e
|
|
2
|
+
`,""))!=null?i:"",s.setAttribute("id","tagToolTag"),e==null||e.appendChild(s),s}static drawLineWithPointList(r,t,i={}){if(t.length<2)return;const e=r.getContext("2d"),{color:o=DEFAULT_COLOR,thickness:s=1,lineCap:l="round",lineType:c=tool.ELineTypes.Line,lineDash:n,hoverEdgeIndex:a}=i;e.save(),(()=>{e.strokeStyle=o,e.lineWidth=s,e.lineCap=l,Array.isArray(n)?e.setLineDash(n):e.setLineDash([])})(),c===tool.ELineTypes.Curve?(a!==void 0&&a>-1&&t.push(t[0]),t=PolygonUtils.createSmoothCurvePointsFromPointList([...t],tool.SEGMENT_NUMBER),a!==void 0&&a>-1&&(t=t.slice((tool.SEGMENT_NUMBER+1)*a,(tool.SEGMENT_NUMBER+1)*(a+1)))):a!==void 0&&a>-1&&(t=[...t,t[0]],t=t.slice(a,a+2));const h=[];e.beginPath(),e.moveTo(t[0].x,t[0].y);for(let f=0;f<t.length-1;f++)t[f].specialEdge&&h.push({i1:f,i2:f+1}),e.lineTo(t[f+1].x,t[f+1].y);e.stroke(),e.save(),e.lineWidth=s*.8,e.lineCap="butt",e.strokeStyle="white",e.setLineDash([3,3]),h.forEach(f=>{const g=t[f.i1],w=t[f.i2];e.beginPath(),e.moveTo(g.x,g.y),e.lineTo(w.x,w.y),e.stroke()}),e.restore();const u=4,y=2;return t.forEach(f=>{f.specialPoint&&(this.drawSpecialPoint(r,f,u+y,o),this.drawSpecialPoint(r,f,u,"white"))}),e.restore(),t}static drawCircle(r,t,i,e={}){const o=r.getContext("2d"),{startAngleDeg:s=0,endAngleDeg:l=360,thickness:c=1,color:n=DEFAULT_COLOR,fill:a=DEFAULT_COLOR}=e,d=UnitUtils.deg2rad(s),h=UnitUtils.deg2rad(l);o.save(),o.beginPath(),o.strokeStyle=n,o.fillStyle=a,o.lineWidth=c,o.arc(t.x,t.y,i,d,h,!1),o.stroke(),a&&o.fill(),o.closePath(),o.restore()}static drawCircleWithFill(r,t,i=3,e={}){const o=r.getContext("2d"),{color:s=DEFAULT_COLOR}=e;o.save();const l=UnitUtils.deg2rad(0),c=UnitUtils.deg2rad(360);o.fillStyle=s,o.beginPath(),o.arc(t.x,t.y,i,l,c,!1),o.fill(),o.restore()}static drawSpecialPoint(r,t,i=6,e){const o=r.getContext("2d"),{x:s,y:l}=t;o.save(),o.beginPath(),o.fillStyle=e;const c=i*1.5,n=c*Math.sqrt(3)/2,a=c/2;o.moveTo(s,l-c),o.lineTo(s-n,l+a),o.lineTo(s+n,l+a),o.closePath(),o.fill(),o.restore()}static drawPolygon(r,t,i={}){const{isClose:e=!1,lineType:o=tool.ELineTypes.Line}=i;return e===!0&&(t=[...t,t[0]]),o===tool.ELineTypes.Curve&&(t=PolygonUtils.createSmoothCurvePointsFromPointList([...t])),this.drawLineWithPointList(r,t,__spreadProps(__spreadValues({},i),{lineType:tool.ELineTypes.Line})),t}static drawPolygonWithFill(r,t,i={}){if(t.length<2)return;const e=r.getContext("2d"),{color:o=DEFAULT_COLOR,lineType:s=tool.ELineTypes.Line}=i;e.save(),e.fillStyle=o,e.beginPath(),s===tool.ELineTypes.Curve&&(t=PolygonUtils.createSmoothCurvePointsFromPointList([...t,t[0]]));const[l,...c]=t;return e.moveTo(l.x,l.y),c.forEach(n=>{e.lineTo(n.x,n.y)}),e.fill(),e.restore(),t}static drawPolygonWithFillAndLine(r,t,i={}){const{strokeColor:e,fillColor:o,thickness:s,lineCap:l,isClose:c,lineType:n}=i,a=this.drawPolygon(r,t,{color:e,thickness:s,lineCap:l,isClose:c,lineType:n});return this.drawPolygonWithFill(r,t,{color:o,lineType:n}),a}static drawPolygonWithKeyPoint(r,t,i={}){const{pointColor:e="white",strokeColor:o}=i,s=this.drawPolygon(r,t,i);return s.forEach(l=>{this.drawCircleWithFill(r,l,4,{color:o}),this.drawCircleWithFill(r,l,3,{color:e})}),s}static drawSelectedPolygonWithFillAndLine(r,t,i={}){const{pointColor:e="white",strokeColor:o}=i,s=this.drawPolygonWithFillAndLine(r,t,i);return s.forEach(l=>{this.drawCircleWithFill(r,l,4,{color:o}),this.drawCircleWithFill(r,l,3,{color:e})}),s}static drawText(r,t,i,e={}){if(!i)return;const o=r.getContext("2d"),{color:s=DEFAULT_COLOR,font:l=tool.DEFAULT_FONT,shadowColor:c="",shadowBlur:n=0,shadowOffsetX:a=0,shadowOffsetY:d=0,textMaxWidth:h=164,offsetX:u=0,offsetY:y=0,textAlign:f="start",lineHeight:g}=e;!o||(o.save(),Object.assign(o,{textAlign:f,fillStyle:s!=null?s:"white",font:l,shadowColor:c,shadowOffsetX:a,shadowOffsetY:d,shadowBlur:n}),this.wrapText(r,i,t.x+u,t.y+y,h,g),o.restore())}static wrapText(r,t,i,e,o,s){if(typeof t!="string"||typeof i!="number"||typeof e!="number")return;const l=r.getContext("2d");typeof o=="undefined"&&(o=r&&r.width||300),typeof s=="undefined"&&(s=r&&parseInt(window.getComputedStyle(r).lineHeight,10)||parseInt(window.getComputedStyle(document.body).lineHeight,10));const c=t.split(`
|
|
3
3
|
`);for(let n=0;n<c.length;n++){const a=c[n].split("");let d="";for(let h=0;h<a.length;h++){const u=d+a[h],f=l.measureText(u).width;o||(o=300),f>o&&h>0?(l.fillText(d,i,e),d=a[h],e+=s):d=u}l.fillText(d,i,e),e+=s}}static drawArrow(r,t,i,e={}){const{color:o=DEFAULT_COLOR,thickness:s=1,lineCap:l="round",theta:c=30,headLen:n=10}=e,a=Math.atan2(t.y-i.y,t.x-i.x)*180/Math.PI,d=(a+c)*Math.PI/180,h=(a-c)*Math.PI/180,u=n*Math.cos(d),y=n*Math.sin(d),f=n*Math.cos(h),g=n*Math.sin(h);r.save(),r.strokeStyle=o,r.lineWidth=s,r.lineCap=l,r.beginPath(),r.moveTo(i.x+u,i.y+y),r.lineTo(i.x,i.y),r.lineTo(i.x+f,i.y+g),r.stroke(),r.restore()}static drawArrowByCanvas(r,t,i,e={}){const o=r.getContext("2d");this.drawArrow(o,t,i,e)}static drawCuboid(r,t,i={}){const{backPoints:e,direction:o,frontPoints:s}=t,{strokeColor:l,thickness:c,fillColor:n}=i,a={color:l,thickness:c};if(e){const h=AxisUtils.default.transformPlain2PointList(e);_DrawUtils.drawPolygon(r,h,__spreadProps(__spreadValues({},a),{isClose:!0}));const u=CuboidUtils.getCuboidAllSideLine(t);u==null||u.forEach(y=>{_DrawUtils.drawLine(r,y.p1,y.p2,__spreadValues({},a))})}const d=AxisUtils.default.transformPlain2PointList(s);if(o&&e&&s){const h=CuboidUtils.getPointListsByDirection({direction:o,frontPoints:s,backPoints:e});h&&_DrawUtils.drawPolygonWithFill(r,h,{color:n})}_DrawUtils.drawPolygon(r,d,__spreadProps(__spreadValues({},a),{isClose:!0}))}static drawCuboidWithText(r,t,i,e){const{strokeColor:o}=i,s=o,{config:l,hiddenText:c,selectedID:n,headerText:a,bottomText:d}=e,{backPoints:h,frontPoints:u,textAttribute:y}=t,f=u.br.x-u.bl.x;_DrawUtils.drawCuboid(r,t,i);let g="";(l==null?void 0:l.isShowOrder)&&t.order&&(t==null?void 0:t.order)>0&&(g=`${t.order}`),t.attribute&&(g=`${g} ${AttributeUtils.default.getAttributeShowText(t.attribute,l==null?void 0:l.attributeList)}`),!c&&h&&g&&_DrawUtils.drawText(r,{x:h.tl.x,y:h.tl.y-5},a!=null?a:g,{color:o,textMaxWidth:300});const w=CuboidUtils.getCuboidTextAttributeOffset({cuboid:t,currentPos:{x:0,y:0},zoom:1,topOffset:16,leftOffset:0});if(!c&&y&&t.id!==n){const x=Math.max(20,f*.8);_DrawUtils.drawText(r,{x:w.left,y:w.top},d!=null?d:y,{color:s,textMaxWidth:x})}}static drawPixel({canvas:r,points:t,size:i,defaultRGBA:e,pixelSize:o=13}){const s=r.getContext("2d"),{width:l,height:c}=i,n=s.getImageData(0,0,l,c),[a,d,h,u]=rgba__default.default(e!=null?e:lbUtils.NULL_COLOR),y=g=>{n.data[g]=a,n.data[g+1]=d,n.data[g+2]=h,n.data[g+3]=Math.floor(255*u)},f=lbUtils.MathUtils.generateCoordinates(o);return t.forEach(g=>{for(const[w,x]of f){const C=(g.y+x)*(n.width*4)+(g.x+w)*4;y(C)}}),s.putImageData(n,0,0),{canvas:r}}static drawHighlightFlag(r){const{canvas:t,position:i,color:e,scale:o=.1}=r,s=t.getContext("2d");if(!!s)for(const l of HIGHLIGHT_ICON_SVG_PATHS){s.beginPath();const c=l.d.split(/(?=[CLMZclmz])/);for(const n of c){const d=n.slice(1).split(" ").map(Number).map((h,u)=>u%2==0?h*o+i.x:h*o+i.y);switch(n[0]){case"M":s.moveTo(d[0],d[1]);break;case"C":s.bezierCurveTo(d[0],d[1],d[2],d[3],d[4],d[5]);break;case"L":s.lineTo(d[0],d[1]);break;case"Z":s.closePath();break}}s.fillStyle=e,s.fill()}}};let DrawUtils=_DrawUtils;DrawUtils.drawImg=(r,t,i={})=>{const e=r.getContext("2d"),{zoom:o=DEFAULT_ZOOM,currentPos:s=DEFAULT_CURRENT_POS,rotate:l=DEFAULT_ROTATE,imgAttribute:c}=i;switch(e.save(),l){case 0:e.translate(s.x,s.y);break;case 90:e.translate(s.x+t.height*o,s.y),e.rotate(90*Math.PI/180);break;case 180:e.translate(s.x+t.width*o,s.y+t.height*o),e.rotate(Math.PI);break;case 270:e.translate(s.x,s.y+t.width*o),e.rotate(270*Math.PI/180);break;default:e.translate(s.x,s.y);break}if(c){const{contrast:n,saturation:a,brightness:d}=c;e.filter=`saturate(${a+100}%) contrast(${n+100}%) brightness(${d+100}%)`}e.drawImage(t,0,0,t.width*o,t.height*o),e.restore()},module.exports=DrawUtils;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var turf=require("@turf/turf"),annotation=require("../../constant/annotation.js"),CommonToolUtils=require("./CommonToolUtils.js"),tool=require("../../constant/tool.js"),AxisUtils=require("./AxisUtils.js"),MathUtils=require("../MathUtils.js"),LineToolUtils=require("./LineToolUtils.js"),__defProp=Object.defineProperty,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(p,e,t)=>e in p?__defProp(p,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):p[e]=t,__spreadValues=(p,e)=>{for(var t in e||(e={}))__hasOwnProp.call(e,t)&&__defNormalProp(p,t,e[t]);if(__getOwnPropSymbols)for(var t of __getOwnPropSymbols(e))__propIsEnum.call(e,t)&&__defNormalProp(p,t,e[t]);return p};class PolygonUtils{static getHoverPolygonID(e,t,n=3,i=tool.ELineTypes.Line){let o="",r=Infinity;const l=AxisUtils.default.axisArea(e,n);return t.forEach(s=>{s.pointList&&l.forEach(c=>{
|
|
1
|
+
"use strict";var turf=require("@turf/turf"),annotation=require("../../constant/annotation.js"),CommonToolUtils=require("./CommonToolUtils.js"),tool=require("../../constant/tool.js"),AxisUtils=require("./AxisUtils.js"),MathUtils=require("../MathUtils.js"),LineToolUtils=require("./LineToolUtils.js"),__defProp=Object.defineProperty,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(p,e,t)=>e in p?__defProp(p,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):p[e]=t,__spreadValues=(p,e)=>{for(var t in e||(e={}))__hasOwnProp.call(e,t)&&__defNormalProp(p,t,e[t]);if(__getOwnPropSymbols)for(var t of __getOwnPropSymbols(e))__propIsEnum.call(e,t)&&__defNormalProp(p,t,e[t]);return p};class PolygonUtils{static getHoverPolygonID(e,t,n=3,i=tool.ELineTypes.Line){let o="",r=Infinity;const l=AxisUtils.default.axisArea(e,n);return t.forEach(s=>{s.pointList&&l.forEach(c=>{if(this.isInPolygon(c,s.pointList,i)){const u=this.calcPolygonSize(s.pointList);u<r&&(o=s.id,r=u)}})}),o}static calcPolygonSize(e=[]){if((e==null?void 0:e.length)<=2)return 0;const t=e.length,n=e.reduce((i,o,r,l)=>{const s=l[(r+1)%t];return i+o.x*s.y-s.x*o.y},0);return Math.abs(n)/2}static isInPolygon(e,t,n=tool.ELineTypes.Line){return t=[...t],n===tool.ELineTypes.Curve&&(t=this.createSmoothCurvePoints(t.reduce((i,o)=>[...i,o.x,o.y],[]),.5,!0,20)),this.isPointInOrOnPolygon(e,t)}static isPointInOrOnPolygon(e,t,n=1e-9){const{x:i,y:o}=e;let r=!1;for(let l=0,s=t.length-1;l<t.length;s=l++){const{x:c,y:u}=t[l],{x:h,y:d}=t[s],y=i<=Math.max(c,h)&&i>=Math.min(c,h)&&o<=Math.max(u,d)&&o>=Math.min(u,d),P=(o-u)*(h-c)-(i-c)*(d-u);if(y&&Math.abs(P)<n)return!0;u>o!=d>o&&i<(h-c)*(o-u)/(d-u)+c&&(r=!r)}return r}static createSmoothCurvePointsFromPointList(e,t=tool.SEGMENT_NUMBER){return this.createSmoothCurvePoints(e.reduce((i,o)=>[...i,o.x,o.y],[]),.5,!1,t).map((i,o)=>{var r;const l=o/(tool.SEGMENT_NUMBER+1),s=Math.floor(l),c=(r=e[s])!=null?r:{};return __spreadValues(s===l?__spreadValues({},c):{specialEdge:c.specialEdge},i)})}static createSmoothCurvePoints(e,t=.5,n=!1,i=tool.SEGMENT_NUMBER){if(e.length<4)return e;const o=[],r=e.slice(0);let l,s,c,u,h,d,y,P,x,m,a,g,f;for(n?(r.unshift(e[e.length-1]),r.unshift(e[e.length-2]),r.unshift(e[e.length-1]),r.unshift(e[e.length-2]),r.push(e[0]),r.push(e[1])):(r.unshift(e[1]),r.unshift(e[0]),r.push(e[e.length-2]),r.push(e[e.length-1])),f=2;f<r.length-4;f+=2)for(c=(r[f+2]-r[f-2])*t,u=(r[f+4]-r[f-0])*t,h=(r[f+3]-r[f-1])*t,d=(r[f+5]-r[f+1])*t,g=0;g<=i;g++)a=g/i,y=2*Math.pow(a,3)-3*Math.pow(a,2)+1,P=-(2*Math.pow(a,3))+3*Math.pow(a,2),x=Math.pow(a,3)-2*Math.pow(a,2)+a,m=Math.pow(a,3)-Math.pow(a,2),l=y*r[f]+P*r[f+2]+x*c+m*u,s=y*r[f+1]+P*r[f+3]+x*h+m*d,o.push(l),o.push(s);const v=[];for(let E=0;E<o.length-1;E+=2)v.push({x:o[E],y:o[E+1]});if(n)for(let E=0;E<i+1;E++){const L=v.shift();v.push(L)}return v}static getPolygonByID(e,t){return e.find(n=>n.id===t)}static getPolygonByIDs(e,t){return t&&(t==null?void 0:t.length)>0?e.filter(n=>t.includes(n.id)):[]}static getHoverEdgeIndex(e,t,n=tool.ELineTypes.Line,i=5){let o=[...t];n===tool.ELineTypes.Curve?o=this.createSmoothCurvePoints(t.reduce((s,c)=>[...s,c.x,c.y],[]),.5,!0,tool.SEGMENT_NUMBER):n===tool.ELineTypes.Line&&o.push(o[0]);let r=-1,l=i;for(let s=0;s<o.length-1;s++){const{length:c}=MathUtils.default.getFootOfPerpendicular(e,o[s],o[s+1],!0);c<l&&(r=s,l=c)}return r===-1?-1:n===tool.ELineTypes.Curve?Math.floor(r/tool.SEGMENT_NUMBER):r}static getClosestPoint(e,t,n=tool.ELineTypes.Line,i=3,o){var r;let l=!1;const s=(r=o==null?void 0:o.isClose)!=null?r:!0;let c="",u=-1,h=Infinity,d=e;const y=20;let P=!1;return t.forEach(x=>{if(!P&&!!x.pointList)switch(n){case tool.ELineTypes.Line:CommonToolUtils.findAllLine(x.pointList,s).forEach((a,g)=>{if(P)return;let{length:f,footPoint:v}=MathUtils.default.getFootOfPerpendicular(e,a.point1,a.point2);const E=MathUtils.default.getLineLength(a.point1,e),L=MathUtils.default.getLineLength(a.point2,e);E<i*2&&(v=a.point1,f=E,P=!0),L<i*2&&(v=a.point2,f=L,P=!0),f<h&&f<i&&(c=x.id,u=g,h=f,d=v,l=!0)});break;case tool.ELineTypes.Curve:{const m=this.createSmoothCurvePoints(x.pointList.reduce((a,g)=>[...a,g.x,g.y],[]),.5,s,y);for(let a=0;a<m.length-1;a++){const{length:g,footPoint:f}=MathUtils.default.getFootOfPerpendicular(e,m[a],m[a+1]);g<h&&g<i&&(c=x.id,u=Math.floor(a/(y+1)),h=g,d=f,l=!0)}}break}}),{dropFoot:d,closestEdgeIndex:u,closestPolygonID:c,hasClosed:l}}static isPointListInPolygon(e,t,n=tool.ELineTypes.Line){return e.every(i=>this.isInPolygon(i,t,n))}static isPointListOutSidePolygon(e,t,n=tool.ELineTypes.Line){return e.some(i=>!this.isInPolygon(i,t,n))}static getPolygonArea(e){let t=0;for(let n=0,i=e.length;n<i;n++){const o=e[n].x,r=e[n===e.length-1?0:n+1].y,l=e[n===e.length-1?0:n+1].x,s=e[n].y;t+=o*r*.5,t-=l*s*.5}return Math.abs(t)}static updatePolygonByRotate(e,t=1,n){let i=1;return e===annotation.ERotateDirection.Anticlockwise&&(i=-1),i*=t,MathUtils.default.rotateRectPointList(i,n)}static deletePolygonLastPoint(e,t,n,i){return n===i.length-1?e:[...e,{x:t[0],y:t[1]}]}static concatBeginAndEnd(e){return e.length<1?e:[...e,e[0]]}static segmentPolygonByPolygon(e,t){var n,i;try{let o=turf.polygon([[...PolygonUtils.concatBeginAndEnd(e.map(l=>[l.x,l.y]))]]);return t.forEach(l=>{const s=turf.polygon([[...PolygonUtils.concatBeginAndEnd(l.pointList.map(u=>[u.x,u.y]))]]),c=turf.difference(o,s);c&&(o=c)}),((i=(n=o==null?void 0:o.geometry)==null?void 0:n.coordinates.map(l=>{var s;return((s=o==null?void 0:o.geometry)==null?void 0:s.type)==="MultiPolygon"?l[0].reduce(PolygonUtils.deletePolygonLastPoint,[]):l.reduce(PolygonUtils.deletePolygonLastPoint,[])}))!=null?i:[]).reduce((l,s)=>{const c=s.length,u=s.filter((h,d)=>{const y=(d+1)%c;return!AxisUtils.default.getIsInScope(h,s[y],1)});return u.length<3?l:[...l,u]},[])}catch(o){console.error(o)}}static getPolygonPointList(e,t){const n=t.find(i=>i.id===e);return n&&n.pointList&&n.pointList.length>0?n.pointList:[]}static getWrapPolygonIndex(e,t){return t.findIndex(n=>PolygonUtils.isPointListInPolygon(e,n.pointList))}static clipPolygonFromWrapPolygon(e,t){const n=PolygonUtils.isPolygonClosewise(t),i=PolygonUtils.isPolygonClosewise(e),o=PolygonUtils.getClosePointDistanceFromPolygon(e[0],t),r=t[o];let l=[...t.slice(0,o),r,...e,e[0],...t.slice(o,t.length)];return n===i&&(l=[...t.slice(0,o),r,e[0],...e.reverse(),...t.slice(o,t.length)]),l}static isPolygonClosewise(e){const t=e.length;let n,i,o,r=0,l;if(t<3)return 0;for(n=0;n<t;n++)i=(n+1)%t,o=(n+2)%t,l=(e[i].x-e[n].x)*(e[o].y-e[i].y),l-=(e[i].y-e[n].y)*(e[o].x-e[i].x),l<0?r--:l>0&&r++;return r>0?1:r<0?-1:0}static getClosePointDistanceFromPolygon(e,t){let n=Number.MAX_SAFE_INTEGER,i=-1;return t.forEach((o,r)=>{const l=LineToolUtils.default.calcDistance(e,o);l<n&&(n=l,i=r)}),i}static combinePolygonWithPolygon(e,t){var n,i;try{const o=turf.polygon([[...PolygonUtils.concatBeginAndEnd(e.pointList.map(u=>[u.x,u.y]))]]),r=turf.polygon([[...PolygonUtils.concatBeginAndEnd(t.pointList.map(u=>[u.x,u.y]))]]),l=turf.union(o,r),s=[],c=e;if(((i=(n=l==null?void 0:l.geometry)==null?void 0:n.coordinates)==null?void 0:i.length)===1){s.push(t.id);const u=l==null?void 0:l.geometry.coordinates.map(h=>{var d;return((d=l==null?void 0:l.geometry)==null?void 0:d.type)==="MultiPolygon"?h[0].reduce(PolygonUtils.deletePolygonLastPoint,[]):h.reduce(PolygonUtils.deletePolygonLastPoint,[])})[0];c.pointList=u}return{newPolygon:c,unionList:s}}catch(o){console.error(o)}}static composePointList(e,t){let n=0,i=0,o=Number.MAX_VALUE;return e.forEach((r,l)=>{t.forEach((s,c)=>{const u=LineToolUtils.default.calcDistance(r,s);u<o&&(n=l+1,i=c+1,o=u)})}),[...e.slice(0,n),...t.slice(i,t.length),...t.slice(0,i),...e.slice(n,e.length)]}static composeSegmentPolygonList(e){var t,n;const i=[];for(let o=0;o<e.length;o++)for(let r=0;r<e.length;r++){const l=(t=e[o])==null?void 0:t.pointList,s=(n=e[r])==null?void 0:n.pointList;o!==r&&l&&s&&PolygonUtils.isPointListInPolygon(s,l)&&(e[o].pointList=PolygonUtils.composePointList(l,s),i.unshift(r))}return i.forEach(o=>{e.splice(o,1)}),e}static computeConvexHull(e){e.sort((o,r)=>o.x!==r.x?o.x-r.x:o.y-r.y);const t=(o,r,l)=>(r.x-o.x)*(l.y-o.y)-(r.y-o.y)*(l.x-o.x),n=[];for(const o of e){for(;n.length>=2&&t(n[n.length-2],n[n.length-1],o)<=0;)n.pop();n.push(o)}const i=[];for(let o=e.length-1;o>=0;o--){const r=e[o];for(;i.length>=2&&t(i[i.length-2],i[i.length-1],r)<=0;)i.pop();i.push(r)}return i.pop(),n.pop(),n.concat(i)}static createConvexHullGroup(e){const t={};return e.forEach(n=>{n.type==="line"&&(t[n.annotation.id]||(t[n.annotation.id]={points:[],convexHull:[]}),t[n.annotation.id].points.push(...n.annotation.pointList))}),Object.keys(t).forEach(n=>{t[n].convexHull=this.computeConvexHull(t[n].points)}),t}}module.exports=PolygonUtils;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var tool=require("../../constant/tool.js"),__defProp=Object.defineProperty,__defProps=Object.defineProperties,__getOwnPropDescs=Object.getOwnPropertyDescriptors,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(e,t,
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var tool=require("../../constant/tool.js"),__defProp=Object.defineProperty,__defProps=Object.defineProperties,__getOwnPropDescs=Object.getOwnPropertyDescriptors,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(e,t,a)=>t in e?__defProp(e,t,{enumerable:!0,configurable:!0,writable:!0,value:a}):e[t]=a,__spreadValues=(e,t)=>{for(var a in t||(t={}))__hasOwnProp.call(t,a)&&__defNormalProp(e,a,t[a]);if(__getOwnPropSymbols)for(var a of __getOwnPropSymbols(t))__propIsEnum.call(t,a)&&__defNormalProp(e,a,t[a]);return e},__spreadProps=(e,t)=>__defProps(e,__getOwnPropDescs(t));function createSmoothCurvePoints(e,t=.5,a=!1,h=16){if(e.length<4)return e;const l=[],r=e.slice(0);let m,_,M,c,u,o,i,p,y,x,n,v,f;for(a?(r.unshift(e[e.length-1]),r.unshift(e[e.length-2]),r.unshift(e[e.length-1]),r.unshift(e[e.length-2]),r.push(e[0]),r.push(e[1])):(r.unshift(e[1]),r.unshift(e[0]),r.push(e[e.length-2]),r.push(e[e.length-1])),f=2;f<r.length-4;f+=2)for(M=(r[f+2]-r[f-2])*t,c=(r[f+4]-r[f-0])*t,u=(r[f+3]-r[f-1])*t,o=(r[f+5]-r[f+1])*t,v=0;v<=h;v++)n=v/h,i=2*Math.pow(n,3)-3*Math.pow(n,2)+1,p=-(2*Math.pow(n,3))+3*Math.pow(n,2),y=Math.pow(n,3)-2*Math.pow(n,2)+n,x=Math.pow(n,3)-Math.pow(n,2),m=i*r[f]+p*r[f+2]+y*M+x*c,_=i*r[f+1]+p*r[f+3]+y*u+x*o,l.push(m),l.push(_);const w=[];for(let s=0;s<l.length-1;s+=2)w.push({x:l[s],y:l[s+1]});if(a)for(let s=0;s<h+1;s++){const g=w.shift();w.push(g)}return w}const createSmoothCurvePointsFromPointList=(e,t=16)=>createSmoothCurvePoints(e.reduce((a,h)=>[...a,h.x,h.y],[]),.5,!1,t);function isInPolygon(e,t,a=tool.ELineTypes.Line){let h=0,l,r;a===tool.ELineTypes.Curve&&(t=createSmoothCurvePoints(t.flatMap(u=>[u.x,u.y]),.5,!0,tool.SEGMENT_NUMBER));const[m,_,M,c]=t.reduce(([u,o,i,p],{x:y,y:x})=>[Math.min(u,y),Math.max(o,y),Math.min(i,x),Math.max(p,x)],[Infinity,-Infinity,Infinity,-Infinity]);if(e.x<m||e.x>_||e.y<M||e.y>c)return!1;l=t[t.length-1];for(let u=0;u<t.length;u++){if(r=t[u],e.y>Math.min(l.y,r.y)&&e.y<=Math.max(l.y,r.y)&&e.x<=Math.max(l.x,r.x)){const o=l.y!==r.y?(e.y-l.y)*(r.x-l.x)/(r.y-l.y)+l.x:l.x;e.x<=o&&h++}if(l=r,h%2!=0&&u>t.length/2)break}return h%2!=0}function getPolygonPointUnderZoom(e,t=1){return e.map(a=>__spreadProps(__spreadValues({},a),{x:a.x*t,y:a.y*t}))}exports.createSmoothCurvePoints=createSmoothCurvePoints,exports.createSmoothCurvePointsFromPointList=createSmoothCurvePointsFromPointList,exports.getPolygonPointUnderZoom=getPolygonPointUnderZoom,exports.isInPolygon=isInPolygon;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{i18n as R,ImgPosUtils as L,toolStyleConverter as k}from"@labelbee/lb-utils";import{isNumber as E}from"lodash";import{EOperationMode as w,EToolName as y}from"../../constant/tool.js";import B from"../../utils/MathUtils.js";import x,{CoordinateUtils as G}from"../../utils/tool/AxisUtils.js";import F from"../../utils/tool/CanvasUtils.js";import _ from"../../utils/tool/CommonToolUtils.js";import H from"../../utils/tool/LineToolUtils.js";import{ELang as z,EGrowthMode as V,EDragStatus as Y}from"../../constant/annotation.js";import P from"../../constant/keyCode.js";import{styleString as X,BASE_ICON as $}from"../../constant/style.js";import U from"../../locales/index.js";import{EMessage as A}from"../../locales/constants.js";import J from"../../utils/ActionsHistory.js";import Z from"../../utils/tool/AttributeUtils.js";import q from"../../utils/tool/DblClickEventListener.js";import v from"../../utils/tool/DrawUtils.js";import Q from"../../utils/tool/RenderDomUtils.js";import O from"../../utils/tool/ZoomUtils.js";import tt from"./eventListener.js";var it=Object.defineProperty,et=Object.defineProperties,st=Object.getOwnPropertyDescriptors,N=Object.getOwnPropertySymbols,ot=Object.prototype.hasOwnProperty,nt=Object.prototype.propertyIsEnumerable,j=(u,t,i)=>t in u?it(u,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):u[t]=i,D=(u,t)=>{for(var i in t||(t={}))ot.call(t,i)&&j(u,i,t[i]);if(N)for(var i of N(t))nt.call(t,i)&&j(u,i,t[i]);return u},T=(u,t)=>et(u,st(t)),rt=(u,t,i)=>new Promise((e,o)=>{var n=r=>{try{a(i.next(r))}catch(l){o(l)}},c=r=>{try{a(i.throw(r))}catch(l){o(l)}},a=r=>r.done?e(r.value):Promise.resolve(r.value).then(n,c);a((i=i.apply(u,t)).next())});const K={[z.Zh]:"cn",[z.US]:"en"},at={min:.2,max:1e3,ratio:.4};class ht extends tt{constructor(t){super();this.isDrag=!1,this.isSpaceKey=!1,this.staticMode=!1,this.operationMode=w.General,this.innerZoom=1,this.basicZoom=.01,this.isSpaceClick=!1,this.isDragStart=!1,this.startTime=0,this.zoomInfo=at,this.initImgPos=()=>rt(this,null,function*(){var d,h;if(!this.imgNode)return;const s=(d=this._imgAttribute)==null?void 0:d.zoomRatio,m=(h=this._imgAttribute)==null?void 0:h.isOriginalSize,{currentPos:g,imgInfo:f,zoom:C}=L.getInitImgPos(this.size,{width:this.imgNode.width,height:this.imgNode.height},this.rotate,s,m);this.setCurrentPos(g),this.currentPosStorage=g,this.setImgInfo(f),this.setZoom(C),this.render(),this.renderBasicCanvas(),this.emit("dependRender"),this.emit("renderZoom",C,g,f)}),this.getCurrentPos=d=>{const{_firstClickCoordinate:h,currentPosStorage:s}=this;try{let m;return h&&s?m={y:s.y+d.y-h.y,x:s.x+d.x-h.x}:m={x:0,y:0},m}catch(m){return console.error(m),{x:0,y:0}}},this.wheelChangePos=(d,h,s)=>{const{currentPos:m,imgNode:g}=this;if(!g){console.error("unable to load image");return}if(this.zoom===this.basicZoom&&h===-1)return;const f=O.wheelChangePos(g,d,h,m,{zoom:s||this.zoom,innerZoom:this.innerZoom,basicZoom:this.basicZoom,zoomMax:this.zoomInfo.max,rotate:this.rotate});if(!f)return;const{currentPos:C,ratio:W,zoom:S,imgInfo:M}=f;this.setZoom(S),this.setCurrentPos(C),this.currentPosStorage=C,this.setImgInfo(M),this.zoomInfo.ratio=W,this.emit("renderZoom",S,C,M)},this.zoomChanged=(d,h=V.Linear)=>{const s=O.zoomChanged(this.zoom,d,h);this.wheelChangePos(this.getGetCenterCoordinate(),s>this.zoom?1:-1,s),this.render(),this.renderBasicCanvas()},this.zoomChangeOnCenter=d=>{this.wheelChangePos(this.getGetCenterCoordinate(),0,d),this.render(),this.renderBasicCanvas()},this.drawStraightLine=(d,h)=>{const{ctx:s}=this;s&&(s.save(),s.lineCap="round",s.lineJoin="round",s.strokeStyle=h.color,s.lineWidth=h.lineWidth,s.globalAlpha=h.globalAlpha,d.forEach((m,g)=>{if(s.beginPath(),g>0){const f=d[g-1];s.save(),s.moveTo(f.x,f.y),s.lineTo(m.x,m.y),s.stroke(),s.restore()}}),s.restore())},this.drawImg=()=>{!this.imgNode||this.hiddenImg===!0||(v.drawImg(this.basicCanvas,this.imgNode,{zoom:this.zoom,currentPos:this.currentPos,rotate:this.rotate,imgAttribute:this._imgAttribute}),this.drawStaticImg())},this.drawStaticImg=()=>{!this.staticImgNode||!this.staticMode||(this.clearCanvas(),v.drawImg(this.canvas,this.staticImgNode,{zoom:this.zoom,currentPos:this.currentPos,rotate:this.rotate,imgAttribute:this._imgAttribute}))};var i,e,o,n,c,a,r,l,b,I,p;this.container=t.container,this.config=_.jsonParser(t.config),this.showDefaultCursor=t.showDefaultCursor||!1,this.destroyCanvas(),this.createCanvas(t.size,t.isAppend),this.imgNode=t.imgNode,this.staticMode=(i=t.staticMode)!=null?i:!1,this.isImgError=!t.imgNode,this.basicImgInfo={width:(o=(e=t.imgNode)==null?void 0:e.width)!=null?o:0,height:(c=(n=t.imgNode)==null?void 0:n.height)!=null?c:0,valid:!0,rotate:0},this.forbidOperation=(a=t.forbidOperation)!=null?a:!1,this.forbidBasicResultRender=(r=t.forbidBasicResultRender)!=null?r:!1,this.size=t.size,this.currentPos={x:0,y:0},this.zoom=1,this.coord={x:-1,y:-1},this.currentPosStorage={x:0,y:0},this.isShowCursor=!1,this.attributeLockList=[],this.history=new J,this.style=(l=t.style)!=null?l:_.jsonParser(X),this._imgAttribute=(b=t.imgAttribute)!=null?b:{},this.isHidden=!1,this.dragStatus=Y.Wait,this.defaultAttribute=(I=t==null?void 0:t.defaultAttribute)!=null?I:"",this.forbidCursorLine=!!t.forbidCursorLine,this.lang=(p=t==null?void 0:t.language)!=null?p:z.Zh,R.changeLanguage(K[this.lang]),this.onMouseDown=this.onMouseDown.bind(this),this.onMouseMove=this.onMouseMove.bind(this),this.onMouseLeave=this.onMouseLeave.bind(this),this.onMouseUp=this.onMouseUp.bind(this),this.onKeyDown=this.onKeyDown.bind(this),this.onKeyUp=this.onKeyUp.bind(this),this.onWheel=this.onWheel.bind(this),this.onLeftDblClick=this.onLeftDblClick.bind(this),this.onRightDblClick=this.onRightDblClick.bind(this),this.onClick=this.onClick.bind(this),this.onRightClick=this.onRightClick.bind(this),this.clearImgDrag=this.clearImgDrag.bind(this),this.dblClickListener=new q(this.container,200),this.coordUtils=new G(this),this.coordUtils.setBasicImgInfo(this.basicImgInfo),this.hiddenImg=t.hiddenImg||!1,t.zoomInfo&&(this.zoomInfo=t.zoomInfo)}onContextmenu(t){t.preventDefault()}get ctx(){var t;return this._ctx||((t=this.canvas)==null?void 0:t.getContext("2d"))}get basicCtx(){var t;return(t=this.basicCanvas)==null?void 0:t.getContext("2d")}get rotate(){var t,i;return(i=(t=this.basicImgInfo)==null?void 0:t.rotate)!=null?i:0}get valid(){var t,i;return(i=(t=this.basicImgInfo)==null?void 0:t.valid)!=null?i:!0}get baseIcon(){return $[this.style.color]}get defaultCursor(){return this.showDefaultCursor?"default":"none"}get dataList(){return[]}get innerPosAndZoom(){return{innerZoom:this.innerZoom,currentPosStorage:this.currentPosStorage}}get isShowDefaultCursor(){return this.showDefaultCursor}get isMultiMoveMode(){return this.operationMode===w.MultiMove}get hasMarkerConfig(){return this.config.markerConfigurable===!0&&this.config.markerList&&this.config.markerList.length>0}setZoom(t){this.zoom=t,this.innerZoom=t,this.coordUtils.setZoomAndCurrentPos(this.zoom,this.currentPos)}setCurrentPos(t){this.currentPos=t,this.coordUtils.setZoomAndCurrentPos(this.zoom,this.currentPos)}setReferenceData(t){this.referenceData=t}setImgInfo(t){this.imgInfo=t}setCurrentPosStorage(t){this.currentPosStorage=t}setOperationMode(t){this.operationMode=t}recoverOperationMode(){this.operationMode===w.MultiMove&&this.setOperationMode(w.General)}updatePosition(t){const{zoom:i,currentPos:e}=t;this.setZoom(i),this.setCurrentPos(e),this.currentPosStorage=e,this.renderBasicCanvas(),this.render()}setLang(t){this.lang=t;const i=K[t];R.changeLanguage(i)}setShowDefaultCursor(t){this.showDefaultCursor=t,this.container.style.cursor=this.defaultCursor}setCustomCursor(t){this.container.style.cursor=t}get forbidMouseOperation(){return this.forbidOperation||this.valid===!1}get pixelRatio(){var t;return F.getPixelRatio((t=this.canvas)==null?void 0:t.getContext("2d"))}init(){this.eventUnbinding(),this.initPosition(),this.eventBinding(),this.render(),this.renderBasicCanvas()}destroy(){this.destroyCanvas(),this.eventUnbinding()}updateCanvasBasicStyle(t,i,e){const o=this.pixelRatio;t.style.position="absolute",t.width=i.width*o,t.height=i.height*o,t.style.width=`${i.width}px`,t.style.height=`${i.height}px`,t.style.left="0",t.style.top="0",t.style.zIndex=`${e} `}createCanvas(t,i=!0){var e,o;const n=this.pixelRatio,c=document.createElement("canvas");this.updateCanvasBasicStyle(c,t,0),this.basicCanvas=c;const a=document.createElement("canvas");this.updateCanvasBasicStyle(a,t,10),i&&(this.container.hasChildNodes()?(this.container.insertBefore(a,this.container.childNodes[0]),this.container.insertBefore(c,this.container.childNodes[0])):(this.container.appendChild(c),this.container.appendChild(a))),this.canvas=a,this.container.style.cursor=this.defaultCursor,(e=this.ctx)==null||e.scale(n,n),(o=this.basicCtx)==null||o.scale(n,n),this.ctx&&(this.ctx.imageSmoothingEnabled=!1)}destroyCanvas(){this.canvas&&this.container.contains(this.canvas)&&this.container.removeChild(this.canvas),this.basicCanvas&&this.container.contains(this.basicCanvas)&&this.container.removeChild(this.basicCanvas),this.clearInvalidPage(),this.clearImgDrag()}setStyle(t){this.style=t,this.render()}setImgNode(t,i={}){this.imgNode=t,this.setBasicImgInfo(D({width:t.width,height:t.height,valid:!0,rotate:0},i)),this.updateZoomInfo(),this.isImgError===!0&&(this.isImgError=!1,this.emit("changeAnnotationShow")),typeof i.valid=="boolean"&&this.setValid(i.valid),this.initImgPos(),this.render(),this.renderBasicCanvas()}updateZoomInfo(t=this.imgNode,i=this.size){var e;if(!t||!i)return;const{min:o}=L.getMinZoomByImgAndSize({canvasSize:i,imgSize:{width:t.width,height:t.height},rotate:this.rotate,zoomRatio:(e=this._imgAttribute)==null?void 0:e.zoomRatio});this.zoomInfo=T(D({},this.zoomInfo),{min:o})}setErrorImg(){const t=this.isImgError;this.isImgError=!0,this.imgNode=void 0,this.setBasicImgInfo({width:0,height:0,valid:!0,rotate:0}),t===!1&&this.emit("changeAnnotationShow")}setBasicImgInfo(t){this.basicImgInfo=t,this.coordUtils.setBasicImgInfo(t)}setForbidOperation(t){this.forbidOperation=t,this.setShowDefaultCursor(t),this.render()}setForbidCursorLine(t){this.forbidCursorLine=t,this.render()}setIsHidden(t){this.isHidden=t,this.emit("hiddenChange")}setDefaultAttribute(t){this.defaultAttribute=t}getCoordinateInOrigin(t){const i=this.canvas.getBoundingClientRect();return{x:(t.clientX-i.left-this.currentPos.x)/this.zoom,y:(t.clientY-i.top-this.currentPos.y)/this.zoom}}getTextIconSvg(t=""){var i;return Z.getTextIconSvg(t,(i=this.config)==null?void 0:i.attributeList,this.config.attributeConfigurable,this.baseIcon)}setIsShowOrder(t){this.config.isShowOrder=t,this.render()}getCoordinate(t){const i=this.canvas.getBoundingClientRect();return{x:t.clientX-i.left,y:t.clientY-i.top}}getCoordinateUnderZoom(t){const i=this.canvas.getBoundingClientRect();return{x:t.clientX-i.left-this.currentPos.x,y:t.clientY-i.top-this.currentPos.y}}getCoordinateUnderZoomByRotate(t){const{x:i,y:e}=this.getCoordinateUnderZoom(t);return this.basicImgInfo.rotate===90?{x:e,y:this.basicImgInfo.height*this.zoom-i}:this.basicImgInfo.rotate===180?{x:this.basicImgInfo.width*this.zoom-i,y:this.basicImgInfo.height*this.zoom-e}:this.basicImgInfo.rotate===270?{x:this.basicImgInfo.width*this.zoom-e,y:i}:{x:i,y:e}}getCoordinateUnderZoomByRotateFromImgPoint(t){const{x:i,y:e}=t;return this.basicImgInfo.rotate===90?{x:(this.basicImgInfo.height-e)*this.zoom+this.currentPos.x,y:i*this.zoom+this.currentPos.y}:this.basicImgInfo.rotate===180?{x:(this.basicImgInfo.width-i)*this.zoom+this.currentPos.x,y:(this.basicImgInfo.height-e)*this.zoom+this.currentPos.y}:this.basicImgInfo.rotate===270?{x:e*this.zoom+this.currentPos.x,y:(this.basicImgInfo.width-i)*this.zoom+this.currentPos.y}:{x:i*this.zoom+this.currentPos.x,y:e*this.zoom+this.currentPos.y}}getGetCenterCoordinate(){return{x:this.size.width/2,y:this.size.height/2}}initPosition(){if(this.basicResult&&this.imgInfo){const{basicResult:t,size:i,imgNode:e,_imgAttribute:o,imgInfo:n,dependToolName:c}=this;if(t&&e&&c){let a=t;switch(c){case y.Polygon:case y.Line:{if(t.pointList){const l=B.calcViewportBoundaries(t.pointList);a={x:l.left,y:l.top,width:l.right-l.left,height:l.bottom-l.top}}break}}const r=L.getBasicRecPos(e,a,i,void 0,o==null?void 0:o.zoomRatio,o==null?void 0:o.isOriginalSize);r&&(this.setCurrentPos(r.currentPos),this.currentPosStorage=this.currentPos,this.setImgInfo(T(D({},n),{width:n.width/this.innerZoom*r.innerZoom,height:n.height/this.innerZoom*r.innerZoom})),this.setZoom(r.innerZoom),this.render(),this.renderBasicCanvas())}}else this.initImgPos()}undo(){this.history.undo()}redo(){this.history.redo()}clearCanvas(){var t;(t=this.ctx)==null||t.clearRect(0,0,this.size.width,this.size.height)}clearBasicCanvas(){var t;(t=this.basicCtx)==null||t.clearRect(0,0,this.size.width,this.size.height)}eventBinding(){this.dblClickListener.addEvent(()=>{},this.onLeftDblClick,this.onRightDblClick),this.container.addEventListener("mousedown",this.onMouseDown),this.container.addEventListener("mousemove",this.onMouseMove),this.container.addEventListener("mouseup",this.onMouseUp),this.container.addEventListener("mouseleave",this.onMouseLeave),this.container.addEventListener("click",this.onClick),this.container.addEventListener("wheel",this.onWheel),document.addEventListener("keydown",this.onKeyDown),document.addEventListener("keyup",this.onKeyUp),window.parent.document.addEventListener("contextmenu",this.onContextmenu,!1)}eventUnbinding(){this.container.removeEventListener("mousedown",this.onMouseDown),this.container.removeEventListener("mousemove",this.onMouseMove),this.container.removeEventListener("mouseup",this.onMouseUp),this.container.removeEventListener("mouseleave",this.onMouseLeave),this.container.removeEventListener("wheel",this.onWheel),this.container.removeEventListener("click",this.onClick),document.removeEventListener("keydown",this.onKeyDown),document.removeEventListener("keyup",this.onKeyUp),window.parent.document.removeEventListener("contextmenu",this.onContextmenu,!1),this.dblClickListener.removeEvent()}clearImgDrag(){this.isDrag=!1,this.isDragStart=!1,this.isSpaceClick=!1,this.startTime=0,this.container.style.cursor=this.defaultCursor,this.forbidCursorLine=!1}clearCursorLine(){this.coord={x:-1,y:-1}}onMouseDown(t){if(!this.canvas||this.isImgError)return!0;const i=this.getCoordinate(t);(this.isSpaceKey&&t.button===0||t.button===2)&&(t.stopPropagation(),this._firstClickCoordinate=i,this.currentPosStorage=this.currentPos,this.isSpaceClick=!0,this.isDragStart=!0,this.startTime=new Date().getTime())}onMouseMove(t){if(!this.canvas||this.isImgError)return!0;const i=this.getCoordinate(t);this.isShowCursor&&(this.coord=i);try{if(!i||!E(i==null?void 0:i.x)||!E(i==null?void 0:i.y))throw new Error("coord error");if(this.coord=i,(this.isSpaceClick||this.isDragStart)&&this._firstClickCoordinate){const e=this.getCurrentPos(i);this.setCurrentPos(e),this.isDrag=!0,this.container.style.cursor="grabbing",this.forbidCursorLine=!0,this.renderBasicCanvas(),this.emit("dependRender"),this.emit("dragMove",{currentPos:e,zoom:this.zoom,imgInfo:this.imgInfo})}this.render()}catch(e){console.error(e)}}onMouseUp(t){if(!this.canvas||this.isImgError)return!0;if(this.container.style.cursor=this.defaultCursor,this.forbidCursorLine=!1,this.isDrag=!1,this.isDragStart=!1,this.isSpaceClick=!1,this.startTime!==0&&this._firstClickCoordinate){const i=new Date().getTime(),e=this.getCoordinate(t);if(i-this.startTime>1e3||this.isSpaceKey===!0||H.calcTwoPointDistance(e,this._firstClickCoordinate)>10)return t.stopPropagation(),this.startTime=0,this.render(),!0}this.startTime=0,t.button===2&&this.onRightClick(t),this.render()}onMouseLeave(){this.clearImgDrag()}onClick(t){}onRightClick(t){}onLeftDblClick(t){}onRightDblClick(t){this.clearImgDrag()}onKeyDown(t){switch((t.keyCode===P.Alt||t.keyCode===P.A)&&t.preventDefault(),t.keyCode){case P.Space:this.isSpaceKey=!0,t.preventDefault();break;case P.Z:if(t.ctrlKey)return t.shiftKey?this.redo():this.undo(),!1;break}return!0}onKeyUp(t){switch(t.keyCode){case P.Space:this.isSpaceKey=!1;break}}exportCustomData(){return{}}onWheel(t,i=!0){if(!this.imgNode||!this.coord)return;t.preventDefault(),t.stopPropagation();const e=this.getCoordinate(t),o=t.deltaY||t.wheelDelta;let n=0;o>0&&this.zoom>this.zoomInfo.min&&(n=-1),o<0&&this.zoom<this.zoomInfo.max&&(n=1),this.wheelChangePos(e,n),this.emit("dependRender"),i&&this.render(),this.renderBasicCanvas()}renderCursorLine(t=(i=>(i=this.style.lineColor[0])!=null?i:"")()){if(!this.ctx||this.forbidCursorLine||this.forbidOperation)return;const{x:i,y:e}=this.coord;v.drawLine(this.canvas,{x:0,y:e},{x:1e4,y:e},{color:t}),v.drawLine(this.canvas,{x:i,y:0},{x:i,y:1e4},{color:t}),v.drawCircleWithFill(this.canvas,{x:i,y:e},1,{color:"white"})}setSize(t){var i;this.size=t,this.updateZoomInfo(),this.container.contains(this.canvas)&&(this.destroyCanvas(),this.createCanvas(t),this.eventUnbinding(),this.init(),((i=this.basicImgInfo)==null?void 0:i.valid)===!1&&this.renderInvalidPage())}setImgAttribute(t){const i=this._imgAttribute;if(this._imgAttribute=t,(i==null?void 0:i.zoomRatio)!==t.zoomRatio||i.isOriginalSize!==t.isOriginalSize){this.initImgPos();return}this.renderBasicCanvas(),this.render()}clearResult(t){}setValid(t){this.basicImgInfo.valid=t,t===!1?(this.renderInvalidPage(),this.clearResult(!1)):this.clearInvalidPage()}setRotate(t){this.basicImgInfo.rotate=t}setBasicResult(t){this.basicResult=t,this.coordUtils.setBasicResult(t),this.initPosition(),this.emit("dependRender")}setDependName(t,i){this.dependToolName=t,this.coordUtils.setDependInfo(t,i)}filterCacheContext(){}setAttributeLockList(t){this.attributeLockList=t,this.filterCacheContext(),this.render()}setConfig(t){this.config=_.jsonParser(t)}setDataInjectionAtCreation(t){this.dataInjectionAtCreation=t}setRenderEnhance(t){this.renderEnhance=t}setCustomRenderStyle(t){this.customRenderStyle=t}updateRotate(){if(this.dependToolName)return this.emit("messageInfo",U.getMessagesByLocale(A.NoRotateInDependence,this.lang)),!1;if(this.dataList.length>0)return this.emit("messageInfo",U.getMessagesByLocale(A.NoRotateNotice,this.lang)),!1;const t=B.getRotate(this.basicImgInfo.rotate);this.basicImgInfo.rotate=t,this.initImgPos(),this.emit("updateResult")}getColor(t="",i=this.config){return k.getColorByConfig({attribute:t,config:i,style:this.style})}getLineColor(t=""){var i,e,o,n,c,a,r,l;if(((i=this.config)==null?void 0:i.attributeConfigurable)===!0){const p=Z.getAttributeIndex(t,(o=(e=this.config)==null?void 0:e.attributeList)!=null?o:[])+1;return((a=(c=(n=this.config)==null?void 0:n.attributeList)==null?void 0:c.find(h=>h.value===t))==null?void 0:a.color)?(l=(r=k.getColorByConfig({attribute:t,config:this.config}))==null?void 0:r.valid)==null?void 0:l.stroke:this.style.attributeLineColor?this.style.attributeLineColor[p]:""}const{color:b,lineColor:I}=this.style;return b&&I?I[b]:""}clearInvalidPage(){this._invalidDOM&&this.container&&this.container.contains(this._invalidDOM)&&(this.container.removeChild(this._invalidDOM),this._invalidDOM=void 0)}renderInvalidPage(){!this.container||this._invalidDOM||(this._invalidDOM=Q.renderInvalidPage(this.container,this.size,this.lang))}renderBasicCanvas(){if(!this.basicCanvas)return;this.clearBasicCanvas(),this.drawImg();const t=3;if(!this.forbidBasicResultRender&&this.basicResult&&this.dependToolName)switch(this.dependToolName){case y.Rect:{v.drawRect(this.basicCanvas,x.changeRectByZoom(this.basicResult,this.zoom,this.currentPos),{color:"rgba(204,204,204,1.00)",thickness:t});break}case y.Polygon:{v.drawPolygonWithFillAndLine(this.basicCanvas,x.changePointListByZoom(this.basicResult.pointList,this.zoom,this.currentPos),{fillColor:"transparent",strokeColor:"rgba(204,204,204,1.00)",isClose:!0,thickness:t});break}case y.Line:{v.drawLineWithPointList(this.basicCanvas,x.changePointListByZoom(this.basicResult.pointList,this.zoom,this.currentPos),{color:"rgba(204,204,204,1.00)",thickness:t});break}}}render(){!this.canvas||!this.ctx||!this.imgNode||this.clearCanvas()}changeStyle(t=this.defaultAttribute){this.emit("changeStyle",{attribute:t})}}export{ht as BasicToolOperation};
|
|
1
|
+
import{i18n as R,ImgPosUtils as L,toolStyleConverter as k}from"@labelbee/lb-utils";import{isNumber as E}from"lodash";import{EOperationMode as w,EToolName as y}from"../../constant/tool.js";import B from"../../utils/MathUtils.js";import x,{CoordinateUtils as G}from"../../utils/tool/AxisUtils.js";import F from"../../utils/tool/CanvasUtils.js";import _ from"../../utils/tool/CommonToolUtils.js";import H from"../../utils/tool/LineToolUtils.js";import{ELang as z,EGrowthMode as V,EDragStatus as Y}from"../../constant/annotation.js";import P from"../../constant/keyCode.js";import{styleString as X,BASE_ICON as $}from"../../constant/style.js";import O from"../../locales/index.js";import{EMessage as U}from"../../locales/constants.js";import J from"../../utils/ActionsHistory.js";import A from"../../utils/tool/AttributeUtils.js";import q from"../../utils/tool/DblClickEventListener.js";import v from"../../utils/tool/DrawUtils.js";import Q from"../../utils/tool/RenderDomUtils.js";import Z from"../../utils/tool/ZoomUtils.js";import tt from"./eventListener.js";var it=Object.defineProperty,et=Object.defineProperties,st=Object.getOwnPropertyDescriptors,N=Object.getOwnPropertySymbols,ot=Object.prototype.hasOwnProperty,nt=Object.prototype.propertyIsEnumerable,j=(u,t,i)=>t in u?it(u,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):u[t]=i,D=(u,t)=>{for(var i in t||(t={}))ot.call(t,i)&&j(u,i,t[i]);if(N)for(var i of N(t))nt.call(t,i)&&j(u,i,t[i]);return u},T=(u,t)=>et(u,st(t)),rt=(u,t,i)=>new Promise((e,s)=>{var a=n=>{try{l(i.next(n))}catch(r){s(r)}},h=n=>{try{l(i.throw(n))}catch(r){s(r)}},l=n=>n.done?e(n.value):Promise.resolve(n.value).then(a,h);l((i=i.apply(u,t)).next())});const K={[z.Zh]:"cn",[z.US]:"en"},at={min:.2,max:1e3,ratio:.4};class ht extends tt{constructor(t){super();this.isDrag=!1,this.isSpaceKey=!1,this.staticMode=!1,this.operationMode=w.General,this.innerZoom=1,this.basicZoom=.01,this.isSpaceClick=!1,this.isDragStart=!1,this.startTime=0,this.zoomInfo=at,this.isOffscreenCanvas=!1,this.initImgPos=()=>rt(this,null,function*(){var d,c;if(!this.imgNode)return;const o=(d=this._imgAttribute)==null?void 0:d.zoomRatio,m=(c=this._imgAttribute)==null?void 0:c.isOriginalSize,{currentPos:g,imgInfo:f,zoom:C}=L.getInitImgPos(this.size,{width:this.imgNode.width,height:this.imgNode.height},this.rotate,o,m);this.setCurrentPos(g),this.currentPosStorage=g,this.setImgInfo(f),this.setZoom(C),this.render(),this.renderBasicCanvas(),this.emit("dependRender"),this.emit("renderZoom",C,g,f)}),this.getCurrentPos=d=>{const{_firstClickCoordinate:c,currentPosStorage:o}=this;try{let m;return c&&o?m={y:o.y+d.y-c.y,x:o.x+d.x-c.x}:m={x:0,y:0},m}catch(m){return console.error(m),{x:0,y:0}}},this.wheelChangePos=(d,c,o)=>{const{currentPos:m,imgNode:g}=this;if(!g){console.error("unable to load image");return}if(this.zoom===this.basicZoom&&c===-1)return;const f=Z.wheelChangePos(g,d,c,m,{zoom:o||this.zoom,innerZoom:this.innerZoom,basicZoom:this.basicZoom,zoomMax:this.zoomInfo.max,rotate:this.rotate});if(!f)return;const{currentPos:C,ratio:W,zoom:S,imgInfo:M}=f;this.setZoom(S),this.setCurrentPos(C),this.currentPosStorage=C,this.setImgInfo(M),this.zoomInfo.ratio=W,this.emit("renderZoom",S,C,M)},this.zoomChanged=(d,c=V.Linear)=>{const o=Z.zoomChanged(this.zoom,d,c);this.wheelChangePos(this.getGetCenterCoordinate(),o>this.zoom?1:-1,o),this.render(),this.renderBasicCanvas()},this.zoomChangeOnCenter=d=>{this.wheelChangePos(this.getGetCenterCoordinate(),0,d),this.render(),this.renderBasicCanvas()},this.drawStraightLine=(d,c)=>{const{ctx:o}=this;o&&(o.save(),o.lineCap="round",o.lineJoin="round",o.strokeStyle=c.color,o.lineWidth=c.lineWidth,o.globalAlpha=c.globalAlpha,d.forEach((m,g)=>{if(o.beginPath(),g>0){const f=d[g-1];o.save(),o.moveTo(f.x,f.y),o.lineTo(m.x,m.y),o.stroke(),o.restore()}}),o.restore())},this.drawImg=()=>{!this.imgNode||this.hiddenImg===!0||(v.drawImg(this.basicCanvas,this.imgNode,{zoom:this.zoom,currentPos:this.currentPos,rotate:this.rotate,imgAttribute:this._imgAttribute}),this.drawStaticImg())},this.drawStaticImg=()=>{!this.staticImgNode||!this.staticMode||(this.clearCanvas(),v.drawImg(this.canvas,this.staticImgNode,{zoom:this.zoom,currentPos:this.currentPos,rotate:this.rotate,imgAttribute:this._imgAttribute}))};var i,e,s,a,h,l,n,r,b,I,p;this.isOffscreenCanvas=!!t.isOffscreenCanvas,this.container=t.container,this.config=_.jsonParser(t.config),this.showDefaultCursor=t.showDefaultCursor||!1,this.destroyCanvas(),this.createCanvas(t.size,t.isAppend),this.imgNode=t.imgNode,this.staticMode=(i=t.staticMode)!=null?i:!1,this.isImgError=!t.imgNode,this.basicImgInfo={width:(s=(e=t.imgNode)==null?void 0:e.width)!=null?s:0,height:(h=(a=t.imgNode)==null?void 0:a.height)!=null?h:0,valid:!0,rotate:0},this.forbidOperation=(l=t.forbidOperation)!=null?l:!1,this.forbidBasicResultRender=(n=t.forbidBasicResultRender)!=null?n:!1,this.size=t.size,this.currentPos={x:0,y:0},this.zoom=1,this.coord={x:-1,y:-1},this.currentPosStorage={x:0,y:0},this.isShowCursor=!1,this.attributeLockList=[],this.history=new J,this.style=(r=t.style)!=null?r:_.jsonParser(X),this._imgAttribute=(b=t.imgAttribute)!=null?b:{},this.isHidden=!1,this.dragStatus=Y.Wait,this.defaultAttribute=(I=t==null?void 0:t.defaultAttribute)!=null?I:"",this.forbidCursorLine=!!t.forbidCursorLine,this.lang=(p=t==null?void 0:t.language)!=null?p:z.Zh,R.changeLanguage(K[this.lang]),this.onMouseDown=this.onMouseDown.bind(this),this.onMouseMove=this.onMouseMove.bind(this),this.onMouseLeave=this.onMouseLeave.bind(this),this.onMouseUp=this.onMouseUp.bind(this),this.onKeyDown=this.onKeyDown.bind(this),this.onKeyUp=this.onKeyUp.bind(this),this.onWheel=this.onWheel.bind(this),this.onLeftDblClick=this.onLeftDblClick.bind(this),this.onRightDblClick=this.onRightDblClick.bind(this),this.onClick=this.onClick.bind(this),this.onRightClick=this.onRightClick.bind(this),this.clearImgDrag=this.clearImgDrag.bind(this),this.dblClickListener=new q(this.container,200),this.coordUtils=new G(this),this.coordUtils.setBasicImgInfo(this.basicImgInfo),this.hiddenImg=t.hiddenImg||!1,t.zoomInfo&&(this.zoomInfo=t.zoomInfo)}onContextmenu(t){t.preventDefault()}get ctx(){var t;return this._ctx||((t=this.canvas)==null?void 0:t.getContext("2d"))}get basicCtx(){var t;return(t=this.basicCanvas)==null?void 0:t.getContext("2d")}get offscreenCtx(){var t;return(t=this.offscreenCanvas)==null?void 0:t.getContext("2d")}get rotate(){var t,i;return(i=(t=this.basicImgInfo)==null?void 0:t.rotate)!=null?i:0}get valid(){var t,i;return(i=(t=this.basicImgInfo)==null?void 0:t.valid)!=null?i:!0}get baseIcon(){return $[this.style.color]}get defaultCursor(){return this.showDefaultCursor?"default":"none"}get dataList(){return[]}get innerPosAndZoom(){return{innerZoom:this.innerZoom,currentPosStorage:this.currentPosStorage}}get isShowDefaultCursor(){return this.showDefaultCursor}get isMultiMoveMode(){return this.operationMode===w.MultiMove}get hasMarkerConfig(){return this.config.markerConfigurable===!0&&this.config.markerList&&this.config.markerList.length>0}setZoom(t){this.zoom=t,this.innerZoom=t,this.coordUtils.setZoomAndCurrentPos(this.zoom,this.currentPos)}setCurrentPos(t){this.currentPos=t,this.coordUtils.setZoomAndCurrentPos(this.zoom,this.currentPos)}setReferenceData(t){this.referenceData=t}setImgInfo(t){this.imgInfo=t}setCurrentPosStorage(t){this.currentPosStorage=t}setOperationMode(t){this.operationMode=t}recoverOperationMode(){this.operationMode===w.MultiMove&&this.setOperationMode(w.General)}updatePosition(t){const{zoom:i,currentPos:e}=t;this.setZoom(i),this.setCurrentPos(e),this.currentPosStorage=e,this.renderBasicCanvas(),this.render()}setLang(t){this.lang=t;const i=K[t];R.changeLanguage(i)}setShowDefaultCursor(t){this.showDefaultCursor=t,this.container.style.cursor=this.defaultCursor}setCustomCursor(t){this.container.style.cursor=t}get forbidMouseOperation(){return this.forbidOperation||this.valid===!1}get pixelRatio(){var t;return F.getPixelRatio((t=this.canvas)==null?void 0:t.getContext("2d"))}init(){this.eventUnbinding(),this.initPosition(),this.eventBinding(),this.render(),this.renderBasicCanvas()}destroy(){this.destroyCanvas(),this.eventUnbinding()}updateCanvasBasicStyle(t,i,e){const s=this.pixelRatio;t.style.position="absolute",t.width=i.width*s,t.height=i.height*s,t.style.width=`${i.width}px`,t.style.height=`${i.height}px`,t.style.left="0",t.style.top="0",t.style.zIndex=`${e} `}createCanvas(t,i=!0){var e,s,a;const h=this.pixelRatio,l=document.createElement("canvas");this.updateCanvasBasicStyle(l,t,0),this.basicCanvas=l;const n=document.createElement("canvas");this.updateCanvasBasicStyle(n,t,10);let r;this.isOffscreenCanvas&&(r=document.createElement("canvas"),this.updateCanvasBasicStyle(r,t,20),this.offscreenCanvas=r),i&&(this.container.hasChildNodes()?(this.isOffscreenCanvas&&r&&this.container.insertBefore(r,this.container.childNodes[0]),this.container.insertBefore(n,this.container.childNodes[0]),this.container.insertBefore(l,this.container.childNodes[0])):(this.container.appendChild(l),this.container.appendChild(n),this.isOffscreenCanvas&&r&&this.container.appendChild(r))),this.canvas=n,this.container.style.cursor=this.defaultCursor,(e=this.ctx)==null||e.scale(h,h),(s=this.basicCtx)==null||s.scale(h,h),(a=this.offscreenCtx)==null||a.scale(h,h),this.ctx&&(this.ctx.imageSmoothingEnabled=!1)}destroyCanvas(){this.canvas&&this.container.contains(this.canvas)&&this.container.removeChild(this.canvas),this.basicCanvas&&this.container.contains(this.basicCanvas)&&this.container.removeChild(this.basicCanvas),this.offscreenCanvas&&this.container.contains(this.offscreenCanvas)&&this.container.removeChild(this.offscreenCanvas),this.clearInvalidPage(),this.clearImgDrag()}setStyle(t){this.style=t,this.render()}setImgNode(t,i={}){this.imgNode=t,this.setBasicImgInfo(D({width:t.width,height:t.height,valid:!0,rotate:0},i)),this.updateZoomInfo(),this.isImgError===!0&&(this.isImgError=!1,this.emit("changeAnnotationShow")),typeof i.valid=="boolean"&&this.setValid(i.valid),this.initImgPos(),this.render(),this.renderBasicCanvas()}updateZoomInfo(t=this.imgNode,i=this.size){var e;if(!t||!i)return;const{min:s}=L.getMinZoomByImgAndSize({canvasSize:i,imgSize:{width:t.width,height:t.height},rotate:this.rotate,zoomRatio:(e=this._imgAttribute)==null?void 0:e.zoomRatio});this.zoomInfo=T(D({},this.zoomInfo),{min:s})}setErrorImg(){const t=this.isImgError;this.isImgError=!0,this.imgNode=void 0,this.setBasicImgInfo({width:0,height:0,valid:!0,rotate:0}),t===!1&&this.emit("changeAnnotationShow")}setBasicImgInfo(t){this.basicImgInfo=t,this.coordUtils.setBasicImgInfo(t)}setForbidOperation(t){this.forbidOperation=t,this.setShowDefaultCursor(t),this.render()}setForbidCursorLine(t){this.forbidCursorLine=t,this.render()}setIsHidden(t){this.isHidden=t,this.emit("hiddenChange")}setDefaultAttribute(t){this.defaultAttribute=t}getCoordinateInOrigin(t){const i=this.canvas.getBoundingClientRect();return{x:(t.clientX-i.left-this.currentPos.x)/this.zoom,y:(t.clientY-i.top-this.currentPos.y)/this.zoom}}getTextIconSvg(t=""){var i;return A.getTextIconSvg(t,(i=this.config)==null?void 0:i.attributeList,this.config.attributeConfigurable,this.baseIcon)}setIsShowOrder(t){this.config.isShowOrder=t,this.render()}getCoordinate(t){const i=this.canvas.getBoundingClientRect();return{x:t.clientX-i.left,y:t.clientY-i.top}}getCoordinateUnderZoom(t){const i=this.canvas.getBoundingClientRect();return{x:t.clientX-i.left-this.currentPos.x,y:t.clientY-i.top-this.currentPos.y}}getCoordinateUnderZoomByRotate(t){const{x:i,y:e}=this.getCoordinateUnderZoom(t);return this.basicImgInfo.rotate===90?{x:e,y:this.basicImgInfo.height*this.zoom-i}:this.basicImgInfo.rotate===180?{x:this.basicImgInfo.width*this.zoom-i,y:this.basicImgInfo.height*this.zoom-e}:this.basicImgInfo.rotate===270?{x:this.basicImgInfo.width*this.zoom-e,y:i}:{x:i,y:e}}getCoordinateUnderZoomByRotateFromImgPoint(t){const{x:i,y:e}=t;return this.basicImgInfo.rotate===90?{x:(this.basicImgInfo.height-e)*this.zoom+this.currentPos.x,y:i*this.zoom+this.currentPos.y}:this.basicImgInfo.rotate===180?{x:(this.basicImgInfo.width-i)*this.zoom+this.currentPos.x,y:(this.basicImgInfo.height-e)*this.zoom+this.currentPos.y}:this.basicImgInfo.rotate===270?{x:e*this.zoom+this.currentPos.x,y:(this.basicImgInfo.width-i)*this.zoom+this.currentPos.y}:{x:i*this.zoom+this.currentPos.x,y:e*this.zoom+this.currentPos.y}}getGetCenterCoordinate(){return{x:this.size.width/2,y:this.size.height/2}}initPosition(){if(this.basicResult&&this.imgInfo){const{basicResult:t,size:i,imgNode:e,_imgAttribute:s,imgInfo:a,dependToolName:h}=this;if(t&&e&&h){let l=t;switch(h){case y.Polygon:case y.Line:{if(t.pointList){const r=B.calcViewportBoundaries(t.pointList);l={x:r.left,y:r.top,width:r.right-r.left,height:r.bottom-r.top}}break}}const n=L.getBasicRecPos(e,l,i,void 0,s==null?void 0:s.zoomRatio,s==null?void 0:s.isOriginalSize);n&&(this.setCurrentPos(n.currentPos),this.currentPosStorage=this.currentPos,this.setImgInfo(T(D({},a),{width:a.width/this.innerZoom*n.innerZoom,height:a.height/this.innerZoom*n.innerZoom})),this.setZoom(n.innerZoom),this.render(),this.renderBasicCanvas())}}else this.initImgPos()}undo(){this.history.undo()}redo(){this.history.redo()}clearCanvas(){var t;(t=this.ctx)==null||t.clearRect(0,0,this.size.width,this.size.height)}clearBasicCanvas(){var t;(t=this.basicCtx)==null||t.clearRect(0,0,this.size.width,this.size.height)}clearOffscreenCanvas(){var t;(t=this.offscreenCtx)==null||t.clearRect(0,0,this.size.width,this.size.height)}eventBinding(){this.dblClickListener.addEvent(()=>{},this.onLeftDblClick,this.onRightDblClick),this.container.addEventListener("mousedown",this.onMouseDown),this.container.addEventListener("mousemove",this.onMouseMove),this.container.addEventListener("mouseup",this.onMouseUp),this.container.addEventListener("mouseleave",this.onMouseLeave),this.container.addEventListener("click",this.onClick),this.container.addEventListener("wheel",this.onWheel),document.addEventListener("keydown",this.onKeyDown),document.addEventListener("keyup",this.onKeyUp),window.parent.document.addEventListener("contextmenu",this.onContextmenu,!1)}eventUnbinding(){this.container.removeEventListener("mousedown",this.onMouseDown),this.container.removeEventListener("mousemove",this.onMouseMove),this.container.removeEventListener("mouseup",this.onMouseUp),this.container.removeEventListener("mouseleave",this.onMouseLeave),this.container.removeEventListener("wheel",this.onWheel),this.container.removeEventListener("click",this.onClick),document.removeEventListener("keydown",this.onKeyDown),document.removeEventListener("keyup",this.onKeyUp),window.parent.document.removeEventListener("contextmenu",this.onContextmenu,!1),this.dblClickListener.removeEvent()}clearImgDrag(){this.isDrag=!1,this.isDragStart=!1,this.isSpaceClick=!1,this.startTime=0,this.container.style.cursor=this.defaultCursor,this.forbidCursorLine=!1}clearCursorLine(){this.coord={x:-1,y:-1}}onMouseDown(t){if(!this.canvas||this.isImgError)return!0;const i=this.getCoordinate(t);(this.isSpaceKey&&t.button===0||t.button===2)&&(t.stopPropagation(),this._firstClickCoordinate=i,this.currentPosStorage=this.currentPos,this.isSpaceClick=!0,this.isDragStart=!0,this.startTime=new Date().getTime())}onMouseMove(t,i=!0){if(!this.canvas||this.isImgError)return!0;const e=this.getCoordinate(t);this.isShowCursor&&(this.coord=e);try{if(!e||!E(e==null?void 0:e.x)||!E(e==null?void 0:e.y))throw new Error("coord error");if(this.coord=e,(this.isSpaceClick||this.isDragStart)&&this._firstClickCoordinate){const s=this.getCurrentPos(e);this.setCurrentPos(s),this.isDrag=!0,this.container.style.cursor="grabbing",this.forbidCursorLine=!0,this.renderBasicCanvas(),this.emit("dependRender"),this.emit("dragMove",{currentPos:s,zoom:this.zoom,imgInfo:this.imgInfo})}i&&this.render()}catch(s){console.error(s)}}onMouseUp(t){if(!this.canvas||this.isImgError)return!0;if(this.container.style.cursor=this.defaultCursor,this.forbidCursorLine=!1,this.isDrag=!1,this.isDragStart=!1,this.isSpaceClick=!1,this.startTime!==0&&this._firstClickCoordinate){const i=new Date().getTime(),e=this.getCoordinate(t);if(i-this.startTime>1e3||this.isSpaceKey===!0||H.calcTwoPointDistance(e,this._firstClickCoordinate)>10)return t.stopPropagation(),this.startTime=0,this.render(),!0}this.startTime=0,t.button===2&&this.onRightClick(t),this.render()}onMouseLeave(){this.clearImgDrag()}onClick(t){}onRightClick(t){}onLeftDblClick(t){}onRightDblClick(t){this.clearImgDrag()}onKeyDown(t){switch((t.keyCode===P.Alt||t.keyCode===P.A)&&t.preventDefault(),t.keyCode){case P.Space:this.isSpaceKey=!0,t.preventDefault();break;case P.Z:if(t.ctrlKey)return t.shiftKey?this.redo():this.undo(),!1;break}return!0}onKeyUp(t){switch(t.keyCode){case P.Space:this.isSpaceKey=!1;break}}exportCustomData(){return{}}onWheel(t,i=!0){if(!this.imgNode||!this.coord)return;t.preventDefault(),t.stopPropagation();const e=this.getCoordinate(t),s=t.deltaY||t.wheelDelta;let a=0;s>0&&this.zoom>this.zoomInfo.min&&(a=-1),s<0&&this.zoom<this.zoomInfo.max&&(a=1),this.wheelChangePos(e,a),this.emit("dependRender"),i&&this.render(),this.renderBasicCanvas()}renderCursorLine(t=(i=>(i=this.style.lineColor[0])!=null?i:"")()){if(!this.ctx||this.forbidCursorLine||this.forbidOperation)return;const{x:i,y:e}=this.coord;v.drawLine(this.canvas,{x:0,y:e},{x:1e4,y:e},{color:t}),v.drawLine(this.canvas,{x:i,y:0},{x:i,y:1e4},{color:t}),v.drawCircleWithFill(this.canvas,{x:i,y:e},1,{color:"white"})}setSize(t){var i;this.size=t,this.updateZoomInfo(),this.container.contains(this.canvas)&&(this.destroyCanvas(),this.createCanvas(t),this.eventUnbinding(),this.init(),((i=this.basicImgInfo)==null?void 0:i.valid)===!1&&this.renderInvalidPage())}setImgAttribute(t){const i=this._imgAttribute;if(this._imgAttribute=t,(i==null?void 0:i.zoomRatio)!==t.zoomRatio||i.isOriginalSize!==t.isOriginalSize){this.initImgPos();return}this.renderBasicCanvas(),this.render()}clearResult(t){}setValid(t){this.basicImgInfo.valid=t,t===!1?(this.renderInvalidPage(),this.clearResult(!1)):this.clearInvalidPage()}setRotate(t){this.basicImgInfo.rotate=t}setBasicResult(t){this.basicResult=t,this.coordUtils.setBasicResult(t),this.initPosition(),this.emit("dependRender")}setDependName(t,i){this.dependToolName=t,this.coordUtils.setDependInfo(t,i)}filterCacheContext(){}setAttributeLockList(t){this.attributeLockList=t,this.filterCacheContext(),this.render()}setConfig(t){this.config=_.jsonParser(t)}setDataInjectionAtCreation(t){this.dataInjectionAtCreation=t}setRenderEnhance(t){this.renderEnhance=t}setCustomRenderStyle(t){this.customRenderStyle=t}updateRotate(){if(this.dependToolName)return this.emit("messageInfo",O.getMessagesByLocale(U.NoRotateInDependence,this.lang)),!1;if(this.dataList.length>0)return this.emit("messageInfo",O.getMessagesByLocale(U.NoRotateNotice,this.lang)),!1;const t=B.getRotate(this.basicImgInfo.rotate);this.basicImgInfo.rotate=t,this.initImgPos(),this.emit("updateResult")}getColor(t="",i=this.config){return k.getColorByConfig({attribute:t,config:i,style:this.style})}getLineColor(t=""){var i,e,s,a,h,l,n,r;if(((i=this.config)==null?void 0:i.attributeConfigurable)===!0){const p=A.getAttributeIndex(t,(s=(e=this.config)==null?void 0:e.attributeList)!=null?s:[])+1;return((l=(h=(a=this.config)==null?void 0:a.attributeList)==null?void 0:h.find(c=>c.value===t))==null?void 0:l.color)?(r=(n=k.getColorByConfig({attribute:t,config:this.config}))==null?void 0:n.valid)==null?void 0:r.stroke:this.style.attributeLineColor?this.style.attributeLineColor[p]:""}const{color:b,lineColor:I}=this.style;return b&&I?I[b]:""}clearInvalidPage(){this._invalidDOM&&this.container&&this.container.contains(this._invalidDOM)&&(this.container.removeChild(this._invalidDOM),this._invalidDOM=void 0)}renderInvalidPage(){!this.container||this._invalidDOM||(this._invalidDOM=Q.renderInvalidPage(this.container,this.size,this.lang))}renderBasicCanvas(){if(!this.basicCanvas)return;this.clearBasicCanvas(),this.drawImg();const t=3;if(!this.forbidBasicResultRender&&this.basicResult&&this.dependToolName)switch(this.dependToolName){case y.Rect:{v.drawRect(this.basicCanvas,x.changeRectByZoom(this.basicResult,this.zoom,this.currentPos),{color:"rgba(204,204,204,1.00)",thickness:t});break}case y.Polygon:{v.drawPolygonWithFillAndLine(this.basicCanvas,x.changePointListByZoom(this.basicResult.pointList,this.zoom,this.currentPos),{fillColor:"transparent",strokeColor:"rgba(204,204,204,1.00)",isClose:!0,thickness:t});break}case y.Line:{v.drawLineWithPointList(this.basicCanvas,x.changePointListByZoom(this.basicResult.pointList,this.zoom,this.currentPos),{color:"rgba(204,204,204,1.00)",thickness:t});break}}}render(){!this.canvas||!this.ctx||!this.imgNode||this.clearCanvas()}changeStyle(t=this.defaultAttribute){this.emit("changeStyle",{attribute:t})}}export{ht as BasicToolOperation};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import b,{isNumber as
|
|
1
|
+
import b,{isNumber as y}from"lodash";import{INVALID_COLOR as _,toolStyleConverter as C}from"@labelbee/lb-utils";import{ESortDirection as w,EDragTarget as S,DEFAULT_TEXT_OFFSET as A}from"../../constant/annotation.js";import{EPolygonPattern as v}from"../../constant/tool.js";import f from"../../utils/tool/AxisUtils.js";import D from"../../utils/tool/CommonToolUtils.js";import P from"../../utils/tool/DrawUtils.js";import L from"../../utils/tool/PolygonUtils.js";import{polygonConfig as x}from"../../constant/defaultConfig.js";import T from"./polygonOperation.js";import{BasicToolOperation as O}from"./basicToolOperation.js";var k=Object.defineProperty,B=Object.defineProperties,R=Object.getOwnPropertyDescriptors,m=Object.getOwnPropertySymbols,U=Object.prototype.hasOwnProperty,j=Object.prototype.propertyIsEnumerable,I=(d,t,e)=>t in d?k(d,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):d[t]=e,u=(d,t)=>{for(var e in t||(t={}))U.call(t,e)&&I(d,e,t[e]);if(m)for(var e of m(t))j.call(t,e)&&I(d,e,t[e]);return d},g=(d,t)=>B(d,R(t));class M extends T{constructor(t){super(t);this.highlightAttributeList=[],this.rightMouseUp=o=>{var a;if(this.drawingPointList.length>0){this.addDrawingPointToPolygonList();return}this.selection.setSelectedIDs(this.hoverID,o.ctrlKey),this.emit("setSelectedIDs",this.selection.selectedIDs);const l=(a=this.polygonList.find(c=>c.id===this.hoverID))==null?void 0:a.attribute;l&&l!==this.defaultAttribute&&this.emit("syncAttribute",l)},this.onKeyDown=()=>{},this.onKeyUp=()=>{},this.renderSingleSelectedPolygon=o=>{var a;if(o){const l=this.getPointCloudLineColor(o),c=f.changePointListByZoom(o.pointList,this.zoom,this.currentPos);P.drawSelectedPolygonWithFillAndLine(this.canvas,c,{fillColor:"transparent",strokeColor:l,pointColor:"white",thickness:2,lineCap:"round",isClose:!0,lineType:(a=this.config)==null?void 0:a.lineType}),o.isRect===!0&&this.showDirectionLine===!0&&(this.renderRectPolygonDirection(c),y(o==null?void 0:o.trackID)&&(o==null?void 0:o.trackID)>=0&&this.renderdrawTrackID(o))}},this.emitUpdatePolygonByDrag=()=>{if(this.dragInfo){const{originPolygonList:o}=this.dragInfo;if(this.selectedIDs.length>0){const a=[];this.polygonList.forEach(l=>{if(this.selectedIDs.includes(l.id)){const c=o.find(p=>p.id===l.id);c&&a.push({newPolygon:l,originPolygon:c})}}),this.emit("updatePolygonByDrag",a)}}};var e,i,s,r,n,h;this.showDirectionLine=(e=t.showDirectionLine)!=null?e:!0,this.forbidAddNew=(i=t.forbidAddNew)!=null?i:!1,this.pointCloudConfig=(s=D.jsonParser(t.config))!=null?s:{},this.hideAttributes=[],this.checkMode=(r=t.checkMode)!=null?r:!1,this.forbidAddNew===!1&&t.checkMode===!0&&(this.forbidAddNew=!0),this.config=g(u({},x),{textConfigurable:!1,attributeConfigurable:!0,attributeList:(h=(n=this.pointCloudConfig)==null?void 0:n.attributeList)!=null?h:[]})}get getSelectedIDs(){return this.selectedIDs}get enableDrag(){return Boolean(this.selectedIDs.length>0&&this.dragInfo)}get visiblePolygonList(){return this.polygonList.filter(t=>!this.hideAttributes.includes(t.attribute))}setHiddenAttributes(t){this.hideAttributes=t}setConfig(t){var e;const i=D.jsonParser(t);this.pointCloudConfig=i,this.config=g(u({},this.config),{attributeList:(e=i==null?void 0:i.attributeList)!=null?e:[]})}dragMouseDown(t){this.checkMode||super.dragMouseDown(t)}deletePolygon(t){this.checkMode||super.deletePolygons(t?[t]:void 0)}deletePolygonPoint(t){this.checkMode||super.deletePolygonPoint(t)}setSelectedIDs(t){this.selection.hardSetSelectedIDs(t),this.selectedIDs.length<2&&this.setSelectedID(this.selectedIDs.length===1?this.selectedIDs[0]:""),this.render()}deleteSelectedID(){super.deleteSelectedID(),this.selection.setSelectedIDs(),this.emit("deleteSelectedIDs")}get selectedPolygons(){return L.getPolygonByIDs(this.polygonList,this.selectedIDs)}updateSelectedPolygonsPoints(t){var e,i;if(this.selectedPolygons&&((e=this.selectedPolygons)==null?void 0:e.length)>0){const s=b.cloneDeep(this.selectedPolygons),r=[];(i=this.selectedPolygons)==null||i.forEach((n,h)=>{n.pointList=n.pointList.map(o=>{var a,l;const{x:c,y:p}=o;return g(u({},o),{x:c+((a=t.x)!=null?a:0),y:p+((l=t.y)!=null?l:0)})}),r.push({originPolygon:s[h],newPolygon:n})}),this.emit("updateResult"),this.emit("updatePolygonByDrag",r),this.render(),this.history.pushHistory(this.polygonList)}}getLineColor(){return"rgba(0, 255, 255, 0.5)"}getPointCloudLineColor(t){return t.valid===!1?_:C.getColorFromConfig({attribute:t.attribute},g(u({},this.pointCloudConfig),{attributeConfigurable:!0}),{}).stroke}renderStaticPolygon(){var t;this.isHidden===!1&&((t=this.visiblePolygonList)==null||t.forEach(e=>{var i,s,r;if([...this.selectedIDs,this.editPolygonID].includes(e.id))return;const n=this.getPointCloudLineColor(e),h=f.changePointListByZoom(e.pointList||[],this.zoom,this.currentPos),o=this.highlightAttributeList.includes(e.attribute);P.drawPolygonWithFillAndLine(this.canvas,h,{fillColor:o?n:"transparent",strokeColor:n,pointColor:"white",thickness:(s=(i=this.style)==null?void 0:i.width)!=null?s:2,lineCap:"round",isClose:!0,lineType:(r=this.config)==null?void 0:r.lineType}),y(e==null?void 0:e.trackID)&&(e==null?void 0:e.trackID)>=0&&this.renderdrawTrackID(e),e.isRect===!0&&this.showDirectionLine===!0&&this.renderRectPolygonDirection(h)}))}renderSelectedPolygon(){var t;(t=this.selectedPolygons)==null||t.forEach(e=>{this.renderSingleSelectedPolygon(e)})}renderdrawTrackID(t){var e,i;if(!((e=t==null?void 0:t.pointList)==null?void 0:e.length))return;const s=t.pointList[t.pointList.length-1],r=f.changePointByZoom(s,this.zoom,this.currentPos),n=((i=t==null?void 0:t.trackID)==null?void 0:i.toString())||"";P.drawText(this.canvas,r,n,u({textAlign:"center",color:"white"},A))}renderRectPolygonDirection(t){t.length<2||P.drawLine(this.canvas,t[0],t[1],{color:"white",thickness:3,lineDash:[6]})}get currentPolygonListByPattern(){return this.polygonList.filter(t=>this.pattern===v.Rect?t.isRect===!0:this.pattern===v.Normal?t.isRect!==!0:!0)}getHoverID(t){var e;const i=this.getCoordinateUnderZoom(t),r=this.currentPolygonListByPattern.map(n=>g(u({},n),{pointList:f.changePointListByZoom(n.pointList,this.zoom)}));return L.getHoverPolygonID(i,r,10,(e=this.config)==null?void 0:e.lineType)}switchToNextPolygon(t=w.ascend){if(this.drawingPointList.length>0)return;const e=this.currentPolygonListByPattern.map(s=>{var r,n,h,o;return g(u({},s),{x:(n=(r=s.pointList[0])==null?void 0:r.x)!=null?n:0,y:(o=(h=s.pointList[0])==null?void 0:h.y)!=null?o:0})}),i=D.getNextSelectedRectID(e,t,this.selectedID);if(i)return this.setSelectedIDs([i.id]),this.render(),[i.id]}setSelectedIdAfterAddingDrawing(t){this.drawingPointList.length!==0&&this.setSelectedID(t)}updateTextAttribute(t){var e,i;const s=this.selectedID;t!==s&&s&&((e=this._textAttributeInstance)==null||e.changeSelected()),t||(i=this._textAttributeInstance)==null||i.clearTextAttribute()}setSelectedID(t){this.updateTextAttribute(t),this.selection.setSelectedIDs(t),this.render()}addPointInDrawing(t){this.forbidAddNew||super.addPointInDrawing(t)}setCanvasSize(t){var e,i;const s=this.pixelRatio;this.size=t,this.setImgInfo(t),this.updateCanvasBasicStyle(this.basicCanvas,t,0),this.updateCanvasBasicStyle(this.canvas,t,10),(e=this.ctx)==null||e.scale(s,s),(i=this.basicCtx)==null||i.scale(s,s),this.initImgPos(),this.renderBasicCanvas(),this.render()}setPolygonValidAndRender(t,e=!1,i){if(e){super.setPolygonValidAndRender(t,i);return}this.emit("validUpdate",t)}onMouseDown(t){if(O.prototype.onMouseDown.call(this,t)||this.forbidMouseOperation||t.ctrlKey===!0||t.button!==0)return;if(this.selectedIDs.length<2)return super.onMouseDown(t);const e=this.getCoordinateUnderZoom(t);this.dragInfo={dragStartCoord:e,dragTarget:S.Plane,initPointList:[],changePointIndex:[0],originPolygon:this.selectedPolygon,dragPrevCoord:e,originPolygonList:this.polygonList,selectedPolygons:this.selectedPolygons}}setResultAndSelectedID(t,e){this.setPolygonList(t),this.setSelectedIDs([e])}setHighlightAttribute(t){this.highlightAttributeList=[t],this.render()}}export{M as default};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import j from"lodash";import{i18n as _}from"@labelbee/lb-utils";import b from"../../utils/MathUtils.js";import N from"../../utils/tool/RectUtils.js";import{ERotateDirection as K,ESortDirection as M,EDragTarget as v,EDragStatus as S,TEXT_ATTRIBUTE_OFFSET as E,DEFAULT_TEXT_OFFSET as R}from"../../constant/annotation.js";import L from"../../constant/keyCode.js";import{EPolygonPattern as x,ERectToolModeType as $,RECT_TOOL_MODE_NAME as W,edgeAdsorptionScope as k,ELineTypes as U,EToolName as B}from"../../constant/tool.js";import w from"../../locales/index.js";import{EMessage as C}from"../../locales/constants.js";import V from"../../utils/ActionsHistory.js";import D from"../../utils/tool/AttributeUtils.js";import g from"../../utils/tool/AxisUtils.js";import O from"../../utils/tool/CanvasUtils.js";import f from"../../utils/tool/CommonToolUtils.js";import P from"../../utils/tool/DrawUtils.js";import u from"../../utils/tool/PolygonUtils.js";import T from"../../utils/tool/StyleUtils.js";import H from"../../utils/uuid.js";import{BasicToolOperation as X}from"./basicToolOperation.js";import Y from"./textAttributeClass.js";import q from"./Selection.js";var G=Object.defineProperty,J=Object.defineProperties,Q=Object.getOwnPropertyDescriptors,Z=Object.getOwnPropertySymbols,tt=Object.prototype.hasOwnProperty,et=Object.prototype.propertyIsEnumerable,F=(I,t,e)=>t in I?G(I,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):I[t]=e,y=(I,t)=>{for(var e in t||(t={}))tt.call(t,e)&&F(I,e,t[e]);if(Z)for(var e of Z(t))et.call(t,e)&&F(I,e,t[e]);return I},m=(I,t)=>J(I,Q(t));const z=164;class it extends X{constructor(t){super(t);this.isAllowDouble=e=>{const{selectedID:i}=this,s=this.getHoverID(e);return!!(i&&i===s)},this.textChange=e=>{this.config.textConfigurable===!1||!this.selectedID||(this.setPolygonList(D.textChange(e,this.selectedID,this.polygonList)),this.emit("selectedChange"),this.render())},this.emitUpdatePolygonByDrag=()=>{if(this.dragInfo){const{originPolygon:e}=this.dragInfo;this.emit("updatePolygonByDrag",[{newPolygon:this.selectedPolygon,originPolygon:e}])}},this.config=f.jsonParser(t.config),this.drawingPointList=[],this.polygonList=[],this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.drawingHistory=new V,this.isCtrl=!1,this.isAlt=!1,this.isCombined=!1,this.pattern=x.Normal,this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.selection=new q(this)}get selectedIDs(){return this.selection.selectedIDs}get selectedID(){return this.selection.selectedID}get minArea(){return this.config.minArea||0}eventBinding(){super.eventBinding(),this.container.removeEventListener("mouseup",this.onMouseUp),this.container.addEventListener("mouseup",this.dragMouseUp),this.dblClickListener.addEvent(this.onMouseUp,this.onLeftDblClick,this.onRightDblClick,this.isAllowDouble)}eventUnbinding(){super.eventUnbinding(),this.container.removeEventListener("mouseup",this.dragMouseUp)}destroy(){super.destroy(),this._textAttributeInstance&&this._textAttributeInstance.clearTextAttribute()}get selectedPolygon(){return u.getPolygonByID(this.visiblePolygonList,this.selectedID)}get selectedPolygons(){return u.getPolygonByIDs(this.polygonList,this.selectedIDs)}get hoverPolygon(){return this.visiblePolygonList.find(t=>t.id===this.hoverID&&t.id!==this.selectedID)}get enableDrag(){return Boolean(this.selectedID&&this.dragInfo)}get visiblePolygonList(){return this.polygonList}get polygonListUnderZoom(){return this.visiblePolygonList.map(t=>m(y({},t),{pointList:g.changePointListByZoom(t.pointList,this.zoom)}))}get selectedText(){var t;return(t=this.selectedPolygon)==null?void 0:t.textAttribute}get isThreePointsMode(){return this.pattern===x.Rect&&this.drawingPointList.length===2}get isTwoPointsMode(){return this.pattern===x.Rect&&this.rectToolMode===$.TwoPoints&&this.drawingPointList.length===1}get dataList(){return this.polygonList}setPattern(t,e=!1){var i;((i=this.drawingPointList)==null?void 0:i.length)>0&&e===!0||(this.pattern=t)}get currentShowList(){let t=[];const[e,i]=f.getRenderResultList(this.polygonList,f.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);return t=e,this.isHidden&&(t=[]),i&&t.push(...i),t}get currentPageResult(){const[t]=f.getRenderResultList(this.polygonList,f.getSourceID(this.basicResult),[]);return t}setResultAndSelectedID(t,e){this.setPolygonList(t),e&&this.setSelectedID(e),this.render()}setResult(t){this.clearActiveStatus(),this.setPolygonList(t),this.render()}setPolygonDataByID(t,e){return this.polygonList.map(i=>i.id===e?y(y({},i),t):i)}rotatePolygon(t=1,e=K.Clockwise,i=this.selectedID){if(!i)return;const s=u.getPolygonByID(this.polygonList,i);if(!s)return;const o=u.updatePolygonByRotate(e,t,s==null?void 0:s.pointList);this.setPolygonList(this.setPolygonDataByID({pointList:o},i)),this.render()}rotatePolygonEdge(t,e=this.selectedID){!e||!u.getPolygonByID(this.polygonList,e)||(this.setPolygonList(this.setPolygonDataByID({pointList:t},e)),this.render())}addPointInDrawing(t){if(!this.imgInfo||this.forbidAddNewPolygonFuc&&this.forbidAddNewPolygonFuc(t))return;const{upperLimitPointNum:e,edgeAdsorption:i}=this.config;if(e&&this.drawingPointList.length>=e){this.emit("messageInfo",`${w.getMessagesByLocale(C.UpperLimitErrorNotice,this.lang)}${e}`);return}this.rectToolMode=localStorage.getItem(W),this.deleteSelectedID();const s=this.getCoordinateUnderZoom(t),o=g.changeDrawOutsideTarget(s,{x:0,y:0},this.imgInfo,this.config.drawOutsideTarget,this.basicResult,this.zoom);if(g.returnClosePointIndex(o,g.changePointListByZoom(this.drawingPointList,this.zoom))===0){this.addDrawingPointToPolygonList();return}const{dropFoot:n}=u.getClosestPoint(o,this.polygonListUnderZoom,this.config.lineType,k),r=g.changePointByZoom(n&&t.altKey===!1&&i?n:o,1/this.zoom);if(this.isThreePointsMode||this.isTwoPointsMode){if(this.isThreePointsMode){const h=b.getRectangleByRightAngle(r,this.drawingPointList);this.drawingPointList=h}else this.isTwoPointsMode&&(this.drawingPointList=this.createRectByTwoPointsMode(this.drawingPointList,r));if(this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(this.drawingPointList)){this.emit("messageInfo",`${w.getMessagesByLocale(C.ForbiddenCreationOutsideBoundary,this.lang)}`),this.drawingPointList=[];return}this.addDrawingPointToPolygonList(!0);return}this.drawingPointList.push(r),this.drawingPointList.length===1?this.drawingHistory.initRecord(this.drawingPointList):this.drawingHistory.pushHistory(this.drawingPointList)}clearResult(){this.setPolygonList([]),this.deleteSelectedID(),this.render()}clearPolygonDrag(){this.drawingPointList=[],this.stopDrag(),this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,this.hoverID=""}clearActiveStatus(){this.clearPolygonDrag(),this.deleteSelectedID()}clearDrawingStatus(){this.drawingPointList=[]}setPolygonList(t){const e=this.polygonList.length;this.polygonList=t,e!==t.length&&this.emit("updatePageNumber")}setSelectedID(t,e=!1){this.selection.setSelectedIDs(t,e),this.render(),this.emit("selectedChange")}setDefaultAttribute(t=""){const e=this.defaultAttribute;if(this.defaultAttribute=t,e!==t&&(this.changeStyle(t),this.emit("changeAttributeSidebar"),this.selectedPolygons.length>0&&(this.selectedPolygons.forEach(i=>{i.attribute=t}),this.history.pushHistory(this.polygonList),this.render()),this._textAttributeInstance)){if(this.attributeLockList.length>0&&!this.attributeLockList.includes(t)){this._textAttributeInstance.clearTextAttribute();return}this._textAttributeInstance.updateIcon(this.getTextIconSvg(t))}}setStyle(t){var e;super.setStyle(t),this._textAttributeInstance&&this.config.attributeConfigurable===!1&&((e=this._textAttributeInstance)==null||e.updateIcon(this.getTextIconSvg()))}setPolygonValidAndRender(t,e){if(!t)return;const i=this.polygonList.map(s=>{var o;if(s.id===t){const l=(o=s==null?void 0:s.valid)!=null?o:!0;return m(y({},s),{valid:e!=null?e:!l})}return s});this.setPolygonList(i),this.history.pushHistory(this.polygonList),this.render(),this.emit("updateResult")}addDrawingPointToPolygonList(t){let{lowerLimitPointNum:e=3}=this.config;e<3&&(e=3);let i;if(this.drawingPointList.length<e){this.drawingPointList=[],this.editPolygonID="";return}if(u.calcPolygonSize(this.drawingPointList||[])<this.minArea){this.emit("messageInfo",`${w.getMessagesByLocale(C.MinAreaLimitErrorNotice,this.lang)}${this.minArea}`);return}const s=f.getSourceID(this.basicResult),o=[...this.polygonList];if(this.editPolygonID){const l=o.find(n=>n.id===this.editPolygonID);if(!l)return;l.pointList=this.drawingPointList,this.editPolygonID=""}else{const l=H(8,62);let n={id:l,sourceID:s,valid:!this.isCtrl,textAttribute:"",pointList:this.drawingPointList,attribute:this.defaultAttribute,order:f.getMaxOrder(o.filter(r=>f.isSameSourceID(r.sourceID,s)))+1};if(this.config.textConfigurable){let r="";r=D.getTextAttribute(this.polygonList.filter(h=>f.isSameSourceID(h.sourceID,s)),this.config.textCheckType),n=m(y({},n),{textAttribute:r})}this.pattern===x.Rect&&t===!0&&(n=m(y({},n),{isRect:!0})),o.push(n),i=n,this.setSelectedIdAfterAddingDrawing(l)}this.setPolygonList(o),this.isCtrl=!1,this.drawingPointList=[],this.history.pushHistory(o),i&&this.emit("polygonCreated",i,this.zoom,this.currentPos)}setSelectedIdAfterAddingDrawing(t){this.drawingPointList.length!==0&&(this.config.textConfigurable?this.setSelectedID(t):this.deleteSelectedID())}getHoverID(t){var e;const i=this.getCoordinateUnderZoom(t),s=this.currentShowList.map(o=>m(y({},o),{pointList:g.changePointListByZoom(o.pointList,this.zoom)}));return u.getHoverPolygonID(i,s,10,(e=this.config)==null?void 0:e.lineType)}getHoverEdgeIndex(t){var e;if(!this.selectedID)return-1;const i=this.selectedPolygon;if(!i)return-1;const s=this.getCoordinateUnderZoom(t),o=g.changePointListByZoom(i.pointList,this.zoom);return u.getHoverEdgeIndex(s,o,(e=this.config)==null?void 0:e.lineType)}getHoverPointIndex(t){if(!this.selectedID)return-1;const e=this.selectedPolygon;if(!e)return-1;const i=this.getCoordinateUnderZoom(t),s=g.changePointListByZoom(e.pointList,this.zoom);return g.returnClosePointIndex(i,s)}deletePolygons(t){var e;if(!t||t.length===0)return;const i=this.polygonList.filter(s=>this.selection.isIdSelected(s.id));this.emit("deletedObject",{deletedObject:i[0],id:i[0].id}),this.setPolygonList(this.polygonList.filter(s=>!this.selection.isIdSelected(s.id))),this.history.pushHistory(this.polygonList),(e=this._textAttributeInstance)==null||e.clearTextAttribute(),this.emit("selectedChange"),this.render()}deletePolygonPoint(t){if(!this.selectedID)return;const{selectedPolygon:e}=this;if(!e)return;let{lowerLimitPointNum:i}=this.config;if(i<3&&(i=3),e.pointList.length<=i){this.emit("messageInfo",`${w.getMessagesByLocale(C.LowerLimitErrorNotice,this.lang)}${i}`);return}e==null||e.pointList.splice(t,1),this.history.pushHistory(this.polygonList),this.render()}spaceKeydown(){var t,e,i;if(this.selectedID){if(((t=this.selectedPolygon)==null?void 0:t.isRect)===!0){this.emit("messageInfo",`${w.getMessagesByLocale(C.UnableToReannotation,this.lang)}`);return}this.editPolygonID=this.selectedID,this.drawingPointList=(i=(e=this.selectedPolygon)==null?void 0:e.pointList)!=null?i:[],this.drawingHistory.empty(),this.drawingHistory.initRecord(this.drawingPointList),this.hoverID="",this.deleteSelectedID(),this.render()}}onTabKeyDown(t){t.preventDefault();let e=M.ascend;t.shiftKey&&(e=M.descend),this.switchToNextPolygon(e)}switchToNextPolygon(t){if(this.drawingPointList.length>0)return;const[e,i]=f.getRenderResultList(this.polygonList,f.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let s=[...e];i&&(s=[...s,...i]);const o=O.getViewPort(this.canvas,this.currentPos,this.zoom),l=s.map(r=>{var h,a,c,d;return m(y({},r),{x:(a=(h=r.pointList[0])==null?void 0:h.x)!=null?a:0,y:(d=(c=r.pointList[0])==null?void 0:c.y)!=null?d:0})}).filter(r=>O.inViewPort({x:r.x,y:r.y},o)),n=f.getNextSelectedRectID(l,t,this.selectedID);if(n){this.setSelectedID(n.id);const{selectedPolygon:r}=this;r&&this.setDefaultAttribute(r.attribute)}this.render()}onKeyDown(t){if(!f.hotkeyFilter(t)||super.onKeyDown(t)===!1||this.selection.triggerKeyboardEvent(t,this.setPolygonList.bind(this)))return;const{keyCode:e}=t;switch(e){case L.Space:this.spaceKeydown();break;case L.Esc:this.drawingPointList=[],this.editPolygonID="";break;case L.F:this.selectedID&&this.setPolygonValidAndRender(this.selectedID);break;case L.Z:if(t.altKey){this.onCombinedExecute();return}this.setIsHidden(!this.isHidden),this.render();break;case L.Delete:this.deletePolygons(this.selectedIDs),this.render();break;case L.Ctrl:this.isCtrl=!0;break;case L.Alt:this.isAlt===!1&&(t.preventDefault(),this.isAlt=!0,this.render());break;case L.Tab:{this.onTabKeyDown(t);break}case L.X:t.altKey&&this.segment();break;default:{if(this.config.attributeConfigurable){const i=D.getAttributeByKeycode(e,this.config.attributeList);i!==void 0&&this.setDefaultAttribute(i)}break}}}onKeyUp(t){switch(super.onKeyUp(t),t.keyCode){case L.Ctrl:this.isCtrl=!1;break;case L.Alt:{const e=this.isAlt;this.isAlt=!1,e===!0&&this.render();break}}}rightMouseUp(t){if(this.drawingPointList.length>0){this.addDrawingPointToPolygonList();return}this.setSelectedID(this.hoverID,t.ctrlKey);const{selectedPolygon:e}=this;e&&this.setDefaultAttribute(e.attribute)}onLeftDblClick(t){if(this.hoverEdgeIndex>-1){const e=this.getCoordinateUnderZoom(t),{selectedPolygon:i}=this;if(!i)return;if(i.isRect===!0){this.emit("messageInfo",`${w.getMessagesByLocale(C.ForbidAddNewPoint,this.lang)}`),this.clearPolygonDrag();return}const{dropFoot:s}=u.getClosestPoint(e,this.polygonListUnderZoom,this.config.lineType,k);if(!s)return;const{upperLimitPointNum:o}=this.config;if(o&&i.pointList.length>=o){this.emit("messageInfo",`${w.getMessagesByLocale(C.UpperLimitErrorNotice,this.lang)}${o}`),this.clearPolygonDrag();return}i==null||i.pointList.splice(this.hoverEdgeIndex+1,0,g.changePointByZoom(s,1/this.zoom)),this.setPolygonDataByID(i,this.selectedID),this.history.pushHistory(this.polygonList),this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.render()}this.dragInfo=void 0}onRightDblClick(t){this.dragInfo=void 0,this.clearImgDrag();const e=this.getHoverID(t),i=this.getHoverPointIndex(t);if(this.hoverPointIndex>-1&&this.hoverPointIndex===i){this.deletePolygonPoint(i),this.dragInfo=void 0,this.hoverPointIndex=-1,this.render();return}this.hoverID===this.selectedID&&this.deletePolygons([e]),this.render()}onMouseDown(t){if(!(super.onMouseDown(t)||this.forbidMouseOperation||t.ctrlKey===!0))return this.dragMouseDown(t),!0}onMouseLeave(){this.dragInfo&&(this.emitUpdatePolygonByDrag(),this.clearPolygonDrag(),this.history.pushHistory(this.polygonList))}dragMouseDown(t){if(t.button!==0)return;const e=this.getHoverID(t);if(!this.selection.isIdSelected(e))return;let i=[0],s=v.Plane;const o=this.selectedPolygons[0].pointList,l=this.getCoordinateUnderZoom(t);if(this.selectedPolygons.length===1){this.dragStatus=S.Start;const n=this.getHoverPointIndex(t),r=this.getHoverEdgeIndex(t);n>-1?(s=v.Point,i=[n]):r>-1&&this.hoverEdgeIndex>-1&&(s=v.Line,i=[r,(r+1)%o.length])}this.dragInfo={dragStartCoord:l,dragTarget:s,initPointList:o,changePointIndex:i,originPolygon:this.selectedPolygon,selectedPolygons:this.selectedPolygons,dragPrevCoord:l,originPolygonList:this.polygonList}}segment(){var t;if(!this.selectedID||((t=this.config)==null?void 0:t.lineType)!==U.Line)return;const e=u.getPolygonPointList(this.selectedID,this.currentShowList),i=this.currentShowList.filter(l=>l.id!==this.selectedID);if(i.length===0||e.length===0)return;const s=u.getWrapPolygonIndex(e,i);let o=[...this.polygonList];if(s===-1){const l=u.segmentPolygonByPolygon(e,i);if(!l)return;const n=l.shift();if(!n)return;let r="",h=!0;const a=f.getSourceID(this.basicResult);let c="";o=this.polygonList.map(d=>{var p,A;return d.id===this.selectedID?(r=d.attribute,h=(p=d==null?void 0:d.valid)!=null?p:!0,c=(A=d==null?void 0:d.textAttribute)!=null?A:"",m(y({},d),{pointList:n})):d}),l.length>0&&l.forEach((d,p)=>{o.push({sourceID:a,id:H(8,62),pointList:d,valid:h,order:f.getMaxOrder(this.currentShowList)+1+p,attribute:r,textAttribute:c})})}else o[s].pointList=u.clipPolygonFromWrapPolygon(e,o[s].pointList),o=o.filter(l=>l.id!==this.selectedID);this.setPolygonList(o),this.history.pushHistory(o),this.render()}onCombinedExecute(){if(!this.selectedID){this.emit("messageInfo",_.t("PolygonsToBeCombinedNeedToBeSelected"));return}this.isCombined=!this.isCombined}combine(t){var e;const i=this.getHoverID(t);if(!i||!this.selectedID||this.selectedID===i)return;if(((e=this.config)==null?void 0:e.lineType)!==U.Line){this.emit("messageInfo",_.t("CurveModeDoesNotSupportCutting"));return}const s=this.polygonList.find(h=>h.id===this.selectedID),o=this.currentShowList.find(h=>h.id===i);if(!o||!s)return;const l=u.combinePolygonWithPolygon(s,o);if(!l)return;const{newPolygon:n,unionList:r}=l;if(r.length===1&&n){const h=this.polygonList.filter(a=>!r.includes(a.id)).map(a=>a.id===this.selectedID?n:a);this.setPolygonList(h),this.history.pushHistory(h),this.render(),this.emit("messageInfo",_.t("CombineSuccess"))}else this.emit("messageInfo",_.t("CombiningFailedNotify"));this.isCombined=!1}isPolygonOutSide(t){if(this.dependToolName&&this.basicCanvas&&this.basicResult){let n=!1;switch(this.dependToolName){case B.Rect:{n=t.filter(r=>!N.isInRect(r,this.basicResult)).length>0;break}case B.Polygon:{n=u.isPointListOutSidePolygon(t,this.basicResult.pointList,this.config.lineType);break}}return n}if(!this.imgInfo)return!1;const{left:e,top:i,right:s,bottom:o}=b.calcViewportBoundaries(g.changePointListByZoom(t,this.zoom)),l=1e-5;return e<0||i<0||s>this.imgInfo.width+l||o>this.imgInfo.height+l}onDragMove(t){const e=this.polygonList.map(i=>{if(this.selectedIDs.includes(i.id)){const s=this.dragPolygon(t,i);if(!s)return i;const o=m(y({},i),{pointList:s});return i.isRect===!0&&this.pattern===x.Normal&&Object.assign(o,{isRect:!1}),o}return i});this.dragInfo.dragPrevCoord=this.getCoordinateUnderZoom(t),this.setPolygonList(e),this.render()}getDragOffset(t,e){var i;const s=this.getCoordinateUnderZoom(t),{dragTarget:o,dragPrevCoord:l,changePointIndex:n,initPointList:r,dragStartCoord:h}=this.dragInfo;if(this.pattern===x.Rect&&(e==null?void 0:e.isRect)===!0&&n&&[v.Line].includes(o)){const a=b.getArrayIndex(n[0]-2,4),c=b.getArrayIndex(n[0]-1,4),d=[r[a],r[c]],p=b.getRectPerpendicularOffset(h,s,d);return{x:p.x/this.zoom,y:p.y/this.zoom}}return((i=this.dragInfo)==null?void 0:i.dragTarget)===v.Plane?{x:(s.x-l.x)/this.zoom,y:(s.y-l.y)/this.zoom}:{x:(s.x-h.x)/this.zoom,y:(s.y-h.y)/this.zoom}}dragPolygon(t,e){let i=j.cloneDeep(e==null?void 0:e.pointList);if(!i||!this.dragInfo)return;const{initPointList:s,dragTarget:o,changePointIndex:l}=this.dragInfo,n=this.getDragOffset(t,e);switch(this.dragStatus=S.Move,o){case v.Plane:i=i.map(r=>m(y({},r),{x:r.x+n.x,y:r.y+n.y}));break;case v.Point:case v.Line:i=i.map((r,h)=>l&&l.includes(h)?m(y({},r),{x:s[h].x+n.x,y:s[h].y+n.y}):r);break}if(this.pattern===x.Rect&&(e==null?void 0:e.isRect)===!0&&o===v.Point&&l&&(i=b.getPointListFromPointOffset(s,l[0],n)),!(this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(i)))return i}onMouseMove(t){if(super.onMouseMove(t)||this.forbidMouseOperation||!this.imgInfo)return;let e=-1,i=-1;const{selectedID:s}=this;if(s&&(this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,e=this.getHoverPointIndex(t),e>-1?this.hoverPointIndex=e:(i=this.getHoverEdgeIndex(t),this.hoverEdgeIndex=i)),this.drawingPointList.length>0)return;const o=this.getHoverID(t);this.hoverID!==o&&(this.hoverID=o,this.render()),this.selectedIDs.length>0&&this.dragInfo&&this.onDragMove(t)}leftMouseUpdateValid(t){const e=this.getHoverID(t);return this.drawingPointList.length===0&&t.ctrlKey===!0&&e?(this.setPolygonValidAndRender(e),!0):!1}stopDrag(){this.dragInfo=void 0,this.dragStatus=S.Wait}leftMouseUp(t){this.leftMouseUpdateValid(t)||this.addPointInDrawing(t)}onMouseUp(t){if(this.isCombined){switch(t.button){case 0:this.combine(t);break;case 2:this.isCombined=!1;break;default:return}return}if(!(super.onMouseUp(t)||this.forbidMouseOperation||!this.imgInfo)){if(this.dragInfo&&this.dragStatus===S.Move){this.emit("updateResult"),this.emitUpdatePolygonByDrag(),this.stopDrag(),this.history.pushHistory(this.polygonList);return}if(this.dragInfo&&this.dragStatus===S.Start){this.stopDrag();return}switch(t.button){case 0:{this.leftMouseUp(t);break}case 2:{this.rightMouseUp(t);break}}this.render()}}dragMouseUp(){this.dragStatus===S.Start&&this.stopDrag()}exportData(){const{polygonList:t}=this;return[t,this.basicImgInfo]}getTextIconSvg(t=""){return D.getTextIconSvg(t,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}getCurrentSelectedData(){const{selectedPolygon:t}=this;if(!t)return;const e=this.getColor(t.attribute),i=t.valid?e==null?void 0:e.valid.stroke:e==null?void 0:e.invalid.stroke;return{width:z,textAttribute:t.textAttribute,color:i}}updateSelectedTextAttribute(t){if(this._textAttributeInstance&&t&&this.selectedID){let e=t;D.textAttributeValidate(this.config.textCheckType,"",e)===!1&&(this.emit("messageError",D.getErrorNotice(this.config.textCheckType,this.lang)),e=""),this.setPolygonList(D.textChange(e,this.selectedID,this.polygonList)),this.emit("updateTextAttribute"),this.render()}}renderTextAttribute(){var t;const{selectedPolygon:e}=this;if(!this.ctx||this.config.textConfigurable===!1||!e)return;const{pointList:i,attribute:s,valid:o,textAttribute:l}=e,{x:n,y:r}=i[i.length-1],h=z,a=g.getOffsetCoordinate({x:n,y:r},this.currentPos,this.zoom),c=this.getColor(s),d=o?c==null?void 0:c.valid.stroke:c==null?void 0:c.invalid.stroke;this._textAttributeInstance||(this._textAttributeInstance=new Y({width:h,container:this.container,icon:this.getTextIconSvg(s),color:d,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((t=this._textAttributeInstance)==null?void 0:t.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${l}`,{left:a.x,top:a.y,color:d,width:h})}renderStaticPolygon(){var t;this.isHidden===!1&&((t=this.visiblePolygonList)==null||t.forEach(e=>{var i,s,o,l,n;if([this.selectedID,this.editPolygonID].includes(e.id))return;const{textAttribute:r,attribute:h}=e,a=this.getColor(h),c=T.getStrokeAndFill(a,e.valid),d=g.changePointListByZoom(e.pointList||[],this.zoom,this.currentPos);P.drawPolygonWithFillAndLine(this.canvas,d,{fillColor:c.fill,strokeColor:c.stroke,pointColor:"white",thickness:(s=(i=this.style)==null?void 0:i.width)!=null?s:2,lineCap:"round",isClose:!0,lineType:(o=this.config)==null?void 0:o.lineType});let p=`${(l=D.getAttributeShowText(h,this.config.attributeList))!=null?l:""}`;((n=this.config)==null?void 0:n.isShowOrder)&&(e==null?void 0:e.order)>0&&(p=`${e.order} ${p}`),P.drawText(this.canvas,d[0],p,y({color:c.stroke},R));const A=d[d.length-1];P.drawText(this.canvas,{x:A.x+E.x,y:A.y+E.y},r,y({color:c.stroke},R))}))}renderSelectedPolygons(){this.selectedPolygons.forEach(t=>{this.renderSelectedPolygon(t)})}renderSelectedPolygon(t){var e,i,s;if(t){const o=this.getColor(t.attribute),l=T.getStrokeAndFill(o,t.valid,{isSelected:!0});P.drawSelectedPolygonWithFillAndLine(this.canvas,g.changePointListByZoom(t.pointList,this.zoom,this.currentPos),{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!0,lineType:(e=this.config)==null?void 0:e.lineType});let n=`${(i=D.getAttributeShowText(t.attribute,this.config.attributeList))!=null?i:""}`;((s=this.config)==null?void 0:s.isShowOrder)&&(t==null?void 0:t.order)>0&&(n=`${t.order} ${n}`),P.drawText(this.canvas,g.changePointByZoom(t.pointList[0],this.zoom,this.currentPos),n,y({color:l.stroke},R)),this.renderTextAttribute()}}renderHoverPolygon(){var t;if(this.hoverID&&this.hoverID!==this.editPolygonID){const{hoverPolygon:e}=this;if(e){let i="";const s=this.getColor(e.attribute);e.valid?i=s.validHover.fill:i=T.getStrokeAndFill(s,!1,{isHover:!0}).fill,P.drawPolygonWithFill(this.canvas,g.changePointListByZoom(e.pointList,this.zoom,this.currentPos),{color:i,lineType:(t=this.config)==null?void 0:t.lineType})}}}renderPolygon(){var t,e,i,s;this.renderStaticPolygon(),this.renderHoverPolygon(),this.renderSelectedPolygons();const o=this.getColor(this.defaultAttribute),l=T.getStrokeAndFill(o,!this.isCtrl);if(((t=this.drawingPointList)==null?void 0:t.length)>0){let n=[...this.drawingPointList],r=g.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos);if(this.isThreePointsMode)n=b.getRectangleByRightAngle(r,n);else if(((e=this.config)==null?void 0:e.edgeAdsorption)&&this.isAlt===!1){const{dropFoot:a}=u.getClosestPoint(r,this.polygonList,(i=this.config)==null?void 0:i.lineType,k/this.zoom);a&&(r=a),n.push(r)}else this.isTwoPointsMode?n=this.createRectByTwoPointsMode(n,r):n.push(r);const h=g.changePointListByZoom(n,this.zoom,this.currentPos);P.drawSelectedPolygonWithFillAndLine(this.canvas,h,{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!1,lineType:this.config.lineType}),this.isTwoPointsMode&&P.drawLine(this.canvas,h[0],h[1],{color:"white",thickness:3,lineDash:[6]})}if(this.hoverPointIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const r=T.getStrokeAndFill(o,n.valid,{isSelected:!0}),h=n==null?void 0:n.pointList[this.hoverPointIndex];if(h){const{x:a,y:c}=g.changePointByZoom(h,this.zoom,this.currentPos);P.drawCircleWithFill(this.canvas,{x:a,y:c},5,{color:r.fill})}}if(this.hoverEdgeIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const r=T.getStrokeAndFill(o,n.valid,{isSelected:!0});P.drawLineWithPointList(this.canvas,g.changePointListByZoom(n.pointList,this.zoom,this.currentPos),{color:r.stroke,thickness:10,hoverEdgeIndex:this.hoverEdgeIndex,lineType:(s=this.config)==null?void 0:s.lineType})}}createRectByTwoPointsMode(t,e){const i=t[0],s=[...t],o=e.x>i.x&&e.y>i.y,l=e.x<i.x&&e.y<i.y;return o||l?s.push({x:i.x,y:e.y},e,{x:e.x,y:i.y}):s.push({x:e.x,y:i.y},e,{x:i.x,y:e.y}),s}render(){!this.ctx||(super.render(),this.renderPolygon(),this.renderCursorLine(this.getLineColor(this.defaultAttribute)))}renderCursorLine(t){if(super.renderCursorLine(t),this.isCombined){const{x:e,y:i}=this.coord,s=10,o=186,l=32;P.drawRectWithFill(this.canvas,{x:e+s,y:i-s*4-1,width:o,height:l},{color:"black"}),P.drawText(this.canvas,{x:e,y:i},_.t("ClickAnotherPolygon"),{textAlign:"center",color:"white",offsetX:o/2+s,offsetY:-(l/2+s/2)}),P.drawRect(this.canvas,{x:e-s,y:i-s,width:s*2,height:s*2},{lineDash:[6],color:"white"})}}undo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.undo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.undo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}redo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.redo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.redo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}deleteSelectedID(){this.setSelectedID("")}}export{it as default};
|
|
1
|
+
import j from"lodash";import{i18n as _}from"@labelbee/lb-utils";import b from"../../utils/MathUtils.js";import N from"../../utils/tool/RectUtils.js";import{ERotateDirection as K,ESortDirection as k,EDragTarget as v,EDragStatus as S,TEXT_ATTRIBUTE_OFFSET as E,DEFAULT_TEXT_OFFSET as M}from"../../constant/annotation.js";import L from"../../constant/keyCode.js";import{EPolygonPattern as w,ERectToolModeType as W,RECT_TOOL_MODE_NAME as $,edgeAdsorptionScope as R,ELineTypes as U,EToolName as O}from"../../constant/tool.js";import x from"../../locales/index.js";import{EMessage as C}from"../../locales/constants.js";import V from"../../utils/ActionsHistory.js";import D from"../../utils/tool/AttributeUtils.js";import c from"../../utils/tool/AxisUtils.js";import B from"../../utils/tool/CanvasUtils.js";import P from"../../utils/tool/CommonToolUtils.js";import f from"../../utils/tool/DrawUtils.js";import u from"../../utils/tool/PolygonUtils.js";import T from"../../utils/tool/StyleUtils.js";import H from"../../utils/uuid.js";import{BasicToolOperation as X}from"./basicToolOperation.js";import q from"./textAttributeClass.js";import Y from"./Selection.js";var G=Object.defineProperty,J=Object.defineProperties,Q=Object.getOwnPropertyDescriptors,F=Object.getOwnPropertySymbols,tt=Object.prototype.hasOwnProperty,et=Object.prototype.propertyIsEnumerable,Z=(I,t,e)=>t in I?G(I,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):I[t]=e,y=(I,t)=>{for(var e in t||(t={}))tt.call(t,e)&&Z(I,e,t[e]);if(F)for(var e of F(t))et.call(t,e)&&Z(I,e,t[e]);return I},m=(I,t)=>J(I,Q(t));const z=164;class it extends X{constructor(t){super(m(y({},t),{isOffscreenCanvas:!0}));this.isAllowDouble=e=>{const{selectedID:i}=this,s=this.getHoverID(e);return!!(i&&i===s)},this.textChange=e=>{this.config.textConfigurable===!1||!this.selectedID||(this.setPolygonList(D.textChange(e,this.selectedID,this.polygonList)),this.emit("selectedChange"),this.render())},this.lastMouseMoveTime=0,this.mouseMoveThrottle=16,this.emitUpdatePolygonByDrag=()=>{if(this.dragInfo){const{originPolygon:e}=this.dragInfo;this.emit("updatePolygonByDrag",[{newPolygon:this.selectedPolygon,originPolygon:e}])}},this.config=P.jsonParser(t.config),this.drawingPointList=[],this.polygonList=[],this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.drawingHistory=new V,this.isCtrl=!1,this.isAlt=!1,this.isCombined=!1,this.pattern=w.Normal,this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.selection=new Y(this)}get selectedIDs(){return this.selection.selectedIDs}get selectedID(){return this.selection.selectedID}get minArea(){return this.config.minArea||0}eventBinding(){super.eventBinding(),this.container.removeEventListener("mouseup",this.onMouseUp),this.container.addEventListener("mouseup",this.dragMouseUp),this.dblClickListener.addEvent(this.onMouseUp,this.onLeftDblClick,this.onRightDblClick,this.isAllowDouble)}eventUnbinding(){super.eventUnbinding(),this.container.removeEventListener("mouseup",this.dragMouseUp)}destroy(){super.destroy(),this._textAttributeInstance&&this._textAttributeInstance.clearTextAttribute()}get selectedPolygon(){return u.getPolygonByID(this.visiblePolygonList,this.selectedID)}get selectedPolygons(){return u.getPolygonByIDs(this.polygonList,this.selectedIDs)}get hoverPolygon(){return this.visiblePolygonList.find(t=>t.id===this.hoverID&&t.id!==this.selectedID)}get enableDrag(){return Boolean(this.selectedID&&this.dragInfo)}get visiblePolygonList(){return this.polygonList}get polygonListUnderZoom(){return this.visiblePolygonList.map(t=>m(y({},t),{pointList:c.changePointListByZoom(t.pointList,this.zoom)}))}get selectedText(){var t;return(t=this.selectedPolygon)==null?void 0:t.textAttribute}get isThreePointsMode(){return this.pattern===w.Rect&&this.drawingPointList.length===2}get isTwoPointsMode(){return this.pattern===w.Rect&&this.rectToolMode===W.TwoPoints&&this.drawingPointList.length===1}get dataList(){return this.polygonList}setPattern(t,e=!1){var i;((i=this.drawingPointList)==null?void 0:i.length)>0&&e===!0||(this.pattern=t)}get currentShowList(){let t=[];const[e,i]=P.getRenderResultList(this.polygonList,P.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);return t=e,this.isHidden&&(t=[]),i&&t.push(...i),t}get currentPageResult(){const[t]=P.getRenderResultList(this.polygonList,P.getSourceID(this.basicResult),[]);return t}setResultAndSelectedID(t,e){this.setPolygonList(t),e&&this.setSelectedID(e),this.render()}setResult(t){this.clearActiveStatus(),this.setPolygonList(t),this.render()}setPolygonDataByID(t,e){return this.polygonList.map(i=>i.id===e?y(y({},i),t):i)}rotatePolygon(t=1,e=K.Clockwise,i=this.selectedID){if(!i)return;const s=u.getPolygonByID(this.polygonList,i);if(!s)return;const o=u.updatePolygonByRotate(e,t,s==null?void 0:s.pointList);this.setPolygonList(this.setPolygonDataByID({pointList:o},i)),this.render()}rotatePolygonEdge(t,e=this.selectedID){!e||!u.getPolygonByID(this.polygonList,e)||(this.setPolygonList(this.setPolygonDataByID({pointList:t},e)),this.render())}addPointInDrawing(t){if(!this.imgInfo||this.forbidAddNewPolygonFuc&&this.forbidAddNewPolygonFuc(t))return;const{upperLimitPointNum:e,edgeAdsorption:i}=this.config;if(e&&this.drawingPointList.length>=e){this.emit("messageInfo",`${x.getMessagesByLocale(C.UpperLimitErrorNotice,this.lang)}${e}`);return}this.rectToolMode=localStorage.getItem($),this.deleteSelectedID();const s=this.getCoordinateUnderZoom(t),o=c.changeDrawOutsideTarget(s,{x:0,y:0},this.imgInfo,this.config.drawOutsideTarget,this.basicResult,this.zoom);if(c.returnClosePointIndex(o,c.changePointListByZoom(this.drawingPointList,this.zoom))===0){this.addDrawingPointToPolygonList();return}const{dropFoot:l}=u.getClosestPoint(o,this.polygonListUnderZoom,this.config.lineType,R),n=c.changePointByZoom(l&&t.altKey===!1&&i?l:o,1/this.zoom);if(this.isThreePointsMode||this.isTwoPointsMode){if(this.isThreePointsMode){const h=b.getRectangleByRightAngle(n,this.drawingPointList);this.drawingPointList=h}else this.isTwoPointsMode&&(this.drawingPointList=this.createRectByTwoPointsMode(this.drawingPointList,n));if(this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(this.drawingPointList)){this.emit("messageInfo",`${x.getMessagesByLocale(C.ForbiddenCreationOutsideBoundary,this.lang)}`),this.drawingPointList=[];return}this.addDrawingPointToPolygonList(!0);return}this.drawingPointList.push(n),this.drawingPointList.length===1?this.drawingHistory.initRecord(this.drawingPointList):this.drawingHistory.pushHistory(this.drawingPointList)}clearResult(){this.setPolygonList([]),this.deleteSelectedID(),this.render()}clearPolygonDrag(){this.drawingPointList=[],this.stopDrag(),this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,this.hoverID=""}clearActiveStatus(){this.clearPolygonDrag(),this.deleteSelectedID()}clearDrawingStatus(){this.drawingPointList=[]}setPolygonList(t){const e=this.polygonList.length;this.polygonList=t,e!==t.length&&this.emit("updatePageNumber")}setSelectedID(t,e=!1){this.selection.setSelectedIDs(t,e),this.render(),this.emit("selectedChange")}setDefaultAttribute(t=""){const e=this.defaultAttribute;if(this.defaultAttribute=t,e!==t&&(this.changeStyle(t),this.emit("changeAttributeSidebar"),this.selectedPolygons.length>0&&(this.selectedPolygons.forEach(i=>{i.attribute=t}),this.history.pushHistory(this.polygonList),this.render()),this._textAttributeInstance)){if(this.attributeLockList.length>0&&!this.attributeLockList.includes(t)){this._textAttributeInstance.clearTextAttribute();return}this._textAttributeInstance.updateIcon(this.getTextIconSvg(t))}}setStyle(t){var e;super.setStyle(t),this._textAttributeInstance&&this.config.attributeConfigurable===!1&&((e=this._textAttributeInstance)==null||e.updateIcon(this.getTextIconSvg()))}setPolygonValidAndRender(t,e){if(!t)return;const i=this.polygonList.map(s=>{var o;if(s.id===t){const r=(o=s==null?void 0:s.valid)!=null?o:!0;return m(y({},s),{valid:e!=null?e:!r})}return s});this.setPolygonList(i),this.history.pushHistory(this.polygonList),this.render(),this.emit("updateResult")}addDrawingPointToPolygonList(t){let{lowerLimitPointNum:e=3}=this.config;e<3&&(e=3);let i;if(this.drawingPointList.length<e){this.drawingPointList=[],this.editPolygonID="";return}if(u.calcPolygonSize(this.drawingPointList||[])<this.minArea){this.emit("messageInfo",`${x.getMessagesByLocale(C.MinAreaLimitErrorNotice,this.lang)}${this.minArea}`);return}const s=P.getSourceID(this.basicResult),o=[...this.polygonList];if(this.editPolygonID){const r=o.find(l=>l.id===this.editPolygonID);if(!r)return;r.pointList=this.drawingPointList,this.editPolygonID=""}else{const r=H(8,62);let l={id:r,sourceID:s,valid:!this.isCtrl,textAttribute:"",pointList:this.drawingPointList,attribute:this.defaultAttribute,order:P.getMaxOrder(o.filter(n=>P.isSameSourceID(n.sourceID,s)))+1};if(this.config.textConfigurable){let n="";n=D.getTextAttribute(this.polygonList.filter(h=>P.isSameSourceID(h.sourceID,s)),this.config.textCheckType),l=m(y({},l),{textAttribute:n})}this.pattern===w.Rect&&t===!0&&(l=m(y({},l),{isRect:!0})),o.push(l),i=l,this.setSelectedIdAfterAddingDrawing(r)}this.setPolygonList(o),this.isCtrl=!1,this.drawingPointList=[],this.history.pushHistory(o),i&&this.emit("polygonCreated",i,this.zoom,this.currentPos)}setSelectedIdAfterAddingDrawing(t){this.drawingPointList.length!==0&&(this.config.textConfigurable?this.setSelectedID(t):this.deleteSelectedID())}getHoverID(t){var e;const i=this.getCoordinateUnderZoom(t),s=this.currentShowList.map(o=>m(y({},o),{pointList:c.changePointListByZoom(o.pointList,this.zoom)}));return u.getHoverPolygonID(i,s,10,(e=this.config)==null?void 0:e.lineType)}getHoverEdgeIndex(t){var e;if(!this.selectedID)return-1;const i=this.selectedPolygon;if(!i)return-1;const s=this.getCoordinateUnderZoom(t),o=c.changePointListByZoom(i.pointList,this.zoom);return u.getHoverEdgeIndex(s,o,(e=this.config)==null?void 0:e.lineType)}getHoverPointIndex(t){if(!this.selectedID)return-1;const e=this.selectedPolygon;if(!e)return-1;const i=this.getCoordinateUnderZoom(t),s=c.changePointListByZoom(e.pointList,this.zoom);return c.returnClosePointIndex(i,s)}deletePolygons(t){var e;if(!t||t.length===0)return;const i=this.polygonList.filter(s=>this.selection.isIdSelected(s.id));this.emit("deletedObject",{deletedObject:i[0],id:i[0].id}),this.setPolygonList(this.polygonList.filter(s=>!this.selection.isIdSelected(s.id))),this.history.pushHistory(this.polygonList),(e=this._textAttributeInstance)==null||e.clearTextAttribute(),this.emit("selectedChange"),this.render()}deletePolygonPoint(t){if(!this.selectedID)return;const{selectedPolygon:e}=this;if(!e)return;let{lowerLimitPointNum:i}=this.config;if(i<3&&(i=3),e.pointList.length<=i){this.emit("messageInfo",`${x.getMessagesByLocale(C.LowerLimitErrorNotice,this.lang)}${i}`);return}e==null||e.pointList.splice(t,1),this.history.pushHistory(this.polygonList),this.render()}spaceKeydown(){var t,e,i;if(this.selectedID){if(((t=this.selectedPolygon)==null?void 0:t.isRect)===!0){this.emit("messageInfo",`${x.getMessagesByLocale(C.UnableToReannotation,this.lang)}`);return}this.editPolygonID=this.selectedID,this.drawingPointList=(i=(e=this.selectedPolygon)==null?void 0:e.pointList)!=null?i:[],this.drawingHistory.empty(),this.drawingHistory.initRecord(this.drawingPointList),this.hoverID="",this.deleteSelectedID(),this.render()}}onTabKeyDown(t){t.preventDefault();let e=k.ascend;t.shiftKey&&(e=k.descend),this.switchToNextPolygon(e)}switchToNextPolygon(t){if(this.drawingPointList.length>0)return;const[e,i]=P.getRenderResultList(this.polygonList,P.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let s=[...e];i&&(s=[...s,...i]);const o=B.getViewPort(this.canvas,this.currentPos,this.zoom),r=s.map(n=>{var h,a,g,d;return m(y({},n),{x:(a=(h=n.pointList[0])==null?void 0:h.x)!=null?a:0,y:(d=(g=n.pointList[0])==null?void 0:g.y)!=null?d:0})}).filter(n=>B.inViewPort({x:n.x,y:n.y},o)),l=P.getNextSelectedRectID(r,t,this.selectedID);if(l){this.setSelectedID(l.id);const{selectedPolygon:n}=this;n&&this.setDefaultAttribute(n.attribute)}this.render()}onKeyDown(t){if(!P.hotkeyFilter(t)||super.onKeyDown(t)===!1||this.selection.triggerKeyboardEvent(t,this.setPolygonList.bind(this)))return;const{keyCode:e}=t;switch(e){case L.Space:this.spaceKeydown();break;case L.Esc:this.drawingPointList=[],this.editPolygonID="";break;case L.F:this.selectedID&&this.setPolygonValidAndRender(this.selectedID);break;case L.Z:if(t.altKey){this.onCombinedExecute();return}this.setIsHidden(!this.isHidden),this.render();break;case L.Delete:this.deletePolygons(this.selectedIDs),this.render();break;case L.Ctrl:this.isCtrl=!0;break;case L.Alt:this.isAlt===!1&&(t.preventDefault(),this.isAlt=!0,this.render());break;case L.Tab:{this.onTabKeyDown(t);break}case L.X:t.altKey&&this.segment();break;default:{if(this.config.attributeConfigurable){const i=D.getAttributeByKeycode(e,this.config.attributeList);i!==void 0&&this.setDefaultAttribute(i)}break}}}onKeyUp(t){switch(super.onKeyUp(t),t.keyCode){case L.Ctrl:this.isCtrl=!1;break;case L.Alt:{const e=this.isAlt;this.isAlt=!1,e===!0&&this.render();break}}}rightMouseUp(t){if(this.drawingPointList.length>0){this.addDrawingPointToPolygonList();return}this.setSelectedID(this.hoverID,t.ctrlKey);const{selectedPolygon:e}=this;e&&this.setDefaultAttribute(e.attribute)}onLeftDblClick(t){if(this.hoverEdgeIndex>-1){const e=this.getCoordinateUnderZoom(t),{selectedPolygon:i}=this;if(!i)return;if(i.isRect===!0){this.emit("messageInfo",`${x.getMessagesByLocale(C.ForbidAddNewPoint,this.lang)}`),this.clearPolygonDrag();return}const{dropFoot:s}=u.getClosestPoint(e,this.polygonListUnderZoom,this.config.lineType,R);if(!s)return;const{upperLimitPointNum:o}=this.config;if(o&&i.pointList.length>=o){this.emit("messageInfo",`${x.getMessagesByLocale(C.UpperLimitErrorNotice,this.lang)}${o}`),this.clearPolygonDrag();return}i==null||i.pointList.splice(this.hoverEdgeIndex+1,0,c.changePointByZoom(s,1/this.zoom)),this.setPolygonDataByID(i,this.selectedID),this.history.pushHistory(this.polygonList),this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.render()}this.dragInfo=void 0}onRightDblClick(t){this.dragInfo=void 0,this.clearImgDrag();const e=this.getHoverID(t),i=this.getHoverPointIndex(t);if(this.hoverPointIndex>-1&&this.hoverPointIndex===i){this.deletePolygonPoint(i),this.dragInfo=void 0,this.hoverPointIndex=-1,this.render();return}this.hoverID===this.selectedID&&this.deletePolygons([e]),this.render()}onMouseDown(t){if(!(super.onMouseDown(t)||this.forbidMouseOperation||t.ctrlKey===!0))return this.dragMouseDown(t),!0}onMouseLeave(){this.dragInfo&&(this.emitUpdatePolygonByDrag(),this.clearPolygonDrag(),this.history.pushHistory(this.polygonList))}dragMouseDown(t){if(t.button!==0)return;const e=this.getHoverID(t);if(!this.selection.isIdSelected(e))return;let i=[0],s=v.Plane;const o=this.selectedPolygons[0].pointList,r=this.getCoordinateUnderZoom(t);if(this.selectedPolygons.length===1){this.dragStatus=S.Start;const l=this.getHoverPointIndex(t),n=this.getHoverEdgeIndex(t);l>-1?(s=v.Point,i=[l]):n>-1&&this.hoverEdgeIndex>-1&&(s=v.Line,i=[n,(n+1)%o.length])}this.dragInfo={dragStartCoord:r,dragTarget:s,initPointList:o,changePointIndex:i,originPolygon:this.selectedPolygon,selectedPolygons:this.selectedPolygons,dragPrevCoord:r,originPolygonList:this.polygonList}}segment(){var t;if(!this.selectedID||((t=this.config)==null?void 0:t.lineType)!==U.Line)return;const e=u.getPolygonPointList(this.selectedID,this.currentShowList),i=this.currentShowList.filter(r=>r.id!==this.selectedID);if(i.length===0||e.length===0)return;const s=u.getWrapPolygonIndex(e,i);let o=[...this.polygonList];if(s===-1){const r=u.segmentPolygonByPolygon(e,i);if(!r)return;const l=r.shift();if(!l)return;let n="",h=!0;const a=P.getSourceID(this.basicResult);let g="";o=this.polygonList.map(d=>{var p,A;return d.id===this.selectedID?(n=d.attribute,h=(p=d==null?void 0:d.valid)!=null?p:!0,g=(A=d==null?void 0:d.textAttribute)!=null?A:"",m(y({},d),{pointList:l})):d}),r.length>0&&r.forEach((d,p)=>{o.push({sourceID:a,id:H(8,62),pointList:d,valid:h,order:P.getMaxOrder(this.currentShowList)+1+p,attribute:n,textAttribute:g})})}else o[s].pointList=u.clipPolygonFromWrapPolygon(e,o[s].pointList),o=o.filter(r=>r.id!==this.selectedID);this.setPolygonList(o),this.history.pushHistory(o),this.render()}onCombinedExecute(){if(!this.selectedID){this.emit("messageInfo",_.t("PolygonsToBeCombinedNeedToBeSelected"));return}this.isCombined=!this.isCombined}combine(t){var e;const i=this.getHoverID(t);if(!i||!this.selectedID||this.selectedID===i)return;if(((e=this.config)==null?void 0:e.lineType)!==U.Line){this.emit("messageInfo",_.t("CurveModeDoesNotSupportCutting"));return}const s=this.polygonList.find(h=>h.id===this.selectedID),o=this.currentShowList.find(h=>h.id===i);if(!o||!s)return;const r=u.combinePolygonWithPolygon(s,o);if(!r)return;const{newPolygon:l,unionList:n}=r;if(n.length===1&&l){const h=this.polygonList.filter(a=>!n.includes(a.id)).map(a=>a.id===this.selectedID?l:a);this.setPolygonList(h),this.history.pushHistory(h),this.render(),this.emit("messageInfo",_.t("CombineSuccess"))}else this.emit("messageInfo",_.t("CombiningFailedNotify"));this.isCombined=!1}isPolygonOutSide(t){if(this.dependToolName&&this.basicCanvas&&this.basicResult){let l=!1;switch(this.dependToolName){case O.Rect:{l=t.filter(n=>!N.isInRect(n,this.basicResult)).length>0;break}case O.Polygon:{l=u.isPointListOutSidePolygon(t,this.basicResult.pointList,this.config.lineType);break}}return l}if(!this.imgInfo)return!1;const{left:e,top:i,right:s,bottom:o}=b.calcViewportBoundaries(c.changePointListByZoom(t,this.zoom)),r=1e-5;return e<0||i<0||s>this.imgInfo.width+r||o>this.imgInfo.height+r}onDragMove(t){const e=this.polygonList.map(i=>{if(this.selectedIDs.includes(i.id)){const s=this.dragPolygon(t,i);if(!s)return i;const o=m(y({},i),{pointList:s});return i.isRect===!0&&this.pattern===w.Normal&&Object.assign(o,{isRect:!1}),o}return i});this.dragInfo.dragPrevCoord=this.getCoordinateUnderZoom(t),this.setPolygonList(e)}getDragOffset(t,e){var i;const s=this.getCoordinateUnderZoom(t),{dragTarget:o,dragPrevCoord:r,changePointIndex:l,initPointList:n,dragStartCoord:h}=this.dragInfo;if(this.pattern===w.Rect&&(e==null?void 0:e.isRect)===!0&&l&&[v.Line].includes(o)){const a=b.getArrayIndex(l[0]-2,4),g=b.getArrayIndex(l[0]-1,4),d=[n[a],n[g]],p=b.getRectPerpendicularOffset(h,s,d);return{x:p.x/this.zoom,y:p.y/this.zoom}}return((i=this.dragInfo)==null?void 0:i.dragTarget)===v.Plane?{x:(s.x-r.x)/this.zoom,y:(s.y-r.y)/this.zoom}:{x:(s.x-h.x)/this.zoom,y:(s.y-h.y)/this.zoom}}dragPolygon(t,e){let i=j.cloneDeep(e==null?void 0:e.pointList);if(!i||!this.dragInfo)return;const{initPointList:s,dragTarget:o,changePointIndex:r}=this.dragInfo,l=this.getDragOffset(t,e);switch(this.dragStatus=S.Move,o){case v.Plane:i=i.map(n=>m(y({},n),{x:n.x+l.x,y:n.y+l.y}));break;case v.Point:case v.Line:i=i.map((n,h)=>r&&r.includes(h)?m(y({},n),{x:s[h].x+l.x,y:s[h].y+l.y}):n);break}if(this.pattern===w.Rect&&(e==null?void 0:e.isRect)===!0&&o===v.Point&&r&&(i=b.getPointListFromPointOffset(s,r[0],l)),!(this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(i)))return i}determineTrigger(t){if(this.isDrag)return"drag";if(this.dragInfo)return"dragSingle";let e="";const i=this.getHoverID(t);this.hoverID!==i?(this.hoverID=i,e=""):e="move";let s=-1,o=-1;const{selectedID:r}=this;return r&&(s=this.getHoverPointIndex(t),s>-1?this.hoverPointIndex!==s?e="":e="noRender":(o=this.getHoverEdgeIndex(t),this.hoverEdgeIndex!==o?e="":e="noRender"),this.hoverEdgeIndex=o,this.hoverPointIndex=s),e}onMouseMove(t){requestAnimationFrame(()=>{const e=Date.now();if(e-this.lastMouseMoveTime<this.mouseMoveThrottle||(this.lastMouseMoveTime=e,super.onMouseMove(t,!1)||this.forbidMouseOperation||!this.imgInfo))return;if(this.drawingPointList.length>0){this.render();return}const i=this.determineTrigger(t);this.selectedIDs.length>0&&this.dragInfo&&this.onDragMove(t),this.render(i)})}leftMouseUpdateValid(t){const e=this.getHoverID(t);return this.drawingPointList.length===0&&t.ctrlKey===!0&&e?(this.setPolygonValidAndRender(e),!0):!1}stopDrag(){this.dragInfo=void 0,this.dragStatus=S.Wait}leftMouseUp(t){this.leftMouseUpdateValid(t)||this.addPointInDrawing(t)}onMouseUp(t){if(this.isCombined){switch(t.button){case 0:this.combine(t);break;case 2:this.isCombined=!1;break;default:return}return}if(!(super.onMouseUp(t)||this.forbidMouseOperation||!this.imgInfo)){if(this.dragInfo&&this.dragStatus===S.Move){this.emit("updateResult"),this.emitUpdatePolygonByDrag(),this.stopDrag(),this.history.pushHistory(this.polygonList);return}if(this.dragInfo&&this.dragStatus===S.Start){this.stopDrag();return}switch(t.button){case 0:{this.leftMouseUp(t);break}case 2:{this.rightMouseUp(t);break}}this.render()}}dragMouseUp(){this.dragStatus===S.Start&&this.stopDrag()}exportData(){const{polygonList:t}=this;return[t,this.basicImgInfo]}getTextIconSvg(t=""){return D.getTextIconSvg(t,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}getCurrentSelectedData(){const{selectedPolygon:t}=this;if(!t)return;const e=this.getColor(t.attribute),i=t.valid?e==null?void 0:e.valid.stroke:e==null?void 0:e.invalid.stroke;return{width:z,textAttribute:t.textAttribute,color:i}}updateSelectedTextAttribute(t){if(this._textAttributeInstance&&t&&this.selectedID){let e=t;D.textAttributeValidate(this.config.textCheckType,"",e)===!1&&(this.emit("messageError",D.getErrorNotice(this.config.textCheckType,this.lang)),e=""),this.setPolygonList(D.textChange(e,this.selectedID,this.polygonList)),this.emit("updateTextAttribute"),this.render()}}renderTextAttribute(){var t;const{selectedPolygon:e}=this;if(!this.ctx||this.config.textConfigurable===!1||!e)return;const{pointList:i,attribute:s,valid:o,textAttribute:r}=e,{x:l,y:n}=i[i.length-1],h=z,a=c.getOffsetCoordinate({x:l,y:n},this.currentPos,this.zoom),g=this.getColor(s),d=o?g==null?void 0:g.valid.stroke:g==null?void 0:g.invalid.stroke;this._textAttributeInstance||(this._textAttributeInstance=new q({width:h,container:this.container,icon:this.getTextIconSvg(s),color:d,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((t=this._textAttributeInstance)==null?void 0:t.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${r}`,{left:a.x,top:a.y,color:d,width:h})}renderStaticPolygon(){var t;this.isHidden===!1&&((t=this.visiblePolygonList)==null||t.forEach(e=>{var i,s,o,r,l;if([this.selectedID,this.editPolygonID].includes(e.id))return;const{textAttribute:n,attribute:h}=e,a=this.getColor(h),g=T.getStrokeAndFill(a,e.valid),d=c.changePointListByZoom(e.pointList||[],this.zoom,this.currentPos);f.drawPolygonWithFillAndLine(this.canvas,d,{fillColor:g.fill,strokeColor:g.stroke,pointColor:"white",thickness:(s=(i=this.style)==null?void 0:i.width)!=null?s:2,lineCap:"round",isClose:!0,lineType:(o=this.config)==null?void 0:o.lineType});let p=`${(r=D.getAttributeShowText(h,this.config.attributeList))!=null?r:""}`;((l=this.config)==null?void 0:l.isShowOrder)&&(e==null?void 0:e.order)>0&&(p=`${e.order} ${p}`),f.drawText(this.canvas,d[0],p,y({color:g.stroke},M));const A=d[d.length-1];f.drawText(this.canvas,{x:A.x+E.x,y:A.y+E.y},n,y({color:g.stroke},M))}))}renderSelectedPolygons(){this.selectedPolygons.forEach(t=>{this.renderSelectedPolygon(t)})}renderSelectedPolygon(t){var e,i,s;if(t){const o=this.getColor(t.attribute),r=T.getStrokeAndFill(o,t.valid,{isSelected:!0});f.drawSelectedPolygonWithFillAndLine(this.canvas,c.changePointListByZoom(t.pointList,this.zoom,this.currentPos),{fillColor:r.fill,strokeColor:r.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!0,lineType:(e=this.config)==null?void 0:e.lineType});let l=`${(i=D.getAttributeShowText(t.attribute,this.config.attributeList))!=null?i:""}`;((s=this.config)==null?void 0:s.isShowOrder)&&(t==null?void 0:t.order)>0&&(l=`${t.order} ${l}`),f.drawText(this.canvas,c.changePointByZoom(t.pointList[0],this.zoom,this.currentPos),l,y({color:r.stroke},M)),this.renderTextAttribute()}}renderHoverPolygon(){var t;if(this.hoverID&&this.hoverID!==this.editPolygonID){const{hoverPolygon:e}=this;if(e){let i="";const s=this.getColor(e.attribute);e.valid?i=s.validHover.fill:i=T.getStrokeAndFill(s,!1,{isHover:!0}).fill,f.drawPolygonWithFill(this.canvas,c.changePointListByZoom(e.pointList,this.zoom,this.currentPos),{color:i,lineType:(t=this.config)==null?void 0:t.lineType})}}}renderPolygon(t){var e,i,s,o;t!=="move"&&this.renderStaticPolygon(),this.renderHoverPolygon(),this.renderSelectedPolygons();const r=this.getColor(this.defaultAttribute),l=T.getStrokeAndFill(r,!this.isCtrl);if(((e=this.drawingPointList)==null?void 0:e.length)>0){let n=[...this.drawingPointList],h=c.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos);if(this.isThreePointsMode)n=b.getRectangleByRightAngle(h,n);else if(((i=this.config)==null?void 0:i.edgeAdsorption)&&this.isAlt===!1){const{dropFoot:g}=u.getClosestPoint(h,this.polygonList,(s=this.config)==null?void 0:s.lineType,R/this.zoom);g&&(h=g),n.push(h)}else this.isTwoPointsMode?n=this.createRectByTwoPointsMode(n,h):n.push(h);const a=c.changePointListByZoom(n,this.zoom,this.currentPos);f.drawSelectedPolygonWithFillAndLine(this.canvas,a,{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!1,lineType:this.config.lineType}),this.isTwoPointsMode&&f.drawLine(this.canvas,a[0],a[1],{color:"white",thickness:3,lineDash:[6]})}if(this.hoverPointIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const h=T.getStrokeAndFill(r,n.valid,{isSelected:!0}),a=n==null?void 0:n.pointList[this.hoverPointIndex];if(a){const{x:g,y:d}=c.changePointByZoom(a,this.zoom,this.currentPos);f.drawCircleWithFill(this.canvas,{x:g,y:d},5,{color:h.fill})}}if(this.hoverEdgeIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const h=T.getStrokeAndFill(r,n.valid,{isSelected:!0});f.drawLineWithPointList(this.canvas,c.changePointListByZoom(n.pointList,this.zoom,this.currentPos),{color:h.stroke,thickness:10,hoverEdgeIndex:this.hoverEdgeIndex,lineType:(o=this.config)==null?void 0:o.lineType})}}createRectByTwoPointsMode(t,e){const i=t[0],s=[...t],o=e.x>i.x&&e.y>i.y,r=e.x<i.x&&e.y<i.y;return o||r?s.push({x:i.x,y:e.y},e,{x:e.x,y:i.y}):s.push({x:e.x,y:i.y},e,{x:i.x,y:e.y}),s}render(t){!this.ctx||(t!=="move"&&super.render(),t!=="drag"&&t!=="dragSingle"&&this.renderPolygon(t),t==="dragSingle"&&this.renderSelectedPolygons(),this.renderCursorLine(this.getLineColor(this.defaultAttribute)))}renderCursorLine(t){requestAnimationFrame(()=>{this.clearOffscreenCanvas();const{x:e,y:i}=this.coord;if(f.drawLine(this.offscreenCanvas,{x:0,y:i},{x:1e4,y:i},{color:t}),f.drawLine(this.offscreenCanvas,{x:e,y:0},{x:e,y:1e4},{color:t}),f.drawCircleWithFill(this.offscreenCanvas,{x:e,y:i},1,{color:"white"}),this.isCombined){const s=10,o=186,r=32;f.drawRectWithFill(this.canvas,{x:e+s,y:i-s*4-1,width:o,height:r},{color:"black"}),f.drawText(this.canvas,{x:e,y:i},_.t("ClickAnotherPolygon"),{textAlign:"center",color:"white",offsetX:o/2+s,offsetY:-(r/2+s/2)}),f.drawRect(this.canvas,{x:e-s,y:i-s,width:s*2,height:s*2},{lineDash:[6],color:"white"})}})}undo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.undo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.undo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}redo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.redo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.redo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}deleteSelectedID(){this.setSelectedID("")}}export{it as default};
|