Long Shadow Reveal
The letters pop up one by one, then a long, flat shadow grows out from them at an angle, giving the text that bold, poster-style depth.
Long shadow
How to use it
- Paste the snippet just before
</head>. - Add
data-long-shadow-revealto a heading on a solid background, and setdata-colorto a shade a little darker than it.
Options
Add any of these attributes to the same element.
data-color | The shadow. Default #4c1d95. |
data-length | How long the shadow grows, in em. Default 0.6. |
data-angle | Which way it falls, in degrees: 45 is down-right, 135 is down-left. Default 45. |
data-duration | Time for the shadow to grow. Default 1000 ms. |
data-stagger | Gap between letters popping up. Default 40 ms. |
data-loop | Pop up, grow, hold, shrink back and pop up again. |
data-hold | How long the text stays before shrinking back, when looping. Default 1800 ms. |
data-delay | Wait before starting. Default 0 ms. |
The shadow is a solid colour, so it reads as flat: pick one a shade darker than whatever the text sits on.
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Long Shadow Reveal: the letters pop up, then a long, flat shadow grows
// out of them at an angle.
// Usage: <h1 data-long-shadow-reveal>Long shadow</h1>
// Optional: data-color (the shadow, default "#4c1d95"; use a solid colour a
// little darker than the background), data-length (in em, default 0.6),
// data-angle (degrees, 45 = down-right, default 45), data-duration (ms for
// the shadow to grow, default 1000), data-stagger (ms between letters,
// default 40), data-loop (shadow shrinks, letters drop, and again),
// data-hold (ms to stay, when looping), data-delay (ms before starting).
var DEFAULTS = { color: '#4c1d95', length: 0.6, angle: 45, duration: 1000, stagger: 40, hold: 1800, delay: 0 };
var css =
'.lshr-word{display:inline-block;white-space:nowrap}' +
'.lshr-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 = 'lshr-word';
w.setAttribute('aria-hidden', 'true');
Array.from(word).forEach(function (c) {
var s = document.createElement('span');
s.className = 'lshr-ch';
s.textContent = c;
w.appendChild(s);
chars.push(s);
});
el.appendChild(w);
});
return chars;
}
// A stack of 1px-apart shadows, `k` (0 to 1) of the full length.
function shadow(el, k) {
var d = el.dataset;
var fs = parseFloat(getComputedStyle(el).fontSize);
var len = Math.round(fs * (+d.length || DEFAULTS.length) * k);
var a = (d.angle !== undefined ? +d.angle : DEFAULTS.angle) * Math.PI / 180;
var dx = Math.cos(a), dy = Math.sin(a), color = d.color || DEFAULTS.color;
var parts = [];
for (var i = 1; i <= len; i++) {
parts.push((dx * i).toFixed(1) + 'px ' + (dy * i).toFixed(1) + 'px 0 ' + color);
}
el.style.textShadow = parts.length ? parts.join(',') : 'none';
}
function play(el) {
var d = el.dataset, s = el._lshr;
var loop = d.loop !== undefined && d.loop !== 'false';
var duration = +d.duration || DEFAULTS.duration;
var stagger = d.stagger !== undefined ? +d.stagger : DEFAULTS.stagger;
var g = { k: 0 };
var tl = anime.createTimeline({
onComplete: function () { if (loop) setTimeout(function () { play(el); }, 300); }
});
tl.add(s.chars, {
opacity: [0, 1],
y: ['0.5em', '0em'],
scale: [0.3, 1],
duration: 650,
ease: 'outBack(2)',
delay: anime.stagger(stagger)
});
tl.add(g, {
k: [0, 1],
duration: duration,
ease: 'outQuart',
onUpdate: function () { shadow(el, g.k); }
}, '-=250');
if (loop) {
tl.add(g, {
k: [1, 0],
duration: duration * 0.5,
ease: 'inQuad',
onUpdate: function () { shadow(el, g.k); }
}, '+=' + (+d.hold || DEFAULTS.hold));
tl.add(s.chars, {
opacity: [1, 0],
y: ['0em', '0.3em'],
scale: [1, 0.6],
duration: 350,
ease: 'inQuad',
delay: anime.stagger(stagger * 0.5)
});
}
}
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-long-shadow-reveal]').forEach(function (el) {
if (el._lshr) return;
el._lshr = { chars: split(el) };
if (reduced()) { shadow(el, 1); return; }
el._lshr.chars.forEach(function (c) { c.style.opacity = 0; });
io.observe(el);
});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><h1 data-long-shadow-reveal data-color="#4c1d95">Long shadow</h1>
<!-- or, a longer shadow falling down-left: -->
<h1 data-long-shadow-reveal data-length="1" data-angle="135">Long shadow</h1>