feat(project): init
This commit is contained in:
140
components/label/utils/paperjs.ts
Normal file
140
components/label/utils/paperjs.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
import paper from "paper"
|
||||
|
||||
// 调整原始比例点位到当前图片的缩放比例
|
||||
export const adjustPoints: (
|
||||
id: string,
|
||||
nestedMap: Map<string, Map<string, [number, any[], any, any[]][]>>,
|
||||
scale: number
|
||||
) => Map<string, Map<string, [number, any[], any, any[]][]>> = (
|
||||
id,
|
||||
nestedMap,
|
||||
scale
|
||||
) => {
|
||||
const adjustedMap = new Map<
|
||||
string,
|
||||
Map<string, [number, any[], any, any[]][]>
|
||||
>()
|
||||
nestedMap.forEach((innerMap, outerKey) => {
|
||||
if (id === outerKey) {
|
||||
const adjustedInnerMap = new Map<
|
||||
string,
|
||||
Array<[number, any[], any, any[]]>
|
||||
>()
|
||||
innerMap.forEach((array, innerKey) => {
|
||||
const adjustedArray = array.map(
|
||||
([id, points, detail, hollowPoints]) => {
|
||||
// let adjustedPoints = points.map((point) => {
|
||||
// let flag = point instanceof paper.Point;
|
||||
// if (flag) {
|
||||
// return [point.x * scale, point.y * scale];
|
||||
// } else {
|
||||
// return point.map((item: any) => item * scale);
|
||||
// }
|
||||
// });
|
||||
|
||||
let adjustedPoints = points.map((child) => {
|
||||
return child.map((seg: any) => {
|
||||
if (seg instanceof paper.Point) {
|
||||
return [seg.x * scale, seg.y * scale]
|
||||
} else if (seg instanceof paper.Segment) {
|
||||
return [seg.point.x * scale, seg.point.y * scale]
|
||||
} else {
|
||||
return seg.map((item: any) => item * scale)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
let adjustedHollowPoints = hollowPoints.map((child) => {
|
||||
return child.map((seg: any) => {
|
||||
if (seg instanceof paper.Point) {
|
||||
return [seg.x * scale, seg.y * scale]
|
||||
} else if (seg instanceof paper.Segment) {
|
||||
return [seg.point.x * scale, seg.point.y * scale]
|
||||
} else {
|
||||
return seg.map((item: any) => item * scale)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
return [id, adjustedPoints, detail, adjustedHollowPoints]
|
||||
}
|
||||
)
|
||||
adjustedInnerMap.set(innerKey, adjustedArray as any)
|
||||
})
|
||||
adjustedMap.set(outerKey, adjustedInnerMap)
|
||||
} else {
|
||||
adjustedMap.set(outerKey, innerMap)
|
||||
}
|
||||
})
|
||||
|
||||
return adjustedMap
|
||||
}
|
||||
|
||||
// 根据比例调整不同图片的点位数据
|
||||
export const adjustAllPoints: (
|
||||
nestedMap: Map<string, Map<string, [number, any[], any, any[]][]>>,
|
||||
allScale: { [x: string]: number }
|
||||
) => Map<string, Map<string, [number, any[], any, any[]][]>> = (
|
||||
nestedMap,
|
||||
allScale
|
||||
) => {
|
||||
const adjustedMap = new Map<
|
||||
string,
|
||||
Map<string, [number, any[], any, any[]][]>
|
||||
>()
|
||||
nestedMap.forEach((innerMap, outerKey) => {
|
||||
const scale = allScale[outerKey]
|
||||
if (scale) {
|
||||
const adjustedInnerMap = new Map<
|
||||
string,
|
||||
Array<[number, any[], any, any[]]>
|
||||
>()
|
||||
innerMap.forEach((array, innerKey) => {
|
||||
const adjustedArray = array.map(
|
||||
([id, points, detail, hollowPoints]) => {
|
||||
// let adjustedPoints = points.map((point) => {
|
||||
// let flag = point instanceof paper.Point;
|
||||
// if (flag) {
|
||||
// return [point.x * scale, point.y * scale];
|
||||
// } else {
|
||||
// return point.map((item: any) => item * scale);
|
||||
// }
|
||||
// });
|
||||
|
||||
let adjustedPoints = points.map((child) => {
|
||||
return child.map((seg: any) => {
|
||||
if (seg instanceof paper.Point) {
|
||||
return [seg.x * scale, seg.y * scale]
|
||||
} else if (seg instanceof paper.Segment) {
|
||||
return [seg.point.x * scale, seg.point.y * scale]
|
||||
} else {
|
||||
return seg.map((item: any) => item * scale)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
let adjustedHollowPoints = hollowPoints.map((child) => {
|
||||
return child.map((seg: any) => {
|
||||
if (seg instanceof paper.Point) {
|
||||
return [seg.x * scale, seg.y * scale]
|
||||
} else if (seg instanceof paper.Segment) {
|
||||
return [seg.point.x * scale, seg.point.y * scale]
|
||||
} else {
|
||||
return seg.map((item: any) => item * scale)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
return [id, adjustedPoints, detail, adjustedHollowPoints]
|
||||
}
|
||||
)
|
||||
adjustedInnerMap.set(innerKey, adjustedArray as any)
|
||||
})
|
||||
adjustedMap.set(outerKey, adjustedInnerMap)
|
||||
} else {
|
||||
adjustedMap.set(outerKey, innerMap)
|
||||
}
|
||||
})
|
||||
|
||||
return adjustedMap
|
||||
}
|
||||
Reference in New Issue
Block a user