Skip to content

Conversation

romankaravia
Copy link
Contributor

@romankaravia romankaravia commented Feb 5, 2021

Related to livingdocsIO/livingdocs-planning#4095 / livingdocsIO/livingdocs-planning#1084

Motivation

For very large images, it is either not possible¹ or not desirable (for perf reasons) to load them in full resolution in the cropping interface. In order to support this use case, a new option originalSize is introduced, which allows to detach the coordinate system of all cropping options (crop.{x,y,width,height}, minResolution, minWidth, etc.) from the pixel resolution of the preview image.

¹ e.g. imgix has a limitation of 8192px per dimension

Considered alternative: Introduce a scale factor that defines the relation between original and preview size. This was not done because it would introduce more complexity: All affected cropping options would have to be scaled and rounded to the nearest integer, which could also introduce subtle changes without any user action.

Changelog

  • Add originalSize option, which allows to display a downscaled version of the image in the cropping interface, but use the original size for crop attributes.

Add -d option to make source maps work in Chrome/Firefox.

From documentation (npx webpack --help):
  -d           shortcut for --debug --devtool eval-cheap-module-source-map --output-pathinfo
Rationale: There is already an eslint config in this repo.
Adding eslint as a dependency means that developers do not need
to install it globally and everybody is using the same version.
@romankaravia romankaravia requested a review from benib February 5, 2021 15:21
src/crop.js Outdated
Comment on lines 541 to 553
checkRatio (previewImageSize) {
if (this.originalSize) {
const expectedRatio = this.originalSize.width / this.originalSize.height
const actualRatio = previewImageSize.width / previewImageSize.height
const percentageChange = ((actualRatio - expectedRatio) / expectedRatio) * 100
if (Math.abs(percentageChange) > 1) {
// eslint-disable-next-line no-console
console.warn('srcissors: Expected and actual image ratio differ by more than 1%: ' +
`${expectedRatio} vs ${actualRatio}`)
}
}
}

Copy link

Choose a reason for hiding this comment

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

I wonder if we should actually just log and move on when this happens. I see that if correctly used, one never has an issue here. But it could lead to pretty bad results if ignored.

Have you thought about showing that error in the UI instead of loading the image in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I agree that console.warn is not super useful, but I don't really have a better idea. Showing an error in the UI directly is a bit difficult because srcissors has a very light UI so far, so I guess the way to achieve this would be to invoke an error callback or event and let the library client deal with it. Is this what you had in mind?

Copy link

Choose a reason for hiding this comment

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

Could we emit a ratio-mismatch (or something better named) event in this case? Like srcissors already has for ready and load and change?

Copy link
Member

Choose a reason for hiding this comment

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

What happens if the preview is the wrong size? Will scissors still produce valid crops?

And why not just throw an error?

Copy link

Choose a reason for hiding this comment

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

Throwing an error sounds like a good solution to me as well.

src/crop.js Outdated
const percentageChange = ((actualRatio - expectedRatio) / expectedRatio) * 100
if (Math.abs(percentageChange) > 1) {
// eslint-disable-next-line no-console
console.warn('srcissors: Expected and actual image ratio differ by more than 1%: ' +
Copy link
Member

Choose a reason for hiding this comment

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

The 'Expected' in the message confused me a bit.

Maybe: scrissors: Displayed image has a different image ratio than the one configured in 'originalRatio' ...?

Add `originalSize` option, which allows to display a downscaled version of the
image in the cropping interface, but use the original size for crop attributes.
@romankaravia
Copy link
Contributor Author

Thank you for the reviews! I changed the behavior to throw an error if the aspect ratio does not match and improved the error message as suggested by Lukas.

@romankaravia romankaravia merged commit 75c54da into master Feb 9, 2021
@delete-merged-branch delete-merged-branch bot deleted the feat-original-size branch February 9, 2021 14:52
@livingdocs-automation
Copy link

🎉 This PR is included in version 2.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

4 participants