04中心から円形に広がるリビール半径
押した地点を中心に半径がlerpで加速しながら円形に広がり、箔を消していく。
使いどころワンタップで開くモバイルクーポン
wipeWIPE_LERP_RATEWIPE_TARGET_R

<main class="stage">
<div class="scv is-demo" id="scv">
<div class="scv__card">
<p class="scv__label">スクラッチカード</p>
<div class="scv__zone" id="zone">
<div class="scv__prize">
<p class="scv__prizeTitle">割引クーポン</p>
<p class="scv__prizeSub">押した地点から円がだんだん大きくなります</p>
</div>
<canvas class="scv__foil" id="foil" width="280" height="150" aria-hidden="true"></canvas>
</div>
<button type="button" class="scv__reveal" id="revealBtn">一気に見る</button>
<p class="scv__live" id="live" aria-live="polite"></p>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 스크래치 카드 04 — press.r 가 목표 반지름(WIPE_TARGET_R)까지 매 프레임
// lerp(WIPE_LERP_RATE) 로 가속 추적해 누르고만 있어도 원형으로 은박이 넓어진다.
@include stage($stage-paper);
.scv { display: flex; justify-content: center; width: 100%; }
.scv__card {
width: min(100%, 336px); background: $subject-blue-deep; color: $subject-cream;
border-radius: $r-card; box-shadow: $shadow-raised; padding: $sp-3 $sp-4;
}
.scv__label { margin: 0 0 $sp-1; font-weight: 800; font-size: 12px; letter-spacing: .04em; }
.scv__zone {
position: relative; border-radius: $r-control; overflow: hidden;
aspect-ratio: 280 / 150; background: #eef0f3;
}
.scv__prize {
position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center;
justify-content: center; gap: $sp-1; text-align: center; padding: $sp-2; z-index: 0;
}
.scv__prizeTitle { margin: 0; font-weight: 800; font-size: clamp(18px, 6vw, 22px); color: $subject-blue-deep; }
.scv__prizeSub { margin: 0; font-weight: 700; font-size: 11px; color: rgba(23, 20, 26, .68); }
.scv__foil { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; z-index: 1; }
.scv.is-open .scv__foil { visibility: hidden; }
.scv__reveal {
margin-top: $sp-1; border: 0; cursor: pointer; background: $subject-cream; color: $subject-blue-deep;
border-radius: $r-pill; font-family: inherit; font-weight: 800; font-size: 11px; padding: $sp-1 $sp-3; line-height: 1.3;
}
.scv__reveal:focus-visible { outline: 3px solid $stage-ink; outline-offset: 2px; }
.scv__live { margin: 0; min-height: 0; font-weight: 700; font-size: 10px; color: $subject-cream; }
@media (max-width: 340px) {
.scv__card { padding: $sp-3; }
}
@media (max-height: 220px) {
.stage { padding: $sp-2; }
.scv__card { padding: $sp-2 $sp-3; }
.scv__label { margin: 0 0 $sp-1; }
.scv__zone { aspect-ratio: 280 / 88; }
.scv__reveal { margin-top: $sp-1; }
.scv__live { margin-top: $sp-1; min-height: .9em; }
}
@media (prefers-reduced-motion: reduce) {
.scv { cursor: default; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #ffffff;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.scv {
display: flex;
justify-content: center;
width: 100%;
}
.scv__card {
width: min(100%, 336px);
background: #0a4fe8;
color: #fff7e6;
border-radius: 16px;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
padding: 12px 16px;
}
.scv__label {
margin: 0 0 4px;
font-weight: 800;
font-size: 12px;
letter-spacing: 0.04em;
}
.scv__zone {
position: relative;
border-radius: 12px;
overflow: hidden;
aspect-ratio: 280/150;
background: #eef0f3;
}
.scv__prize {
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 4px;
text-align: center;
padding: 8px;
z-index: 0;
}
.scv__prizeTitle {
margin: 0;
font-weight: 800;
font-size: clamp(18px, 6vw, 22px);
color: #0a4fe8;
}
.scv__prizeSub {
margin: 0;
font-weight: 700;
font-size: 11px;
color: rgba(23, 20, 26, 0.68);
}
.scv__foil {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
touch-action: none;
z-index: 1;
}
.scv.is-open .scv__foil {
visibility: hidden;
}
.scv__reveal {
margin-top: 4px;
border: 0;
cursor: pointer;
background: #fff7e6;
color: #0a4fe8;
border-radius: 999px;
font-family: inherit;
font-weight: 800;
font-size: 11px;
padding: 4px 12px;
line-height: 1.3;
}
.scv__reveal:focus-visible {
outline: 3px solid #17141a;
outline-offset: 2px;
}
.scv__live {
margin: 0;
min-height: 0;
font-weight: 700;
font-size: 10px;
color: #fff7e6;
}
@media (max-width: 340px) {
.scv__card {
padding: 12px;
}
}
@media (max-height: 220px) {
.stage {
padding: 8px;
}
.scv__card {
padding: 8px 12px;
}
.scv__label {
margin: 0 0 4px;
}
.scv__zone {
aspect-ratio: 280/88;
}
.scv__reveal {
margin-top: 4px;
}
.scv__live {
margin-top: 4px;
min-height: 0.9em;
}
}
@media (prefers-reduced-motion: reduce) {
.scv {
cursor: default;
}
}(function () {
// 기법: 누른 지점을 중심으로 반지름을 목표값까지 lerp(선형보간)로 매 프레임 가속 추적한다.
// 실제 드래그 없이 누르고 있기만 해도 반지름이 스스로 자라나 원형으로 은박이 사라진다.
var root = document.getElementById('scv');
var zone = document.getElementById('zone');
var foil = document.getElementById('foil');
var ctx = foil.getContext('2d');
var revealBtn = document.getElementById('revealBtn');
var live = document.getElementById('live');
var FW = foil.width, FH = foil.height;
var WIPE_TARGET_R = Math.ceil(Math.sqrt(FW * FW + FH * FH) / 2); // 카드 대각선 절반 — 어느 지점을 눌러도 끝까지 덮는다
var WIPE_LERP_RATE = 1.5; // 확장 과정이 눈에 오래 보이도록 가속을 늦춘다
function seedRand(seed) {
var s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
var foilRand = seedRand(424041);
function paintFoil() {
ctx.globalCompositeOperation = 'source-over';
ctx.clearRect(0, 0, FW, FH);
var grad = ctx.createLinearGradient(0, 0, FW, FH);
grad.addColorStop(0, '#dfe3ea');
grad.addColorStop(.5, '#f4f6f9');
grad.addColorStop(1, '#c9ced8');
ctx.fillStyle = grad;
ctx.fillRect(0, 0, FW, FH);
ctx.strokeStyle = 'rgba(23,20,26,.14)';
ctx.lineWidth = 1;
for (var i = 0; i < 18; i += 1) {
var x = foilRand() * FW, y = foilRand() * FH;
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + (foilRand() * 20 - 10), y + (foilRand() * 8 - 4));
ctx.stroke();
}
}
var press = null; // {x,y,r}
function wipe() {
ctx.globalCompositeOperation = 'destination-out';
ctx.beginPath();
ctx.arc(press.x, press.y, press.r, 0, Math.PI * 2);
ctx.fill();
}
function clearedRatio() {
var data = ctx.getImageData(0, 0, FW, FH).data;
var clear = 0, total = data.length / 4;
for (var i = 3; i < data.length; i += 4) { if (data[i] < 40) clear += 1; }
return clear / total;
}
var opened = false;
function openCard() {
if (opened) return;
opened = true;
ctx.globalCompositeOperation = 'destination-out';
ctx.fillRect(0, 0, FW, FH);
root.classList.add('is-open');
live.textContent = '割引クーポンが開きました';
}
function pos(e) {
var r = foil.getBoundingClientRect();
return { x: (e.clientX - r.left) * (FW / r.width), y: (e.clientY - r.top) * (FH / r.height) };
}
paintFoil();
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var holding = false;
zone.addEventListener('pointerdown', function (e) {
if (opened) return;
root.classList.remove('is-demo');
holding = true;
var p = pos(e);
press = { x: p.x, y: p.y, r: 4 };
wipe();
if (zone.setPointerCapture) zone.setPointerCapture(e.pointerId);
});
zone.addEventListener('pointermove', function (e) {
if (!holding || opened) return;
var p = pos(e);
press.x = p.x; press.y = p.y;
});
window.addEventListener('pointerup', function () { holding = false; });
revealBtn.addEventListener('click', function () { root.classList.remove('is-demo'); openCard(); });
// 카드 끝까지 원형 확장 → 다시 덮임 → 다른 지점에서 재확장을 한 루프 안에서 반복한다.
var demoRand = seedRand(424042);
var DEMO_POINTS = [
{ x: FW * (0.30 + demoRand() * 0.08), y: FH * (0.36 + demoRand() * 0.08) },
{ x: FW * (0.66 + demoRand() * 0.08), y: FH * (0.58 + demoRand() * 0.08) },
{ x: FW * (0.42 + demoRand() * 0.08), y: FH * (0.30 + demoRand() * 0.08) }
];
var DEMO_STARTS = [220, 950, 1680];
var DEMO_LOOP_MS = 3200;
var demoCycle = -1;
var demoPress = null;
function resetDemo() {
opened = false;
root.classList.remove('is-open');
paintFoil();
demoCycle = -1;
demoPress = null;
}
function runDemo(tMod, dtS) {
var next = demoCycle + 1;
if (next < DEMO_STARTS.length && tMod >= DEMO_STARTS[next]) {
demoCycle = next;
var p = DEMO_POINTS[demoCycle];
demoPress = { x: p.x, y: p.y, r: 4 };
opened = false;
root.classList.remove('is-open');
paintFoil(); // 이전 사이클에서 열렸던 은박을 다시 덮는다
}
if (demoPress && !opened) {
demoPress.r += (WIPE_TARGET_R - demoPress.r) * Math.min(1, WIPE_LERP_RATE * dtS);
press = demoPress;
wipe();
if (clearedRatio() > .82) openCard();
}
}
if (reduce) root.classList.remove('is-demo');
var elapsed = 0, lastTs = null;
function tick(ts) {
if (lastTs === null) lastTs = ts;
var dt = Math.min(ts - lastTs, 48);
var dtS = dt / 1000;
lastTs = ts;
if (holding && !opened && press) {
press.r += (WIPE_TARGET_R - press.r) * Math.min(1, WIPE_LERP_RATE * dtS);
wipe();
if (clearedRatio() > .82) openCard();
}
if (!reduce && root.classList.contains('is-demo')) {
elapsed += dt;
var tMod = elapsed % DEMO_LOOP_MS;
if (tMod < dt) resetDemo();
runDemo(tMod, dtS);
}
requestAnimationFrame(tick);
}
requestAnimationFrame(tick);
})();import { useEffect, useRef } from "react";
import styles from "./ScratchCardRevealRadialWipeLerp.module.scss";
type Press = { x: number; y: number; r: number };
export default function ScratchCardRevealRadialWipeLerp() {
const rootRef = useRef<HTMLDivElement>(null);
const zoneRef = useRef<HTMLDivElement>(null);
const foilRef = useRef<HTMLCanvasElement>(null);
const revealBtnRef = useRef<HTMLButtonElement>(null);
const liveRef = useRef<HTMLParagraphElement>(null);
useEffect(() => {
const root = rootRef.current;
const zone = zoneRef.current;
const foil = foilRef.current;
const revealBtn = revealBtnRef.current;
const live = liveRef.current;
if (!root || !zone || !foil || !revealBtn || !live) return;
const ctx = foil.getContext("2d");
if (!ctx) return;
const FW = foil.width, FH = foil.height;
const WIPE_TARGET_R = Math.ceil(Math.sqrt(FW * FW + FH * FH) / 2); // 카드 대각선 절반 — 어느 지점을 눌러도 끝까지 덮는다
const WIPE_LERP_RATE = 1.5; // 확장 과정이 눈에 오래 보이도록 가속을 늦춘다
function seedRand(seed: number) {
let s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
const foilRand = seedRand(424041);
function paintFoil() {
ctx!.globalCompositeOperation = "source-over";
ctx!.clearRect(0, 0, FW, FH);
const grad = ctx!.createLinearGradient(0, 0, FW, FH);
grad.addColorStop(0, "#dfe3ea");
grad.addColorStop(.5, "#f4f6f9");
grad.addColorStop(1, "#c9ced8");
ctx!.fillStyle = grad;
ctx!.fillRect(0, 0, FW, FH);
ctx!.strokeStyle = "rgba(23,20,26,.14)";
ctx!.lineWidth = 1;
for (let i = 0; i < 18; i += 1) {
const x = foilRand() * FW, y = foilRand() * FH;
ctx!.beginPath();
ctx!.moveTo(x, y);
ctx!.lineTo(x + (foilRand() * 20 - 10), y + (foilRand() * 8 - 4));
ctx!.stroke();
}
}
let press: Press | null = null;
function wipe() {
ctx!.globalCompositeOperation = "destination-out";
ctx!.beginPath();
ctx!.arc(press!.x, press!.y, press!.r, 0, Math.PI * 2);
ctx!.fill();
}
function clearedRatio() {
const data = ctx!.getImageData(0, 0, FW, FH).data;
let clear = 0;
const total = data.length / 4;
for (let i = 3; i < data.length; i += 4) { if (data[i] < 40) clear += 1; }
return clear / total;
}
let opened = false;
function openCard() {
if (opened) return;
opened = true;
ctx!.globalCompositeOperation = "destination-out";
ctx!.fillRect(0, 0, FW, FH);
root!.classList.add("is-open");
live!.textContent = "할인 쿠폰이 열렸어요";
}
function pos(e: PointerEvent) {
const r = foil!.getBoundingClientRect();
return { x: (e.clientX - r.left) * (FW / r.width), y: (e.clientY - r.top) * (FH / r.height) };
}
paintFoil();
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let holding = false;
function onDown(e: PointerEvent) {
if (opened) return;
root!.classList.remove("is-demo");
holding = true;
const p = pos(e);
press = { x: p.x, y: p.y, r: 4 };
wipe();
zone!.setPointerCapture?.(e.pointerId);
}
function onMove(e: PointerEvent) {
if (!holding || opened || !press) return;
const p = pos(e);
press.x = p.x; press.y = p.y;
}
function onUp() { holding = false; }
zone.addEventListener("pointerdown", onDown);
zone.addEventListener("pointermove", onMove);
window.addEventListener("pointerup", onUp);
function onReveal() { root!.classList.remove("is-demo"); openCard(); }
revealBtn.addEventListener("click", onReveal);
// 카드 끝까지 원형 확장 → 다시 덮임 → 다른 지점에서 재확장을 한 루프 안에서 반복한다.
const demoRand = seedRand(424042);
const DEMO_POINTS: Array<{ x: number; y: number }> = [
{ x: FW * (0.30 + demoRand() * 0.08), y: FH * (0.36 + demoRand() * 0.08) },
{ x: FW * (0.66 + demoRand() * 0.08), y: FH * (0.58 + demoRand() * 0.08) },
{ x: FW * (0.42 + demoRand() * 0.08), y: FH * (0.30 + demoRand() * 0.08) }
];
const DEMO_STARTS = [220, 950, 1680];
const DEMO_LOOP_MS = 3200;
let demoCycle = -1;
let demoPress: Press | null = null;
function resetDemo() {
opened = false;
root!.classList.remove("is-open");
paintFoil();
demoCycle = -1;
demoPress = null;
}
function runDemo(tMod: number, dtS: number) {
const next = demoCycle + 1;
if (next < DEMO_STARTS.length && tMod >= DEMO_STARTS[next]) {
demoCycle = next;
const p = DEMO_POINTS[demoCycle];
demoPress = { x: p.x, y: p.y, r: 4 };
opened = false;
root!.classList.remove("is-open");
paintFoil(); // 이전 사이클에서 열렸던 은박을 다시 덮는다
}
if (demoPress && !opened) {
demoPress.r += (WIPE_TARGET_R - demoPress.r) * Math.min(1, WIPE_LERP_RATE * dtS);
press = demoPress;
wipe();
if (clearedRatio() > .82) openCard();
}
}
if (reduce) root.classList.remove("is-demo");
let elapsed = 0;
let lastTs: number | null = null;
let raf = 0;
function tick(ts: number) {
if (lastTs === null) lastTs = ts;
const dt = Math.min(ts - lastTs, 48);
const dtS = dt / 1000;
lastTs = ts;
if (holding && !opened && press) {
press.r += (WIPE_TARGET_R - press.r) * Math.min(1, WIPE_LERP_RATE * dtS);
wipe();
if (clearedRatio() > .82) openCard();
}
if (!reduce && root!.classList.contains("is-demo")) {
elapsed += dt;
const tMod = elapsed % DEMO_LOOP_MS;
if (tMod < dt) resetDemo();
runDemo(tMod, dtS);
}
raf = requestAnimationFrame(tick);
}
raf = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(raf);
zone.removeEventListener("pointerdown", onDown);
zone.removeEventListener("pointermove", onMove);
window.removeEventListener("pointerup", onUp);
revealBtn.removeEventListener("click", onReveal);
};
}, []);
return (
<main className={styles.stage}>
<div className={`${styles.scv} is-demo`} ref={rootRef}>
<div className={styles.scv__card}>
<p className={styles.scv__label}>스크래치 카드</p>
<div className={styles.scv__zone} ref={zoneRef}>
<div className={styles.scv__prize}>
<p className={styles.scv__prizeTitle}>할인 쿠폰</p>
<p className={styles.scv__prizeSub}>누른 지점에서 원이 점점 커져요</p>
</div>
<canvas className={styles.scv__foil} ref={foilRef} width={280} height={150} aria-hidden="true" />
</div>
<button type="button" className={styles.scv__reveal} ref={revealBtnRef}>한 번에 보기</button>
<p className={styles.scv__live} ref={liveRef} aria-live="polite"></p>
</div>
</div>
</main>
);
}04 中心から円形に広がるリビール半径 — 計測 累積面積 23.424% · 動いたフレーム 23/23GODRICH オリジナル · SCSS と React は zip に同梱 · スクラッチカード リビール 9パターン — ピクセル計算で仕上げる
このデモが属する記事
スクラッチカード リビール 9パターン — ピクセル計算で仕上げる観察記録 09:00 — 指先が触れた瞬間、03番の箔の破片が重力を受けて飛び散る。09:04 — 06番の進行リングが62%で止まる。09:07 — 09番の画面上のカードが全部同時に開く。9パターンの「スクラッチカード」セットです。 この記事のデモをすべて見る


