BLK-62Blocks

CSSだけで動くモザイク背景|非同期タイルのヒーロー

画像不要・動きはCSSだけ(操作部のみJS)。色と図形が別周期で切り替わる、CTA付き背景の実装例です。12x6のグリッドに大・中・小のタイルを敷き、タイルごとに4.8〜10.3秒の周期で塗り・三角形・線枠を入れ替えます。見出しとCTAの白い一枚もタイルと同じgridに置くので、375pxでも上下左右にタイルが残ります。読み込み直後は設計した1枚目の構図から始まり、1〜3秒で各タイルが順に切り替わり始めます。動きは負のanimation-delayとopacity・transformだけで、図形は静的なclip-pathです。プレビュー下で再生・一時停止・静止表示と3つの配色を試せます(操作部だけReactのstateを使い、外してもCSSだけで動きます)。動きを減らす設定では1枚目の構図で止まります。

プレビュー

制作の考え方

見た瞬間の印象を、
設計でつくる

色の面積・余白・動きの強さまで決めてから組み立てます。派手さではなく、伝わる速さを基準に選びます。

動き
配色

再生中: タイルごとに4.8〜10.3秒の別周期で切り替わります

案件へ持ち込むときに変える所と、根拠にした仕様
配色
PALETTES の tone1〜5 と cta。cta は白文字との対比 4.5:1 以上の色にする
周期
cycleOf()。全タイルを同じ秒数にすると一斉に切り替わる格子に見える
位相
phaseOf()。6〜28%の範囲なら、読み込み直後は全タイルが1枚目の図形を見せる
タイル配置
TILESの at(列・行・幅・高さ)と faces(色と図形を3枚、または "line")。2〜6列目の2〜5行目は一枚の下になる
操作部
<MosaicRotateBand controls={false} /> で外せる。ページの背景として5秒を超えて動かし続けるなら、一時停止の手段はページのどこかに残す

根拠にした仕様(2026-11-16 確認)

  • W3C「Understanding SC 2.2.2 Pause, Stop, Hide」: レベルA。自動で始まり5秒を超えて続く動きは、止める・隠す手段を用意する(G4・G186)。この見本の「一時停止」はその手段(2026-10-04 更新版)
  • MDN「animation-delay」: 負の値はアニメーションを即座に、周期の途中から始める。Baseline Widely available(2015年9月から)
  • MDN「animation-play-state」: paused から戻すと、止めた位置から続く。Baseline Widely available(2015年9月から)
  • MDN「prefers-reduced-motion」: 端末の「動きを減らす」設定を読む。Baseline Widely available(2020年1月から)
X でシェア

説明の全文を読む

画像不要・動きはCSSだけ(操作部のみJS)。色と図形が別周期で切り替わる、CTA付き背景の実装例です。12x6のグリッドに大・中・小のタイルを敷き、タイルごとに4.8〜10.3秒の周期で塗り・三角形・線枠を入れ替えます。見出しとCTAの白い一枚もタイルと同じgridに置くので、375pxでも上下左右にタイルが残ります。読み込み直後は設計した1枚目の構図から始まり、1〜3秒で各タイルが順に切り替わり始めます。動きは負のanimation-delayとopacity・transformだけで、図形は静的なclip-pathです。プレビュー下で再生・一時停止・静止表示と3つの配色を試せます(操作部だけReactのstateを使い、外してもCSSだけで動きます)。動きを減らす設定では1枚目の構図で止まります。

追加日:
2026-09-16
更新日:
2026-11-17
依存:
なし
tags
#css-animation #animated-background #hero #cta #mosaic #grid #clip-path #keyframes #no-image #copy-paste
"use client";

import { useEffect, useState, type CSSProperties } from "react";

