本文へスキップ
HELLO!
HIGASHIYUKI®.COM
0   /   100

カーソル

カーソルの丸が、乗った丸いボタン・カード・リンクの行と同じ形に変わって重なる

デモ

操作:カーソルを動かす。 「さわってみる」を押すと自動再生が止まり、マウスで動かせます。

仕様

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

しくみと調整

カーソルの淡い丸は、乗った部品の形に変わると、手前ではなく部品の後ろに回り込みます。文字やボタンの線は隠れず、その下に淡い色が置かれたようになります。丸いアイコンボタンなら同じ大きさの円に、カードならカードと同じ四角に、本文のリンクならその行を覆う横長の角丸に変わります。部品の輪郭どおりに重なるので、どこを押せるかと一緒に、押せる範囲の広さまで伝わります。

後ろに回すために、セクションに isolation の isolate を指定して重なり順の基準にし、丸を z-index -1で描いています。これで丸は、セクションの背景の上、中身の奥に入ります。そのぶん部品は背景を塗らず、線だけで組みました。丸の塗りも半透明にはせず、背景色にスレートカラーを混ぜた不透明な色にして、内側にスレートカラーの細い線を引いています。

形を合わせたい部品には data-cursor-shape を付けておくだけで、乗るたびに大きさと角の丸みを測ります。リンクでは文字の幅に左右6pxずつ足し、getClientRects でポインタのいる行を選ぶので、2行に折り返したリンクでも、乗っている行にだけ重なります。スクロールで部品が動いたときは、ポインタの下の部品を測り直します。リンクに重ねる形の左右の余白と角の丸みはスクリプトの値で、ふだんの丸の直径と色は CSS 変数にしてあります。

形は横の位置・縦の位置・幅・高さ・角の丸みの5つに分け、どれも quickTo で0.3秒かけて追わせています。値を別々に動かしているので、円から横長の角丸へ、そこからカードの四角へと、途切れずに形が移ります。ポインタの位置には暗い小さな点を遅れずに置いていて、丸が遅れても指している所は見失いません。キーボードでは :focus-visible が当たるときだけ同じ形に重ね、フォーカスの枠線は消さずに残します。

コード

HTML
<!-- カーソルに形をなぞってほしい部品に data-cursor-shape を付ける。リンク(インライン)は行を囲む四角、ほかは部品の大きさと角の丸みになる -->
<section class="shape-area" aria-labelledby="shape-area-title">
  <div class="shape-area__copy">
    <p class="shape-area__label">SERVICES</p>
    <h2 class="shape-area__title" id="shape-area-title">暮らしの相談を、<br>二つの窓口で</h2>
    <p class="shape-area__lead">はじめての方は、まず<a href="/flow/" data-cursor-shape>ご相談の流れ</a>をご覧ください。<br>料金と対応地域は、<a href="/plans/" data-cursor-shape>プランの一覧</a>にまとめています。</p>
    <div class="shape-area__controls">
      <button class="shape-round" type="button" aria-label="前へ" data-cursor-shape>
        <svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M19 12H5M11 6l-6 6 6 6" /></svg>
      </button>
      <button class="shape-round" type="button" aria-label="次へ" data-cursor-shape>
        <svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M5 12h14M13 6l6 6-6 6" /></svg>
      </button>
    </div>
  </div>
  <ul class="shape-area__cards">
    <li>
      <a class="shape-card" href="/services/home/" data-cursor-shape>
        <svg class="shape-card__art shape-card__art--slate" viewBox="0 0 224 216" aria-hidden="true" focusable="false">
          <path d="M52 176V112l60-48 60 48v64z" /><path class="cut" d="M96 176v-38h32v38z" /><circle cx="176" cy="52" r="14" />
        </svg>
        <h3 class="shape-card__title">住まいの相談</h3>
        <p class="shape-card__text">引っ越しやリフォームの段取りを、一緒に考えます。</p>
        <span class="shape-card__more">詳しく見る →</span>
      </a>
    </li>
    <li>
      <a class="shape-card" href="/services/money/" data-cursor-shape>
        <svg class="shape-card__art shape-card__art--sage" viewBox="0 0 224 216" aria-hidden="true" focusable="false">
          <ellipse cx="84" cy="164" rx="36" ry="10" /><rect x="48" y="130" width="72" height="34" /><ellipse class="rim" cx="84" cy="130" rx="36" ry="10" />
          <ellipse cx="148" cy="164" rx="30" ry="9" /><rect x="118" y="100" width="60" height="64" /><ellipse class="rim" cx="148" cy="100" rx="30" ry="9" />
        </svg>
        <h3 class="shape-card__title">お金の相談</h3>
        <p class="shape-card__text">家計の見直しから保険まで、専門の担当がお答えします。</p>
        <span class="shape-card__more">詳しく見る →</span>
      </a>
    </li>
  </ul>
  <!-- 独自カーソル。丸はセクションの中身の奥(セクションの背景の上)、点は手前に描く -->
  <span class="shape-cursor__shape" aria-hidden="true"></span>
  <span class="shape-cursor__dot" aria-hidden="true"></span>
