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

Automatically Handle Image Resize Calculations #551

Open
mdeslippe opened this issue Jul 28, 2023 · 0 comments
Open

Automatically Handle Image Resize Calculations #551

mdeslippe opened this issue Jul 28, 2023 · 0 comments

Comments

@mdeslippe
Copy link

mdeslippe commented Jul 28, 2023

Hello,

With the current implementation, if the image element that is being used to perform the crop happens to change size, the completed crop value does not automatically update to reflect the new image dimensions. If you use the stale completed crop to attempt to process the crop with a canvas, the crop result is incorrect.

This is how I was able to get around the issue manually:

useEffect(() => {
	if (imageRef.current === null || crop === undefined) {
		return;
	}

	const resizeObserver = new ResizeObserver(() => {
		setCrop((crop) => {
			if (imageRef.current === null || crop === undefined) {
				setCompletedCrop(undefined);
				return undefined;
			} else {
				setCompletedCrop(
					convertToPixelCrop(crop, imageRef.current.width, imageRef.current.height)
				);
				return crop;
			}
		});
	});

	resizeObserver.observe(imageRef.current);
	return () => resizeObserver.disconnect();
}, [imageRef.current, crop === undefined]);

Could you please add an automatic completed crop update when the element resizes?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant