ISSUE-017 - Implemented image overlay solution that I'm happy with.
Shout-out to Blair Wadman for revealing a method to display an image at 1:1 pixel scale and having the div that houses it automatically match that size without any fuss. (Here: https://befused.com/css/flexbox-prevent-image-shrinking/).
This commit is contained in:
1 parent
8780d2a9db
commit
f51df58d17
12 files changed
+70
-94
No files matched your search
@@ -6,7 +6,6 @@ import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSe
|
||||
import {
|
||||
initNewOverlayItem,
|
||||
setCurrentOverlayId,
|
||||
setPosition,
|
||||
setVisibility,
|
||||
} from "../../../features/overlayTool/overlayToolSlice";
|
||||
|
||||
@@ -36,7 +35,7 @@ export function OverlayControl() {
|
||||
if (currentOverlayItem == null) {
|
||||
dispatch(initNewOverlayItem({
|
||||
overlayId: overlayIds[idNum],
|
||||
initialPosition: { x: 400, y: 300 },
|
||||
initialPosition: { x: 0, y: 0 },
|
||||
setVisible: true,
|
||||
}));
|
||||
} else {
|
||||
|
||||
@@ -5,8 +5,9 @@ import { useDrag } from "@use-gesture/react";
|
||||
import { useDimensions } from "../../../utils/hooks";
|
||||
import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSelector";
|
||||
import { setPosition } from "../../../features/overlayTool/overlayToolSlice";
|
||||
import { OverlayImage, OverlayImageContainer, OverlayImageSubContainer } from "./styles";
|
||||
|
||||
import { OverlayItemContainer } from "./styles";
|
||||
const overlayImages = require.context("../../../public/images/overlays", true);
|
||||
|
||||
type OverlayDisplayProps = {
|
||||
containerDimensions: {
|
||||
@@ -27,10 +28,11 @@ export function OverlayDisplay({
|
||||
} = useSelector(overlayToolSelector);
|
||||
|
||||
// Monitor element dimensions to calculate min/max drag coordinates.
|
||||
const [overlayItemRef, {
|
||||
const [overlayItemImageRef, {
|
||||
width: overlayItemWidth,
|
||||
height: overlayItemHeight,
|
||||
}] = useDimensions<HTMLDivElement>();
|
||||
}] = useDimensions<HTMLImageElement>();
|
||||
console.log(overlayItemWidth, overlayItemHeight);
|
||||
const
|
||||
minX = -bleedover,
|
||||
minY = -bleedover,
|
||||
@@ -62,7 +64,6 @@ export function OverlayDisplay({
|
||||
return null;
|
||||
}
|
||||
|
||||
const { position } = currentOverlayItem;
|
||||
|
||||
return transition((style, visible) =>
|
||||
visible ? (
|
||||
@@ -77,15 +78,14 @@ export function OverlayDisplay({
|
||||
}}
|
||||
>
|
||||
<animated.div style={{ ...style, position: "absolute" }}>
|
||||
<OverlayItemContainer
|
||||
ref={overlayItemRef}
|
||||
visible={visible}
|
||||
tempColor={currentOverlayId}
|
||||
>
|
||||
{currentOverlayId}
|
||||
<br />
|
||||
(x:{Math.floor(position.x)}px, y:{Math.floor(position.y)}px)
|
||||
</OverlayItemContainer>
|
||||
<OverlayImageContainer>
|
||||
<OverlayImageSubContainer>
|
||||
<OverlayImage
|
||||
ref={overlayItemImageRef}
|
||||
src={overlayImages(`./${currentOverlayId}`)}
|
||||
/>
|
||||
</OverlayImageSubContainer>
|
||||
</OverlayImageContainer>
|
||||
</animated.div>
|
||||
</animated.div>
|
||||
) : null,
|
||||
|
||||
@@ -1,22 +1,28 @@
|
||||
import styled from "styled-components";
|
||||
|
||||
export type OverlayItemContainerProps = {
|
||||
visible: boolean,
|
||||
tempColor: string,
|
||||
/**
|
||||
* scale is a percentage (0-100%)
|
||||
* */
|
||||
type OverlayImageProps = {
|
||||
src: string,
|
||||
scale?: number,
|
||||
};
|
||||
export const OverlayItemContainer = styled.div.attrs<OverlayItemContainerProps>(({
|
||||
visible,
|
||||
export const OverlayImage = styled.img.attrs<OverlayImageProps>(({
|
||||
src,
|
||||
}) => ({
|
||||
style: {
|
||||
display: visible ? undefined : "none",
|
||||
},
|
||||
}))<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};
|
||||
box-shadow: 0px 7px 11px 1px black;
|
||||
draggable: false,
|
||||
src,
|
||||
}))<OverlayImageProps>`
|
||||
width: ${({ scale = 100 }) => `${scale}%`};
|
||||
`;
|
||||
OverlayItemContainer.displayName = "OverlayItemContainer";
|
||||
OverlayImage.displayName = "OverlayImage";
|
||||
|
||||
export const OverlayImageContainer = styled.div`
|
||||
display: flex;
|
||||
`;
|
||||
OverlayImageContainer.displayName = "OverlayImageContainer";
|
||||
|
||||
export const OverlayImageSubContainer = styled.div`
|
||||
flex-shrink: 0;
|
||||
`;
|
||||
OverlayImageSubContainer.displayName = "OverlayImageSubContainer";
|
||||
Reference in new issue
Block a user