Skip to content

Commit

Permalink
climbing: Fix when image is smaller than its container (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
jvaclavik authored Feb 15, 2024
1 parent 672ebfe commit 6ea02be
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const MouseTrackingLine = ({ routeNumber }) => {
const lastPoint = path[path.length - 1];
const lastPointPositionInPx = getPixelPosition(lastPoint);
const mousePositionWithEditorPosition = addOffsets(
['editorPosition'],
['editorPosition', 'imageContainer'],
mousePosition,
);
const closerMousePositionPoint = mousePositionWithEditorPosition
Expand Down
2 changes: 1 addition & 1 deletion src/components/FeaturePanel/Climbing/Editor/RoutePath.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const RoutePath = ({ route, routeNumber }) => {
) {
if (!isLineInteractiveAreaHovered) setIsLineInteractiveAreaHovered(true);

const position = addOffsets(['editorPosition'], {
const position = addOffsets(['editorPosition', 'imageContainer'], {
x: e.clientX,
y: e.clientY,
units: 'px',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const RoutesEditor = ({

setIsPointMoving(true);
const newCoordinate = getPercentagePosition(
addOffsets(['editorPosition'], position),
addOffsets(['editorPosition', 'imageContainer'], position),
);

const closestPoint = findCloserPoint(newCoordinate);
Expand Down
13 changes: 13 additions & 0 deletions src/components/FeaturePanel/Climbing/contexts/ClimbingContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type ImageSize = {
type ClimbingContextType = {
editorPosition: PositionPx;
imageSize: ImageSize;
imageContainerSize: ImageSize;
isPointMoving: boolean;
isRouteSelected: (routeNumber: number) => boolean;
isPointSelected: (pointNumber: number) => boolean;
Expand All @@ -52,6 +53,7 @@ type ClimbingContextType = {
setIsPointClicked: (isPointClicked: boolean) => void;
setEditorPosition: (position: PositionPx) => void;
setImageSize: (ImageSize) => void;
setImageContainerSize: (ImageSize) => void;
splitPaneHeight: number | null;
setSplitPaneHeight: (height: number | null) => void;
photoPaths: Array<string>;
Expand Down Expand Up @@ -143,6 +145,10 @@ export const ClimbingContextProvider = ({ children, feature }: Props) => {
const [showDebugMenu, setShowDebugMenu] = useState(false);
const [isEditMode, setIsEditMode] = useState(false);
const [imageSize, setImageSize] = useState({ width: 0, height: 0 });
const [imageContainerSize, setImageContainerSize] = useState({
width: 0,
height: 0,
});
const [routes, setRoutes] = useState<Array<ClimbingRoute>>(initialRoutes);
const [splitPaneHeight, setSplitPaneHeight] = useState<number | null>(null);
const [isPointMoving, setIsPointMoving] = useState<boolean>(false);
Expand Down Expand Up @@ -242,6 +248,7 @@ export const ClimbingContextProvider = ({ children, feature }: Props) => {
editorPosition,
scrollOffset,
imageSize,
imageContainerSize,
photoZoom,
});

Expand Down Expand Up @@ -299,6 +306,10 @@ export const ClimbingContextProvider = ({ children, feature }: Props) => {
const [width, height] = getContainedSizeImage(photoRef.current);
const { left, top } = photoRef.current.getBoundingClientRect();

setImageContainerSize({
width: photoRef.current.width,
height: photoRef.current.height,
});
setImageSize({
width,
height,
Expand Down Expand Up @@ -384,6 +395,8 @@ export const ClimbingContextProvider = ({ children, feature }: Props) => {
gradeTable,
setGradeTable,
preparePhotosAndSetFirst,
imageContainerSize,
setImageContainerSize,
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export const getMachineFactory = ({
if (!props) return;
const { x, y } = props.position;
const newCoordinate = getPercentagePosition(
addOffsets(['scrollOffset', 'editorPosition'], {
addOffsets(['scrollOffset', 'editorPosition', 'imageContainer'], {
x,
y,
units: 'px',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Position, PositionPx } from '../types';
import { roundNumber } from './number';

export type CountPositionEntity = 'editorPosition' | 'scrollOffset';
export type CountPositionEntity =
| 'editorPosition'
| 'scrollOffset'
| 'imageContainer';

export const positionUtilsFactory = ({
editorPosition,
scrollOffset,
imageSize,
imageContainerSize,
photoZoom,
}) => {
const getPixelPosition = ({ x, y }: Position): PositionPx => ({
Expand Down Expand Up @@ -35,6 +39,13 @@ export const positionUtilsFactory = ({
units: 'px',
};
}
if (offset === 'imageContainer') {
return {
x: acc.x - (imageContainerSize.width - imageSize.width) / 2,
y: acc.y - (imageContainerSize.height - imageSize.height) / 2,
units: 'px',
};
}
return {
x: scrollOffset.x + acc.x,
y: scrollOffset.y + acc.y,
Expand Down

0 comments on commit 6ea02be

Please sign in to comment.