05二本の指がそれぞれ消すマルチタッチスクラッチ
複数のポインターが同時に触れると、それぞれの経路を別々に追跡し同時に消す。
使いどころ友達と一緒に削るイベントカード
pointersscratchLineadvance

<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 *;
// 스크래치 카드 05 — pointerdown 마다 e.pointerId 를 Map 키로 저장해 여러 포인터의
// 마지막 좌표를 독립적으로 추적한다. 한 손가락의 move 가 다른 손가락 좌표를 덮지 않는다.
@include stage($stage-ink);
.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.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: #17141a;
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.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 () {
// 기법: pointerdown 마다 e.pointerId 를 키로 하는 Map 에 마지막 좌표를 저장해
// 여러 포인터(멀티터치)가 서로를 덮어쓰지 않고 각자의 경로를 동시에 긁게 한다.
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 SCRATCH_BRUSH_R = 14;
function seedRand(seed) {
var s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
var foilRand = seedRand(424051);
function paintFoil() {
ctx.globalCompositeOperation = 'source-over';
ctx.clearRect(0, 0, FW, FH);
var grad = ctx.createLinearGradient(0, 0, FW, FH);
grad.addColorStop(0, '#5b6cff');
grad.addColorStop(.5, '#8f9bff');
grad.addColorStop(1, '#3c46c9');
ctx.fillStyle = grad;
ctx.fillRect(0, 0, FW, FH);
ctx.strokeStyle = 'rgba(23,20,26,.16)';
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();
}
}
function scratchAt(x, y) {
ctx.globalCompositeOperation = 'destination-out';
ctx.beginPath();
ctx.arc(x, y, SCRATCH_BRUSH_R, 0, Math.PI * 2);
ctx.fill();
}
function scratchLine(x0, y0, x1, y1) {
var dx = x1 - x0, dy = y1 - y0;
var dist = Math.max(1, Math.hypot(dx, dy));
var steps = Math.max(1, Math.ceil(dist / 6));
for (var i = 0; i <= steps; i += 1) {
scratchAt(x0 + dx * (i / steps), y0 + dy * (i / steps));
}
}
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 pointers = new Map(); // pointerId -> {x,y}
zone.addEventListener('pointerdown', function (e) {
if (opened) return;
root.classList.remove('is-demo');
var p = pos(e);
pointers.set(e.pointerId, p);
scratchAt(p.x, p.y);
if (zone.setPointerCapture) zone.setPointerCapture(e.pointerId);
});
zone.addEventListener('pointermove', function (e) {
if (opened || !pointers.has(e.pointerId)) return;
var prev = pointers.get(e.pointerId);
var p = pos(e);
scratchLine(prev.x, prev.y, p.x, p.y);
pointers.set(e.pointerId, p);
if (clearedRatio() > .82) openCard();
});
function release(e) { pointers.delete(e.pointerId); }
window.addEventListener('pointerup', release);
window.addEventListener('pointercancel', release);
revealBtn.addEventListener('click', function () { root.classList.remove('is-demo'); openCard(); });
// 데모 경로 두 개를 서로 다른 pointerId(9001·9002)로 흉내내 동시 긁기를 재현한다
var demoRand = seedRand(424052);
function buildPath(baseX, baseY, count) {
var pts = [];
for (var i = 0; i < count; i += 1) {
pts.push({
t: 240 + i * 96,
x: baseX + (i % 4) * (FW * 0.12) + (demoRand() - .5) * 6,
y: baseY + Math.floor(i / 4) * (FH * 0.2) + (demoRand() - .5) * 6
});
}
return pts;
}
var pathA = buildPath(FW * 0.1, FH * 0.14, 12);
var pathB = buildPath(FW * 0.52, FH * 0.5, 12);
var DEMO_LOOP_MS = 4000;
var stepA = 0, stepB = 0;
var lastA = null, lastB = null;
function resetDemo() {
opened = false;
root.classList.remove('is-open');
paintFoil();
stepA = 0; stepB = 0; lastA = null; lastB = null;
}
function advance(path, stepRef, lastRef, tMod) {
var step = stepRef.v, last = lastRef.v;
while (step < path.length && path[step].t <= tMod) {
var p = path[step];
if (last) scratchLine(last.x, last.y, p.x, p.y); else scratchAt(p.x, p.y);
last = p;
step += 1;
}
stepRef.v = step; lastRef.v = last;
}
function runDemo(tMod) {
var sa = { v: stepA }, la = { v: lastA };
var sb = { v: stepB }, lb = { v: lastB };
advance(pathA, sa, la, tMod);
advance(pathB, sb, lb, tMod);
stepA = sa.v; lastA = la.v;
stepB = sb.v; lastB = lb.v;
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);
lastTs = ts;
if (!reduce && root.classList.contains('is-demo')) {
elapsed += dt;
var tMod = elapsed % DEMO_LOOP_MS;
if (tMod < dt) resetDemo();
runDemo(tMod);
}
requestAnimationFrame(tick);
}
requestAnimationFrame(tick);
})();import { useEffect, useRef } from "react";
import styles from "./ScratchCardRevealMultitouchPointer.module.scss";
type Pt = { x: number; y: number };
export default function ScratchCardRevealMultitouchPointer() {
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 SCRATCH_BRUSH_R = 14;
function seedRand(seed: number) {
let s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
const foilRand = seedRand(424051);
function paintFoil() {
ctx!.globalCompositeOperation = "source-over";
ctx!.clearRect(0, 0, FW, FH);
const grad = ctx!.createLinearGradient(0, 0, FW, FH);
grad.addColorStop(0, "#5b6cff");
grad.addColorStop(.5, "#8f9bff");
grad.addColorStop(1, "#3c46c9");
ctx!.fillStyle = grad;
ctx!.fillRect(0, 0, FW, FH);
ctx!.strokeStyle = "rgba(23,20,26,.16)";
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();
}
}
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 scratchLine(x0: number, y0: number, x1: number, y1: number) {
const dx = x1 - x0, dy = y1 - y0;
const dist = Math.max(1, Math.hypot(dx, dy));
const steps = Math.max(1, Math.ceil(dist / 6));
for (let i = 0; i <= steps; i += 1) scratchAt(x0 + dx * (i / steps), y0 + dy * (i / steps));
}
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;
const pointers = new Map<number, Pt>();
function onDown(e: PointerEvent) {
if (opened) return;
root!.classList.remove("is-demo");
const p = pos(e);
pointers.set(e.pointerId, p);
scratchAt(p.x, p.y);
zone!.setPointerCapture?.(e.pointerId);
}
function onMove(e: PointerEvent) {
if (opened || !pointers.has(e.pointerId)) return;
const prev = pointers.get(e.pointerId)!;
const p = pos(e);
scratchLine(prev.x, prev.y, p.x, p.y);
pointers.set(e.pointerId, p);
if (clearedRatio() > .82) openCard();
}
function release(e: PointerEvent) { pointers.delete(e.pointerId); }
zone.addEventListener("pointerdown", onDown);
zone.addEventListener("pointermove", onMove);
window.addEventListener("pointerup", release);
window.addEventListener("pointercancel", release);
function onReveal() { root!.classList.remove("is-demo"); openCard(); }
revealBtn.addEventListener("click", onReveal);
const demoRand = seedRand(424052);
type DemoPt = { t: number; x: number; y: number };
function buildPath(baseX: number, baseY: number, count: number) {
const pts: DemoPt[] = [];
for (let i = 0; i < count; i += 1) {
pts.push({
t: 240 + i * 96,
x: baseX + (i % 4) * (FW * 0.12) + (demoRand() - .5) * 6,
y: baseY + Math.floor(i / 4) * (FH * 0.2) + (demoRand() - .5) * 6
});
}
return pts;
}
const pathA = buildPath(FW * 0.1, FH * 0.14, 12);
const pathB = buildPath(FW * 0.52, FH * 0.5, 12);
const DEMO_LOOP_MS = 4000;
let stepA = 0, stepB = 0;
let lastA: DemoPt | null = null, lastB: DemoPt | null = null;
function resetDemo() {
opened = false;
root!.classList.remove("is-open");
paintFoil();
stepA = 0; stepB = 0; lastA = null; lastB = null;
}
function advance(path: DemoPt[], step: number, last: DemoPt | null, tMod: number): [number, DemoPt | null] {
while (step < path.length && path[step].t <= tMod) {
const p = path[step];
if (last) scratchLine(last.x, last.y, p.x, p.y); else scratchAt(p.x, p.y);
last = p;
step += 1;
}
return [step, last];
}
function runDemo(tMod: number) {
[stepA, lastA] = advance(pathA, stepA, lastA, tMod);
[stepB, lastB] = advance(pathB, stepB, lastB, tMod);
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);
lastTs = ts;
if (!reduce && root!.classList.contains("is-demo")) {
elapsed += dt;
const tMod = elapsed % DEMO_LOOP_MS;
if (tMod < dt) resetDemo();
runDemo(tMod);
}
raf = requestAnimationFrame(tick);
}
raf = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(raf);
zone.removeEventListener("pointerdown", onDown);
zone.removeEventListener("pointermove", onMove);
window.removeEventListener("pointerup", release);
window.removeEventListener("pointercancel", release);
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>
);
}05 二本の指がそれぞれ消すマルチタッチスクラッチ — 計測 累積面積 17.315% · 動いたフレーム 10/23GODRICH オリジナル · SCSS と React は zip に同梱 · スクラッチカード リビール 9パターン — ピクセル計算で仕上げる
このデモが属する記事
スクラッチカード リビール 9パターン — ピクセル計算で仕上げる観察記録 09:00 — 指先が触れた瞬間、03番の箔の破片が重力を受けて飛び散る。09:04 — 06番の進行リングが62%で止まる。09:07 — 09番の画面上のカードが全部同時に開く。9パターンの「スクラッチカード」セットです。 この記事のデモをすべて見る


