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

Incorrect crop area output when the input is SVG #381

Open
JohnCido opened this issue May 7, 2022 · 4 comments
Open

Incorrect crop area output when the input is SVG #381

JohnCido opened this issue May 7, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@JohnCido
Copy link

JohnCido commented May 7, 2022

Describe the bug
When trying to crop an SVG file, the final output cropped area is wrong. So any code that relies on this output will generate wrong image output.

To Reproduce
Steps to reproduce the behavior:

  1. Open this sandbox
  2. Download this image
    crop test file
  3. Open this file in the sandbox
  4. Crop it and see the output

Expected behavior
Crop the exact area that the user has defined using the cropper.

@ValentinH ValentinH added the bug Something isn't working label May 9, 2022
@ValentinH
Copy link
Owner

Indeed, something is wrong with the sizes computation. First thing I noticed is that naturalWidth/naturalHeight doesn't exist for SVGs and we are using this to measure to media size here:

const naturalWidth = this.imageRef?.naturalWidth || this.videoRef?.videoWidth || 0
const naturalHeight = this.imageRef?.naturalHeight || this.videoRef?.videoHeight || 0

I guess we should use width/height in that case but we need to check if something else needs to be adjusted.

@raed667
Copy link
Contributor

raed667 commented Aug 4, 2022

Cropping works with some other SVGs, for example this image https://user-images.githubusercontent.com/1442690/182855138-e84d1916-8a19-4152-89a8-4c9074f54dfd.svg

Could it be that the difference is that the original svg has width="100%" height="100%" viewBox="0 0 3200 3200" and my example has width="640" height="480" viewBox="0 0 640 480" ?

@tonyfarney
Copy link

I am facing the same issue here. Is there some workaround?

@0xturner
Copy link

I've encountered the same issue. Only SVG's with a fixed height and width are cropped correctly.

Not a perfect solution, but a workaround that worked for my use case is using the view box dimensions as a fallback values if a fixed height and width are not present. I preprocess SVG files, adding a fixed height and width, before passing to react-easy-crop's Cropper

const svg = svgDoc.querySelector("svg");

if (
  !svg ||
  (isFixedPixelValue(svg.getAttribute("width")) &&
    isFixedPixelValue(svg.getAttribute("height")))
) {
  return;
}

const [, , viewBoxWidth, viewBoxHeight] =
  svg.getAttribute("viewBox")?.split(/[\s,]+/) ?? [];

if (!viewBoxWidth || !viewBoxHeight) {
  return;
}

svg.setAttribute("width", viewBoxWidth);
svg.setAttribute("height", viewBoxHeight);

...

function isFixedPixelValue(value: string | null) {
  return value && (/^\d*\.?\d+px$/.test(value) || /^\d*\.?\d+$/.test(value));
}

@JohnCido JohnCido changed the title Incorrect corp area output when the input is SVG Incorrect crop area output when the input is SVG Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants