| 80 | // to sit over the clicked thumbnail, then animate the transform away so it |
| 81 | // visually grows from the thumbnail into the full view. |
| 82 | const playFlip = () => { |
| 83 | const img = imgRef.current; |
| 84 | if (!originRect || !img || hasPlayedRef.current) { |
| 85 | return; |
| 86 | } |
| 87 | const final = img.getBoundingClientRect(); |
| 88 | if (!final.width || !final.height) { |
| 89 | // Not laid out yet (image still loading) — onLoad will retry. |
| 90 | return; |
| 91 | } |
| 92 | hasPlayedRef.current = true; |
| 93 | |
| 94 | // A single uniform scale (not independent sx/sy) so the image keeps its |
| 95 | // aspect ratio and never warps mid-flight. `min` contains the start frame |
| 96 | // within the thumbnail's footprint, so it reads as lifting off from there. |
| 97 | const scale = Math.min( |
| 98 | originRect.width / final.width, |
| 99 | originRect.height / final.height, |
| 100 | ); |
| 101 | // Translate centers together (transform-origin is the center too), so the |
| 102 | // image grows out of where the thumbnail sat rather than from a corner. |
| 103 | const dx = |
| 104 | originRect.left + originRect.width / 2 - (final.left + final.width / 2); |
| 105 | const dy = |
| 106 | originRect.top + originRect.height / 2 - (final.top + final.height / 2); |
| 107 | // Transform scales the corner radius along with everything else, so a fixed |
| 108 | // radius would render near-square at the small start scale and then "snap" |
| 109 | // round. Pre-divide by the scale so the rendered radius stays a constant |
| 110 | // ~16px (rounded-16) for the whole animation; read the resting value off the |
| 111 | // element so it tracks the class instead of a hardcoded number. |
| 112 | const restRadius = |
| 113 | parseFloat(getComputedStyle(img).borderTopLeftRadius) || 16; |
| 114 | |
| 115 | img.style.transformOrigin = 'center center'; |
| 116 | img.style.transition = 'none'; |
| 117 | img.style.transform = `translate(${dx}px, ${dy}px) scale(${scale})`; |
| 118 | img.style.borderRadius = `${restRadius / scale}px`; |
| 119 | img.style.opacity = '1'; |
| 120 | // Two frames: let the browser paint the start state first, then transition |
| 121 | // to identity — doing both in one frame can coalesce and snap. |
| 122 | requestAnimationFrame(() => { |
| 123 | requestAnimationFrame(() => { |
| 124 | img.style.transition = |
| 125 | 'transform 300ms cubic-bezier(0.16, 1, 0.3, 1), border-radius 300ms cubic-bezier(0.16, 1, 0.3, 1)'; |
| 126 | img.style.transform = 'none'; |
| 127 | img.style.borderRadius = `${restRadius}px`; |
| 128 | }); |
| 129 | }); |
| 130 | }; |
| 131 | |
| 132 | useLayoutEffect(() => { |
| 133 | playFlip(); |