カード
カードを傾けると、箔押し部分だけに虹色の光が走る
デモ
デモを読み込めませんでした。
操作:カーソルを動かす。「さわってみる」を押すと自動再生が止まり、マウスで動かせます。
仕様
- 用途
- カード
- 動作
- DOM+WebGL
- 種類
- シェーダー
- 動作確認
- three 0.186.0・gsap 3.15.0
- 公開日
しくみと調整
カードを傾けると、枠や星に施したホログラム箔に、傾けた方向へ向かって虹色の光がスーッと走ります。写真や文字はほとんど光らないので、箔押しされた部分だけがくっきりと浮かび上がって見えます。
本物のホログラム箔には目に見えないほど細かい溝が刻まれていて、光が当たる角度と見る角度によって強く反射する色が変わります。この仕組みを画面上で色を計算するプログラム(シェーダー)で再現し、カードの傾きと溝の向きから反射する色を割り出しています。枠の溝は斜めに、星と右下の丸印の溝は中心から同心円状に入れているため、図柄の中では色が輪を描くようにくるくると動きます。
また、箔の表面を細かなマス目に分け、それぞれが少し違う方向を向いた小さな鏡のように光を返すよう設定しました。自分の目のほうへ光を跳ね返すマスだけがキラッと光るため、カードを傾けるたびにきらめく場所が移り変わります。シルバーの下地には左右に配置した縦長の照明が映り込み、傾きに合わせて表面を光の筋が横切っていきます。
カード本体の傾きにはCSSの rotateY と rotateX を使い、その上に重ねた1枚の canvas に箔の反射だけを描画しています。こうすることで、見出しやリンクは通常のHTMLのまま扱えますし、カードの傾きと箔の光り方を別々に制御できるため、キーボードでカードを選んだときはカードを平らに保ったまま箔だけを光らせることも可能です。
カードの傾きは最大18度に抑え、カーソルの動きに対して0.6秒かけて滑らかに減速しながら追いつく power3.out のイージングを設定しています。虹色の主張を弱めると落ち着いたシルバー箔のような質感に近づき、逆に色の移り変わりを速くすると、わずかに傾けただけでも色が大きく変化する華やかな見た目になります。
コード
<!-- 傾くのは .holo__card だけ。外の .holo は傾かず、ポインタの位置を測る要素と奥行き(perspective)を持つ -->
<article class="holo">
<div class="holo__card">
<!-- 印刷の部分。実際のサイトでは、ここをカードの画像(img)に替える -->
<div class="holo__art" role="img" aria-label="夜明け前の空に光る星"></div>
<!-- 箔は canvas に描く。箔の無い所は透明なので、下の印刷が見える -->
<canvas class="holo__foil" aria-hidden="true"></canvas>
<p class="holo__label">LIMITED EDITION</p>
<p class="holo__label holo__label--end">Vol.03</p>
<h3 class="holo__title"><a href="/collection/026/">宵の明星</a></h3>
<p class="holo__text">ここに説明文が入ります。</p>
<p class="holo__label holo__number">No. 026 / 300</p>
</div>
</article>.holo {
/* 印刷の部分の色。箔の色は、シェーダーが光と角度から決める */
--body-top: #262a31;
--body-bottom: #181a1f;
--ink: #eeece6;
--label: #a8a6a0;
width: min(344px, 100%);
margin-inline: auto;
perspective: 1056px; /* 小さいほど遠近が強くなり、カードの上と下で光り方の差が大きくなる */
touch-action: pan-y; /* 指で横になぞると傾き、縦はページのスクロールに任せる */
}
.holo__card {
/* カードの幅を 344 としたときの 1 の大きさ。中の位置は、シェーダーの箔の形と同じ寸法で書く */
--u: calc(100cqw / 344);
container-type: inline-size;
position: relative;
aspect-ratio: 344 / 480;
color: var(--ink);
}
.holo__card::before {
content: "";
position: absolute;
inset: 0;
border-radius: calc(12 * var(--u)); /* シェーダーが描く箔の角と同じ丸み */
background: linear-gradient(160deg, var(--body-top), var(--body-bottom));
}
/* 夜明け前の空と星の画像の代わり。CSS のグラデーションで描いている */
.holo__art {
position: absolute;
left: calc(26 * var(--u));
top: calc(52 * var(--u));
width: calc(292 * var(--u));
height: calc(248 * var(--u));
border-radius: calc(4 * var(--u));
--star: rgba(255, 255, 255, 0.85);
background:
radial-gradient(circle at 14% 16%, var(--star) 0 1px, transparent 1.6px), radial-gradient(circle at 31% 9%, var(--star) 0 1px, transparent 1.6px),
radial-gradient(circle at 78% 12%, var(--star) 0 1px, transparent 1.6px), radial-gradient(circle at 88% 30%, var(--star) 0 1px, transparent 1.6px),
radial-gradient(circle at 9% 44%, var(--star) 0 1px, transparent 1.6px), radial-gradient(circle at 64% 6%, var(--star) 0 1px, transparent 1.6px),
radial-gradient(circle at 93% 52%, var(--star) 0 1px, transparent 1.6px),
radial-gradient(120% 34% at 18% 112%, #14171e 0 60%, transparent 60.5%),
radial-gradient(100% 30% at 90% 110%, #1b1e28 0 60%, transparent 60.5%),
radial-gradient(90% 46% at 50% 100%, rgba(232, 150, 118, 0.6), transparent 70%),
linear-gradient(180deg, #1a2a44 0%, #3b3478 46%, #8a4f7e 76%, #d88670 100%);
}
.holo__foil {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
pointer-events: none;
}
.holo__label {
position: absolute;
top: calc(28 * var(--u));
left: calc(28 * var(--u));
margin: 0;
color: var(--label);
font-size: calc(11 * var(--u));
font-weight: 700;
font-variant-numeric: tabular-nums;
line-height: 1;
letter-spacing: 0.14em;
white-space: nowrap;
}
.holo__label--end { left: auto; right: calc(26 * var(--u)); }
.holo__number { top: calc(436 * var(--u)); }
.holo__title {
position: absolute;
left: calc(26 * var(--u));
top: calc(316 * var(--u));
margin: 0;
font-size: calc(24 * var(--u));
line-height: 1.3;
letter-spacing: -0.01em;
}
.holo__title a { color: inherit; text-decoration: none; }
.holo__title a:focus-visible { outline: 2px solid var(--ink); outline-offset: 4px; border-radius: 2px; }
.holo__text {
position: absolute;
left: calc(26 * var(--u));
top: calc(356 * var(--u));
width: calc(292 * var(--u));
margin: 0;
color: var(--label);
font-size: calc(14 * var(--u));
line-height: 1.7;
letter-spacing: 0.04em;
}import * as THREE from "three";
import { gsap } from "gsap";
const MAX_TILT = 18; // カードの端にポインタがあるときの傾き(度)
const FOCUS_TILT = { x: -6, y: 14 }; // キーボードでフォーカスしたときに、箔だけに当てる角度(カードは傾けない)
const FOIL = 0.62; // 虹の濃さ(0 = 銀だけ、1 = 虹だけ)
const SPREAD = 1.8; // 傾きに対する虹の流れやすさ。上げると、少し傾けただけで色がひと回りする
const LIGHT = new THREE.Vector3(-0.32, -0.5, 1).normalize(); // 光の向き(画面の座標。x は右、y は下、z は手前)
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
// 箔の形は、カードの幅を 344 とした寸法で描く(中心が原点、y は下向き)。形を変えるときは foil() を書き換える
const fragmentShader = /* glsl */ `
precision highp float; uniform vec3 uEye, uLight; uniform float uPx; varying vec2 vUv;
const vec2 SIZE = vec2(344.0, 480.0);
float box(vec2 p, vec2 b, float r) { vec2 d = abs(p) - b + r; return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0) - r; }
vec3 hash3(vec2 p) { vec3 q = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973)); q += dot(q, q.yxz + 33.33); return fract((q.xxy + q.yzz) * q.zyx); }
// 戻り値は箔の縁までの距離(負が箔の中)。h は浮き出しの高さ、g は溝の向き(虹の色が変わる向き)
float foil(vec2 p, out float h, out vec2 g) {
g = vec2(0.7071); // 枠と線は斜めの溝
float card = box(p, SIZE * 0.5, 12.0);
float d = max(card, -card - 14.0); // 外の枠
h = smoothstep(0.0, 3.0, -card) * smoothstep(0.0, 3.0, card + 14.0) * 2.0;
d = min(d, abs(box(p - vec2(0.0, -64.0), vec2(146.0, 124.0), 4.0)) - 1.25); // 画像の縁の線
d = min(d, max(abs(p.y - 176.0) - 0.75, abs(p.x) - 146.0)); // 下の区切りの線
// 星の図柄。光ごとに、軸を稜線にした 2 つの斜面に切り立てる
vec2 q = p - vec2(0.0, -64.0);
float r = length(q), a = atan(q.y, q.x) + 1.5708, k = floor(a / 0.7854 + 0.5), phi = a - k * 0.7854;
float tip = mod(k, 2.0) < 0.5 ? 74.0 : 48.0; vec2 edge = vec2(20.33, 8.42) - vec2(tip, 0.0);
float star = dot(r * vec2(cos(phi), abs(sin(phi))) - vec2(tip, 0.0), normalize(vec2(edge.y, -edge.x)));
float rings = min(min(abs(r - 92.0) - 1.5, abs(r - 100.0) - 0.75), max(abs(phi * r) - 0.75, abs(r - 108.0) - 4.0));
if (min(star, rings) < 0.0) g = q / max(r, 1.0); // 図柄と印は同心円の溝
h += max(-star, 0.0) * 0.45; d = min(d, min(star, rings));
vec2 c = p - vec2(128.0, 201.0); float rc = length(c); // 右下の丸い印
if (rc < 16.0) g = c / max(rc, 1.0); h += smoothstep(16.0, 13.0, rc) * (1.0 + 0.3 * sin(rc * 1.2));
return min(d, rc - 16.0);
}
// 左右に 1 本ずつ立てた縦長の照明。どちらへ傾けても、どちらかの筋が面を横切る
float lamp(vec3 r) { return exp(-pow((abs(r.x) - 0.22) / 0.12, 2.0)); }
void main() {
vec2 p = (vec2(vUv.x, 1.0 - vUv.y) - 0.5) * SIZE, g, unused;
float h, hx, hy, d = foil(p, h, g);
foil(p + vec2(0.75, 0.0), hx, unused); foil(p + vec2(0.0, 0.75), hy, unused);
float inCard = clamp(0.5 - box(p, SIZE * 0.5, 12.0) / uPx, 0.0, 1.0), m = clamp(0.5 - d / uPx, 0.0, 1.0) * inCard;
vec3 V = normalize(uEye - vec3(p, 0.0)), N = normalize(vec3(-(hx - h) / 0.75, -(hy - h) / 0.75, 1.0)), Rf = reflect(-V, N);
float env = 0.24 + 0.26 * smoothstep(-0.3, 0.8, -Rf.y) + 1.05 * lamp(Rf); // 銀の下地に映る明かり
// 虹: 光と目の向きの、面に沿った成分の和で色相を決める(回折格子と同じ考え方)。斜めの縞も足す
float hue = dot(uLight.xy + V.xy, g) * ${SPREAD.toFixed(3)} + dot(p, vec2(0.6, -0.8)) / 260.0;
vec3 rainbow = 0.5 + 0.5 * cos(6.2832 * (hue + vec3(0.0, 0.33, 0.67))), color = env * mix(vec3(1.0), rainbow * 1.3, ${FOIL.toFixed(3)});
// きらめき: マスごとに向きのばらばらな小さな鏡。光を目へ返すマスだけ光る
vec3 rnd = hash3(floor(p / 5.0)), facet = normalize(vec3((rnd.xy - 0.5) * 0.9, 1.0));
float glint = smoothstep(0.986, 0.997, dot(reflect(-uLight, facet), V)), spot = length(fract(p / 5.0) - 0.5 - (rnd.yz - 0.5) * 0.5) * 5.0;
color += glint * smoothstep(1.7, 0.3, spot) * (0.8 + 0.8 * rnd.z) * mix(vec3(1.0), rainbow, 0.35);
float sheen = lamp(reflect(-V, vec3(0.0, 0.0, 1.0))) * 0.06 * inCard; // 印刷の部分は、ニスの淡いつやだけ
gl_FragColor = vec4(color * m + vec3(sheen * (1.0 - m)), m + sheen * (1.0 - m));
}
`;
const vertexShader = "varying vec2 vUv; void main() { vUv = uv; gl_Position = vec4(position.xy, 0.0, 1.0); }";
document.querySelectorAll(".holo").forEach((area) => {
const card = area.querySelector(".holo__card");
const uniforms = { uEye: { value: new THREE.Vector3() }, uLight: { value: new THREE.Vector3() }, uPx: { value: 1 } };
const renderer = new THREE.WebGLRenderer({ canvas: card.querySelector(".holo__foil"), alpha: true });
renderer.setPixelRatio(Math.max(window.devicePixelRatio, 2)); // 細いきらめきと縁が、傾けて縮んでもつぶれないように
const plane = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), new THREE.ShaderMaterial({ uniforms, vertexShader, fragmentShader }));
const scene = new THREE.Scene().add(plane), camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
const tilt = { x: 0, y: 0 }, turn = new THREE.Quaternion();
let lean = reduceMotion ? 0 : 1; // 0 のときはカードを傾けず、箔の光り方だけを変える
const draw = () => {
card.style.transform = `rotateY(${tilt.y * lean}deg) rotateX(${tilt.x * lean}deg)`;
// 目と光の向きを、傾いたカードの座標に直して渡す(CSS の rotateY → rotateX と同じ回転の逆)
turn.setFromEuler(new THREE.Euler(THREE.MathUtils.degToRad(tilt.x), THREE.MathUtils.degToRad(tilt.y), 0, "YXZ")).invert();
const perspective = parseFloat(getComputedStyle(area).perspective) * (344 / card.clientWidth);
uniforms.uEye.value.set(0, 0, perspective).applyQuaternion(turn);
uniforms.uLight.value.copy(LIGHT).applyQuaternion(turn);
renderer.render(scene, camera);
};
const to = (x, y) => (reduceMotion ? (Object.assign(tilt, { x, y }), draw()) : gsap.to(tilt, { x, y, duration: 0.6, ease: "power3.out", overwrite: true, onUpdate: draw }));
const sizes = new ResizeObserver(() => {
renderer.setSize(card.clientWidth, card.clientHeight, false); // 大きさは CSS が決めるので、style は書き換えない
uniforms.uPx.value = 344 / (card.clientWidth * renderer.getPixelRatio());
draw();
});
sizes.observe(card);
area.addEventListener("pointermove", (e) => {
const r = area.getBoundingClientRect(); // 傾いて形の変わるカードではなく、傾かない外側の要素で測る
to(-(((e.clientY - r.top) / r.height) * 2 - 1) * MAX_TILT, (((e.clientX - r.left) / r.width) * 2 - 1) * MAX_TILT);
});
for (const type of ["pointerleave", "pointercancel"]) area.addEventListener(type, () => to(0, 0)); // cancel は指でスクロールし始めたとき
// キーボードのフォーカスでは、枠がまっすぐ表示されるようカードは平らのまま、箔だけを光らせる
area.addEventListener("focusin", (e) => {
if (e.target.matches(":focus-visible")) { lean = 0; to(FOCUS_TILT.x, FOCUS_TILT.y); }
});
area.addEventListener("focusout", () => { lean = reduceMotion ? 0 : 1; to(0, 0); });
area.destroy = () => { // PJAX の遷移でカードごと破棄するときに呼ぶ。放っておくと WebGL のコンテキストが積み上がる
gsap.killTweensOf(tilt); sizes.disconnect();
plane.geometry.dispose(); plane.material.dispose(); renderer.dispose(); renderer.forceContextLoss();
};
});










