Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to snap-to-grid when adding a microphone point #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,7 @@ If you'd like to cite this project, please use this BibTex:
year={2024}
}
```

## 🖱️ Snap-to-Grid Functionality

You can now snap a microphone point to the nearest grid intersection by using `Shift + Click` when adding a microphone point. This allows for more precise placement of microphones on the grid.
17 changes: 13 additions & 4 deletions src/components/MicMasterFlex.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ const MicMasterFlex = () => {
y: -((point.y - (window.innerHeight / 2) - pan.y) / zoom),
});

// Snap point to nearest grid intersection
const snapToGrid = (point: Point): Point => {
const step = gridSize / gridDivisions;
return {
x: Math.round(point.x / step) * step,
y: Math.round(point.y / step) * step,
};
};

// Handle mouse down on the grid
const handleMouseDown = (e: React.MouseEvent) => {
if (!svgRef.current) return;
Expand Down Expand Up @@ -89,11 +98,11 @@ const MicMasterFlex = () => {

if (Math.abs(point.x - dragStart!.x) < 5 && Math.abs(point.y - dragStart!.y) < 5) {
if (mode === 'add') {
// Add new microphone at exact position
// Add new microphone at exact position or snapped to grid
const newMic: Microphone = {
id: `mic-${Date.now()}`,
x: gridPoint.x,
y: gridPoint.y,
x: e.shiftKey ? snapToGrid(gridPoint).x : gridPoint.x,
y: e.shiftKey ? snapToGrid(gridPoint).y : gridPoint.y,
};
setMicrophones([...microphones, newMic]);
} else if (mode === 'delete' && hoveredMic) {
Expand Down Expand Up @@ -357,4 +366,4 @@ const MicMasterFlex = () => {
);
};

export default MicMasterFlex;
export default MicMasterFlex;