Skip to content
This repository was archived by the owner on Jan 28, 2026. It is now read-only.

Commit 51c0acc

Browse files
author
ge85riz
committed
✨ feat(frontend): chair snapping feature works for tables that are rotated 90, 180 and 270 degrees as well
1 parent af372c0 commit 51c0acc

3 files changed

Lines changed: 48 additions & 10 deletions

File tree

frontend/src/components/CanvasTooltip.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const tooltipSteps = [
2222
text: "Undo/Redo: Press \"<b>CTRL + Z</b>\" / \"<b>CTRL + SHIFT + Z</b>\".",
2323
icon: "↩️",
2424
},
25+
{
26+
text: "Chair Snapping: Chairs automatically snap to tables if the tables are rotated <b>0°, 90°, 180°, or 270°</b>. Otherwise, deactivated",
27+
icon: "🪑",
28+
},
2529
];
2630

2731
export const CanvasTooltip = () => {

frontend/src/components/canvas/EventListeners.tsx

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import type { Table } from "components/canvas/elements/Table.tsx";
2020
import type { KonvaEventObject } from "konva/lib/Node";
2121
import type { Wall } from "components/canvas/elements/Wall.tsx";
2222
import type { SelectionRectangleType } from "components/canvas/elements/SelectionRectangle.tsx";
23-
import { findStageCenterCoordinates, validateCanvasElementDeletion } from "components/canvas/utils/functions.tsx";
23+
import { validateCanvasElementDeletion } from "components/canvas/utils/functions.tsx";
2424
import toast from "react-hot-toast";
2525

2626

@@ -260,7 +260,7 @@ export const handleMouseDown = (
260260
const y2 = (pointer.y - stage.y()) / scale;
261261
const { x1, y1 } = quickWallCoordinates;
262262

263-
const stageCenter = findStageCenterCoordinates(stageRef);
263+
const stageCenter = { x: 0, y: 0 };
264264
let element = shapeFactory("wall", stageCenter) as Wall;
265265
element = { ...element, x1, y1, x2, y2 } as Wall;
266266

@@ -352,14 +352,48 @@ export const handleDragEnd = (e: Konva.KonvaEventObject<DragEvent>, el: ElementP
352352
angle: angle,
353353
};
354354
} else {
355-
if (Math.abs(table.rotation) < 1) {
356-
const { radius: padding } = el;
357-
const { width, height, x: tableX, y: tableY } = table;
355+
let normalizedRotation = table.rotation % 360;
356+
if (normalizedRotation < 0) normalizedRotation += 360;
357+
358+
if (normalizedRotation < 1 ||
359+
(89 < normalizedRotation && normalizedRotation < 91) ||
360+
(179 < normalizedRotation && normalizedRotation < 181) ||
361+
(269 < normalizedRotation && normalizedRotation < 271) ||
362+
normalizedRotation > 359) {
358363

359-
const centerX = tableX + width! / 2;
360-
const centerY = tableY + height! / 2;
364+
const { radius: padding } = el;
365+
const tableX = table.x;
366+
const tableY = table.y;
367+
let { width, height } = table;
368+
369+
let centerX = 0;
370+
let centerY = 0;
371+
372+
if (normalizedRotation < 1 || normalizedRotation > 359) {
373+
// 0 degrees - normal case
374+
centerX = tableX + width! / 2;
375+
centerY = tableY + height! / 2;
376+
} else if (89 < normalizedRotation && normalizedRotation < 91) {
377+
// 90 degrees - width becomes height, height becomes width
378+
const tempWidth = width;
379+
width = height;
380+
height = tempWidth;
381+
centerX = tableX - width! / 2;
382+
centerY = tableY + height! / 2;
383+
} else if (179 < normalizedRotation && normalizedRotation < 181) {
384+
// 180 degrees - table is flipped
385+
centerX = tableX - width! / 2;
386+
centerY = tableY - height! / 2;
387+
} else if (269 < normalizedRotation && normalizedRotation < 271) {
388+
// 270 degrees - width becomes height, height becomes width
389+
const tempWidth = width;
390+
width = height;
391+
height = tempWidth;
392+
centerX = tableX + width! / 2;
393+
centerY = tableY - height! / 2;
394+
}
361395

362-
// Coordinates relative to table center
396+
// Coordinates relative to table center (using chair position, not table position)
363397
const dx = x - centerX;
364398
const dy = y - centerY;
365399

frontend/src/components/canvas/elements/StagePreview.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ function StagePreview({ state, mainStage }: { state: AppState; mainStage: Konva.
5050
<Stage
5151
width={(window.innerWidth - 150) / 4}
5252
height={window.innerHeight / 4}
53-
scaleX={mainStage === null ? 0.25 : mainStage.scaleX() * 0.0625}
54-
scaleY={mainStage === null ? 0.25 : mainStage.scaleY() * 0.0625}
53+
scaleX={mainStage === null ? 0.25 : mainStage.scaleX() * 0.125}
54+
scaleY={mainStage === null ? 0.25 : mainStage.scaleY() * 0.125}
5555
x={mainStage === null ? 0 : (window.innerWidth - 250) / 8 + mainStage.x() * 0.0625}
5656
y={mainStage === null ? 0 : window.innerHeight / 8 + mainStage.y() * 0.0625}
5757
>

0 commit comments

Comments
 (0)