-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
feat(web): animate zoom toggle with cubicOut easing #26731
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import { canCopyImageToClipboard } from '$lib/utils/asset-utils'; | |
| import { BaseEventManager } from '$lib/utils/base-event-manager.svelte'; | ||
| import { PersistedLocalStorage } from '$lib/utils/persisted'; | ||
| import type { ZoomImageWheelState } from '@zoom-image/core'; | ||
| import { cubicOut } from 'svelte/easing'; | ||
|
|
||
| const isShowDetailPanel = new PersistedLocalStorage<boolean>('asset-viewer-state', false); | ||
|
|
||
|
|
@@ -21,6 +22,7 @@ export type Events = { | |
|
|
||
| export class AssetViewerManager extends BaseEventManager<Events> { | ||
| #zoomState = $state(createDefaultZoomState()); | ||
| #animationFrameId: number | null = null; | ||
|
|
||
| imgRef = $state<HTMLImageElement | undefined>(); | ||
| isShowActivityPanel = $state(false); | ||
|
|
@@ -45,6 +47,7 @@ export class AssetViewerManager extends BaseEventManager<Events> { | |
| } | ||
|
|
||
| set zoom(zoom: number) { | ||
| this.cancelZoomAnimation(); | ||
| this.zoomState = { ...this.zoomState, currentZoom: zoom }; | ||
| } | ||
|
|
||
|
|
@@ -69,7 +72,35 @@ export class AssetViewerManager extends BaseEventManager<Events> { | |
| this.#zoomState = state; | ||
| } | ||
|
|
||
| cancelZoomAnimation() { | ||
| if (this.#animationFrameId !== null) { | ||
| cancelAnimationFrame(this.#animationFrameId); | ||
| this.#animationFrameId = null; | ||
| } | ||
| } | ||
|
|
||
| animatedZoom(targetZoom: number, duration = 300) { | ||
| this.cancelZoomAnimation(); | ||
|
|
||
| const startZoom = this.#zoomState.currentZoom; | ||
| const startTime = performance.now(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need this?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what the question is refering to: using performance.now() or using #isAnimatingFrame as a guard, or maybe we should be using I'll take a stab at all of those ;-)
|
||
|
|
||
| const frame = (currentTime: number) => { | ||
| const elapsed = currentTime - startTime; | ||
| const linearProgress = Math.min(elapsed / duration, 1); | ||
| const easedProgress = cubicOut(linearProgress); | ||
| const interpolatedZoom = startZoom + (targetZoom - startZoom) * easedProgress; | ||
|
|
||
| this.zoomState = { ...this.#zoomState, currentZoom: interpolatedZoom }; | ||
|
|
||
| this.#animationFrameId = linearProgress < 1 ? requestAnimationFrame(frame) : null; | ||
| }; | ||
|
|
||
| this.#animationFrameId = requestAnimationFrame(frame); | ||
| } | ||
|
|
||
| resetZoomState() { | ||
| this.cancelZoomAnimation(); | ||
| this.zoomState = createDefaultZoomState(); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be more elegant to
if (this.#animationFrameId) { return; }? I.e., let the animation finish instead of cancelling it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cancelling is intentional here —
animatedZoomis called fromonZoom()which toggles between zoom 1 and 2. If the user clicks the zoom button mid-animation, we want to immediately start animating toward the new target (potentially the opposite direction) rather than ignoring their input until the current animation finishes. An early return would make the UI feel unresponsive in that case.Also, with an early return, calling
animatedZoom(2)whileanimatedZoom(3)is running would silently drop the zoom-to-2 request, which isn't the latest user intent.