/**
 * CSSだけで動くモザイク背景|非同期タイルのヒーロー(BLK-62)
 *
 * 案件で変える所(このファイルの中の名前):
 * - 配色      … PALETTES(tone1〜5 がタイルの色、cta が主ボタンと見出し上のラベル)
 * - 周期      … cycleOf()(タイルごとの1周の秒数。4.8〜10.3秒に散らしている)
 * - 位相      … phaseOf()(読み込んだ瞬間に1枚目の図形がどこまで進んでいるか)
 * - タイル配置 … TILES(12列x6行のどこに何を敷くか・3枚の図形の順番)
 * - 操作部    … controls={false} で外せる。外しても動きは CSS だけで続く
 *
 * 技法メモ:
 * - **負の animation-delay で位相をずらすと、JSも交差監視APIも要らない**。
 *   1タイルにフェイスを3枚重ね、同じ @keyframes を負の遅延で配ると、各フェイスが自分の持ち時間だけ
 *   現れる循環になる。負の遅延はアニメーションを「途中から」始めるので、SSRの初期HTMLがそのまま動いている状態になる。
 * - **周期をタイルごとに変えないと必ず揃う**。位相だけずらしても duration が同じなら全タイルが同じ間隔で
 *   切り替わり「一斉に動く格子」に見える。
 * - **読み込み直後は全タイルが1枚目のフェイスを見せる**位相にしてある。1枚目に塗り・三角形・線枠を
 *   混ぜて配置しておくと、最初の画面が設計した構図になり、1〜3秒のうちに各タイルが順に切り替わり始める。
 *   同じ1枚目が「静止表示」と prefers-reduced-motion のときの絵にもなる。
 * - **図形の切り替えは clip-path を静的に持たせる**。clip-path 自体は動かさず、opacity と transform だけで
 *   フェイスを入れ替える。
 * - **中央の一枚もタイルと同じ grid に置く**。カードが覆う列と行だけを空けておけば、狭い幅でも
 *   上下左右にタイルが残る(幅ごとに覆う列を変えるだけで構図が保てる)。
 */

type Tone = 1 | 2 | 3 | 4 | 5;
type Shape = "full" | "tl" | "tr" | "br" | "bl" | "top" | "bottom" | "inset" | "notch";
/** 1枚のフェイス: [色, 図形] か、塗らない線枠 "line" */
type Face = [Tone, Shape] | "line";
type Tile = { at: [number, number, number, number]; faces: [Face, Face, Face] };

/** 図形。すべて直線だけで切る(円・角丸は使わない) */
const SHAPES: Record<Shape, string> = {
  full: "none",
  tl: "polygon(0 0, 100% 0, 0 100%)",
  tr: "polygon(0 0, 100% 0, 100% 100%)",
  br: "polygon(100% 0, 100% 100%, 0 100%)",
  bl: "polygon(0 0, 100% 100%, 0 100%)",
  top: "polygon(0 0, 100% 0, 100% 52%, 0 52%)",
  bottom: "polygon(0 48%, 100% 48%, 100% 100%, 0 100%)",
  inset: "polygon(20% 20%, 80% 20%, 80% 80%, 20% 80%)",
  notch: "polygon(0 0, 100% 0, 100% 60%, 60% 100%, 0 100%)",
};

/** 配色。tone1 が濃い差し色、tone4・5 が地の灰と墨。cta は白文字で 4.5:1 以上になる色にする */
const PALETTES = {
  amber: { label: "アンバー", tone1: "#d97706", tone2: "#fbbf24", tone3: "#fde68a", tone4: "#e5e7eb", tone5: "#1f2937", cta: "#b45309" },
  ai: { label: "藍", tone1: "#1d4ed8", tone2: "#60a5fa", tone3: "#bfdbfe", tone4: "#e2e8f0", tone5: "#0f172a", cta: "#1d4ed8" },
  wakakusa: { label: "若草", tone1: "#15803d", tone2: "#4ade80", tone3: "#bbf7d0", tone4: "#e7e5e4", tone5: "#1c1917", cta: "#15803d" },
} as const;
type PaletteKey = keyof typeof PALETTES;

