Paper Drift In
The element drifts down into place like a dropped sheet of paper: it swings from side to side, tipping up at the end of each swing, and the swings die away as it settles flat.
Letter receivedFrom: Grandma
How to use it
- Paste the snippet just before
</head>. - Add
data-paper-drift-into any element.
Options
Add any of these attributes to the same element.
data-distance | How far above its place it starts, in px. Default 140. |
data-sway | How far it swings to each side, in px. Default 46. |
data-swings | How many swings it makes on the way down. Default 3. |
data-tilt | How far it tips at the end of each swing, in degrees. Default 14. |
data-duration | How long the whole drift takes. Default 2200 ms. |
data-delay | Wait before the first entrance. Default 0 ms. |
data-loop | Hold, fade away and drift in again, swinging the other way first. |
data-hold | How long it stays before fading, when looping. Default 1400 ms. |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Paper Drift In: the element drifts down into place like a falling sheet
// of paper, swinging side to side, tipping up at the end of each swing
// and settling flat as it lands.
// Usage: <div data-paper-drift-in>…</div>
// Optional: data-distance (how far above its place it starts, in px,
// default 140), data-sway (how far it swings to each side, in px, default
// 46), data-swings (how many swings it makes, default 3), data-tilt (how
// far it tips at the end of a swing, in degrees, default 14),
// data-duration (ms, default 2200), data-delay (ms before the first
// entrance), data-loop (hold, fade away and drift in again, starting from
// the other side), data-hold (ms to stay when looping, default 1400).
var DEFAULTS = { distance: 140, sway: 46, swings: 3, tilt: 14, duration: 2200, delay: 0, hold: 1400 };
function reduced() {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}
function num(v, def) {
return v === undefined || v === '' || isNaN(+v) ? def : +v;
}
// Where it is at time p (0 to 1): a pendulum swing that dies away while
// the sheet sinks, lifting a little and tipping at the end of each swing.
function pose(o, p) {
var phase = p * o.swings * Math.PI;
var fade = Math.pow(1 - p, 1.3);
var s = Math.sin(phase) * o.side;
return {
x: o.sway * s * fade,
y: -o.distance * Math.pow(1 - p, 1.4) - o.sway * 0.22 * s * s * fade,
rotate: -o.tilt * s * fade
};
}
function enter(el, side, done) {
var d = el.dataset;
var t = num(d.duration, DEFAULTS.duration) || DEFAULTS.duration;
var o = {
distance: num(d.distance, DEFAULTS.distance),
sway: num(d.sway, DEFAULTS.sway),
swings: Math.max(0.5, num(d.swings, DEFAULTS.swings)),
tilt: num(d.tilt, DEFAULTS.tilt),
side: side
};
var clock = { p: 0 };
var start = pose(o, 0);
anime.utils.set(el, { x: start.x, y: start.y, rotate: start.rotate, opacity: 0 });
anime.animate(clock, {
p: 1,
duration: t,
ease: 'linear',
onUpdate: function () {
var q = pose(o, clock.p);
anime.utils.set(el, { x: q.x, y: q.y, rotate: q.rotate, opacity: Math.min(1, clock.p / 0.22) });
},
onComplete: function () {
anime.utils.set(el, { x: 0, y: 0, rotate: 0, opacity: 1 });
done();
}
});
}
function cycle(el) {
var d = el.dataset;
var s = el._pdi;
enter(el, s.n++ % 2 ? -1 : 1, function () {
if (d.loop === undefined || d.loop === 'false') return;
setTimeout(function () {
anime.animate(el, {
opacity: 0,
duration: 400,
ease: 'out(2)',
onComplete: function () {
// Wait until it's back on screen before the next entrance.
setTimeout(function () {
if (s.visible) cycle(el);
else s.waiting = true;
}, 500);
}
});
}, num(d.hold, DEFAULTS.hold));
});
}
function init() {
var io = new IntersectionObserver(function (entries) {
entries.forEach(function (e) {
var s = e.target._pdi;
s.visible = e.isIntersecting;
if (!e.isIntersecting) return;
if (!s.started) {
s.started = true;
setTimeout(function () { cycle(e.target); }, num(e.target.dataset.delay, DEFAULTS.delay));
} else if (s.waiting) {
s.waiting = false;
cycle(e.target);
}
});
}, { threshold: 0.2 });
document.querySelectorAll('[data-paper-drift-in]').forEach(function (el) {
if (el._pdi) return;
el._pdi = { n: 0 };
if (reduced()) return;
anime.utils.set(el, { opacity: 0 });
io.observe(el);
});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><div class="card" data-paper-drift-in>Letter received</div>
<!-- or a longer, lazier fall: -->
<div class="card" data-paper-drift-in data-distance="240" data-swings="4" data-duration="3200">Letter received</div>