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

fix: replace objectFit: 'auto-cover' by 'cover' with adjusted logic #466

Merged
merged 2 commits into from
Jun 26, 2023

Conversation

kruchkou
Copy link
Contributor

@kruchkou kruchkou commented Jun 12, 2023

Release notes

the objectFit auto-cover option was removed and replaced by cover. While doing so, the logic detecting the right value to choose between horizontal and vertical was rewritten from scratch. The algorithm is now much better at picking the right choice.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 12, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit ecc084a:

Sandbox Source
react-easy-crop Configuration
react-easy-crop (forked) PR

@kruchkou
Copy link
Contributor Author

Hi, @ValentinH!

Any chance that functionality could be added?
Best regards

@ValentinH
Copy link
Owner

Thanks for this PR. Before merging, I need to take a bit of time to understand why we need so many options. I'm wondering if this new behavior should not be what auto-cover was supposed to be doing in the first place 😁
By reading the description, I think that fill would be more explicit than fit though. I'll play with the examples tomorrow.

@kruchkou
Copy link
Contributor Author

Thanks for the answer!

I'm wondering if this new behavior should not be what auto-cover was supposed to be doing in the first place 😁

I was thinking the same as that's how the default object-fit: cover works 😁

Agree on 'fill', just want to note that 'object-fit: fill' usually stretches the image
Let me know if you would like to see any changes

@kruchkou
Copy link
Contributor Author

One more note here:
I've just checked that 'auto-cover' actually looks the same as contain

It could make sense to mark auto-cover as deprecated and name the new option just 'cover'

@ValentinH
Copy link
Owner

@kruchkou I understand why I said that what you are looking for is what auto-cover is supposed to be doing: just checking if the image is wide or tall isn't enough: we also need to take the container aspect into account.
If you check this sandbox that is comparing both auto-cover and the new fit object-fit on each combination of tall/wide images and tall/wide containers, we can see that each time only one of them is correct.

Therefore, I don't think we need a new option. Instead, we need to also take the container aspect into account when picking between vertical-cover and horizontal-cover. What do you think?

If we manage to have this work, I would just name this option cover to align with the standard CSS option.

@kruchkou
Copy link
Contributor Author

kruchkou commented Jun 14, 2023

@ValentinH
Now I see your point. That is not related to my particular case (square container, square crop area), but related to the other ones.

Therefore, I don't think we need a new option. Instead, we need to also take the container aspect into account when picking between vertical-cover and horizontal-cover.

I assume that comparing the container aspect and the picture aspect could help to determine the proper image size and alignment.

@ValentinH
Copy link
Owner

The decision should be like this from what I could observe:

  • if the image is square, then the fit logic is better
  • else if the image aspect is similar to the container one (horizontal/horizontal or vertical/vertical), then the auto-cover logic is better
  • else, the fit logic is better

Therefore, I would implement the above logic as part of a new coveroption, remove the auto-cover and bump a major version.

@ValentinH ValentinH added the major Increment the major version when merged label Jun 20, 2023
@ValentinH ValentinH changed the title feat: add objectFit: 'fit' fix: replace objectFit: 'auto-cover' by 'cover' with adjusted logic Jun 20, 2023
@ValentinH
Copy link
Owner

ValentinH commented Jun 20, 2023

@kruchkou I managed to find a simple rule to pick between horizontal and vertical cover:

mediaAspect < containerAspect ? 'horizontal-cover' : 'vertical-cover'

I updated the PR to rename auto-cover to cover. If the logic fits your need, I'll merge this and bump a major release.

Demo: https://codesandbox.io/s/react-easy-crop-forked-d93p44?file=/src/index.js
image

@kruchkou
Copy link
Contributor Author

Hello @ValentinH!

That is exactly what I was needed, thanks for the changes!

@ValentinH ValentinH merged commit 010888b into ValentinH:main Jun 26, 2023
3 checks passed
@github-actions
Copy link

🚀 PR was released in v5.0.0 🚀

@github-actions github-actions bot added the released This issue/pull request has been released. label Jun 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
major Increment the major version when merged released This issue/pull request has been released.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants