Character Blur-in
Letters come into focus one after another, sharpening from a soft blur and settling to size as they fade in, like a lens pulling focus across the line.
Into focus
How to use it
- Paste the snippet just before
</head>. - Add
data-character-blur-into any heading or text element.
Options
Add any of these attributes to the same element.
data-loop | Come into focus, hold, blur away and come back. |
data-hold | How long the text stays before blurring away, when looping. Default 1800 ms. |
data-blur | How blurry the letters start, in px. Default 14. |
data-duration | How long each letter takes to sharpen. Default 1200 ms. |
data-stagger | Gap between letters. Default 45 ms. |
data-delay | Wait before the first letter. Default 0 ms. |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Character Blur-in: letters come into focus one after another, sharpening
// from a soft blur and settling to size as they fade in, like a lens pulling
// focus across the line.
// Usage: <h1 data-character-blur-in>Into focus</h1>
// Optional: data-loop (letters blur away and come back into focus),
// data-hold (ms to stay before blurring away), data-blur (px, default 14),
// data-duration (ms per letter), data-stagger (ms between letters),
// data-delay (ms before starting).
var DEFAULTS = { blur: 14, duration: 1200, stagger: 45, delay: 0, hold: 1800 };
var css =
'.cbi-word{display:inline-block;white-space:nowrap}' +
'.cbi-char{display:inline-block;will-change:transform,filter,opacity}';
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 = 'cbi-word';
w.setAttribute('aria-hidden', 'true');
Array.from(word).forEach(function (c) {
var s = document.createElement('span');
s.className = 'cbi-char';
s.textContent = c;
w.appendChild(s);
chars.push(s);
});
el.appendChild(w);
});
return chars;
}
function play(el, chars) {
var d = el.dataset;
var loop = d.loop !== undefined && d.loop !== 'false';
var duration = +d.duration || DEFAULTS.duration;
var stagger = +d.stagger || DEFAULTS.stagger;
var blur = 'blur(' + (+d.blur || DEFAULTS.blur) + 'px)';
var tl = anime.createTimeline({ loop: loop, loopDelay: 300, delay: +d.delay || DEFAULTS.delay });
tl.add(chars, {
opacity: { from: 0, to: 1, duration: duration * 0.6 },
filter: [blur, 'blur(0px)'],
scale: [1.3, 1],
duration: duration,
ease: 'out(3)',
delay: anime.stagger(stagger)
});
if (loop) {
// Drift out of focus in the same direction, letter by letter.
tl.add(chars, {
opacity: 0,
filter: blur,
scale: 0.9,
duration: duration * 0.6,
ease: 'in(2)',
delay: anime.stagger(stagger * 0.6)
}, '+=' + (+d.hold || DEFAULTS.hold));
}
}
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._cbiChars);
});
}, { threshold: 0.2 });
document.querySelectorAll('[data-character-blur-in]').forEach(function (el) {
if (el._cbiChars) return;
var chars = split(el);
el._cbiChars = chars;
if (reduced()) return;
anime.utils.set(chars, { opacity: 0 });
io.observe(el);
});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><h1 data-character-blur-in>Into focus</h1>
<!-- or, looping: -->
<h1 data-character-blur-in data-loop>Into focus</h1>