From 89d8a93964b4e62bc20e97ec512a9499f321dfad Mon Sep 17 00:00:00 2001 From: trafficlunar Date: Sat, 18 Jan 2025 22:50:47 +0000 Subject: [PATCH] fix: rectangle select negative dragging for x and y --- src/components/canvas/Canvas.tsx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/components/canvas/Canvas.tsx b/src/components/canvas/Canvas.tsx index 2f152d9..c5e4b08 100644 --- a/src/components/canvas/Canvas.tsx +++ b/src/components/canvas/Canvas.tsx @@ -244,11 +244,15 @@ function Canvas() { setSelectionCoords(() => { const newSelection: CoordinateArray = []; - // todo: fix dragging from bottom to top - const isEven = radius % 2 == 0; + const startX = Math.min(dragStartCoords.x, mouseCoords.x); + const endX = Math.max(dragStartCoords.x, mouseCoords.x); + const startY = Math.min(dragStartCoords.y, mouseCoords.y); + const endY = Math.max(dragStartCoords.y, mouseCoords.y); - for (let x = dragStartCoords.x; x < mouseCoords.x + (isEven ? radius : radius - 1); x++) { - for (let y = dragStartCoords.y; y < mouseCoords.y + (isEven ? radius : radius - 1); y++) { + const isRadiusEven = radius % 2 == 0; + + for (let x = startX; x < endX + (isRadiusEven ? radius : radius - 1); x++) { + for (let y = startY; y < endY + (isRadiusEven ? radius : radius - 1); y++) { newSelection.push([x, y]); } }