ISSUE-017 - Implemented drag bounds that consider the dimensions of dragged element.
This is bugged as the resize observer I'm using doesn't update itself when I reveal the OverlayItem from hiding.
This commit is contained in:
1 parent
861d702a27
commit
aba5cc20be
5 files changed
+51
-25
No files matched your search
@@ -23,15 +23,6 @@ export function OverlayControl() {
|
||||
/* For prototyping use only */
|
||||
const [idNum, setIdNum] = useState(0);
|
||||
|
||||
const getRandomArbitrary = (min: number, max: number) =>
|
||||
Math.random() * (max - min) + min;
|
||||
|
||||
const posRandomizer = () => {
|
||||
dispatch(setPosition({
|
||||
x: Math.floor(getRandomArbitrary(-50, 850)),
|
||||
y: Math.floor(getRandomArbitrary(-50, 650)),
|
||||
}));
|
||||
};
|
||||
const nextIdNum = () => {
|
||||
const nextIdNum = (idNum + 1) % overlayIds.length;
|
||||
setIdNum(nextIdNum);
|
||||
@@ -45,7 +36,7 @@ export function OverlayControl() {
|
||||
if (currentOverlayItem == null) {
|
||||
dispatch(initNewOverlayItem({
|
||||
overlayId: overlayIds[idNum],
|
||||
initialPosition: { x: 0, y: 300 },
|
||||
initialPosition: { x: 400, y: 300 },
|
||||
setVisible: true,
|
||||
}));
|
||||
} else {
|
||||
@@ -63,12 +54,6 @@ export function OverlayControl() {
|
||||
|
||||
{/* For prototyping use only */}
|
||||
<br />
|
||||
<a
|
||||
onClick={posRandomizer}
|
||||
>
|
||||
Random Location
|
||||
</a>
|
||||
<br />
|
||||
<a
|
||||
onClick={nextIdNum}
|
||||
>
|
||||
|
||||
@@ -2,24 +2,47 @@ import { useSelector, useDispatch } from "react-redux";
|
||||
import { useTransition, animated } from "@react-spring/web";
|
||||
import { useDrag } from "@use-gesture/react";
|
||||
|
||||
import { useDimensions } from "../../../utils/hooks";
|
||||
import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSelector";
|
||||
import { setPosition } from "../../../features/overlayTool/overlayToolSlice";
|
||||
|
||||
import { OverlayItemContainer } from "./styles";
|
||||
|
||||
export function OverlayDisplay() {
|
||||
type OverlayDisplayProps = {
|
||||
containerDimensions: {
|
||||
width: number,
|
||||
height: number,
|
||||
},
|
||||
bleedover?: number,
|
||||
};
|
||||
export function OverlayDisplay({
|
||||
containerDimensions: { width: containerWidth, height: containerHeight },
|
||||
bleedover = 0,
|
||||
}: OverlayDisplayProps) {
|
||||
const dispatch = useDispatch();
|
||||
const {
|
||||
currentOverlayId,
|
||||
currentOverlayItem,
|
||||
visible,
|
||||
} = useSelector(overlayToolSelector);
|
||||
|
||||
const dispatch = useDispatch();
|
||||
// Monitor element dimensions to calculate min/max drag coordinates.
|
||||
const [overlayItemRef, {
|
||||
width: overlayItemWidth,
|
||||
height: overlayItemHeight,
|
||||
}] = useDimensions<HTMLDivElement>();
|
||||
const
|
||||
minX = -bleedover,
|
||||
minY = -bleedover,
|
||||
maxX = containerWidth - overlayItemWidth + bleedover,
|
||||
maxY = containerHeight - overlayItemHeight + bleedover;
|
||||
|
||||
const bindOverlayItem = useDrag(({ args, delta }) => {
|
||||
const newX = args[0].xPos + delta[0];
|
||||
const newY = args[0].yPos + delta[1];
|
||||
dispatch(setPosition({
|
||||
x: args[0].xPos + delta[0],
|
||||
y: args[0].yPos + delta[1],
|
||||
x: newX >= minX && newX <= maxX ? newX : args[0].xPos,
|
||||
y: newY >= minY && newY <= maxY ? newY : args[0].yPos,
|
||||
}));
|
||||
});
|
||||
|
||||
@@ -54,7 +77,11 @@ export function OverlayDisplay() {
|
||||
}}
|
||||
>
|
||||
<animated.div style={{ ...style, position: "absolute" }}>
|
||||
<OverlayItemContainer visible={visible} tempColor={currentOverlayId}>
|
||||
<OverlayItemContainer
|
||||
ref={overlayItemRef}
|
||||
visible={visible}
|
||||
tempColor={currentOverlayId}
|
||||
>
|
||||
{currentOverlayId}
|
||||
<br />
|
||||
(x:{Math.floor(position.x)}px, y:{Math.floor(position.y)}px)
|
||||
|
||||
@@ -12,6 +12,7 @@ export const OverlayItemContainer = styled.div.attrs<OverlayItemContainerProps>(
|
||||
},
|
||||
}))<OverlayItemContainerProps>`
|
||||
/* Temporary. Overlay will be transparent and based on an image. */
|
||||
/* width: ${({ tempColor }) => tempColor == "red" ? "150px" : "100px"}; */
|
||||
width: 150px;
|
||||
height: 150px;
|
||||
background-color: ${({ tempColor }) => tempColor};
|
||||
|
||||
Reference in new issue
Block a user