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

Prevent jump when zooming out with double tap #292

Merged
merged 2 commits into from May 11, 2024

Conversation

Pascal-So
Copy link
Contributor

Hi, I'm back with another PR :)

This time I took a look at the zooming animation that appears when double-tapping. Specifically, when zooming out, the previous implementation sometimes had a visual jump at the start of the zoom-out animation. I recorded an example in this video:

zoomout-old.webm

The problem was that the x and y variables were updated on every touch, meaning that when zooming out, the zoom location would first jump to the coordinate where the touch happened. My first instinct to fix this was to just only update the coordinates when zooming in, and leave them at the same value when zooming out. Unfortunately this doesn't work, since the start of the zoom-out animation will be wherever the zoom-in animation ended, and thus will still jump if the user has panned around in the meantime.

To fix this, I had to make some significant changes to the animateZoom function. This function now starts by determining a start and end location for the zoom animation, and then only passing an inner function to requestAnimationFrame instead of re-running the calculations on every frame. The start of the zoom-out animation can now be computed from the current state.

The new behaviour looks like this:

zoomout-new.webm

I believe that these changes also make the animateZoom function more readable, but of course I'm biased since this time I'm the one who wrote it 😅 so I'll let you be the judge of that

Copy link

stackblitz bot commented May 10, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

netlify bot commented May 10, 2024

Deploy Preview for zoom-image-library ready!

Name Link
🔨 Latest commit 0a7928c
🔍 Latest deploy log https://app.netlify.com/sites/zoom-image-library/deploys/663f67603ac2860008ed287e
😎 Deploy Preview https://deploy-preview-292--zoom-image-library.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@Pascal-So
Copy link
Contributor Author

Pascal-So commented May 10, 2024

One note on the side: I've also removed the preventDefault call from the touchstart handler since to me it looks like this is not needed. In the immich project (the reason why I found this library), the preventDefault is even a bit annoying because the user can't clear the hover state from other ui elements such as navigation arrows by touching the image.

If you see a reason why we shouldn't change this then I'm happy to add it back in. Feel free to object on that

@Pascal-So
Copy link
Contributor Author

Also, do you need me to run the changeset command again?

@willnguyen1312
Copy link
Owner

Yeah, everything looks great. Just please add changeset as it will make my job easy to release it 😊

Comment on lines +274 to +276
function lerp(a: number, b: number, t: number): number {
return a * (1 - t) + b * t
}
Copy link
Owner

Choose a reason for hiding this comment

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

Oh man, thanks so much! I really miss Math back in university and high school ❤️ What I do nowadays in the industry as FrontEnd engineer has almost nothing to do with numbers, mostly logic 🙈 I should start spending more time on this beautiful stuff 😍

@Pascal-So
Copy link
Contributor Author

done :)

Copy link
Owner

@willnguyen1312 willnguyen1312 left a comment

Choose a reason for hiding this comment

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

Thanks again for your beautiful PR 🫶, you opened my mind 😊

@willnguyen1312 willnguyen1312 merged commit 3abc2d9 into willnguyen1312:main May 11, 2024
7 checks passed
@github-actions github-actions bot mentioned this pull request May 11, 2024
@Pascal-So Pascal-So deleted the fix/double-tap-zoom-out branch May 11, 2024 22:10
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.

None yet

2 participants