feat: auto hide rect bounding when not hover

This commit is contained in:
SikongJueluo 2025-06-13 21:05:44 +08:00
parent dcadb97a7f
commit 2270022bbe
1 changed files with 55 additions and 8 deletions

View File

@ -9,15 +9,18 @@
y: item.y, y: item.y,
draggable: true, draggable: true,
id: `group-${item.id}`, id: `group-${item.id}`,
}" @dragstart="handleDragStart" @dragend="handleDragEnd" @mouseover="handleCanvasObjectMouseOver"> }" @dragstart="handleDragStart" @dragend="handleDragEnd" @mouseover="handleCanvasObjectMouseOver"
<v-rect :config="item.config" /> @mouseout="handleCanvasObjectMouseOut">
<v-rect v-show="!isUndefined(item.box)" :config="{ <v-rect v-show="!isUndefined(item.box)" :config="{
...item.box, ...item.box,
visible: !isUndefined(item.box), visible: !isUndefined(item.box) && item.isHoverring,
stroke: 'red', stroke: 'rgb(125,193,239)',
strokeWidth: 3, strokeWidth: 2.5,
dash: [10, 5],
cornerRadius: 10
}"> }">
</v-rect> </v-rect>
<v-rect :config="item.config" />
</v-group> </v-group>
</template> </template>
@ -39,7 +42,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { isNull, isUndefined } from "mathjs"; import { isNull, isUndefined, ObjectNode } from "mathjs";
import Konva from "konva"; import Konva from "konva";
import type { import type {
VGroup, VGroup,
@ -68,6 +71,7 @@ type CanvasObject = {
id: string; id: string;
x: number; x: number;
y: number; y: number;
isHoverring: boolean;
box?: CanvasObjectBox; box?: CanvasObjectBox;
}; };
@ -137,6 +141,7 @@ onMounted(() => {
id: id, id: id,
x: x, x: x,
y: y, y: y,
isHoverring: false,
}); });
} }
}); });
@ -327,7 +332,13 @@ function handleMouseUp() {
node === selectRect.value?.getNode() node === selectRect.value?.getNode()
) )
return false; return false;
return Konva.Util.haveIntersection(selBox);
const nodeConfig = objMap.get(node.id());
if (isUndefined(nodeConfig) || isUndefined(nodeConfig.box)) {
return;
}
return Konva.Util.haveIntersection(selBox, nodeConfig.box);
}); });
selectedIds.value = selected.map((node: Konva.Node) => node.id()); selectedIds.value = selected.map((node: Konva.Node) => node.id());
@ -351,9 +362,10 @@ function handleCanvasObjectMouseOver(evt: Event) {
// Get client rect // Get client rect
const objectConfig = objMap.get(object.id()); const objectConfig = objMap.get(object.id());
if (isUndefined(objectConfig)) { if (isUndefined(objectConfig)) {
console.error(`Not found object id: ${object.id()}`);
return; return;
} }
// Get clientBox for first time
if (isUndefined(objectConfig.box)) { if (isUndefined(objectConfig.box)) {
if ( if (
objectConfig.config.width && objectConfig.config.width &&
@ -368,9 +380,44 @@ function handleCanvasObjectMouseOver(evt: Event) {
); );
} else console.error("Could not calculate rect bounding"); } else console.error("Could not calculate rect bounding");
} }
objectConfig.isHoverring = true;
}
function handleCanvasObjectMouseOut(evt: Event) {
if (isNull(evt.target)) return;
const target = evt.target;
let object = null;
if (target instanceof Konva.Group) {
if (!target.hasChildren()) return;
object = target.children[0];
} else if (target instanceof Konva.Rect) {
object = target;
} else {
console.trace(`Not Konva class: ${target}`);
return;
}
// Get client rect
const objectConfig = objMap.get(object.id());
if (isUndefined(objectConfig)) {
return;
}
objectConfig.isHoverring = false;
} }
</script> </script>
<style> <style>
@import "../assets/main.css"; @import "../assets/main.css";
</style> </style>
<style module>
@import "../assets/main.css";
.primary {
color: rgb(125, 193, 239)
}
</style>