</section>

CSS
/* isolation でセクションを重なりの基準にし、z-index: -1 の丸をセクションの背景の上・中身の奥に描く */
.shape-area {
  --bg: #f2f1ed;
  --ink: #262626;
  --mute: #6b6b6b;
  --line: rgba(38, 38, 38, 0.12);
  --shape-fill: #dddfde; /* 背景色にスレートカラーを 16% 混ぜた不透明な色 */
  --shape-edge: #688297;
  --shape-rest: 36px; /* ふだんの丸の直径 */
  isolation: isolate;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr));
  align-items: center;
  gap: 64px;
  padding: 96px clamp(16px, 7.5vw, 96px);
  background: var(--bg);
  color: var(--ink);
}

.shape-area__label { margin: 0; color: var(--mute); font-size: 13px; font-weight: 700; letter-spacing: 0.14em; }

.shape-area__title {
  margin: 24px 0 0;
  font-size: clamp(32px, 5vw, 44px);
  font-weight: 800;
  line-height: 1.1;
  letter-spacing: -0.02em;
}

.shape-area__lead { margin: 32px 0 0; font-size: 18px; line-height: 1.8; letter-spacing: 0.04em; word-break: auto-phrase; }
.shape-area__lead a { color: inherit; text-underline-offset: 5px; }
.shape-area__controls { display: flex; gap: 16px; margin-top: 48px; }

/* 背景を塗らない(丸が後ろに透けて見えるように) */
.shape-round {
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  padding: 0;
  border: 1px solid var(--ink);
  border-radius: 50%;
  background: none;
  color: var(--ink);
}

.shape-round svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.shape-area__cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 32px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.shape-card {
  display: block;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 4px;
  color: inherit;
  text-decoration: none;
}

