Skip to content

Commit

Permalink
img-zoom | Invert scale and translation transformations so image does…
Browse files Browse the repository at this point in the history
… not fly off screen
  • Loading branch information
Auroratide committed Sep 22, 2024
1 parent 8f12f3b commit 88d983d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-horses-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@auroratide/img-zoom": patch
---

Invert translation and scale transformations so image does not fly offscreen
21 changes: 4 additions & 17 deletions components/img-zoom/src/animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,19 @@ export type AnimationConfig = {

export type Animator = (from: HTMLImageElement, dest: HTMLImageElement) => AnimationDescription | AnimationDescription[]

const EASE_OUT_CUBIC = "cubic-bezier(0.33, 1, 0.68, 1)"

export function pop({
duration = 400,
}: AnimationConfig = {}): Animator {
return (from, dest) => {
const relativeTransform = getRelativeTransform(dest, from)
return [{
keyframes: [ {
transform: `scale(${relativeTransform.scale})`,
}, {
transform: "scale(1)",
} ],
options: {
duration,
easing: "ease-out",
},
}, {
keyframes: [ {
transform: `translate(${relativeTransform.tx}px, ${relativeTransform.ty}px)`,
transform: `translate(${relativeTransform.tx}px, ${relativeTransform.ty}px) scale(${relativeTransform.scale})`,
}, {
transform: "translate(0px, 0px)",
transform: "translate(0px, 0px) scale(1)",
} ],
options: {
duration,
easing: EASE_OUT_CUBIC,
},
}]
}
Expand Down Expand Up @@ -96,8 +83,8 @@ function getRelativeTransform(from: HTMLElement, dest: HTMLElement): {
const [dx, dy] = getCenter(d)

const r = dw / fw
const tx = (dx - fx) / r
const ty = (dy - fy) / r
const tx = dx - fx
const ty = dy - fy

return {
scale: r,
Expand Down

0 comments on commit 88d983d

Please sign in to comment.