Skip to content

Instantly share code, notes, and snippets.

@smashingpat
Created June 11, 2019 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smashingpat/df609c77840f97bfdb910e2ec27b7108 to your computer and use it in GitHub Desktop.
Save smashingpat/df609c77840f97bfdb910e2ec27b7108 to your computer and use it in GitHub Desktop.
create FLIP animation
function calculateFlipPositions(firstRect: ClientRect, lastRect: ClientRect) {
return {
x: firstRect.left - lastRect.left,
y: firstRect.top - lastRect.top,
w: firstRect.width / lastRect.width,
h: firstRect.height / lastRect.height,
};
}
function triggerRepaint(element: HTMLElement) {
element.offsetHeight;
}
export function createFlipAnimation(firstElement: HTMLElement) {
const firstRect = firstElement.getBoundingClientRect();
return function start(lastElement = firstElement) {
lastElement.style.transition = 'none';
triggerRepaint(lastElement);
const lastRect = lastElement.getBoundingClientRect();
const calc = calculateFlipPositions(firstRect, lastRect);
lastElement.style.transform = `translate(${calc.x}px, ${calc.y}px) scale(${calc.w}, ${calc.h})`;
lastElement.style.transformOrigin = 'top left';
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment