文字の呼吸

文字の呼吸

by prodouga

HTML

<div class="container">
  <p aria-label="Prodouga">
    <span aria-hidden="true">P</span<span aria-hidden="true">r</span><span aria-hidden="true">o</span><span aria-hidden="true">d</span><span aria-hidden="true">o</span><span aria-hidden="true">u</span><span aria-hidden="true">g</span><span aria-hidden="true">a</span><span aria-hidden="true">.</span><span aria-hidden="true">c</span></span><span aria-hidden="true">o</span></span><span aria-hidden="true">m</span>
</p>
</div>

CSS

@font-face {
  font-family: "Geist";
  src: url("https://pham.codes/d/GeistMonoVF.woff2") format("woff2");
}

p {
  font-family: "Geist";
  font-size: 128px;
  margin: 0;

  background-color: white;
  background-image: linear-gradient(0deg, white 60%, #333333);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  -moz-background-clip: text;
  -moz-text-fill-color: transparent;

  --wght-min: 32;
  --wght-max: 240;
}

p > span {
  animation: breath 1.5s alternate ease-in-out;
  animation-iteration-count: infinite;
  animation-delay: 1s;
  animation-fill-mode: both;
}

@keyframes breath {
  0% {
    font-variation-settings: "wght" var(--wght-min);
  }
  100% {
    font-variation-settings: "wght" var(--wght-max);
  }
}

/* Setup */

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

html,
body {
  height: 100%;
  background: #C24;
  color: white;
}

JavaScript

const spans = document.querySelectorAll('p span');

const numLetters = spans.length;

spans.forEach(function(span, i) {
    const mappedIndex = i - (numLetters / 2)
    span.style.animationDelay = (mappedIndex * 0.25) + 's';
});