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

Focal zoom with flex-centered images #611

Open
enxi2 opened this issue Feb 20, 2022 · 2 comments
Open

Focal zoom with flex-centered images #611

enxi2 opened this issue Feb 20, 2022 · 2 comments

Comments

@enxi2
Copy link

enxi2 commented Feb 20, 2022

Describe the bug

Hello, I'm trying to add panzoom to an image gallery popup, but am running into issues with the focal zoom when using a centered image. I've read through the previous issues on this topic, but still can't quite understand why the focal zoom isn't working as expected when using a centered image.

Expected behavior

Here's an implementation of what I would like with focal zoom using plain JS: https://codepen.io/erinxi/pen/WNXzovm

Actual behavior

Here's a minimal reproduction of the gallery popup with panzoom: https://codepen.io/erinxi/pen/GROxNVa
Zooming in to the tiger's eye does not focus on the eye like it would in the demo https://timmywil.com/panzoom/demo/.

If I understand correctly, calculations are done in the parent element's coordinates space (with 0, 0 in the top left)? And in the panzoom demo this is accommodated for using margins, however when using flexbox there are no margins? Any insight or suggestions on the appropriate workaround on this would be greatly appreciated!

@timmywil
Copy link
Owner

Thanks for opening an issue. I see you're adjusting the clientX to the middle. I've considered doing the same whenever flex is set to centered, but that just sounds janky and I'd be bound to miss some use case.

That said, there's got to be a better way to account for both use cases and I think it's possible by accounting for the elem's position, but I'm getting the math wrong somewhere. I've adjusted your plain JS example to do the same calculations done in Panzoom. I'm going to play with your example more and see if I can work out calculations to account for both use cases.

https://codepen.io/timmywil/pen/rNYrpNz

@enxi2
Copy link
Author

enxi2 commented Feb 26, 2022

Thanks for the example! For now I've been setting the image to the top-left and manually calculating a starting position / scale (needed for fitting the image to the parent container anyways).

const zoom =
  imageAspect > parentAspect // wide or tall
    ? parentW / imageW : parentH / imageH;
panzoom.current.zoom(zoom);
panzoom.current.pan((parentW - imageW) / 2 / zoom, (parentH - imageH) / 2 / zoom);

@timmywil timmywil changed the title Focal zoom with centered images Focal zoom with flex-centered images Feb 28, 2022
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

No branches or pull requests

2 participants