/**
 * 12列 x 6行のどこに敷くか(at = [列, 行, 列幅, 行幅]・1始まり)と、順に見せる3枚のフェイス。
 * 2〜6列目の2〜5行目はどの幅でも中央の一枚の下になるので敷かない。
 * 空けたセル(1行目7列・4行目11列・6行目6列)はそのまま白い余白として図版になる。
 */
const TILES: Tile[] = [
  { at: [1, 1, 2, 1], faces: [[1, "full"], [3, "br"], [5, "top"]] },
  { at: [3, 1, 1, 1], faces: ["line", [2, "full"], [4, "tl"]] },
  { at: [4, 1, 3, 1], faces: [[3, "tl"], [5, "full"], [1, "bottom"]] },
  { at: [8, 1, 2, 2], faces: [[2, "full"], [1, "bl"], "line"] },
  { at: [10, 1, 1, 1], faces: [[5, "tr"], [3, "full"], [2, "inset"]] },
  { at: [11, 1, 2, 2], faces: [[1, "br"], [4, "full"], [3, "notch"]] },
  { at: [1, 2, 1, 2], faces: [[4, "full"], [1, "tl"], [2, "bottom"]] },
  { at: [7, 2, 1, 1], faces: [[1, "inset"], [2, "tr"], [5, "full"]] },
  { at: [10, 2, 1, 3], faces: ["line", [1, "full"], [3, "bl"]] },
  { at: [7, 3, 2, 2], faces: [[1, "tr"], [3, "full"], [5, "br"]] },
  { at: [9, 3, 1, 1], faces: [[3, "full"], [5, "inset"], [1, "tl"]] },
  { at: [11, 3, 2, 1], faces: [[5, "full"], [2, "top"], [1, "full"]] },
  { at: [1, 4, 1, 1], faces: [[2, "bl"], "line", [1, "full"]] },
  { at: [9, 4, 1, 2], faces: [[2, "top"], [1, "full"], [4, "tr"]] },
  { at: [12, 4, 1, 2], faces: [[3, "full"], [1, "notch"], "line"] },
  { at: [7, 5, 2, 1], faces: [[4, "full"], [2, "br"], [1, "top"]] },
  { at: [10, 5, 2, 2], faces: [[1, "tl"], [2, "full"], [5, "bottom"]] },
  { at: [1, 5, 2, 2], faces: [[5, "notch"], [3, "full"], [2, "tr"]] },
  { at: [3, 6, 2, 1], faces: [[2, "full"], [1, "tr"], [4, "full"]] },
  { at: [5, 6, 1, 1], faces: [[1, "full"], "line", [3, "br"]] },
  { at: [7, 6, 3, 1], faces: [[3, "bottom"], [1, "full"], [2, "tl"]] },
  { at: [12, 6, 1, 1], faces: [[1, "bl"], [4, "full"], [2, "full"]] },
];

/** 1周の秒数。タイルごとに 4.8s〜10.3s へ散らす(揃わせないための本体) */
const cycleOf = (i: number) => 4.8 + (i % 6) * 1.1;
/**
 * 読み込んだ瞬間の進み具合(0〜1)。keyframes で1枚目が見えている 6〜28% の中に散らすので、
 * 最初は全タイルが1枚目を見せ、残り時間の差でタイルごとに 0.1〜2.5 秒後から順に切り替わる。
 */
const phaseOf = (i: number) => 0.06 + ((i * 0.37) % 1) * 0.22;

/** フェイス f の負の遅延。0 → 1 → 2 の順に、周期の3分の1ずつずらして現れる */
function delayOf(i: number, f: number) {
  const cycle = cycleOf(i);
  return -cycle * ((phaseOf(i) + (f === 0 ? 0 : (3 - f) / 3)) % 1);
}

const MODES = [
  { key: "play", label: "再生" },
  { key: "pause", label: "一時停止" },
  { key: "still", label: "静止表示" },
] as const;
type Mode = (typeof MODES)[number]["key"];

const STATUS: Record<Mode, string> = {
  play: "再生中: タイルごとに4.8〜10.3秒の別周期で切り替わります",
  pause: "一時停止中: 再生を押すと止めた位置から続きます",
  still: "静止表示: 動きを減らす設定のときと同じ1枚目の構図です",
};

