3D Extrude Reveal
The letters rise up out of the page one after another. Each lifts from a faint print, growing a solid extruded side and a soft shadow beneath it, and overshoots a touch before it settles.
Extrude
How to use it
- Paste the snippet just before
</head>. - Add
data-extrude-revealto a heading or short line of text.
Options
Add any of these attributes to the same element.
data-color | The extruded side. Default the text colour, darkened. |
data-depth | How far the letters rise, in em. Default 0.12. |
data-duration | How long each letter takes to rise. Default 1000 ms. |
data-stagger | Gap between letters. Default 60 ms. |
data-loop | Rise, hold, sink back into the page and rise again. |
data-hold | How long the letters stay up, when looping. Default 1800 ms. |
data-delay | Wait before starting. Default 0 ms. |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// 3D Extrude Reveal: letters rise up out of the page one by one, growing a
// solid extruded side and a soft shadow as they come up.
// Usage: <h1 data-extrude-reveal>Extrude</h1>
// Optional: data-color (the extruded side, default the text colour mixed
// with black), data-depth (in em, default 0.12), data-duration (ms per
// letter, default 1000), data-stagger (ms between letters, default 60),
// data-loop (sink back into the page and rise again), data-hold (ms to stay
// up, when looping), data-delay (ms before starting).
var DEFAULTS = { depth: 0.12, duration: 1000, stagger: 60, hold: 1800, delay: 0 };
var css =
'.exr-word{display:inline-block;white-space:nowrap}' +
'.exr-ch{display:inline-block;will-change:transform}';
var style = document.createElement('style');
style.textContent = css;
document.head.appendChild(style);
function reduced() {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}
function split(el) {
var text = el.textContent.replace(/\s+/g, ' ').trim();
el.setAttribute('aria-label', text);
el.textContent = '';
var chars = [];
text.split(' ').forEach(function (word, i) {
if (i) el.appendChild(document.createTextNode(' '));
var w = document.createElement('span');
w.className = 'exr-word';
w.setAttribute('aria-hidden', 'true');
Array.from(word).forEach(function (c) {
var s = document.createElement('span');
s.className = 'exr-ch';
s.textContent = c;
w.appendChild(s);
chars.push({ el: s, k: 0 });
});
el.appendChild(w);
});
return chars;
}
// Draws a letter raised by `k` (0 = flat on the page, 1 = full depth): the
// face lifts up-left and a stack of 1px shadows fills in the side.
function draw(c, depth, side) {
var k = c.k, dz = depth * k;
var layers = Math.round(dz);
var parts = [];
for (var i = 1; i <= layers; i++) parts.push((i * 0.6).toFixed(1) + 'px ' + i + 'px 0 ' + side);
var soft = Math.max(0, dz);
parts.push((soft * 0.8).toFixed(1) + 'px ' + (soft * 1.4 + 1).toFixed(1) + 'px ' + (soft * 1.2).toFixed(1) + 'px rgba(0,0,0,' + (0.22 * Math.min(1, k)).toFixed(3) + ')');
c.el.style.textShadow = parts.join(',');
c.el.style.transform = 'translate(' + (-dz * 0.6).toFixed(1) + 'px,' + (-dz).toFixed(1) + 'px)';
c.el.style.opacity = Math.min(1, 0.1 + k * 2.5).toFixed(3);
}
function rise(el, up, done) {
var s = el._exr, d = el.dataset;
var cs = getComputedStyle(el);
var depth = Math.max(3, parseFloat(cs.fontSize) * (+d.depth || DEFAULTS.depth));
var side = d.color || 'color-mix(in srgb, ' + cs.color + ' 55%, #000)';
var duration = +d.duration || DEFAULTS.duration;
var stagger = d.stagger !== undefined ? +d.stagger : DEFAULTS.stagger;
var left = s.chars.length;
s.chars.forEach(function (c, i) {
anime.animate(c, {
k: up ? [0, 1] : [1, 0],
duration: up ? duration : duration * 0.5,
delay: i * (up ? stagger : stagger * 0.5),
ease: up ? 'outBack(1.6)' : 'inQuad',
onUpdate: function () { draw(c, depth, side); },
onComplete: function () {
if (!up) c.el.style.opacity = 0;
if (--left === 0 && done) done();
}
});
});
}
function play(el) {
var d = el.dataset;
var loop = d.loop !== undefined && d.loop !== 'false';
rise(el, true, function () {
if (!loop) return;
setTimeout(function () {
rise(el, false, function () { setTimeout(function () { play(el); }, 400); });
}, +d.hold || DEFAULTS.hold);
});
}
function init() {
var io = new IntersectionObserver(function (entries) {
entries.forEach(function (e) {
if (!e.isIntersecting) return;
io.unobserve(e.target);
var el = e.target;
setTimeout(function () { play(el); }, +el.dataset.delay || DEFAULTS.delay);
});
}, { threshold: 0.2 });
document.querySelectorAll('[data-extrude-reveal]').forEach(function (el) {
if (el._exr) return;
el._exr = { chars: split(el) };
var cs = getComputedStyle(el);
var depth = Math.max(3, parseFloat(cs.fontSize) * (+el.dataset.depth || DEFAULTS.depth));
var side = el.dataset.color || 'color-mix(in srgb, ' + cs.color + ' 55%, #000)';
el._exr.chars.forEach(function (c) {
c.k = reduced() ? 1 : 0;
draw(c, depth, side);
if (!reduced()) c.el.style.opacity = 0;
});
if (!reduced()) io.observe(el);
});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><h1 data-extrude-reveal>Extrude</h1>
<!-- or, deeper, with a navy side: -->
<h1 data-extrude-reveal data-depth="0.2" data-color="#1e1b4b">Extrude</h1>