Curtain Reveal
A solid block of colour sweeps across the text and pulls away again, leaving the text behind it, like a title card in a film trailer. Sweep left, right, up or down.
Curtain up
How to use it
- Paste the snippet just before
</head>. - Add
data-curtain-revealto any heading or text element.
Options
Add any of these attributes to the same element.
data-color | The block’s colour. Default #a855f7. |
data-direction | Which way the block travels: right (default), left, up or down. |
data-loop | Reveal, hold, sweep back over to take the text away, and reveal again. |
data-hold | How long the text stays before the next sweep, when looping. Default 1800 ms. |
data-duration | Time for each sweep, in and out. Default 1200 ms. |
data-delay | Wait before starting. Default 0 ms. |
The block hugs the text, not the full width of the element, so it works on a centred heading too.
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Curtain Reveal: a coloured block sweeps across the text, and as it pulls
// away the text is left behind, as if it was painted on by the block.
// Usage: <h1 data-curtain-reveal>Now showing</h1>
// Optional: data-color (the block's colour; default #a855f7), data-direction
// (which way the block travels: "right", the default, "left", "up" or
// "down"), data-loop (the block sweeps back over to take the text away, then
// reveals it again), data-hold (ms to stay before sweeping away),
// data-duration (ms for each sweep), data-delay (ms before starting).
var DEFAULTS = { color: '#a855f7', direction: 'right', duration: 1200, delay: 0, hold: 1800 };
// The block grows from `from`, then shrinks towards `to`, along `axis`.
var SIDES = {
right: { axis: 'scaleX', from: '0% 50%', to: '100% 50%' },
left: { axis: 'scaleX', from: '100% 50%', to: '0% 50%' },
down: { axis: 'scaleY', from: '50% 0%', to: '50% 100%' },
up: { axis: 'scaleY', from: '50% 100%', to: '50% 0%' }
};
var css =
'.crv-box{position:relative;display:inline-block}' +
'.crv-block{position:absolute;inset:-0.04em -0.1em;border-radius:0.06em;pointer-events:none;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;
}
// Wraps the contents in an inline box, so the block hugs the text rather
// than the full width of a heading, and adds the block on top.
function build(el) {
var box = document.createElement('span');
box.className = 'crv-box';
var text = document.createElement('span');
while (el.firstChild) text.appendChild(el.firstChild);
var block = document.createElement('span');
block.className = 'crv-block';
block.setAttribute('aria-hidden', 'true');
block.style.background = el.dataset.color || DEFAULTS.color;
box.appendChild(text);
box.appendChild(block);
el.appendChild(box);
return { box: box, text: text, block: block };
}
function play(el, parts) {
var d = el.dataset;
var loop = d.loop !== undefined && d.loop !== 'false';
var duration = +d.duration || DEFAULTS.duration;
var side = SIDES[d.direction] || SIDES[DEFAULTS.direction];
var cover = {}; cover[side.axis] = [0, 1];
var uncover = {}; uncover[side.axis] = [1, 0];
var half = duration / 2;
function sweep(tl, showText) {
tl.set(parts.block, { transformOrigin: side.from })
.add(parts.block, Object.assign({ duration: half, ease: 'inOutQuart' }, cover))
// Swap what's behind the block while it covers the text completely.
.set(parts.text, { opacity: showText ? 1 : 0 })
.set(parts.block, { transformOrigin: side.to })
.add(parts.block, Object.assign({ duration: half, ease: 'inOutQuart' }, uncover), '+=60');
}
var tl = anime.createTimeline({ loop: loop, loopDelay: 400, delay: +d.delay || DEFAULTS.delay });
sweep(tl, true);
if (loop) {
tl.add({ duration: +d.hold || DEFAULTS.hold });
sweep(tl, false);
}
}
function init() {
var io = new IntersectionObserver(function (entries) {
entries.forEach(function (e) {
if (!e.isIntersecting) return;
io.unobserve(e.target);
play(e.target, e.target._crv);
});
}, { threshold: 0.4 });
document.querySelectorAll('[data-curtain-reveal]').forEach(function (el) {
if (el._crv) return;
var parts = (el._crv = build(el));
var axis = (SIDES[el.dataset.direction] || SIDES[DEFAULTS.direction]).axis;
parts.block.style.transform = axis + '(0)';
if (reduced()) return;
parts.text.style.opacity = 0;
io.observe(el);
});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><h1 data-curtain-reveal>Curtain up</h1>
<!-- a pink block sweeping down, looping: -->
<h1 data-curtain-reveal data-color="#ec4899" data-direction="down" data-loop>Curtain up</h1>