05드래그 거리에 번지는 좋아요·싫어요 도장
카드를 끄는 만큼 좋아요·싫어요 도장의 불투명도가 비선형으로 진해진다.
쓰임취향 매칭 카드 판정 표시
STAMP_OPACITY_CURVE_POWERSTAMP_RANGE_PXRELEASE_VELOCITY_MIN

<main class="stage">
<div class="so is-demo" id="so">
<p class="so__caption">비선형 곡선으로 진해지는 좋아요·싫어요 도장</p>
<div class="so__scene" id="soScene">
<div class="so__card" id="soCard" tabindex="0" role="group" aria-roledescription="스와이프 카드">
<span class="so__stamp so__stamp--like" id="soLike" aria-hidden="true">LIKE</span>
<span class="so__stamp so__stamp--nope" id="soNope" aria-hidden="true">NOPE</span>
<p class="so__label" id="soLabel">여행 메모</p>
</div>
<button class="so__btn so__btn--nope" id="soBtnNope" type="button" aria-label="싫어요">✕</button>
<button class="so__btn so__btn--like" id="soBtnLike" type="button" aria-label="좋아요">♥</button>
</div>
<p class="so__status" id="soStatus" aria-live="polite">대기 중</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 값 하나: 드래그 거리를 STAMP_OPACITY_CURVE_POWER 승으로 매핑해 LIKE·NOPE 도장이
// 비선형(중심 근처 느림 → 임계 근처 급함)으로 진해진다.
$duration: $dur-base;
$easing: $ease-out;
$color: $subject-mint;
@include stage($stage-paper);
.so { display: grid; gap: $sp-2; justify-items: center; width: 100%; }
.so__caption { margin: 0; font-size: 12px; font-weight: 600; color: $subject-ink; }
.so__scene {
position: relative;
width: 220px;
height: 190px;
display: grid;
place-items: center;
}
.so__card {
position: relative;
width: 160px;
height: 180px;
border-radius: $r-card;
background: linear-gradient(160deg, $subject-cream, $subject-sky);
box-shadow: $shadow-raised;
display: grid;
place-items: center;
touch-action: none;
cursor: grab;
will-change: transform;
overflow: hidden;
}
.so__card:active { cursor: grabbing; }
.so__card:focus-visible { outline: 2px solid $subject-blue-light; outline-offset: 3px; }
.so__label { margin: 0; font-size: 14px; font-weight: 700; color: $subject-ink; padding: 0 $sp-3; text-align: center; }
.so__stamp {
position: absolute;
top: $sp-3;
border: 3px solid currentColor;
border-radius: $r-xs;
padding: $sp-1 $sp-2;
font-size: 13px;
font-weight: 800;
letter-spacing: .08em;
opacity: 0;
pointer-events: none;
}
.so__stamp--like { left: $sp-3; color: $subject-mint; transform: rotate(-14deg); }
.so__stamp--nope { right: $sp-3; color: $error; transform: rotate(14deg); }
.so__btn {
position: absolute;
bottom: $sp-3;
width: 34px;
height: 34px;
border-radius: $r-pill;
border: 0;
background: $stage-paper;
box-shadow: $shadow-press;
font-size: 15px;
line-height: 1;
cursor: pointer;
display: grid;
place-items: center;
z-index: 2;
}
.so__btn--nope { left: $sp-3; color: $error; }
.so__btn--like { right: $sp-3; color: $subject-mint; }
.so__btn:focus-visible { outline: 2px solid $subject-blue-light; outline-offset: 2px; }
.so__status { margin: 0; font-size: 11px; color: $subject-ink; font-weight: 600; }
@media (max-width: 340px) {
.so { gap: $sp-2; }
.so__scene { width: 150px; height: 108px; }
.so__card { width: 78px; height: 92px; }
.so__label { font-size: 10px; padding: 0 $sp-1; }
.so__stamp { font-size: 9px; padding: 0 $sp-1; top: $sp-1; }
.so__btn { width: 22px; height: 22px; font-size: 11px; bottom: $sp-1; }
.so__btn--nope { left: $sp-1; }
.so__btn--like { right: $sp-1; }
}
@media (prefers-reduced-motion: reduce) {
.so__card { transition: none; }
}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);
}
.so {
display: grid;
gap: 8px;
justify-items: center;
width: 100%;
}
.so__caption {
margin: 0;
font-size: 12px;
font-weight: 600;
color: #17141a;
}
.so__scene {
position: relative;
width: 220px;
height: 190px;
display: grid;
place-items: center;
}
.so__card {
position: relative;
width: 160px;
height: 180px;
border-radius: 16px;
background: linear-gradient(160deg, #fff7e6, #6ec8ff);
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
display: grid;
place-items: center;
touch-action: none;
cursor: grab;
will-change: transform;
overflow: hidden;
}
.so__card:active {
cursor: grabbing;
}
.so__card:focus-visible {
outline: 2px solid #5b8cf8;
outline-offset: 3px;
}
.so__label {
margin: 0;
font-size: 14px;
font-weight: 700;
color: #17141a;
padding: 0 12px;
text-align: center;
}
.so__stamp {
position: absolute;
top: 12px;
border: 3px solid currentColor;
border-radius: 4px;
padding: 4px 8px;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.08em;
opacity: 0;
pointer-events: none;
}
.so__stamp--like {
left: 12px;
color: #4cd4a6;
transform: rotate(-14deg);
}
.so__stamp--nope {
right: 12px;
color: #c2361a;
transform: rotate(14deg);
}
.so__btn {
position: absolute;
bottom: 12px;
width: 34px;
height: 34px;
border-radius: 999px;
border: 0;
background: #ffffff;
box-shadow: 0 6px 12px rgba(23, 20, 26, 0.32);
font-size: 15px;
line-height: 1;
cursor: pointer;
display: grid;
place-items: center;
z-index: 2;
}
.so__btn--nope {
left: 12px;
color: #c2361a;
}
.so__btn--like {
right: 12px;
color: #4cd4a6;
}
.so__btn:focus-visible {
outline: 2px solid #5b8cf8;
outline-offset: 2px;
}
.so__status {
margin: 0;
font-size: 11px;
color: #17141a;
font-weight: 600;
}
@media (max-width: 340px) {
.so {
gap: 8px;
}
.so__scene {
width: 150px;
height: 108px;
}
.so__card {
width: 78px;
height: 92px;
}
.so__label {
font-size: 10px;
padding: 0 4px;
}
.so__stamp {
font-size: 9px;
padding: 0 4px;
top: 4px;
}
.so__btn {
width: 22px;
height: 22px;
font-size: 11px;
bottom: 4px;
}
.so__btn--nope {
left: 4px;
}
.so__btn--like {
right: 4px;
}
}
@media (prefers-reduced-motion: reduce) {
.so__card {
transition: none;
}
}(function () {
"use strict";
// 값 하나: 드래그 거리를 STAMP_OPACITY_CURVE_POWER 승으로 매핑해 도장이 선형이 아니라
// 중심 근처에선 느리게, 임계 근처에선 급하게 진해진다.
var STAMP_OPACITY_CURVE_POWER = 2.4;
var STAMP_RANGE_PX = 150;
var RELEASE_VELOCITY_MIN = 0.38;
var EXIT_PX = 320;
var CYCLE_S = 2;
var root = document.getElementById("so");
var card = document.getElementById("soCard");
var label = document.getElementById("soLabel");
var like = document.getElementById("soLike");
var nope = document.getElementById("soNope");
var status = document.getElementById("soStatus");
var btnLike = document.getElementById("soBtnLike");
var btnNope = document.getElementById("soBtnNope");
var reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
function seedRand(seed) {
var s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function clamp(v, lo, hi) { return Math.max(lo, Math.min(hi, v)); }
var CARDS = ["여행 메모", "아침 루틴", "저녁 산책", "주말 카페", "홈트 기록", "독서 목록"];
var rand = seedRand(64);
var idx = Math.floor(rand() * CARDS.length);
var dragging = false;
var startX = 0, lastX = 0, lastT = 0, vx = 0;
var x = 0, phase = "idle", exitDir = 1;
function setCard() { label.textContent = CARDS[idx % CARDS.length]; }
setCard();
// 비선형 매핑: ratio^POWER — 중심 근처는 낮게 눌려 있다가 끝에서 급격히 1 로 치솟는다.
function stampOpacity(px) {
var ratio = clamp(Math.abs(px) / STAMP_RANGE_PX, 0, 1);
return Math.pow(ratio, STAMP_OPACITY_CURVE_POWER);
}
function render() {
var rot = clamp(x * 0.07, -18, 18);
card.style.transform = "translateX(" + x.toFixed(1) + "px) rotate(" + rot.toFixed(2) + "deg)";
var op = stampOpacity(x);
like.style.opacity = x > 0 ? op.toFixed(3) : "0";
nope.style.opacity = x < 0 ? op.toFixed(3) : "0";
}
function nextCard() {
idx += 1; setCard();
x = 0; vx = 0; phase = "idle";
render();
status.textContent = "대기 중";
}
function judge(velocity, dir) {
root.classList.remove("is-demo");
var flung = Math.abs(velocity) >= RELEASE_VELOCITY_MIN;
if (reduce) {
if (flung) { status.textContent = dir > 0 ? "좋아요 처리됨" : "싫어요 처리됨"; nextCard(); }
else { x = 0; render(); status.textContent = "제자리로 돌아옴"; phase = "idle"; }
return;
}
if (flung) { phase = "exit"; exitDir = dir; status.textContent = dir > 0 ? "좋아요 처리됨" : "싫어요 처리됨"; }
else { phase = "settle"; status.textContent = "제자리로 돌아옴"; }
}
function startDrag(clientX) {
root.classList.remove("is-demo");
dragging = true; phase = "idle";
startX = clientX; lastX = clientX; lastT = performance.now(); vx = 0;
}
function moveDrag(clientX) {
if (!dragging) return;
var now = performance.now();
var dt = Math.max(now - lastT, 1);
vx = (clientX - lastX) / dt;
lastX = clientX; lastT = now;
x = clientX - startX;
render();
}
function endDrag() {
if (!dragging) return;
dragging = false;
judge(vx, x >= 0 ? 1 : -1);
}
card.addEventListener("pointerdown", function (e) { card.setPointerCapture(e.pointerId); startDrag(e.clientX); });
card.addEventListener("pointermove", function (e) { moveDrag(e.clientX); });
card.addEventListener("pointerup", endDrag);
card.addEventListener("pointercancel", endDrag);
function keyVerdict(dir) {
root.classList.remove("is-demo");
x = dir * 60; render();
judge(dir * (RELEASE_VELOCITY_MIN + 0.3), dir);
}
card.addEventListener("keydown", function (e) {
if (e.key === "ArrowRight") { e.preventDefault(); keyVerdict(1); }
else if (e.key === "ArrowLeft") { e.preventDefault(); keyVerdict(-1); }
});
btnLike.addEventListener("click", function () { keyVerdict(1); });
btnNope.addEventListener("click", function () { keyVerdict(-1); });
function step(dt) {
if (phase === "exit") {
x += exitDir * Math.max(Math.abs(vx), RELEASE_VELOCITY_MIN * 1.4) * dt;
render();
if (Math.abs(x) > EXIT_PX) nextCard();
} else if (phase === "settle") {
x += (0 - x) * 0.22;
render();
if (Math.abs(x) < 0.4) { x = 0; phase = "idle"; render(); }
}
}
// .is-demo 자동 시연: 실제 pointer 없이 x·judge() 를 같은 경로로 호출한다.
var start = null, lastNow = 0, prevT = 0, synthPhase = "wait";
function synth(t) {
if (t > 0.4 && t < 0.7 && synthPhase === "wait") synthPhase = "peekLike";
if (synthPhase === "peekLike") {
var p = clamp((t - 0.4) / 0.3, 0, 1);
x = p * 90; render();
if (t >= 0.7) { synthPhase = "afterPeek"; judge(0.1, 1); }
}
if (t > 1.25 && t < 1.55 && synthPhase === "afterPeek") synthPhase = "commit";
if (synthPhase === "commit") {
var q = clamp((t - 1.25) / 0.3, 0, 1);
x = -q * 150; render();
if (t >= 1.55) { synthPhase = "done"; judge(-(RELEASE_VELOCITY_MIN + 0.4), -1); }
}
}
function loop(now) {
if (start === null) { start = now; lastNow = now; lastT = now; }
var dt = Math.min(now - lastNow, 48);
lastNow = now;
var t = ((now - start) / 1000) % CYCLE_S;
if (t < prevT) synthPhase = "wait";
prevT = t;
if (!reduce && root.classList.contains("is-demo")) synth(t);
if (!reduce) step(dt);
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
})();// StampOpacityDragSwipeCard — vanilla 판(run/422/demo/05-swipe-deck-precision-stamp-opacity-drag/index.html) 의 React 포트.
// 값 하나: LIKE/NOPE 스탬프의 불투명도는 이동거리에 선형이 아니라
// (|px|/STAMP_RANGE_PX) 를 STAMP_OPACITY_CURVE_POWER 제곱한 값이다(초반은 거의 안 보이다 막판에 확 짙어짐).
import { useEffect, useRef, useState } from "react";
import type { PointerEvent as ReactPointerEvent } from "react";
import styles from "./StampOpacityDragSwipeCard.module.scss";
type Box<T> = { current: T };
const STAMP_OPACITY_CURVE_POWER = 2.4;
const STAMP_RANGE_PX = 150;
const RELEASE_VELOCITY_MIN = 0.4;
const EXIT_PX = 320;
const CYCLE_S = 2;
const CARDS = ["아침 루틴", "저녁 산책", "주말 카페", "홈트 기록", "독서 목록", "여행 메모"];
function seedRand(seed: number) {
let s = seed >>> 0;
return function (): number {
s = (s * 1664525 + 1013904223) >>> 0;
return s / 4294967296;
};
}
function clamp(v: number, lo: number, hi: number): number {
return Math.max(lo, Math.min(hi, v));
}
function stampOpacity(px: number): number {
return Math.pow(clamp(Math.abs(px) / STAMP_RANGE_PX, 0, 1), STAMP_OPACITY_CURVE_POWER);
}
export default function StampOpacityDragSwipeCard() {
const cardRef = useRef<HTMLDivElement | null>(null);
const likeRef = useRef<HTMLSpanElement | null>(null);
const nopeRef = useRef<HTMLSpanElement | null>(null);
const posRef = useRef<number>(0) as Box<number>;
const velRef = useRef<number>(0) as Box<number>;
const phaseRef = useRef<string>("idle") as Box<string>;
const exitDirRef = useRef<number>(1) as Box<number>;
const draggingRef = useRef<boolean>(false) as Box<boolean>;
const startXRef = useRef<number>(0) as Box<number>;
const lastXRef = useRef<number>(0) as Box<number>;
const lastTRef = useRef<number>(0) as Box<number>;
const idxRef = useRef<number>(Math.floor(seedRand(53)() * CARDS.length)) as Box<number>;
const frameRef = useRef<number>(0) as Box<number>;
const [label, setLabel] = useState(CARDS[idxRef.current % CARDS.length]);
const [status, setStatus] = useState("대기 중");
const [demo, setDemo] = useState(true);
const [reduce, setReduce] = useState(false);
function render(): void {
const card = cardRef.current;
const like = likeRef.current;
const nope = nopeRef.current;
if (!card || !like || !nope) return;
const rot = clamp(posRef.current * 0.05, -16, 16);
card.style.transform = "translateX(" + posRef.current.toFixed(1) + "px) rotate(" + rot.toFixed(2) + "deg)";
const o = stampOpacity(posRef.current);
if (posRef.current > 0) { like.style.opacity = o.toFixed(3); nope.style.opacity = "0"; }
else { nope.style.opacity = o.toFixed(3); like.style.opacity = "0"; }
}
function nextCard(): void {
idxRef.current += 1;
setLabel(CARDS[idxRef.current % CARDS.length]);
posRef.current = 0; velRef.current = 0; phaseRef.current = "idle";
render();
setStatus("대기 중");
}
function judge(velocity: number, dir: number): void {
setDemo(false);
const flung = Math.abs(velocity) >= RELEASE_VELOCITY_MIN;
if (reduce) {
if (flung) { setStatus(dir > 0 ? "좋아요 처리됨" : "싫어요 처리됨"); nextCard(); }
else { posRef.current = 0; render(); setStatus("제자리로 돌아옴"); phaseRef.current = "idle"; }
return;
}
if (flung) {
phaseRef.current = "exit"; exitDirRef.current = dir;
setStatus(dir > 0 ? "좋아요 처리됨" : "싫어요 처리됨");
} else {
phaseRef.current = "settle";
setStatus("제자리로 돌아옴");
}
}
function startDrag(clientX: number): void {
setDemo(false);
draggingRef.current = true; phaseRef.current = "idle";
startXRef.current = clientX; lastXRef.current = clientX; lastTRef.current = performance.now(); velRef.current = 0;
}
function moveDrag(clientX: number): void {
if (!draggingRef.current) return;
const now = performance.now();
const dt = Math.max(now - lastTRef.current, 1);
velRef.current = (clientX - lastXRef.current) / dt;
lastXRef.current = clientX; lastTRef.current = now;
posRef.current = clientX - startXRef.current;
render();
}
function endDrag(): void {
if (!draggingRef.current) return;
draggingRef.current = false;
judge(velRef.current, posRef.current >= 0 ? 1 : -1);
}
function onPointerDown(e: ReactPointerEvent<HTMLDivElement>): void {
e.currentTarget.setPointerCapture(e.pointerId);
startDrag(e.clientX);
}
function onPointerMove(e: ReactPointerEvent<HTMLDivElement>): void { moveDrag(e.clientX); }
function onPointerUp(): void { endDrag(); }
function keyVerdict(dir: number): void {
setDemo(false);
posRef.current = dir * 40; render();
judge(dir * (RELEASE_VELOCITY_MIN + 0.2), dir);
}
function onKeyDown(e: any): void {
if (e.key === "ArrowRight") { e.preventDefault(); keyVerdict(1); }
else if (e.key === "ArrowLeft") { e.preventDefault(); keyVerdict(-1); }
}
function step(dt: number): void {
if (phaseRef.current === "exit") {
const speed = Math.max(Math.abs(velRef.current), RELEASE_VELOCITY_MIN * 1.4);
posRef.current += exitDirRef.current * speed * dt;
render();
if (Math.abs(posRef.current) > EXIT_PX) nextCard();
} else if (phaseRef.current === "settle") {
posRef.current += (0 - posRef.current) * 0.22;
render();
if (Math.abs(posRef.current) < 0.5) { posRef.current = 0; phaseRef.current = "idle"; render(); }
}
}
function synth(t: number, synthPhaseRef: Box<string>): void {
if (t > 0.4 && t < 0.75 && synthPhaseRef.current === "wait") synthPhaseRef.current = "toLike";
if (synthPhaseRef.current === "toLike") {
const p = clamp((t - 0.4) / 0.35, 0, 1);
posRef.current = p * 130; render();
if (t >= 0.75) { synthPhaseRef.current = "afterLike"; judge(RELEASE_VELOCITY_MIN + 0.3, 1); }
}
if (t > 1.3 && t < 1.65 && synthPhaseRef.current === "afterLike") synthPhaseRef.current = "toNope";
if (synthPhaseRef.current === "toNope") {
const q = clamp((t - 1.3) / 0.35, 0, 1);
posRef.current = -q * 130; render();
if (t >= 1.65) { synthPhaseRef.current = "done"; judge(-(RELEASE_VELOCITY_MIN + 0.3), -1); }
}
}
useEffect(() => {
setReduce(window.matchMedia("(prefers-reduced-motion: reduce)").matches);
}, []);
useEffect(() => {
let start = 0, lastNow = 0, prevT = 0;
const synthPhaseRef: Box<string> = { current: "wait" };
function loop(now: number): void {
if (start === 0) { start = now; lastNow = now; }
const dt = Math.min(now - lastNow, 48);
lastNow = now;
const t = ((now - start) / 1000) % CYCLE_S;
if (t < prevT) synthPhaseRef.current = "wait";
prevT = t;
if (!reduce && demo) synth(t, synthPhaseRef);
if (!reduce) step(dt);
frameRef.current = requestAnimationFrame(loop);
}
frameRef.current = requestAnimationFrame(loop);
return () => cancelAnimationFrame(frameRef.current);
}, [reduce, demo]);
return (
<main className={styles.stage}>
<div className={styles.so}>
<p className={styles.caption}>제곱 곡선으로 짙어지는 판정 스탬프</p>
<div className={styles.scene}>
<div
className={styles.card}
ref={cardRef}
tabIndex={0}
role="group"
aria-roledescription="스와이프 카드"
onPointerDown={onPointerDown}
onPointerMove={onPointerMove}
onPointerUp={onPointerUp}
onKeyDown={onKeyDown}
>
<span className={styles.stampLike} ref={likeRef} aria-hidden="true">LIKE</span>
<span className={styles.stampNope} ref={nopeRef} aria-hidden="true">NOPE</span>
<p className={styles.label}>{label}</p>
</div>
<button className={styles.btnNope} type="button" aria-label="싫어요" onClick={() => keyVerdict(-1)}>✕</button>
<button className={styles.btnLike} type="button" aria-label="좋아요" onClick={() => keyVerdict(1)}>♥</button>
</div>
<p className={styles.status} aria-live="polite">{status}</p>
</div>
</main>
);
}05 드래그 거리에 번지는 좋아요·싫어요 도장 — 측정 누적면적 24.239% · 움직인 프레임 6/23GODRICH 원작 · zip 에 SCSS·React 포함 · 카드 스와이프 ui 9가지 — 드래그를 물리 계산으로 다듬기
이 데모가 속한 글
카드 스와이프 ui 9가지 — 드래그를 물리 계산으로 다듬기카드 스와이프 ui 아홉 개를 모아 놓고 봤다. 엄지 끝 속도가 02번 기준을 넘지 못한 순간, 카드는 손을 놓아도 날아가지 않고 03번처럼 스프링으로 튕겨 제자리에 앉는다. 마지막 장까지 넘긴 자리엔 08번 빈 카 이 글의 데모 전부 보기