const SOURCES = [
  {
    label: "W3C「Understanding SC 2.2.2 Pause, Stop, Hide」",
    href: "https://www.w3.org/WAI/WCAG22/Understanding/pause-stop-hide.html",
    note: "レベルA。自動で始まり5秒を超えて続く動きは、止める・隠す手段を用意する(G4・G186)。この見本の「一時停止」はその手段(2026-10-04 更新版)",
  },
  {
    label: "MDN「animation-delay」",
    href: "https://developer.mozilla.org/en-US/docs/Web/CSS/animation-delay",
    note: "負の値はアニメーションを即座に、周期の途中から始める。Baseline Widely available(2015年9月から)",
  },
  {
    label: "MDN「animation-play-state」",
    href: "https://developer.mozilla.org/en-US/docs/Web/CSS/animation-play-state",
    note: "paused から戻すと、止めた位置から続く。Baseline Widely available(2015年9月から)",
  },
  {
    label: "MDN「prefers-reduced-motion」",
    href: "https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion",
    note: "端末の「動きを減らす」設定を読む。Baseline Widely available(2020年1月から)",
  },
] as const;

const codeClass = "font-mono text-[11px] whitespace-nowrap text-gray-900";

function FaceLayer({ face, cycle, delay }: { face: Face; cycle: number; delay: number }) {
  const timing = { animationDuration: `${cycle.toFixed(2)}s`, animationDelay: `${delay.toFixed(2)}s` };
  if (face === "line") {
    return <span className="mrb-face mrb-line absolute inset-0 block" style={timing} />;
  }
  const [tone, shape] = face;
  return (
    <span
      className="mrb-face absolute inset-0 block"
      style={{ ...timing, background: `var(--mrb-tone${tone})`, clipPath: SHAPES[shape] } as CSSProperties}
    />
  );
}

