fix(tag): fix label tag

This commit is contained in:
zhangheng
2026-04-07 13:52:27 +08:00
parent 92fc5c47e5
commit 5db5e18049
2 changed files with 33 additions and 3 deletions

View File

@@ -5328,6 +5328,15 @@ export const usePaperStore = create<PaperState>((set) => ({
}) as paper.Item[] }) as paper.Item[]
tagItems.forEach((item) => { tagItems.forEach((item) => {
if (item.data?.tag_category === "mask") return if (item.data?.tag_category === "mask") return
const anchor = item.data?.tag_anchor
if (Array.isArray(anchor) && anchor.length >= 2) {
const anchorX = Number(anchor[0])
const anchorY = Number(anchor[1])
if (Number.isFinite(anchorX) && Number.isFinite(anchorY)) {
item.scale(1 / newScale, new paper.Point(anchorX, anchorY))
return
}
}
item.scale(1 / newScale) item.scale(1 / newScale)
}) })
} }

View File

@@ -19,8 +19,20 @@ const useRenderTag = () => {
const dataMap = useLabelStore.getState().label const dataMap = useLabelStore.getState().label
const activeImage = useBottomToolsStore.getState().activeImage const activeImage = useBottomToolsStore.getState().activeImage
const group = usePaperStore.getState().group const group = usePaperStore.getState().group
if (!group) return
const visible = useTopToolsStore.getState().showTags const visible = useTopToolsStore.getState().showTags
const configs = useTopToolsStore.getState().showTagsConfigs const configs = useTopToolsStore.getState().showTagsConfigs
const groupScale =
Number.isFinite(group.scaling?.x) && group.scaling.x !== 0
? group.scaling.x
: 1
const applyTagCompensation = (
item: paper.Item,
anchor: [number, number]
) => {
if (Math.abs(groupScale - 1) < 0.000001) return
item.scale(1 / groupScale, new paper.Point(anchor[0], anchor[1]))
}
// 清空之前渲染的标签 // 清空之前渲染的标签
removeTagTexts() removeTagTexts()
@@ -38,7 +50,8 @@ const useRenderTag = () => {
const path = usePaperStore.getState().getItemById(id) const path = usePaperStore.getState().getItemById(id)
if (path) { if (path) {
const bounds = path.bounds const bounds = path.bounds
let pos = [bounds.left, bounds.top - 6] const tagAnchor: [number, number] = [bounds.left, bounds.top - 6]
const pos = [...tagAnchor]
let pathData = path.data let pathData = path.data
let content = `` let content = ``
if (configs.includes("ID")) if (configs.includes("ID"))
@@ -68,10 +81,12 @@ const useRenderTag = () => {
id, id,
type: "tag", type: "tag",
tag_category: "text", tag_category: "text",
tag_anchor: tagAnchor,
}, },
parent: group, parent: group,
visible, visible,
}) })
applyTagCompensation(text, tagAnchor)
// 文本框 // 文本框
const textMask = new paper.Path.Rectangle(text.bounds) const textMask = new paper.Path.Rectangle(text.bounds)
@@ -84,10 +99,12 @@ const useRenderTag = () => {
id, id,
type: "tag", type: "tag",
tag_category: "text_mask", tag_category: "text_mask",
tag_anchor: tagAnchor,
}, },
parent: group, parent: group,
visible, visible,
}) })
applyTagCompensation(textMask, tagAnchor)
// 标注对象外接框 // 标注对象外接框
const mask = new paper.Path.Rectangle(bounds) const mask = new paper.Path.Rectangle(bounds)
mask.set({ mask.set({
@@ -108,7 +125,7 @@ const useRenderTag = () => {
path.children.forEach((child) => { path.children.forEach((child) => {
const segments = (child as paper.Path).segments const segments = (child as paper.Path).segments
segments.forEach((segment) => { segments.forEach((segment) => {
let pointTextPosition = [ const pointTextPosition: [number, number] = [
segment.point.x - 10, segment.point.x - 10,
segment.point.y + 5, segment.point.y + 5,
] ]
@@ -122,17 +139,19 @@ const useRenderTag = () => {
id, id,
type: "tag", type: "tag",
tag_category: "point_text", tag_category: "point_text",
tag_anchor: pointTextPosition,
}, },
parent: group, parent: group,
visible: visible && configs.includes("点ID"), visible: visible && configs.includes("点ID"),
}) })
applyTagCompensation(pointText, pointTextPosition)
index++ index++
}) })
}) })
} else { } else {
const segments = (path as paper.Path).segments const segments = (path as paper.Path).segments
segments.forEach((segment, index) => { segments.forEach((segment, index) => {
let pointTextPosition = [ const pointTextPosition: [number, number] = [
segment.point.x - 10, segment.point.x - 10,
segment.point.y + 5, segment.point.y + 5,
] ]
@@ -146,10 +165,12 @@ const useRenderTag = () => {
id, id,
type: "tag", type: "tag",
tag_category: "point_text", tag_category: "point_text",
tag_anchor: pointTextPosition,
}, },
parent: group, parent: group,
visible: visible && configs.includes("点ID"), visible: visible && configs.includes("点ID"),
}) })
applyTagCompensation(pointText, pointTextPosition)
}) })
} }
} }