07완전히 벗겨지면 터지는 컨페티
면적이 100%에 가까워지면 중력과 반발 속도를 가진 컨페티 조각들이 카드 위로 튀어 오른다.
쓰임당첨 확정 축하 연출
burstConfettistepConfettiCONFETTI_BOUNCE

<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>
<canvas class="scv__confetti" id="confetti" 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 *;
// 스크래치 카드 07 — 긁은 비율이 88% 를 넘으면 burstConfetti() 가 CONFETTI_COUNT 개를
// 한 번에 만들고 stepConfetti() 가 중력·바닥 반발(bounce)을 매 프레임 적분한다.
@include stage($stage-orange);
.scv { display: flex; justify-content: center; width: 100%; }
.scv__card {
width: min(100%, 300px); background: $subject-blue-deep; color: $subject-cream;
border-radius: $r-card; box-shadow: $shadow-raised; padding: $sp-4;
}
.scv__label { margin: 0 0 $sp-2; font-weight: 800; font-size: 12px; letter-spacing: .04em; }
.scv__zone {
position: relative; border-radius: $r-control; overflow: hidden;
aspect-ratio: 280 / 150; background: $stage-paper;
}
.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__confetti { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 2; }
.scv.is-open .scv__foil { visibility: hidden; }
.scv__reveal {
margin-top: $sp-2; 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-paper; outline-offset: 2px; }
.scv__live { margin: $sp-1 0 0; min-height: 1em; 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: #ff4d1f;
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%, 300px);
background: #0a4fe8;
color: #fff7e6;
border-radius: 16px;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
padding: 16px;
}
.scv__label {
margin: 0 0 8px;
font-weight: 800;
font-size: 12px;
letter-spacing: 0.04em;
}
.scv__zone {
position: relative;
border-radius: 12px;
overflow: hidden;
aspect-ratio: 280/150;
background: #ffffff;
}
.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__confetti {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: 2;
}
.scv.is-open .scv__foil {
visibility: hidden;
}
.scv__reveal {
margin-top: 8px;
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 #ffffff;
outline-offset: 2px;
}
.scv__live {
margin: 4px 0 0;
min-height: 1em;
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 () {
// 기법: 긁은 비율이 88% 를 넘는 순간 CONFETTI_COUNT 개의 색종이를 한 번에 만들어
// 중력으로 낙하시키고 카드 바닥에 닿으면 반발계수만큼 튕기게(bounce) 물리 적분한다.
var root = document.getElementById('scv');
var zone = document.getElementById('zone');
var foil = document.getElementById('foil');
var ctx = foil.getContext('2d');
var confettiCanvas = document.getElementById('confetti');
var cctx = confettiCanvas.getContext('2d');
var revealBtn = document.getElementById('revealBtn');
var live = document.getElementById('live');
var FW = foil.width, FH = foil.height;
var CONFETTI_COUNT = 24;
var CONFETTI_GRAVITY = 620;
var CONFETTI_BOUNCE = 0.42;
var SCRATCH_BRUSH_R = 15;
function seedRand(seed) {
var s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
var foilRand = seedRand(424071);
var confettiRand = seedRand(424072);
function paintFoil() {
ctx.globalCompositeOperation = 'source-over';
ctx.clearRect(0, 0, FW, FH);
var grad = ctx.createLinearGradient(0, 0, FW, FH);
grad.addColorStop(0, '#ff8a5c');
grad.addColorStop(.5, '#ffb488');
grad.addColorStop(1, '#e85f2a');
ctx.fillStyle = grad;
ctx.fillRect(0, 0, FW, FH);
ctx.strokeStyle = 'rgba(23,20,26,.16)';
ctx.lineWidth = 1;
for (var i = 0; i < 20; i += 1) {
var x = foilRand() * FW, y = foilRand() * FH;
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + (foilRand() * 22 - 11), y + (foilRand() * 8 - 4));
ctx.stroke();
}
}
function scratchAt(x, y) {
ctx.globalCompositeOperation = 'destination-out';
ctx.beginPath();
ctx.arc(x, y, SCRATCH_BRUSH_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 COLORS = ['#0a4fe8', '#fff7e6', '#ff4d1f', '#ffd23f'];
var confetti = [];
function burstConfetti() {
confetti = [];
for (var i = 0; i < CONFETTI_COUNT; i += 1) {
confetti.push({
x: FW * (0.3 + confettiRand() * 0.4),
y: FH * 0.3,
vx: (confettiRand() - .5) * 220,
vy: -180 - confettiRand() * 140,
rot: confettiRand() * Math.PI * 2,
angVel: (confettiRand() - .5) * 8,
w: 5 + confettiRand() * 3,
h: 3 + confettiRand() * 2,
color: COLORS[i % COLORS.length],
bounces: 0
});
}
}
function stepConfetti(dtMs) {
var dtS = dtMs / 1000;
for (var i = 0; i < confetti.length; i += 1) {
var c = confetti[i];
c.vy += CONFETTI_GRAVITY * dtS;
c.x += c.vx * dtS;
c.y += c.vy * dtS;
c.rot += c.angVel * dtS;
if (c.y > FH - 6 && c.vy > 0 && c.bounces < 3) {
c.y = FH - 6;
c.vy *= -CONFETTI_BOUNCE;
c.vx *= 0.7;
c.bounces += 1;
}
}
}
function drawConfetti() {
cctx.clearRect(0, 0, FW, FH);
for (var i = 0; i < confetti.length; i += 1) {
var c = confetti[i];
cctx.save();
cctx.translate(c.x, c.y);
cctx.rotate(c.rot);
cctx.fillStyle = c.color;
cctx.fillRect(-c.w / 2, -c.h / 2, c.w, c.h);
cctx.restore();
}
}
var opened = false, burst = 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 maybeBurst(ratio) {
if (!burst && ratio > .88) { burst = true; burstConfetti(); }
if (ratio > .96) openCard();
}
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 dragging = false;
zone.addEventListener('pointerdown', function (e) {
if (opened) return;
root.classList.remove('is-demo');
dragging = true;
var p = pos(e);
scratchAt(p.x, p.y);
maybeBurst(clearedRatio());
if (zone.setPointerCapture) zone.setPointerCapture(e.pointerId);
});
zone.addEventListener('pointermove', function (e) {
if (!dragging || opened) return;
var p = pos(e);
scratchAt(p.x, p.y);
maybeBurst(clearedRatio());
});
window.addEventListener('pointerup', function () { dragging = false; });
revealBtn.addEventListener('click', function () {
root.classList.remove('is-demo');
if (!burst) { burst = true; burstConfetti(); }
openCard();
});
var demoRand = seedRand(424073);
var demoPath = [];
(function buildDemoPath() {
for (var i = 0; i < 24; i += 1) {
demoPath.push({
t: 220 + i * 78,
x: (FW * 0.12) + (i % 6) * (FW * 0.15) + (demoRand() - .5) * 6,
y: (FH * 0.22) + Math.floor(i / 6) * (FH * 0.24) + (demoRand() - .5) * 6
});
}
})();
var DEMO_LOOP_MS = 4200;
var demoStep = 0;
function resetDemo() {
opened = false; burst = false;
root.classList.remove('is-open');
paintFoil();
confetti = [];
demoStep = 0;
}
function runDemo(tMod) {
while (demoStep < demoPath.length && demoPath[demoStep].t <= tMod) {
var p = demoPath[demoStep];
scratchAt(p.x, p.y);
demoStep += 1;
maybeBurst(clearedRatio());
}
}
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);
lastTs = ts;
if (!reduce && root.classList.contains('is-demo')) {
elapsed += dt;
var tMod = elapsed % DEMO_LOOP_MS;
if (tMod < dt) resetDemo();
runDemo(tMod);
}
if (!reduce) stepConfetti(dt);
drawConfetti();
requestAnimationFrame(tick);
}
requestAnimationFrame(tick);
})();import { useEffect, useRef } from "react";
import styles from "./ScratchCardRevealPrizeConfettiBurst.module.scss";
type Confetto = {
x: number; y: number; vx: number; vy: number; rot: number; angVel: number;
w: number; h: number; color: string; bounces: number;
};
export default function ScratchCardRevealPrizeConfettiBurst() {
const rootRef = useRef<HTMLDivElement>(null);
const zoneRef = useRef<HTMLDivElement>(null);
const foilRef = useRef<HTMLCanvasElement>(null);
const confettiRef = 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 confettiCanvas = confettiRef.current;
const revealBtn = revealBtnRef.current;
const live = liveRef.current;
if (!root || !zone || !foil || !confettiCanvas || !revealBtn || !live) return;
const ctx = foil.getContext("2d");
const cctx = confettiCanvas.getContext("2d");
if (!ctx || !cctx) return;
const FW = foil.width, FH = foil.height;
const CONFETTI_COUNT = 24;
const CONFETTI_GRAVITY = 620;
const CONFETTI_BOUNCE = 0.42;
const SCRATCH_BRUSH_R = 15;
function seedRand(seed: number) {
let s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
const foilRand = seedRand(424071);
const confettiRand = seedRand(424072);
function paintFoil() {
ctx!.globalCompositeOperation = "source-over";
ctx!.clearRect(0, 0, FW, FH);
const grad = ctx!.createLinearGradient(0, 0, FW, FH);
grad.addColorStop(0, "#ff8a5c");
grad.addColorStop(.5, "#ffb488");
grad.addColorStop(1, "#e85f2a");
ctx!.fillStyle = grad;
ctx!.fillRect(0, 0, FW, FH);
ctx!.strokeStyle = "rgba(23,20,26,.16)";
ctx!.lineWidth = 1;
for (let i = 0; i < 20; i += 1) {
const x = foilRand() * FW, y = foilRand() * FH;
ctx!.beginPath();
ctx!.moveTo(x, y);
ctx!.lineTo(x + (foilRand() * 22 - 11), y + (foilRand() * 8 - 4));
ctx!.stroke();
}
}
function scratchAt(x: number, y: number) {
ctx!.globalCompositeOperation = "destination-out";
ctx!.beginPath();
ctx!.arc(x, y, SCRATCH_BRUSH_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;
}
const COLORS = ["#0a4fe8", "#fff7e6", "#ff4d1f", "#ffd23f"];
let confetti: Confetto[] = [];
function burstConfetti() {
confetti = [];
for (let i = 0; i < CONFETTI_COUNT; i += 1) {
confetti.push({
x: FW * (0.3 + confettiRand() * 0.4),
y: FH * 0.3,
vx: (confettiRand() - .5) * 220,
vy: -180 - confettiRand() * 140,
rot: confettiRand() * Math.PI * 2,
angVel: (confettiRand() - .5) * 8,
w: 5 + confettiRand() * 3,
h: 3 + confettiRand() * 2,
color: COLORS[i % COLORS.length],
bounces: 0
});
}
}
function stepConfetti(dtMs: number) {
const dtS = dtMs / 1000;
for (let i = 0; i < confetti.length; i += 1) {
const c = confetti[i];
c.vy += CONFETTI_GRAVITY * dtS;
c.x += c.vx * dtS;
c.y += c.vy * dtS;
c.rot += c.angVel * dtS;
if (c.y > FH - 6 && c.vy > 0 && c.bounces < 3) {
c.y = FH - 6;
c.vy *= -CONFETTI_BOUNCE;
c.vx *= 0.7;
c.bounces += 1;
}
}
}
function drawConfetti() {
cctx!.clearRect(0, 0, FW, FH);
for (let i = 0; i < confetti.length; i += 1) {
const c = confetti[i];
cctx!.save();
cctx!.translate(c.x, c.y);
cctx!.rotate(c.rot);
cctx!.fillStyle = c.color;
cctx!.fillRect(-c.w / 2, -c.h / 2, c.w, c.h);
cctx!.restore();
}
}
let opened = false, burst = 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 maybeBurst(ratio: number) {
if (!burst && ratio > .88) { burst = true; burstConfetti(); }
if (ratio > .96) openCard();
}
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 dragging = false;
function onDown(e: PointerEvent) {
if (opened) return;
root!.classList.remove("is-demo");
dragging = true;
const p = pos(e);
scratchAt(p.x, p.y);
maybeBurst(clearedRatio());
zone!.setPointerCapture?.(e.pointerId);
}
function onMove(e: PointerEvent) {
if (!dragging || opened) return;
const p = pos(e);
scratchAt(p.x, p.y);
maybeBurst(clearedRatio());
}
function onUp() { dragging = false; }
zone.addEventListener("pointerdown", onDown);
zone.addEventListener("pointermove", onMove);
window.addEventListener("pointerup", onUp);
function onReveal() {
root!.classList.remove("is-demo");
if (!burst) { burst = true; burstConfetti(); }
openCard();
}
revealBtn.addEventListener("click", onReveal);
const demoRand = seedRand(424073);
type DemoPt = { t: number; x: number; y: number };
const demoPath: DemoPt[] = [];
for (let i = 0; i < 24; i += 1) {
demoPath.push({
t: 220 + i * 78,
x: (FW * 0.12) + (i % 6) * (FW * 0.15) + (demoRand() - .5) * 6,
y: (FH * 0.22) + Math.floor(i / 6) * (FH * 0.24) + (demoRand() - .5) * 6
});
}
const DEMO_LOOP_MS = 4200;
let demoStep = 0;
function resetDemo() {
opened = false; burst = false;
root!.classList.remove("is-open");
paintFoil();
confetti = [];
demoStep = 0;
}
function runDemo(tMod: number) {
while (demoStep < demoPath.length && demoPath[demoStep].t <= tMod) {
const p = demoPath[demoStep];
scratchAt(p.x, p.y);
demoStep += 1;
maybeBurst(clearedRatio());
}
}
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);
lastTs = ts;
if (!reduce && root!.classList.contains("is-demo")) {
elapsed += dt;
const tMod = elapsed % DEMO_LOOP_MS;
if (tMod < dt) resetDemo();
runDemo(tMod);
}
if (!reduce) stepConfetti(dt);
drawConfetti();
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" />
<canvas className={styles.scv__confetti} ref={confettiRef} 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>
);
}07 완전히 벗겨지면 터지는 컨페티 — 측정 누적면적 14.252% · 움직인 프레임 16/23GODRICH 원작 · zip 에 SCSS·React 포함 · 스크래치 카드 리빌 9가지 — 긁은 면적을 픽셀로 계산하기
이 데모가 속한 글
스크래치 카드 리빌 9가지 — 긁은 면적을 픽셀로 계산하기관찰 기록 09:00 — 손끝이 닿자마자 03번 은박 조각이 중력을 받아 튀어 나간다. 09:04 — 06번 진행 링이 62%에서 멈춰 채워지지 않는다. 09:07 — 09번 화면 속 카드 전부가 한 번에 열린다. 이 글의 데모 전부 보기


