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

Adding objectFit property #51

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Conversation

drijnkels
Copy link
Contributor

Adds the objectFit property to the Cropper as requested in #14.

Similar implementation as the react-easy-crop version apart from the cover option. Also fixes a small JSDoc issue.

Copy link
Owner

@ValentinH ValentinH left a comment

Choose a reason for hiding this comment

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

I think that some JS logic is missing regarding the image size handling based on objectFit. See this part of the code on react-easy-crop: https://github.com/ValentinH/react-easy-crop/blob/bba2b53bab1d409b719b9e48d156a7c35f8755bd/src/Cropper.tsx#L374-L385

In addition, why not also supporting cover?

@drijnkels
Copy link
Contributor Author

Completely missed that part! I initially started on this to get a feel for the package. I have some time tonight, I'll take another look and also look at the cover option.

@Shackless
Copy link

@drijnkels any news on this?

@drijnkels
Copy link
Contributor Author

I spent a little time on it but got distracted by work. The cover option is added but I have a hard time understanding why renderedMediaSize is being recalculated everytime computeSizes is called. @ValentinH could you explain this? I'm not seeing any issues in the svelte version that is not doing the recalculation.

@ValentinH
Copy link
Owner

I spent a little time on it but got distracted by work. The cover option is added but I have a hard time understanding why renderedMediaSize is being recalculated everytime computeSizes is called. @ValentinH could you explain this? I'm not seeing any issues in the svelte version that is not doing the recalculation.

In the react version, computeSizes is called in many cases including when the window is resized or when the objectFit property is changed. Therefore, we need to recompute renderedMediaSize as it depends on the objectFit and on the container size.

@@ -17,6 +17,8 @@
export let crossOrigin: HTMLImgAttributes['crossorigin'] = null
export let restrictPosition = true
export let tabindex: number | undefined = undefined
export let objectFit: ObjectFit = 'contain'
export let mediaObjectFit: ObjectFit = objectFit
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think mediaObjectFit should be exported as it's an property that is computed internally.

@@ -29,6 +31,7 @@
let rafDragTimeout: number | null = null
let rafZoomTimeout: number | null = null

const imageObjectFitClass = {"contain": 'image_contain', "cover": "horizontal-cover", "horizontal-cover": "image_horizontal_cover", 'vertical-cover': "image_vertical_cover"};
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
const imageObjectFitClass = {"contain": 'image_contain', "cover": "horizontal-cover", "horizontal-cover": "image_horizontal_cover", 'vertical-cover': "image_vertical_cover"};
const imageObjectFitClass = {"contain": 'image_contain', "cover": "image_horizontal_cover", "horizontal-cover": "image_horizontal_cover", 'vertical-cover': "image_vertical_cover"};

Comment on lines +201 to +202
// const newZoom = zoom - (e.deltaY * zoomSpeed) / 200 // Can give 1.01 due to rounding errors
const newZoom = (e.deltaY > 0) ? zoom - (zoomSpeed / 2) : zoom + (zoomSpeed / 2);
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think that this change is intended as part of this PR.

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

Successfully merging this pull request may close these issues.

3 participants