.shape-card__art { display: block; width: 100%; height: auto; background: var(--art-bg); fill: var(--art-fg); }
.shape-card__art--slate { --art-bg: #62839d; --art-fg: #93b2c9; }
.shape-card__art--sage { --art-bg: #6f9770; --art-fg: #a2c49f; }
.shape-card__art .cut { fill: var(--art-bg); }
.shape-card__art .rim { fill: var(--art-bg); stroke: var(--art-fg); stroke-width: 4; }
.shape-card__title { margin: 20px 0 0; font-size: 20px; font-weight: 700; line-height: 1.3; letter-spacing: -0.01em; }
.shape-card__text { margin: 8px 0 0; color: var(--mute); font-size: 14px; line-height: 1.7; letter-spacing: 0.04em; word-break: auto-phrase; }
.shape-card__more { display: block; margin-top: 24px; font-size: 14px; font-weight: 600; letter-spacing: 0.04em; }

.shape-area :focus-visible { outline: 2px solid var(--ink); outline-offset: 4px; }

.shape-cursor__shape,
.shape-cursor__dot {
  position: fixed;
  left: 0;
  top: 0;
  pointer-events: none;
  visibility: hidden;
}

.shape-cursor__shape {
  z-index: -1;
  box-sizing: border-box;
  background: var(--shape-fill);
  box-shadow: inset 0 0 0 2px var(--shape-edge);
}

.shape-cursor__dot { z-index: 1; width: 8px; height: 8px; margin: -4px 0 0 -4px; border-radius: 50%; background: var(--ink); }

/* スクリプトが有効にしたときだけ、セクションの中で本物のカーソルを隠す */
.has-shape-cursor,
.has-shape-cursor * { cursor: none; }

JavaScript
import { gsap } from "gsap";

const PAD = 6; // リンクの行を囲む四角を、文字の幅から左右に広げる量(px)
const BAND = 4; // 行を囲む四角の角の丸み(px)
// タッチ端末にはポインタが無いので、独自カーソルは表示せず本物のカーソルのままにする
const canHover = window.matchMedia("(hover: hover) and (pointer: fine)").matches;
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;

document.querySelectorAll(".shape-area").forEach((area) => {
  const shape = area.querySelector(".shape-cursor__shape");
  const dot = area.querySelector(".shape-cursor__dot");
  if (!shape || !dot || !canHover) return;
  area.classList.add("has-shape-cursor");
  const rest = parseFloat(getComputedStyle(area).getPropertyValue("--shape-rest"));
  const state = { x: 0, y: 0, w: rest, h: rest, r: rest / 2 };
  const draw = () => gsap.set(shape, { x: state.x - state.w / 2, y: state.y - state.h / 2, width: state.w, height: state.h, borderRadius: state.r });
  // 位置・幅・高さ・角の丸みを別々に追わせる(scale で合わせると枠線まで太る)。
  // quickTo に duration 0 を渡すと動かないので、動きを減らす設定ではその場に置く
  const to = {};
  for (const key of Object.keys(state)) {
    to[key] = reduceMotion ? (v) => ((state[key] = v), draw()) : gsap.quickTo(state, key, { duration: 0.3, ease: "power3", onUpdate: draw });
  }

  // 部品の形。インラインのリンクはポインタのいる行を囲む四角(行の高さ)、ほかは要素の大きさと角の丸み
  const fit = (el, x, y) => {
    const css = getComputedStyle(el);
    if (css.display === "inline") {
      const rects = [...el.getClientRects()];
      const line = rects.find((r) => y >= r.top && y <= r.bottom) ?? rects[0];
      const height = parseFloat(getComputedStyle(el.parentElement).lineHeight);
      return { x: line.left + line.width / 2, y: line.top + line.height / 2, w: line.width + PAD * 2, h: height, r: BAND };
    }
    const box = el.getBoundingClientRect();
    const radius = css.borderTopLeftRadius;
    const r = radius.endsWith("%") ? (Math.min(box.width, box.height) * parseFloat(radius)) / 100 : parseFloat(radius);
    return { x: box.left + box.width / 2, y: box.top + box.height / 2, w: box.width, h: box.height, r };
  };
  const goal = (el, x, y) => (el ? fit(el, x, y) : { x, y, w: rest, h: rest, r: rest / 2 });
  const aim = (g) => Object.keys(g).forEach((key) => to[key](g[key]));

  // 隠れていたら、左上から飛んでこないよう、その形で置いてから表示する
  let shown = false;
  const show = (g, withDot) => {
    if (withDot) gsap.set(dot, { autoAlpha: 1 });
    if (shown) return;
    shown = true;
    Object.assign(state, g);
    draw();
    gsap.to(shape, { autoAlpha: 1, duration: reduceMotion ? 0 : 0.2 });
  };
  const hide = () => {
    shown = false;
    gsap.to([shape, dot], { autoAlpha: 0, duration: reduceMotion ? 0 : 0.2 });
  };

  let pointer = null;
  const update = () => {
    const { x, y } = pointer;
    const hit = document.elementFromPoint(x, y)?.closest("[data-cursor-shape]");
    const g = goal(hit && area.contains(hit) ? hit : null, x, y);
    gsap.set(dot, { x, y });
    show(g, true);
    aim(g);
  };
  area.addEventListener("pointermove", (event) => {
    if (event.pointerType === "touch") return;
    pointer = { x: event.clientX, y: event.clientY };
    update();
  });
  area.addEventListener("pointerleave", () => {
    pointer = null;
    hide();
  });
  // スクロールで部品が動いても、ポインタの下の部品に合わせ直す
  window.addEventListener("scroll", () => pointer && update(), { passive: true });

  // キーボードで移ったときも、その部品の形に重ねる(マウスで押したときのフォーカスは除く)
  area.addEventListener("focusin", (event) => {
    const el = event.target.closest("[data-cursor-shape]");
    if (!el || !event.target.matches(":focus-visible")) return;
    const g = fit(el, 0, 0);
    show(g, false);
    aim(g);
  });
  area.addEventListener("focusout", () => pointer || hide());
});

インタラクションデザインについて、
お気軽にご相談ください。

ここに並べているのは、Web サイトの中で動く UI のサンプルです。新しく作るサイトに組み込むことも、いま公開中のサイトに、ボタンやヒーロー、スクロールの演出だけを足すこともできます。設計から実装までの進め方はWeb サイト制作、端末ごとの見え方や表示速度の考え方はレスポンシブウェブサイトのページにまとめています。映像として作る動きをお探しの場合はモーショングラフィックスをご覧ください。