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

Constrain image when rotated #53

Merged
merged 6 commits into from
Aug 28, 2019

Conversation

MattyBalaam
Copy link
Contributor

@MattyBalaam MattyBalaam commented Aug 22, 2019

Adds support for constraining an image in the crop area when rotated.

See #52

@ValentinH
Copy link
Owner

I've played again with your demo and I actually think we should combine this one and the one you made before where the crop size was changed.

I'm not sure where to draw the limit but I think the crop size should only be modified if the image width (or height) gets smaller than the cropper. Another limit that might be easier to define would be to prevent the crop size being bigger than the container ones. What do you think?

For example, this is what should the crop size be when rotation is 0:
image

And this is for 90deg rotation:
image

If you want there is also a vertical image (cat.jpeg) in the example folder 😉

@MattyBalaam
Copy link
Contributor Author

I have managed to accomplish this (at least on landscape, I’ll take a look at portrait later when I have some time) by adding an additional check to the getCropSize function.

@ValentinH
Copy link
Owner

I don't know if you pushed your latest changes, but I actually see the opposite behavior when the image is close to 90deg: it crop size gets even wider.
image

@MattyBalaam
Copy link
Contributor Author

MattyBalaam commented Aug 23, 2019

Apologies, try again now.

Did a flawed rebase.

@MattyBalaam
Copy link
Contributor Author

I’ve looked at the cat image now and I’ll need to apply some extra logic to handle the portrait case.

@ValentinH
Copy link
Owner

Yes this looks great now! Just one question, do we want to also resize the crop area if the image is zoomed? It's actually not needed but might be simpler: otherwise, if the user zooms, rotate, and zooms back, we need to resize again while zooming back (don't know if this is clear ^^)

@MattyBalaam
Copy link
Contributor Author

I see, so this could be done with an additional check in the crop size calculation which accepts the zoom value perhaps.

@MattyBalaam
Copy link
Contributor Author

I’ve pushed a change purely for handling the sizing on landscape images. I couldn’t get a nice result zooming the crop area.

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.

Just a few code style comments but overall this looks great to me! You still need to fix the existing unit tests. Also would you like to add a few test cases for your new functions? You can also add one or 2 end-to-end tests using Cypress if you are interested :)

src/helpers.js Outdated
@@ -25,10 +42,12 @@ export function getCropSize(imgWidth, imgHeight, aspect) {
* @param {number} zoom zoom value
* @returns {{x: number, y number}}
*/
export function restrictPosition(position, imageSize, cropSize, zoom) {
export function restrictPosition(position, imageSize, cropSize, zoom, rotation) {
Copy link
Owner

Choose a reason for hiding this comment

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

Here you should set the default value for the rotation to 0.

src/helpers.js Outdated
@@ -157,3 +176,39 @@ export function getCenter(a, b) {
y: (b.y + a.y) / 2,
}
}

function rotate(x, y, xm, ym, a) {
Copy link
Owner

Choose a reason for hiding this comment

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

Could you use more verbose variable names so that it's easier to understand what each of them do? 🙂

src/helpers.js Outdated
@@ -157,3 +176,39 @@ export function getCenter(a, b) {
y: (b.y + a.y) / 2,
}
}

function rotate(x, y, xm, ym, a) {
var cos = Math.cos,
Copy link
Owner

Choose a reason for hiding this comment

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

Also please use const here and create a new variable for a (rotationInRadians maybe) to avoid overwriting to function arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I was being lazy and had just copied in something I found online, will rewrite to be more descriptive in a couple of days

@ValentinH
Copy link
Owner

BTW I've just added support for rotation gestures on mobile. It should not have any impact with your PR but let's see.

@ValentinH
Copy link
Owner

Hey @mbalaam, just wanted to let you know that I'll be offline for the next 3 weeks starting from tomorrow evening. Thus, if you need this feature soon, we need to wrap this up before :)

@MattyBalaam
Copy link
Contributor Author

MattyBalaam commented Aug 27, 2019 via email

@MattyBalaam MattyBalaam changed the title experiment Constrain image when rotated Aug 28, 2019
@MattyBalaam
Copy link
Contributor Author

I have updated the docs and unit tests. I’ve not used Cypress and won’t have time to learn it in the next day or so unfortunately.

One thing I have noticed is that there will be some floating numbers with a few decimal places. I wondered if it might be a good idea to round these in any helpers that return to a component.

@ValentinH ValentinH merged commit 0d9efb0 into ValentinH:master Aug 28, 2019
@ValentinH
Copy link
Owner

Wonderful! Thank you so much for taking care of this! ❤️

@MattyBalaam
Copy link
Contributor Author

My pleasure.

@ValentinH
Copy link
Owner

ValentinH commented Aug 28, 2019

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