Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#287 image aspect ratio #301

Merged
merged 7 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions public/shapes/image.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Group, Rect, Text } from 'react-konva';
import { Group, Text, Image as KonvaImage } from 'react-konva';
import useImage from 'use-image';

interface Props {
width: number;
Expand All @@ -7,18 +8,11 @@ interface Props {

export const NoImageSelected: React.FC<Props> = props => {
const { width, height } = props;
const [image] = useImage('/shapes/image.svg');

return (
<Group x={0} y={0} width={width} height={height}>
<Rect
x={0}
y={0}
width={width}
height={height}
strokeWidth={2}
stroke="black"
fill="white"
/>
<KonvaImage x={0} y={0} width={width} height={height} image={image} />
<Text
x={0}
y={0}
Expand All @@ -30,6 +24,7 @@ export const NoImageSelected: React.FC<Props> = props => {
align="center"
ellipsis={true}
verticalAlign="middle"
textDecoration="underline"
/>
</Group>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const imageShapeRestrictions: ShapeSizeRestrictions = {
minHeight: 10,
maxWidth: -1,
maxHeight: -1,
defaultWidth: 520,
defaultHeight: 520,
defaultWidth: 300,
defaultHeight: 300,
};

const shapeType: ShapeType = 'image';
Expand Down Expand Up @@ -42,7 +42,12 @@ export const ImageShape = forwardRef<any, ShapeProps>((props, ref) => {
imageRef.current.filters([]); // Remove filter
imageRef.current.getLayer()?.batchDraw(); // Redraw
}
}, [image, otherProps?.imageBlackAndWhite]);
}, [
image,
otherProps?.imageBlackAndWhite,
restrictedWidth,
restrictedHeight,
]);

return (
<Group
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { forwardRef, useRef } from 'react';
import classes from './image-upload.widget.module.css';
import { useCanvasContext } from '@/core/providers';

interface Props {
onImageUploaded: (srcData: string) => void;
Expand All @@ -9,14 +10,37 @@ export const ImageUploadWidget = forwardRef<HTMLInputElement, Props>(
(props, ref) => {
const { onImageUploaded } = props;
const fileInputRef = useRef<HTMLInputElement>(null);
const { selectionInfo, updateShapeSizeAndPosition, shapes } =
useCanvasContext();

const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (file) {
const reader = new FileReader();
reader.onloadend = () => {
if (reader.result) {
onImageUploaded(reader.result as string);
const img = new Image();
img.src = reader.result as string;

img.onload = () => {
const aspectRatio = img.width / img.height;
const imageSelected = shapes.find(
shape => shape.id === selectionInfo.selectedShapesIds[0]
);
if (imageSelected) {
updateShapeSizeAndPosition(
imageSelected.id,
{ x: imageSelected.x, y: imageSelected.y },
{
width: imageSelected.width,
height: imageSelected.width / aspectRatio,
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could encapsulate this an utils function and place it under common/utils/image.utils.ts

Something like:

** Not sure about the params

const adjustSizeKeepingAspectRatio = (imageSize : Size, componentSize : Size) : Size => 

then we can use the same funcion in the drag and load image (We don't repeat code)

false
);
}

onImageUploaded(reader.result as string);
};
}
};
reader.readAsDataURL(file);
Expand Down
18 changes: 16 additions & 2 deletions src/pods/canvas/use-drop-image-from-desktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {
getScrollFromDiv,
} from './canvas.util';
import { calculateShapeOffsetToXDropCoordinate } from './use-monitor.business';
import { getImageShapeSizeRestrictions } from '@/common/components/front-basic-sapes';

export const useDropImageFromDesktop = (
dropRef: React.MutableRefObject<HTMLDivElement>
) => {
const { addNewShape, stageRef } = useCanvasContext();
const { addNewShape, updateShapeSizeAndPosition, stageRef } =
useCanvasContext();

// TODO: #231 move this to utils / business
// https://github.com/Lemoncode/quickmock/issues/231
Expand Down Expand Up @@ -59,7 +61,19 @@ export const useDropImageFromDesktop = (
calculateShapeOffsetToXDropCoordinate(konvaCoord.x, 'image');
const positionY = konvaCoord.y;

addNewShape('image', positionX, positionY, { imageSrc: img.src });
const newImg = addNewShape('image', positionX, positionY, {
imageSrc: img.src,
});

// Preserves aspect ratio
const aspectRatio = img.width / img.height;
const defaultWidth = getImageShapeSizeRestrictions().defaultWidth;
updateShapeSizeAndPosition(
newImg,
{ x: positionX, y: positionY },
{ width: defaultWidth, height: defaultWidth / aspectRatio },
false
);
};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useRef } from 'react';
import classes from './image-selector.component.module.css';
import { useCanvasContext } from '@/core/providers';

interface Props {
label: string;
Expand All @@ -9,6 +10,8 @@ interface Props {
export const ImageSrc: React.FC<Props> = props => {
const { label, onChange } = props;
const fileInputRef = useRef<HTMLInputElement | null>(null);
const { selectionInfo, updateShapeSizeAndPosition, shapes } =
useCanvasContext();

const handleClick = () => {
if (fileInputRef.current) {
Expand All @@ -22,7 +25,28 @@ export const ImageSrc: React.FC<Props> = props => {
const reader = new FileReader();
reader.onloadend = () => {
if (reader.result) {
onChange(reader.result as string);
const img = new Image();
img.src = reader.result as string;

img.onload = () => {
const aspectRatio = img.width / img.height;
const imageSelected = shapes.find(
shape => shape.id === selectionInfo.selectedShapesIds[0]
);
if (imageSelected) {
updateShapeSizeAndPosition(
imageSelected.id,
{ x: imageSelected.x, y: imageSelected.y },
{
width: imageSelected.width,
height: imageSelected.width / aspectRatio,
},
false
);
}

onChange(reader.result as string);
};
}
};
reader.readAsDataURL(file);
Expand Down
Loading