Planning FLIP animation library
Summarized a detailed approach to implement a reusable FLIP animation library that batches measurements and DOM changes with requestAnimationFrame to avoid layout thrashing, ensuring smooth 60fps animations on shared layout updates like grid reordering; next step is coding the class and example usage as one concise snippet.
Here's a minimal, reusable “FLIP” helper and an example of using it to animate a grid reorder. The helper batches reads and writes and only animates via transform/opacity so the compositor can keep up with 60 fps.
<!doctype html>
<meta charset="utf-8">
<style>
body { font: 14px sans-serif; margin: 20px; }
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
gap: 12px;
}
.card {
background: linear-gradient(135deg, #6c6, #49f);
color: white;
padding: 24px;
border-radius: 8px;
box-shadow: 0 8px 16px rgba(0,0,0,0.1);
will-change: transform, opacity;
}
button { margin-bottom: 12px; padding: 8px 16px; }
</style>
<button id="shuffle">Shuffle</button>
<div class="grid" id="grid"></div>
<script>
// --- FLIP helper ----------------------------------------------------------
class FLIPAnimator {
constructor(container, itemSelector, defaults = {}) {
this.container = container;
this.itemSelector = itemSelector;
this.defaults = Object.assign({ duration: 300, easing: 'ease' }, defaults);
}
// Capture positions/sizes of current items
_measure() {
const items = Array.from(this.container.querySelectorAll(this.itemSelector));
const rects = new Map();
items.forEach(el => {
const r = el.getBoundingClientRect();
rects.set(el, { top: r.top, left: r.left, width: r.width, height: r.height });
});
return rects;
}
/**
* Run a FLIP animation around a DOM mutation.
* @param {Function} mutateFn - apply your DOM changes inside this callback.
* @param {Object} [opts] - {duration, easing}
*/
flip(mutateFn, opts = {}) {
const config = Object.assign({}, this.defaults, opts);
// READ 1: First positions
const first = this._measure();
// MUTATE: apply layout changes
mutateFn();
// Force the DOM change to apply before measuring again
// Using rAF gives the browser a tick to apply styles without thrashing.
requestAnimationFrame(() => {
// READ 2: Last positions
const last = this._measure();
// WRITE: Invert & play
first.forEach((from, el) => {
const to = last.get(el);
if (!to) return; // element might have been removed
const dx = from.left - to.left;
const dy = from.top - to.top;
const sx = from.width / to.width;
const sy = from.height / to.height;
// Invert
el.style.transition = 'none';
el.style.transformOrigin = 'top left';
el.style.transform = `translate(${dx}px, ${dy}px) scale(${sx}, ${sy})`;
el.style.opacity = '1';
// Force style flush so transition can run
// eslint-disable-next-line no-unused-expressions
el.offsetWidth;
// Play
el.style.transition = `transform ${config.duration}ms ${config.easing}, opacity ${config.duration}ms ${config.easing}`;
el.style.transform = 'translate(0,0) scale(1,1)';
el.style.opacity = '1';
});
});
}
}
// --- Example usage --------------------------------------------------------
const grid = document.getElementById('grid');
// Populate with some cards
const items = Array.from({ length: 16 }, (_, i) => i + 1);
items.forEach(n => {
const el = document.createElement('div');
el.className = 'card';
el.textContent = `Item ${n}`;
grid.appendChild(el);
});
const flip = new FLIPAnimator(grid, '.card');
document.getElementById('shuffle').addEventListener('click', () => {
flip.flip(() => {
// mutate: randomly reorder nodes
const kids = Array.from(grid.children);
kids.sort(() => Math.random() - 0.5);
kids.forEach(child => grid.appendChild(child));
}, { duration: 400, easing: 'cubic-bezier(0.22, 1, 0.36, 1)' });
});
</script>
How it works
- First: measure all items’ bounding boxes.
- Last: apply your layout change (reorder, add/remove, resize), then measure again.
- Invert: for each element, compute the delta between first and last and set a
transform that “moves” it back to where it came from (no transition).
- Play: in the next frame, clear the transform with a CSS transition, so the element animates from its old place to the new one.
Reads (measurements) and writes (style changes) are grouped separately and scheduled with requestAnimationFrame to avoid interleaving them and causing layout thrashing. Animations are done with transform/opacity, which stay on the compositor, giving you smooth 60 fps transitions.
You can wrap any DOM mutation in flip.flip(() => { /* mutate DOM */ }) to get a smooth shared-layout animation. Adjust duration/easing per call or globally via the constructor.