Typography · 31 of 33

Weight Wave

A wave of font-weight travels through the letters — rare variable-font trick

HTML · CSS · JavaScript · No dependencies · MIT

HTML

<!-- Requires a variable font, e.g. Roboto Flex from Google Fonts:
<link href="https://fonts.googleapis.com/css2?family=Roboto+Flex:opsz,slnt,wdth,wght@8..144,-10..0,25..151,100..1000&display=swap" rel="stylesheet"> -->

<p class="weight-wave-headline" id="weightWaveHeadline"></p>

CSS

.weight-wave-headline {
  font-family: 'Roboto Flex', sans-serif;
  display: flex;
  justify-content: center;
  font-size: 3.5rem;
}

.weight-wave-char {
  display: inline-block;
  font-variation-settings: 'wght' 100;
}

JavaScript

var headline = document.getElementById('weightWaveHeadline');
var text = 'Heavy Light';
var SPEED = 0.06;

headline.innerHTML = text.split('').map(function (c) {
  return '<span class="weight-wave-char">' + (c === ' ' ? '&nbsp;' : c) + '</span>';
}).join('');
var chars = headline.querySelectorAll('.weight-wave-char');

var phase = 0;
function tick() {
  phase += SPEED;
  chars.forEach(function (el, i) {
    // A wave of font-weight 100..900 travels through the word
    var w = 500 + Math.sin(phase - i * 0.6) * 400;
    el.style.fontVariationSettings = "'wght' " + Math.round(w);
  });
  requestAnimationFrame(tick);
}
requestAnimationFrame(tick);