Let's ensure that all rectangles strictly snap to the grid points:
I've updated the code to ensure rectangles strictly snap to the grid points. Here are the key changes:
In the Canvas2D component:
snapToGrid function to use Math.floor instead of Math.round, ensuring coordinates always snap to the nearest grid point that's less than or equal to the current positionhandleMouseUp function to ensure rectangle width and height are always multiples of the grid sizeIn the main page component:
gridSize constant to match the one in Canvas2DhandleUpdateRectangle function to ensure all rectangle properties (x, y, width, height) are snapped to the gridThese changes ensure that:
Now the rectangles will only snap to whole squares on the grid, making the layout more precise and predictable.
Let's modify the code to allow rectangles to overlap by exactly one grid square:
I've modified the code to allow rectangles to overlap by exactly one grid square, but not more than that. Here are the key changes:
Added a new hasExcessiveOverlap function in both components that:
true only if the overlap exceeds one grid cell in either dimensionfalse if there's no overlap or if the overlap is within the allowed limit (one grid cell)Updated the overlap checking logic in:
handleMouseUp in Canvas2D (when creating new rectangles)handleKeyDown in Canvas2D (when moving rectangles with arrow keys)handleAddRectangle in the main page componenthandleUpdateRectangle in the main page componentNow you can:
This gives you the flexibility to create adjacent or slightly overlapping foundation elements while still maintaining a reasonable layout.