export default function MosaicRotateBand({
  controls = true,
  headingTag: Heading = "h2",
}: {
  controls?: boolean;
  /** ページの最初の見出しとして置くなら "h1" */
  headingTag?: "h1" | "h2" | "p";
}) {
  const [mode, setMode] = useState<Mode>("play");
  const [paletteKey, setPaletteKey] = useState<PaletteKey>("amber");
  const [reducedBySystem, setReducedBySystem] = useState(false);
  const palette = PALETTES[paletteKey];

  // 端末の「動きを減らす」設定が有効なら CSS 側が常に静止させるので、表示の文言だけ合わせる
  useEffect(() => {
    const query = window.matchMedia("(prefers-reduced-motion: reduce)");
    const sync = () => setReducedBySystem(query.matches);
    sync();
    query.addEventListener("change", sync);
    return () => query.removeEventListener("change", sync);
  }, []);

  const vars = {
    "--mrb-tone1": palette.tone1,
    "--mrb-tone2": palette.tone2,
    "--mrb-tone3": palette.tone3,
    "--mrb-tone4": palette.tone4,
    "--mrb-tone5": palette.tone5,
    "--mrb-cta": palette.cta,
  } as CSSProperties;

  return (
    <div className="mrb @container w-full max-w-5xl" style={vars}>
      <div
        data-mode={mode}
        className="relative grid grid-cols-12 grid-rows-[52px_repeat(4,auto)_60px] gap-1.5 overflow-hidden border border-gray-200 bg-white p-1.5 @xl:min-h-[500px] @xl:grid-rows-[repeat(6,minmax(52px,1fr))] @xl:gap-2 @xl:p-2"
      >
        {/* 動く地。情報を持たないので支援技術からは隠す */}
        {TILES.map(({ at: [col, row, w, h], faces }, i) => (
          <span
            key={i}
            aria-hidden="true"
            className="pointer-events-none relative block"
            style={{ gridColumn: `${col} / span ${w}`, gridRow: `${row} / span ${h}` }}
          >
            {faces.map((face, f) => (
              <FaceLayer key={f} face={face} cycle={cycleOf(i)} delay={delayOf(i, f)} />
            ))}
          </span>
        ))}

        {/*
          中央の一枚。タイルと同じ grid の 2〜5行目に置く。狭い幅は 2〜11列(左右1列と上下1行にタイルが残る)、
          広い幅は 2〜7列・2〜6列に寄せて右側のタイルを広く見せる。白い box-shadow で1周の余白を持たせ、
          タイルの端が枠線に接して線が太って見えるのを防ぐ。
        */}
        <div className="relative z-10 col-start-2 col-end-12 row-start-2 row-end-6 self-center border border-gray-200 bg-white px-5 py-6 shadow-[0_0_0_8px_#fff] @xl:col-end-8 @xl:px-8 @xl:py-9 @3xl:col-end-7 @3xl:px-9">
          <p className="flex items-center gap-3 text-xs font-bold text-(--mrb-cta)">
            <span aria-hidden="true" className="h-px w-8 bg-(--mrb-cta)" />
            制作の考え方
          </p>

          <Heading className="mt-3 text-[clamp(1.3rem,3.6cqi,1.95rem)] leading-[1.5] font-bold tracking-tight text-gray-900 @xl:mt-4">
            見た瞬間の印象を、
            <br />
            設計でつくる
          </Heading>

          <p className="mt-3 text-[14px] leading-[1.85] text-gray-700 [word-break:auto-phrase] @xl:mt-4 @xl:text-[15px] @xl:leading-[1.9]">
            色の面積・余白・動きの強さまで決めてから組み立てます。派手さではなく、伝わる速さを基準に選びます。
          </p>

          <div className="mt-5 flex flex-wrap items-center gap-x-6 gap-y-3 border-t border-gray-200 pt-5 @xl:mt-7 @xl:pt-6">
            <a
              href="#"
              className="bg-(--mrb-cta) px-5 py-3 text-sm font-semibold text-white transition-[filter] hover:brightness-90 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-(--mrb-cta)"
            >
              制作の進め方を見る
            </a>
            <a
              href="#"
              className="text-sm font-semibold text-gray-900 underline decoration-gray-300 decoration-1 underline-offset-4 transition-colors hover:decoration-(--mrb-cta) focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-(--mrb-cta)"
            >
              料金の考え方
            </a>
          </div>
        </div>
      </div>

      {controls ? (
        <div className="mt-3 border border-gray-200 bg-white px-3 py-3 text-[13px] text-gray-700 @xl:px-4">
          <div className="flex flex-wrap items-center gap-x-6 gap-y-3">
            <div role="group" aria-label="動き" className="flex items-center gap-2">
              <span className="text-xs font-semibold text-gray-500">動き</span>
              <div className="flex">
                {MODES.map(({ key, label }) => (
                  <button
                    key={key}
                    type="button"
                    aria-pressed={mode === key}
                    onClick={() => setMode(key)}
                    className="-ml-px min-h-9 border border-gray-300 px-3 font-semibold first:ml-0 hover:bg-gray-50 focus-visible:relative focus-visible:z-10 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-gray-900 aria-pressed:border-gray-900 aria-pressed:bg-gray-900 aria-pressed:text-white"
                  >
                    {label}
                  </button>
                ))}
              </div>
            </div>
            <div role="group" aria-label="配色" className="flex items-center gap-2">
              <span className="text-xs font-semibold text-gray-500">配色</span>
              <div className="flex">
                {(Object.keys(PALETTES) as PaletteKey[]).map((key) => {
                  const p = PALETTES[key];
                  return (
                    <button
                      key={key}
                      type="button"
                      aria-pressed={paletteKey === key}
                      onClick={() => setPaletteKey(key)}
                      className="-ml-px flex min-h-9 items-center gap-1.5 border border-gray-300 px-2.5 font-semibold first:ml-0 hover:bg-gray-50 focus-visible:relative focus-visible:z-10 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-gray-900 aria-pressed:border-gray-900 aria-pressed:bg-gray-900 aria-pressed:text-white"
                    >
                      <span aria-hidden="true" className="flex">
                        {[p.tone1, p.tone2, p.tone3].map((c) => (
                          <span key={c} className="block size-2.5" style={{ background: c }} />
                        ))}
                      </span>
                      {p.label}
                    </button>
                  );
                })}
              </div>
            </div>
          </div>
          <p role="status" className="mt-2 text-xs leading-relaxed text-gray-600">
            {reducedBySystem ? "端末の「動きを減らす」設定が有効なため、1枚目の構図で静止しています" : STATUS[mode]}
          </p>
        </div>
      ) : null}

      {controls ? (
        <details className="mt-3 border border-gray-200 bg-white px-3 py-2.5 text-[12px] leading-[1.8] text-gray-600 @xl:px-4">
          <summary className="cursor-pointer font-semibold text-gray-900 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-gray-900">
            案件へ持ち込むときに変える所と、根拠にした仕様
          </summary>
          <dl className="mt-3 grid gap-x-5 gap-y-2 @xl:grid-cols-[7rem_1fr]">
            <dt className="font-semibold text-gray-800">配色</dt>
            <dd>
              <code className={codeClass}>PALETTES</code> の tone1〜5 と cta。cta は白文字との対比 4.5:1 以上の色にする
            </dd>
            <dt className="font-semibold text-gray-800">周期</dt>
            <dd>
              <code className={codeClass}>cycleOf()</code>。全タイルを同じ秒数にすると一斉に切り替わる格子に見える
            </dd>
            <dt className="font-semibold text-gray-800">位相</dt>
            <dd>
              <code className={codeClass}>phaseOf()</code>。6〜28%の範囲なら、読み込み直後は全タイルが1枚目の図形を見せる
            </dd>
            <dt className="font-semibold text-gray-800">タイル配置</dt>
            <dd>
              <code className={codeClass}>TILES</code> の at(列・行・幅・高さ)と faces(色と図形を3枚、または &quot;line&quot;)。2〜6列目の2〜5行目は一枚の下になる
            </dd>
            <dt className="font-semibold text-gray-800">操作部</dt>
            <dd>
              <code className={codeClass}>{"<MosaicRotateBand controls={false} />"}</code> で外せる。ページの背景として5秒を超えて動かし続けるなら、一時停止の手段はページのどこかに残す
            </dd>
          </dl>
          <p className="mt-3 font-semibold text-gray-800">根拠にした仕様(2026-11-16 確認)</p>
          <ul className="mt-1 space-y-1">
            {SOURCES.map((source) => (
              <li key={source.href}>
                <a
                  href={source.href}
                  target="_blank"
                  rel="noopener noreferrer"
                  className="underline decoration-gray-300 underline-offset-4 hover:decoration-gray-600 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-gray-900"
                >
                  {source.label}
                </a>
                : {source.note}
              </li>
            ))}
          </ul>
        </details>
      ) : null}

      {/* このブロック内で完結する keyframes と、操作部・reduced-motion の切り替え */}
      <style>{`
        .mrb-face {
          opacity: 0;
          animation-name: mrb-face-cycle;
          animation-timing-function: ease-in-out;
          animation-iteration-count: infinite;
        }
        .mrb-face:first-child { opacity: 1; }
        .mrb-line { box-shadow: inset 0 0 0 1.5px var(--mrb-tone1); }
        @keyframes mrb-face-cycle {
          0%          { opacity: 0; transform: scale(0.86); }
          6%, 30%     { opacity: 1; transform: scale(1); }
          39%, 100%   { opacity: 0; transform: scale(1.05); }
        }
        .mrb [data-mode="pause"] .mrb-face { animation-play-state: paused; }
        .mrb [data-mode="still"] .mrb-face { animation: none; }
        @media (prefers-reduced-motion: reduce) {
          .mrb .mrb-face { animation: none; }
        }
      `}</style>
    </div>
  );
}

shadcn CLI でプロジェクトに追加

npx shadcn@latest add https://designs.first-ch.com/r/mosaic-rotate-band.json