Image crop out of bounds on load
See original GitHub issueHello. I am trying to get my image cropped immediately on load within set aspect road. however when using the code below, from the documentation here, my crop ends up being incorrect.
const onLoad = useCallback(img => {
imgRef.current = img;
const aspect = 16 / 9;
const width = img.width / aspect < img.height * aspect ? 100 : ((img.height * aspect) / img.width) * 100;
const height = img.width / aspect > img.height * aspect ? 100 : (img.width / aspect / img.height) * 100;
const y = (100 - height) / 2;
const x = (100 - width) / 2;
setCrop({
unit: '%',
width,
height,
x,
y,
aspect,
});
return false; // Return false if you set crop state in here.
}, []);
This is the image:
and here is how it looks after load:
and here is how i expect it to look:
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
crop uiimageview which is out of bounds of another view
I have an UIImaveView(red one) which I have rotated to 45°. In this I a drawed an UIImage with a gradient and places...
Read more >Cropping an image out of bounds stretches the output image
If I specify a crop area that is outside of the image area then the output image will only contain a stretched version...
Read more >crop - Fastly Developer Hub
If the specified cropped region is outside the bounds of the image, the transformation will fail with the error "Invalid transformation for requested...
Read more >Cropping an Image using OpenCV - LearnOpenCV
Load the height and width to specify the range till which the smaller patches need to be cropped out. For this, use the...
Read more >Customize an image | Jetpack Compose - Android Developers
Specify a contentScale option to crop or change how an image is scaled inside its bounds. By default, if you don't specify a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks I’ve updated the documentation and added some helpers to make it easier to do % images now, in v10:
It’s also on the codesandbox link: https://codesandbox.io/s/react-image-crop-demo-with-react-hooks-y831o
Sure, here’s my full function that gets the default crop based on the image size and the size of the element that contains the crop component. I hope it helps @supasus!