Skip to content

Commit

Permalink
feat: #comment updated snapToGrid
Browse files Browse the repository at this point in the history
  • Loading branch information
scrt-dev committed Sep 30, 2024
1 parent 4d1c9bc commit 80fc5f3
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/components/DeviceNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ const DeviceNode: React.FC<DeviceNodeProps> = ({
}
}, []);

const snapToGrid = (x: number, y: number) => {
const snappedX = Math.round(x / device.gridSize) * device.gridSize;
const snappedY = Math.round(y / device.gridSize) * device.gridSize;
return { x: snappedX, y: snappedY };
};
const snapToGrid = React.useCallback(
(x: number, y: number) => {
const snappedX = Math.round(x / device.gridSize) * device.gridSize;
const snappedY = Math.round(y / device.gridSize) * device.gridSize;
return { x: snappedX, y: snappedY };
},
[device.gridSize]
);

useEffect(() => {
const handleMouseMove = (e: MouseEvent) => {
Expand Down Expand Up @@ -83,7 +86,7 @@ const DeviceNode: React.FC<DeviceNodeProps> = ({
window.removeEventListener("mousemove", handleMouseMove);
window.removeEventListener("mouseup", handleMouseUp);
};
}, [isDragging, device.id, onMove, position, device.gridSize]);
}, [isDragging, device.id, onMove, position, snapToGrid]);

const handleMouseDown = (e: React.MouseEvent) => {
e.preventDefault();
Expand Down

0 comments on commit 80fc5f3

Please sign in to comment.