Merge pull request #111 from fiskbil/main
Change cropper movement from accept/reject to clamping.
This commit is contained in:
commit
964795ceb5
@ -33,6 +33,39 @@ interface Props {
|
|||||||
minWidth: number
|
minWidth: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const clamp = (
|
||||||
|
newPos: number,
|
||||||
|
newLength: number,
|
||||||
|
oldPos: number,
|
||||||
|
oldLength: number,
|
||||||
|
minLength: number,
|
||||||
|
maxLength: number
|
||||||
|
) => {
|
||||||
|
if (newPos !== oldPos && newLength === oldLength) {
|
||||||
|
if (newPos < 0) {
|
||||||
|
return [0, oldLength]
|
||||||
|
}
|
||||||
|
if (newPos + newLength > maxLength) {
|
||||||
|
return [maxLength - oldLength, oldLength]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (newLength < minLength) {
|
||||||
|
if (newPos === oldPos) {
|
||||||
|
return [newPos, minLength]
|
||||||
|
}
|
||||||
|
return [newPos + newLength - minLength, minLength]
|
||||||
|
}
|
||||||
|
if (newPos < 0) {
|
||||||
|
return [0, newPos + newLength]
|
||||||
|
}
|
||||||
|
if (newPos + newLength > maxLength) {
|
||||||
|
return [newPos, maxLength - newPos]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [newPos, newLength]
|
||||||
|
}
|
||||||
|
|
||||||
const Croper = (props: Props) => {
|
const Croper = (props: Props) => {
|
||||||
const { minHeight, minWidth, maxHeight, maxWidth, scale } = props
|
const { minHeight, minWidth, maxHeight, maxWidth, scale } = props
|
||||||
const [x, setX] = useRecoilState(croperX)
|
const [x, setX] = useRecoilState(croperX)
|
||||||
@ -63,18 +96,12 @@ const Croper = (props: Props) => {
|
|||||||
console.log('focus')
|
console.log('focus')
|
||||||
}
|
}
|
||||||
|
|
||||||
const checkTopBottomLimit = (newY: number, newHeight: number) => {
|
const clampLeftRight = (newX: number, newWidth: number) => {
|
||||||
if (newY > 0 && newHeight > minHeight && newY + newHeight <= maxHeight) {
|
return clamp(newX, newWidth, x, width, minWidth, maxWidth)
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const checkLeftRightLimit = (newX: number, newWidth: number) => {
|
const clampTopBottom = (newY: number, newHeight: number) => {
|
||||||
if (newX > 0 && newWidth > minWidth && newX + newWidth <= maxWidth) {
|
return clamp(newY, newHeight, y, height, minHeight, maxHeight)
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const onPointerMove = (e: PointerEvent) => {
|
const onPointerMove = (e: PointerEvent) => {
|
||||||
@ -90,33 +117,31 @@ const Croper = (props: Props) => {
|
|||||||
const moveTop = () => {
|
const moveTop = () => {
|
||||||
const newHeight = evData.initHeight - offsetY
|
const newHeight = evData.initHeight - offsetY
|
||||||
const newY = evData.initY + offsetY
|
const newY = evData.initY + offsetY
|
||||||
if (checkTopBottomLimit(newY, newHeight)) {
|
const [clampedY, clampedHeight] = clampTopBottom(newY, newHeight)
|
||||||
setHeight(newHeight)
|
setHeight(clampedHeight)
|
||||||
setY(newY)
|
setY(clampedY)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const moveBottom = () => {
|
const moveBottom = () => {
|
||||||
const newHeight = evData.initHeight + offsetY
|
const newHeight = evData.initHeight + offsetY
|
||||||
if (checkTopBottomLimit(evData.initY, newHeight)) {
|
const [clampedY, clampedHeight] = clampTopBottom(evData.initY, newHeight)
|
||||||
setHeight(newHeight)
|
setHeight(clampedHeight)
|
||||||
}
|
setY(clampedY)
|
||||||
}
|
}
|
||||||
|
|
||||||
const moveLeft = () => {
|
const moveLeft = () => {
|
||||||
const newWidth = evData.initWidth - offsetX
|
const newWidth = evData.initWidth - offsetX
|
||||||
const newX = evData.initX + offsetX
|
const newX = evData.initX + offsetX
|
||||||
if (checkLeftRightLimit(newX, newWidth)) {
|
const [clampedX, clampedWidth] = clampLeftRight(newX, newWidth)
|
||||||
setWidth(newWidth)
|
setWidth(clampedWidth)
|
||||||
setX(newX)
|
setX(clampedX)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const moveRight = () => {
|
const moveRight = () => {
|
||||||
const newWidth = evData.initWidth + offsetX
|
const newWidth = evData.initWidth + offsetX
|
||||||
if (checkLeftRightLimit(evData.initX, newWidth)) {
|
const [clampedX, clampedWidth] = clampLeftRight(evData.initX, newWidth)
|
||||||
setWidth(newWidth)
|
setWidth(clampedWidth)
|
||||||
}
|
setX(clampedX)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isResizing) {
|
if (isResizing) {
|
||||||
@ -166,13 +191,12 @@ const Croper = (props: Props) => {
|
|||||||
if (isMoving) {
|
if (isMoving) {
|
||||||
const newX = evData.initX + offsetX
|
const newX = evData.initX + offsetX
|
||||||
const newY = evData.initY + offsetY
|
const newY = evData.initY + offsetY
|
||||||
if (
|
const [clampedX, clampedWidth] = clampLeftRight(newX, evData.initWidth)
|
||||||
checkLeftRightLimit(newX, evData.initWidth) &&
|
const [clampedY, clampedHeight] = clampTopBottom(newY, evData.initHeight)
|
||||||
checkTopBottomLimit(newY, evData.initHeight)
|
setWidth(clampedWidth)
|
||||||
) {
|
setHeight(clampedHeight)
|
||||||
setX(newX)
|
setX(clampedX)
|
||||||
setY(newY)
|
setY(clampedY)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user