diff --git a/src/hooks/useMode.js b/src/hooks/useMode.js index ffca9f48..3b24d85f 100644 --- a/src/hooks/useMode.js +++ b/src/hooks/useMode.js @@ -140,6 +140,33 @@ export function useMode() { let newY = pointer.y if (isGuideLineMode && isGuideDotMode && mode === Mode.EDIT) { + const closestHorizontalLine = getClosestHorizontalLine(pointer, horizontalLineArray) + const closetVerticalLine = getClosestVerticalLine(pointer, verticalLineArray) + const xDiff = Math.abs(pointer.x - closetVerticalLine.x1) + const yDiff = Math.abs(pointer.y - closestHorizontalLine.y1) + + const x = pointer.x - guideLineLength * Math.floor(pointer.x / guideLineLength) + const y = pointer.y - guideLineLength * Math.floor(pointer.y / guideLineLength) + + const xRate = x / guideLineLength + const yRate = y / guideLineLength + const isAttachX = xRate >= 0.4 && xRate <= 0.7 + const isAttachY = yRate >= 0.4 && yRate <= 0.7 + + if (isAttachX && isAttachY) { + newX = Math.floor(pointer.x / guideLineLength) * guideLineLength + guideLineLength / 2 + newY = Math.floor(pointer.y / guideLineLength) * guideLineLength + guideLineLength / 2 + } else { + if (Math.min(xDiff, yDiff) <= 20) { + if (xDiff < yDiff) { + newX = closetVerticalLine.x1 + newY = pointer.y + } else { + newX = pointer.x + newY = closestHorizontalLine.y1 + } + } + } } else if (isGuideDotMode && mode === Mode.EDIT) { const x = pointer.x - guideDotLength * Math.floor(pointer.x / guideDotLength) const y = pointer.y - guideDotLength * Math.floor(pointer.y / guideDotLength)