03傾き値が積み重ねる影レイヤーのボタン
回転角度値を計算してその大きさだけ影を複数層ずらして重ね、角度が小さくなるほど影の層が再び一つに重なり平らになる。
使いどころ商品購入ボタン・アプリダウンロードボタン
MAX_DEGLERPboxShadow

<main class="stage">
<div class="sk is-demo" id="sk">
<p class="sk__hint"><span>傾いた方向と逆側に、影が3層ずれて重なります</span></p>
<div class="sk__scene" id="scene">
<button class="sk__btn" id="btn" type="button" aria-label="今すぐ購入 — 傾きに応じて影が重なるボタン">
<span class="sk__label">今すぐ購入</span>
</button>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
@include stage($stage-orange);
.sk {
display: grid;
gap: $sp-4;
justify-items: center;
}
.sk__hint {
margin: 0;
font-size: 13px;
line-height: 1.5;
text-align: center;
max-width: 260px;
color: $subject-ink;
}
.sk__scene {
perspective: 640px;
padding: $sp-6;
}
.sk__btn {
position: relative;
border: 0;
cursor: pointer;
padding: $sp-4 $sp-8;
border-radius: $r-control;
background: $subject-cream;
color: $subject-ink;
font-size: 16px;
font-weight: 700;
transform-style: preserve-3d;
will-change: transform;
}
.sk__btn:focus-visible {
outline: 2px solid $subject-ink;
outline-offset: 3px;
}
.sk__label {
position: relative;
z-index: 1;
}
@media (prefers-reduced-motion: reduce) {
.sk__btn { transition: none; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #ff4d1f;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.sk {
display: grid;
gap: 16px;
justify-items: center;
}
.sk__hint {
margin: 0;
font-size: 13px;
line-height: 1.5;
text-align: center;
max-width: 260px;
color: #17141a;
}
.sk__scene {
perspective: 640px;
padding: 24px;
}
.sk__btn {
position: relative;
border: 0;
cursor: pointer;
padding: 16px 32px;
border-radius: 12px;
background: #fff7e6;
color: #17141a;
font-size: 16px;
font-weight: 700;
transform-style: preserve-3d;
will-change: transform;
}
.sk__btn:focus-visible {
outline: 2px solid #17141a;
outline-offset: 3px;
}
.sk__label {
position: relative;
z-index: 1;
}
@media (prefers-reduced-motion: reduce) {
.sk__btn {
transition: none;
}
}(function () {
var root = document.getElementById('sk');
var scene = document.getElementById('scene');
var btn = document.getElementById('btn');
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var MAX_DEG = 12;
var LERP = 0.15;
var targetNX = 0, targetNY = 0;
var curNX = 0, curNY = 0;
var hasPointer = false;
function clamp(v, lo, hi) { return Math.max(lo, Math.min(hi, v)); }
function syntheticTarget(t) { return [Math.sin(t * 0.95 + 0.4), Math.sin(t * 1.35)]; }
// 그림자 3겹 — 기운 반대 방향으로 오프셋이 커지고, 층마다 흐림·투명도가 달라 깊이가 읽힌다
function render(nx, ny) {
var rotY = nx * MAX_DEG;
var rotX = -ny * MAX_DEG;
btn.style.transform = 'rotateX(' + rotX.toFixed(2) + 'deg) rotateY(' + rotY.toFixed(2) + 'deg)';
var ox = -nx, oy = -ny;
var layers = [];
for (var i = 1; i <= 3; i++) {
var d = i * 7;
var blur = i * 9;
var alpha = (0.22 - i * 0.03).toFixed(2);
layers.push((ox * d).toFixed(1) + 'px ' + (oy * d + i * 3).toFixed(1) + 'px ' + blur + 'px rgba(23,20,26,' + alpha + ')');
}
btn.style.boxShadow = layers.join(', ');
}
var t0 = null;
function loop(now) {
if (t0 === null) t0 = now;
var t = (now - t0) / 1000;
if (!hasPointer && !reduce) {
var s = syntheticTarget(t);
targetNX = s[0]; targetNY = s[1];
}
curNX += (targetNX - curNX) * LERP;
curNY += (targetNY - curNY) * LERP;
render(curNX, curNY);
requestAnimationFrame(loop);
}
function pointFromEvent(e) {
var r = scene.getBoundingClientRect();
var nx = ((e.clientX - r.left) / r.width) * 2 - 1;
var ny = ((e.clientY - r.top) / r.height) * 2 - 1;
return [clamp(nx, -1, 1), clamp(ny, -1, 1)];
}
scene.addEventListener('pointermove', function (e) {
hasPointer = true;
root.classList.remove('is-demo');
var p = pointFromEvent(e);
targetNX = p[0]; targetNY = p[1];
});
scene.addEventListener('pointerleave', function () {
hasPointer = false;
targetNX = 0; targetNY = 0;
});
btn.addEventListener('click', function () {
root.classList.remove('is-demo');
hasPointer = true;
targetNX = -0.8; targetNY = 0.5;
if (reduce) { render(targetNX, targetNY); }
setTimeout(function () { if (!hasPointer) { targetNX = 0; targetNY = 0; if (reduce) { render(targetNX, targetNY); } } }, 240);
});
btn.addEventListener('keydown', function (e) {
var step = 0.22;
var moves = { ArrowLeft: [-step, 0], ArrowRight: [step, 0], ArrowUp: [0, -step], ArrowDown: [0, step] };
var m = moves[e.key];
if (!m) return;
e.preventDefault();
root.classList.remove('is-demo');
hasPointer = true;
targetNX = clamp(targetNX + m[0], -1, 1);
targetNY = clamp(targetNY + m[1], -1, 1);
if (reduce) { render(targetNX, targetNY); }
});
if (reduce) {
render(-0.4, 0.3);
} else {
requestAnimationFrame(loop);
}
})();// ShadowStackButton — vanilla 판(run/393/demo/03-pointer-tilt-3d-shadow-stack/index.html) 의 React 포트.
// 정규화된 포인터 좌표의 반대 방향으로 그림자 3겹을 서로 다른 거리·흐림·투명도로 쌓아 깊이를 만든다.
// 값·수치는 vanilla 와 같다.
import { useEffect, useRef, useState } from "react";
import type { PointerEvent as ReactPointerEvent } from "react";
import styles from "./ShadowStackButton.module.scss";
type Box<T> = { current: T };
type KeyEvt = { key: string; preventDefault(): void };
const MAX_DEG = 12;
const LERP = 0.15;
const LABEL = "지금 구매하기 — 기울기에 따라 그림자가 쌓이는 버튼";
function clamp(v: number, lo: number, hi: number): number {
return Math.max(lo, Math.min(hi, v));
}
function syntheticTarget(t: number): [number, number] {
return [Math.sin(t * 0.95 + 0.4), Math.sin(t * 1.35)];
}
export default function ShadowStackButton() {
const sceneRef = useRef<HTMLDivElement | null>(null);
const btnRef = useRef<HTMLButtonElement | null>(null);
const targetXRef = useRef<number>(0) as Box<number>;
const targetYRef = useRef<number>(0) as Box<number>;
const curXRef = useRef<number>(0) as Box<number>;
const curYRef = useRef<number>(0) as Box<number>;
const hasPointerRef = useRef<boolean>(false) as Box<boolean>;
const frameRef = useRef<number>(0) as Box<number>;
const t0Ref = useRef<number>(0) as Box<number>;
const [demo, setDemo] = useState(true);
const [reduce, setReduce] = useState(false);
function render(nx: number, ny: number): void {
const btn = btnRef.current;
if (!btn) return;
const rotY = nx * MAX_DEG;
const rotX = -ny * MAX_DEG;
btn.style.transform = "rotateX(" + rotX.toFixed(2) + "deg) rotateY(" + rotY.toFixed(2) + "deg)";
const ox = -nx;
const oy = -ny;
const layers: string[] = [];
for (let i = 1; i <= 3; i++) {
const d = i * 7;
const blur = i * 9;
const alpha = (0.22 - i * 0.03).toFixed(2);
layers.push((ox * d).toFixed(1) + "px " + (oy * d + i * 3).toFixed(1) + "px " + blur + "px rgba(23,20,26," + alpha + ")");
}
btn.style.boxShadow = layers.join(", ");
}
useEffect(() => {
setReduce(window.matchMedia("(prefers-reduced-motion: reduce)").matches);
}, []);
useEffect(() => {
if (reduce) {
render(-0.4, 0.3);
return;
}
function loop(now: number): void {
if (t0Ref.current === 0) t0Ref.current = now;
const t = (now - t0Ref.current) / 1000;
if (!hasPointerRef.current) {
const s = syntheticTarget(t);
targetXRef.current = s[0];
targetYRef.current = s[1];
}
curXRef.current += (targetXRef.current - curXRef.current) * LERP;
curYRef.current += (targetYRef.current - curYRef.current) * LERP;
render(curXRef.current, curYRef.current);
frameRef.current = requestAnimationFrame(loop);
}
frameRef.current = requestAnimationFrame(loop);
return () => cancelAnimationFrame(frameRef.current);
}, [reduce]);
function pointFromEvent(event: ReactPointerEvent<HTMLDivElement>): [number, number] {
const scene = sceneRef.current;
if (!scene) return [0, 0];
const r = scene.getBoundingClientRect();
const nx = ((event.clientX - r.left) / r.width) * 2 - 1;
const ny = ((event.clientY - r.top) / r.height) * 2 - 1;
return [clamp(nx, -1, 1), clamp(ny, -1, 1)];
}
function onPointerMove(event: ReactPointerEvent<HTMLDivElement>): void {
hasPointerRef.current = true;
setDemo(false);
const p = pointFromEvent(event);
targetXRef.current = p[0];
targetYRef.current = p[1];
}
function onPointerLeave(): void {
hasPointerRef.current = false;
targetXRef.current = 0;
targetYRef.current = 0;
}
function onClick(): void {
setDemo(false);
hasPointerRef.current = true;
targetXRef.current = -0.8;
targetYRef.current = 0.5;
if (reduce) render(targetXRef.current, targetYRef.current);
window.setTimeout(() => {
if (!hasPointerRef.current) {
targetXRef.current = 0;
targetYRef.current = 0;
if (reduce) render(targetXRef.current, targetYRef.current);
}
}, 240);
}
function onKeyDown(event: KeyEvt): void {
const step = 0.22;
const moves: { [key: string]: [number, number] } = {
ArrowLeft: [-step, 0],
ArrowRight: [step, 0],
ArrowUp: [0, -step],
ArrowDown: [0, step],
};
const m = moves[event.key];
if (!m) return;
event.preventDefault();
setDemo(false);
hasPointerRef.current = true;
targetXRef.current = clamp(targetXRef.current + m[0], -1, 1);
targetYRef.current = clamp(targetYRef.current + m[1], -1, 1);
if (reduce) render(targetXRef.current, targetYRef.current);
}
const cls = [styles.sk, demo ? styles.isDemo : ""].filter(Boolean).join(" ");
return (
<main className={styles.stage}>
<div className={cls}>
<p className={styles.hint}>
<span>기운 방향의 반대쪽으로 그림자 세 겹이 어긋나 쌓입니다</span>
</p>
<div className={styles.scene} ref={sceneRef} onPointerMove={onPointerMove} onPointerLeave={onPointerLeave}>
<button className={styles.btn} ref={btnRef} type="button" aria-label={LABEL} onClick={onClick} onKeyDown={onKeyDown}>
<span className={styles.label}>지금 구매하기</span>
</button>
</div>
</div>
</main>
);
}03 傾き値が積み重ねる影レイヤーのボタン — 計測 累積面積 6.126% · 動いたフレーム 22/23GODRICH オリジナル · SCSS と React は zip に同梱 · css ボタン 立体感 9種 — クリックまで再現
このデモが属する記事
css ボタン 立体感 9種 — クリックまで再現9種のcss ボタン 立体感デモをここに並べた。 カーソルがボタン上のどこにあるかを角度値ひとつに変えると、平らな面はどう立体に見えるのか? 座標値がrotateX・rotateYを決めるボタン、傾きの大きさ値が縁の光の強さを決めるボタン、 この記事のデモをすべて見る


