add boarder

This commit is contained in:
SikongJueluo 2025-06-12 21:49:23 +08:00
parent f340c86a41
commit 1538bb9d07
No known key found for this signature in database
1 changed files with 68 additions and 25 deletions

View File

@ -3,23 +3,25 @@
<v-stage class="h-full w-full" ref="stage" :config="stageSize" @mousedown="handleMouseDown" <v-stage class="h-full w-full" ref="stage" :config="stageSize" @mousedown="handleMouseDown"
@mousemove="handleMouseMove" @mouseup="handleMouseUp" @click="handleStageClick"> @mousemove="handleMouseMove" @mouseup="handleMouseUp" @click="handleStageClick">
<v-layer ref="layer"> <v-layer ref="layer">
<v-star v-for="item in list" :key="item.id" :config="{ <template v-for="item in list" :key="item.id">
<v-group :config="{
x: item.x, x: item.x,
y: item.y, y: item.y,
rotation: item.rotation,
id: item.id,
draggable: true, draggable: true,
numPoints: 5, id: `group-${item.id}`,
innerRadius: 30, }" @dragstart="handleDragStart" @dragend="handleDragEnd">
outerRadius: 50, <v-rect :config="{
fill: '#89b717', x: item.box.x,
opacity: 0.8, y: item.box.y,
shadowColor: 'black', width: item.box.width,
shadowBlur: 10, height: item.box.height,
shadowOpacity: 0.6, stroke: 'red',
scaleX: item.scale, strokeWidth: 1,
scaleY: item.scale, }">
}" @dragstart="handleDragStart" @dragend="handleDragEnd" /> </v-rect>
</v-group>
</template>
<v-transformer ref="transformer" /> <v-transformer ref="transformer" />
<v-rect ref="selectRect" v-if="selectionRectangle.visible" :config="{ <v-rect ref="selectRect" v-if="selectionRectangle.visible" :config="{
x: Math.min(selectionRectangle.x1, selectionRectangle.x2), x: Math.min(selectionRectangle.x1, selectionRectangle.x2),
@ -54,15 +56,50 @@ const stageSize = {
height: window.innerHeight, height: window.innerHeight,
}; };
const list = ref<Konva.NodeConfig[]>([]); type CanvasObject = {
id: string;
x: number;
y: number;
// rotation: number;
// scale: number;
object: Konva.Node;
box: {
x: number;
y: number;
width: number;
height: number;
};
};
const list = ref<CanvasObject[]>([]);
onMounted(() => { onMounted(() => {
for (let n = 0; n < 100; n++) { for (let n = 0; n < 100; n++) {
const id = Math.round(Math.random() * 10000).toString();
const x = Math.random() * stageSize.width;
const y = Math.random() * stageSize.height;
const rotation = Math.random() * 180;
const scale = Math.random();
const star = new Konva.Star({
rotation: rotation,
id: id,
numPoints: 5,
innerRadius: 30,
outerRadius: 50,
fill: "#89b717",
opacity: 0.8,
scaleX: scale,
scaleY: scale,
});
list.value.push({ list.value.push({
id: Math.round(Math.random() * 10000).toString(), id: id,
x: Math.random() * stageSize.width, x: x,
y: Math.random() * stageSize.height, y: y,
rotation: Math.random() * 180, // rotation: rotation,
scale: Math.random(), // scale: scale,
object: star,
box: star.getClientRect(),
}); });
} }
}); });
@ -85,6 +122,13 @@ const selectionRectangle = reactive({
y2: 0, y2: 0,
}); });
onMounted(() => {
if (isNull(transformer.value)) return;
const selectedBox = transformer.value.getNode();
selectedBox.resizeEnabled(false);
selectedBox.rotateEnabled(false);
});
function degToRad(angle: number) { function degToRad(angle: number) {
return (angle / 180) * Math.PI; return (angle / 180) * Math.PI;
@ -286,12 +330,11 @@ function handleMouseUp() {
node === selectRect.value?.getNode() node === selectRect.value?.getNode()
) )
return false; return false;
return Konva.Util.haveIntersection(selBox, node.getClientRect()); return Konva.Util.haveIntersection(selBox);
}); });
selectedIds.value = selected.map((node: Konva.Node) => node.id()); selectedIds.value = selected.map((node: Konva.Node) => node.id());
} }
</script> </script>
<style> <style>