Typography · 22 of 33

Gooey Text

Letters merge and separate like goo

HTML · CSS · JavaScript · No dependencies · MIT

HTML

<svg width="0" height="0">
  <filter id="gooey-filter" color-interpolation-filters="sRGB">
    <feGaussianBlur in="SourceGraphic" stdDeviation="1.5" result="blur" />
    <feColorMatrix in="blur" mode="matrix"
      values="1 0 0 0 0  0 1 0 0 0  0 0 1 0 0  0 0 0 20 -8.4" result="goo" />
    <feComposite in="SourceGraphic" in2="goo" operator="atop"/>
  </filter>
</svg>

<div class="gooey-wrapper">
  <h2 class="gooey-text"><span class="gooey-char">G</span><span class="gooey-char">o</span><span class="gooey-char">o</span><span class="gooey-char">e</span><span class="gooey-char">y</span></h2>
</div>

CSS

.gooey-wrapper {
  filter: url('#gooey-filter');
  padding: 1rem; /* room so gooey blobs don't clip */
  text-align: center;
}

.gooey-text {
  display: inline-block;
  font-size: 3.5rem;
  font-weight: 700; /* thick strokes merge better */
  color: #7c5cff;
}

.gooey-char {
  display: inline-block;
  position: relative; /* animate left, not transform, so the filter covers every letter */
  animation: gooey-drift 2.4s ease-in-out infinite alternate;
}

.gooey-char:nth-child(odd) { animation-direction: alternate-reverse; }
.gooey-char:nth-child(2) { animation-delay: 0.15s; }
.gooey-char:nth-child(3) { animation-delay: 0.3s; }
.gooey-char:nth-child(4) { animation-delay: 0.45s; }
.gooey-char:nth-child(5) { animation-delay: 0.6s; }

@keyframes gooey-drift {
  0% { left: -0.18em; }
  100% { left: 0.18em; }
}

JavaScript

var wrap = document.querySelector('.gooey-wrapper');
var text = document.querySelector('.gooey-text');
var blur = document.querySelector('#gooey-filter feGaussianBlur');
var matrix = document.querySelector('#gooey-filter feColorMatrix');

// A bare url(#id) resolves against the document base URL. A <base> tag
// (CodePen previews, some extensions) turns it into a cross-document
// reference that Chrome won't load, so reference the filter by the page's
// own absolute URL instead.
var base = location.href.split('#')[0];
var ref = /^https?:/.test(base) ? 'url("' + base + '#gooey-filter")' : 'url(#gooey-filter)';

// Blur is in pixels, so scale it with the font size: LEVEL is "goo per 50px
// of text". 1.5 is subtle, 4 is obvious, 8+ melts the word into one blob.
var LEVEL = 1.5;
var K = 20;
var THRESHOLD = Math.max(0.2, 0.45 - 0.02 * LEVEL);

function applyGoo() {
  var sd = LEVEL * parseFloat(getComputedStyle(text).fontSize) / 50;
  blur.setAttribute('stdDeviation', sd.toFixed(2));
  matrix.setAttribute('values',
    '1 0 0 0 0  0 1 0 0 0  0 0 1 0 0  0 0 0 ' + K + ' ' + (-(K * THRESHOLD)).toFixed(1));
  // Re-attach the filter so Chrome repaints with the new primitives.
  wrap.style.filter = 'none';
  void wrap.offsetWidth;
  wrap.style.filter = ref;
}

applyGoo();
window.addEventListener('load', applyGoo);
window.addEventListener('resize', applyGoo);