Hi! I'm HIGASHIYUKI® Art Director | Photo & Videographer | Web Engineer

配色

音楽

Lo-Fi #001by ST®AND.(音源のサイト。新しいタブで開きます)

ジャンル

ヒーロー

文字化けのように記号が入れ替わりながら単語が切り替わる

デモ

仕様

用途
ヒーロー
動作
DOM
種類
レシピ
動作確認
three 0.186.0・gsap 3.15.0
公開日

しくみと調整

表示されている単語が、いったん意味をなさない漢字やかな・記号にばらけ、左の文字から順番に次の単語へと定まっていきます。仕組みはシンプルで、確定した文字数をイージングに沿って増やしながら、まだ確定していない位置には毎フレーム乱数で選び直した文字を表示しているだけです。

1つの単語を1.1秒ほどしっかり読ませてから、0.7秒かけて崩しながら次の単語へ切り替えます。イージングには power2.inOut を使っているため、文字の確定はゆっくり始まり、中盤で一気に進んで、最後の数文字はまた落ち着いて決まります。

崩れに使う文字は、全角のかな・漢字・記号だけに絞りました。和文の全角文字はどれも同じ横幅なので、font-kerning で字詰めを切っておけば、どんな文字に入れ替わっても単語全体の幅が変わりません。語の長さがガタガタと揺れないぶん、文字だけが入れ替わってすっきりと見えます。崩れに使う文字の種類をさらに絞ると、変化に一定の規則性が見えて面白みが出ます。

スクリーンリーダーには、切り替わる単語の一覧をまとめた固定の文言を渡しています。毎フレーム書き換わる文字をそのまま読ませても読み上げが割り込むだけなので、崩れて見える演出用の文字は読み上げさせないことにしました。

コード

HTML
<div class="word-scramble">
  <p class="word-scramble__label" aria-hidden="true">ブランド</p>
  <p class="word-scramble__sr">ブランド、デザイン、動画制作、運用支援。</p>
  <button class="word-scramble__toggle" type="button" aria-pressed="false">一時停止</button>
</div>

CSS
.word-scramble {
  display: grid;
  justify-items: center;
  gap: 24px;
  padding: 96px 6vw;
  background: #f2f1ed;
  color: #262626;
  text-align: center;
}

/* 和文の全角の字はどれも同じ幅。字詰め(kern)だけ切ると、字が入れ替わっても語の幅が変わらない */
.word-scramble__label {
  margin: 0;
  font-size: clamp(32px, 7vw, 64px);
  font-weight: 800;
  line-height: 1;
  letter-spacing: 0.08em;
  font-kerning: none;
}

/* スクリーンリーダーにだけ届く固定の文言。画面には表示しない */
.word-scramble__sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.word-scramble__toggle {
  height: 40px;
  padding: 0 20px;
  border: 0;
  border-radius: 999px;
  box-shadow: inset 0 0 0 1px #262626;
  background: transparent;
  color: #262626;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.04em;
  cursor: pointer;
}

.word-scramble__toggle[aria-pressed="true"] { background: #262626; color: #f2f1ed; }
.word-scramble__toggle:focus-visible { outline: 2px solid #262626; outline-offset: 4px; }

JavaScript
import { gsap } from "gsap";

const WORDS = ["ブランド", "デザイン", "動画制作", "運用支援"];
// スクランブルに使う字は全角だけにする。半角を混ぜると、字が入れ替わるたびに語の幅が揺れる
const GLYPHS = "アイウカキクサシスタチツナニヌハヒフマミムヤユヨラリルワヲン日月火水木金土山川空色形線点面#%&*+=?";
const HOLD = 1.1; // 単語を読ませる時間
const SCRAMBLE = 0.7; // スクランブルして次の単語になるまでの時間
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;

document.querySelectorAll(".word-scramble").forEach((root) => {
  const label = root.querySelector(".word-scramble__label");
  const toggle = root.querySelector(".word-scramble__toggle");
  const state = { p: 0 };

  const tl = gsap.timeline({ repeat: -1 });
  WORDS.forEach((word, i) => {
    const next = WORDS[(i + 1) % WORDS.length];
    tl.call(() => (label.textContent = word))
      .to({}, { duration: HOLD })
      .to(state, {
        p: 1,
        duration: SCRAMBLE,
        ease: "power2.inOut",
        onUpdate: () => {
          // 確定した字数はイージングで進める。未確定の位置は毎フレーム字を選び直す
          const settled = Math.floor(state.p * next.length);
          let text = "";
          for (let c = 0; c < next.length; c++) text += c < settled ? next[c] : GLYPHS[(Math.random() * GLYPHS.length) | 0];
          label.textContent = text;
        },
        onComplete: () => (state.p = 0),
      });
  });

  // 動きを減らす設定では止めず、スクランブルをほぼ一瞬にする(読ませる間だけは残す)
  if (reduceMotion) tl.timeScale(20);

  toggle.addEventListener("click", () => {
    const paused = !tl.paused();
    tl.paused(paused);
    toggle.setAttribute("aria-pressed", String(paused));
  });
});

ヒーローをすべて見る