fix: select not work

This commit is contained in:
SikongJueluo 2025-07-01 20:12:39 +08:00
parent fbd13f8f2f
commit 262c5e4003
No known key found for this signature in database
1 changed files with 62 additions and 60 deletions

View File

@ -12,7 +12,7 @@
> >
<v-layer ref="layerRef"> <v-layer ref="layerRef">
<template <template
ref="canvasObjects" ref="canvasObjectsRef"
v-for="item in objMap.values()" v-for="item in objMap.values()"
:key="item.id" :key="item.id"
> >
@ -111,19 +111,24 @@ function calculateRectBounding(
width: number, width: number,
height: number, height: number,
rotation: number, rotation: number,
scale: number = 1,
padding?: number, padding?: number,
) { ) {
// Apply scale to dimensions first
const scaledWidth = width * scale;
const scaledHeight = height * scale;
// calculate bounding box for rotated rectangle // calculate bounding box for rotated rectangle
const radians = (rotation * Math.PI) / 180; const radians = (rotation * Math.PI) / 180;
const cos = Math.cos(radians); const cos = Math.cos(radians);
const sin = Math.sin(radians); const sin = Math.sin(radians);
// calculate corners of the rectangle // calculate corners of the scaled rectangle
const corners = [ const corners = [
{ x: 0, y: 0 }, { x: 0, y: 0 },
{ x: width, y: 0 }, { x: scaledWidth, y: 0 },
{ x: width, y: height }, { x: scaledWidth, y: scaledHeight },
{ x: 0, y: height }, { x: 0, y: scaledHeight },
].map((point) => ({ ].map((point) => ({
x: point.x * cos - point.y * sin, x: point.x * cos - point.y * sin,
y: point.x * sin + point.y * cos, y: point.x * sin + point.y * cos,
@ -135,20 +140,22 @@ function calculateRectBounding(
const minY = Math.min(...corners.map((p) => p.y)); const minY = Math.min(...corners.map((p) => p.y));
const maxY = Math.max(...corners.map((p) => p.y)); const maxY = Math.max(...corners.map((p) => p.y));
if (padding) if (padding) {
const scaledPadding = padding * scale;
return { return {
x: minX - padding, x: minX - scaledPadding,
y: minY - padding, y: minY - scaledPadding,
width: maxX - minX + padding * 2, width: maxX - minX + scaledPadding * 2,
height: maxY - minY + padding * 2, height: maxY - minY + scaledPadding * 2,
}; };
else } else {
return { return {
x: minX, x: minX,
y: minY, y: minY,
width: maxX - minX, width: maxX - minX,
height: maxY - minY, height: maxY - minY,
}; };
}
} }
const objMap = reactive<Map<string, CanvasObject>>(new Map()); const objMap = reactive<Map<string, CanvasObject>>(new Map());
@ -178,11 +185,11 @@ onMounted(() => {
} }
}); });
const layerRef = useTemplateRef<VLayer>("layer"); const layerRef = useTemplateRef<VLayer>("layerRef");
const canvasObjectsRef = useTemplateRef<HTMLTemplateElement[]>("canvasObjects"); const canvasObjectsRef = useTemplateRef<HTMLTemplateElement[]>("canvasObjectsRef");
const transformerRef = useTemplateRef<VTransformer>("transformer"); const transformerRef = useTemplateRef<VTransformer>("transformerRef");
const selectRectRef = useTemplateRef<VNode>("selectRect"); const selectRectRef = useTemplateRef<VNode>("selectRectRef");
const stageRef = useTemplateRef<VStage>("stage"); const stageRef = useTemplateRef<VStage>("StageRef");
const isDragging = ref(false); const isDragging = ref(false);
const dragItemId = ref<string | null>(null); const dragItemId = ref<string | null>(null);
@ -207,6 +214,26 @@ onMounted(() => {
selectedBox.rotateEnabled(false); selectedBox.rotateEnabled(false);
}); });
// Update transformer nodes when selection changes
watch(selectedIds, () => {
if (isNull(transformerRef.value)) return;
const nodes = selectedIds.value.map((id) => {
if (isNull(layerRef.value)) {
console.error("layer is null");
return null;
}
for (let node of layerRef.value.getNode().children) {
if (node instanceof Konva.Group && node.id() === `group-${id}`)
return node;
}
}) as Konva.Node[];
// console.log(nodes);
if (!isUndefined(nodes)) transformerRef.value.getNode().nodes(nodes);
});
function handleCacheChange(e: Event) { function handleCacheChange(e: Event) {
const target = e.target as HTMLInputElement; const target = e.target as HTMLInputElement;
const shouldCache = isNull(target) ? false : target.checked; const shouldCache = isNull(target) ? false : target.checked;
@ -227,14 +254,6 @@ function handleDragStart(e: Event) {
// save drag element: // save drag element:
const target = e.target as unknown as Konva.Node; const target = e.target as unknown as Konva.Node;
dragItemId.value = target.id(); dragItemId.value = target.id();
// move current element to the top:
// const item = list.value.find((i) => i.id === dragItemId.value);
// if (isUndefined(item)) return;
//
// const index = list.value.indexOf(item);
// list.value.splice(index, 1);
// list.value.push(item);
} }
function handleDragEnd() { function handleDragEnd() {
@ -242,24 +261,6 @@ function handleDragEnd() {
dragItemId.value = null; dragItemId.value = null;
} }
// Update transformer nodes when selection changes
watch(selectedIds, () => {
if (isNull(transformerRef.value)) return;
const nodes = selectedIds.value.map((id) => {
if (isNull(layerRef.value)) {
console.error("layer is null");
return null;
}
for (let node of layerRef.value.getNode().children) {
if (node instanceof Konva.Group && node.id() === `group-${id}`)
return node;
}
}) as Konva.Node[];
if (!isUndefined(nodes)) transformerRef.value.getNode().nodes(nodes);
});
// Mouse event handlers // Mouse event handlers
function handleStageClick(e: { target: any; evt: MouseEvent }) { function handleStageClick(e: { target: any; evt: MouseEvent }) {
@ -315,7 +316,7 @@ function handleMouseDown(e: Event) {
x: (pos.x - stage.x()) / stage.scaleX(), x: (pos.x - stage.x()) / stage.scaleX(),
y: (pos.y - stage.y()) / stage.scaleY(), y: (pos.y - stage.y()) / stage.scaleY(),
}; };
selectionRectangle.visible = true; selectionRectangle.visible = true;
selectionRectangle.x1 = relativePos.x; selectionRectangle.x1 = relativePos.x;
selectionRectangle.y1 = relativePos.y; selectionRectangle.y1 = relativePos.y;
@ -341,7 +342,7 @@ function handleMouseMove(e: Event) {
x: (pos.x - stage.x()) / stage.scaleX(), x: (pos.x - stage.x()) / stage.scaleX(),
y: (pos.y - stage.y()) / stage.scaleY(), y: (pos.y - stage.y()) / stage.scaleY(),
}; };
selectionRectangle.x2 = relativePos.x; selectionRectangle.x2 = relativePos.x;
selectionRectangle.y2 = relativePos.y; selectionRectangle.y2 = relativePos.y;
} }
@ -367,6 +368,7 @@ function handleMouseUp() {
height: Math.abs(selectionRectangle.y2 - selectionRectangle.y1), height: Math.abs(selectionRectangle.y2 - selectionRectangle.y1),
}; };
// console.log(`Stage Scale: ${stageScale.value}`);
let currentSelectedIds = []; let currentSelectedIds = [];
for (let [key, shape] of objMap) { for (let [key, shape] of objMap) {
const shapeConfig = objMap.get(shape.id); const shapeConfig = objMap.get(shape.id);
@ -375,22 +377,21 @@ function handleMouseUp() {
} }
if (isUndefined(shapeConfig.box)) { if (isUndefined(shapeConfig.box)) {
if (isUndefined(shapeConfig.box)) { if (
if ( shapeConfig.config.width &&
shapeConfig.config.width && shapeConfig.config.height &&
shapeConfig.config.height && shapeConfig.config.rotation
shapeConfig.config.rotation ) {
) { shapeConfig.box = calculateRectBounding(
shapeConfig.box = calculateRectBounding( shapeConfig.config.width,
shapeConfig.config.width, shapeConfig.config.height,
shapeConfig.config.height, shapeConfig.config.rotation,
shapeConfig.config.rotation, stageScale.value,
5, 5,
); );
} else { } else {
console.error("Could not calculate rect bounding"); console.error("Could not calculate rect bounding");
return; return;
}
} }
} }
@ -482,6 +483,7 @@ function handleCanvasObjectMouseOver(evt: Event) {
objectConfig.config.width, objectConfig.config.width,
objectConfig.config.height, objectConfig.config.height,
objectConfig.config.rotation, objectConfig.config.rotation,
stageScale.value, //
5, 5,
); );
} else console.error("Could not calculate rect bounding"); } else console.error("Could not calculate rect bounding");