04잉크 번짐
클릭한 자리에 염료 밀도를 한 덩어리 심고 확산은 낮게·이류만 남기면 종이 위 잉크가 젖은 경계를 만들며 자라납니다. 밀도가 진할수록 느리게 번지게 곱셈 감쇠를 얹으면 번짐 단계가 보입니다.
쓰임클릭 반응 인터랙티브 배경
Math.exppointerdown

<main class="stage stage--paper">
<div class="fa" id="fa">
<p class="fa__hint">
<svg class="fa__ico" viewBox="0 0 256 256" aria-hidden="true"><g fill="currentColor"><path d="M180 208a52 52 0 0 1-104 0c0-44 52-120 52-120s52 76 52 120Z" opacity=".2"/><path d="M133.87 41.42a8 8 0 0 0-11.74 0C107.42 57.11 68 104.61 68 152a60 60 0 0 0 120 0c0-47.39-39.42-94.89-54.13-110.58ZM128 196a44 44 0 0 1-44-44c0-31.68 26.05-68.51 44-90.06 17.95 21.55 44 58.38 44 90.06a44 44 0 0 1-44 44Z"/></g></svg>
<span>종이를 클릭하면 그 자리에 염료 방울이 떨어져 젖은 경계를 그리며 번집니다</span>
</p>
<div class="fa__field" id="field" role="application" aria-label="잉크 번짐 실험판" tabindex="0">
<canvas class="fa__cv" id="cv"></canvas>
<b class="fa__badge" aria-hidden="true">32<span class="fa__badgeUnit">격자</span></b>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 잉크 번짐 — 움직임은 캔버스 rAF 루프 안에 있다. CSS 는 무대·판·배지만 맡는다. @keyframes 없음.
// 염료 팔레트는 어둡고 채도 낮은 회보라 3단(옅은 번짐 → 중간 → 진한 코어) — 종이 위 먹빛이라
// 채도를 처음부터 낮게 잡아 BD26 을 지킨다(세 색 모두 채도 <0.21).
$duration: $dur-loop;
$easing: $ease-spring;
$color: $subject-ink;
@include stage($stage-paper);
.fa {
--dye-0: #cdc8d6;
--dye-1: #8f89a3;
--dye-2: #3a3550;
display: flex; flex-direction: column; gap: $sp-2;
width: 100%; max-width: 424px;
}
.fa__hint {
display: flex; align-items: center; gap: $sp-1; margin: 0;
font-weight: 700; font-size: clamp(10px, 2.8vw, 12px); line-height: 1.35;
color: $subject-ink;
}
.fa__ico { width: 16px; height: 16px; flex: 0 0 auto; color: #8f89a3; }
.fa__field {
position: relative; height: 208px;
border-radius: $r-card; overflow: hidden;
background: $stage-paper;
box-shadow: inset 0 0 0 1px rgba($subject-ink, .14);
cursor: crosshair; touch-action: none;
}
.fa__field:focus-visible { outline: 2px solid #3a3550; outline-offset: 2px; }
.fa__cv { display: block; width: 100%; height: 100%; }
.fa__badge {
position: absolute; right: $sp-2; top: $sp-2;
display: flex; align-items: baseline; gap: $sp-1;
padding: $sp-1 $sp-2; border-radius: $r-pill;
background: rgba($subject-ink, .78);
color: $subject-cream; font-weight: 700; font-size: 11px;
}
.fa__badgeUnit { font-weight: 700; }
@media (max-width: 340px) {
.fa { gap: $sp-1; }
.fa__field { height: 124px; }
.fa__badge { font-size: 10px; padding: 0 $sp-1; }
}
// reduce — 루프가 멈춘 한 장이 남으므로 커서 신호만 바꾼다.
@media (prefers-reduced-motion: reduce) {
.fa.is-still .fa__field { cursor: default !important; }
}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);
}
.fa {
--dye-0: #cdc8d6;
--dye-1: #8f89a3;
--dye-2: #3a3550;
display: flex;
flex-direction: column;
gap: 8px;
width: 100%;
max-width: 424px;
}
.fa__hint {
display: flex;
align-items: center;
gap: 4px;
margin: 0;
font-weight: 700;
font-size: clamp(10px, 2.8vw, 12px);
line-height: 1.35;
color: #17141a;
}
.fa__ico {
width: 16px;
height: 16px;
flex: 0 0 auto;
color: #8f89a3;
}
.fa__field {
position: relative;
height: 208px;
border-radius: 16px;
overflow: hidden;
background: #ffffff;
box-shadow: inset 0 0 0 1px rgba(23, 20, 26, 0.14);
cursor: crosshair;
touch-action: none;
}
.fa__field:focus-visible {
outline: 2px solid #3a3550;
outline-offset: 2px;
}
.fa__cv {
display: block;
width: 100%;
height: 100%;
}
.fa__badge {
position: absolute;
right: 8px;
top: 8px;
display: flex;
align-items: baseline;
gap: 4px;
padding: 4px 8px;
border-radius: 999px;
background: rgba(23, 20, 26, 0.78);
color: #fff7e6;
font-weight: 700;
font-size: 11px;
}
.fa__badgeUnit {
font-weight: 700;
}
@media (max-width: 340px) {
.fa {
gap: 4px;
}
.fa__field {
height: 124px;
}
.fa__badge {
font-size: 10px;
padding: 0 4px;
}
}
@media (prefers-reduced-motion: reduce) {
.fa.is-still .fa__field {
cursor: default !important;
}
}// 잉크 번짐 — 확산은 낮게 두고 이류만 남긴다. 클릭한 자리에 밀도 한 덩어리(가우시안)를 심으면
// 종이 위 잉크가 젖은 경계를 그리며 자란다. 밀도가 진할수록 곱셈 감쇠(decay = 1 - k*density)로
// 더 천천히 번지게 해 번짐 단계가 눈에 보이게 한다 — 안정 유체 이류(01)와 같은 격자·advect 코드를 공유.
var root = document.getElementById('fa');
var field = document.getElementById('field');
var cv = document.getElementById('cv');
var ctx = cv.getContext('2d', { willReadFrequently: true });
var css = getComputedStyle(root);
var PAL0 = css.getPropertyValue('--dye-0').trim();
var PAL1 = css.getPropertyValue('--dye-1').trim();
var PAL2 = css.getPropertyValue('--dye-2').trim();
var N = 32;
var SIZE = (N + 2) * (N + 2);
var IX = function (x, y) { return x + (N + 2) * y; };
var u = new Float32Array(SIZE), v = new Float32Array(SIZE);
var u0 = new Float32Array(SIZE), v0 = new Float32Array(SIZE);
var d = new Float32Array(SIZE), d0 = new Float32Array(SIZE);
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var W = 0, H = 0, raf = 0, seedT = 0;
function fit() {
var r = field.getBoundingClientRect();
W = r.width; H = r.height;
cv.width = Math.round(W); cv.height = Math.round(H);
}
// 01 과 동일한 후방 이류(semi-Lagrangian) — 확산 없이 값을 옮기기만 해도 경계가 또렷하게 남는다.
function advect(b, dt) {
var src = b === 'u' ? u0 : (b === 'v' ? v0 : d0);
var dst = b === 'u' ? u : (b === 'v' ? v : d);
for (var j = 1; j <= N; j++) {
for (var i = 1; i <= N; i++) {
var x = i - dt * N * u0[IX(i, j)];
var y = j - dt * N * v0[IX(i, j)];
if (x < 0.5) x = 0.5; else if (x > N + 0.5) x = N + 0.5;
var i0 = x | 0; var i1 = i0 + 1;
if (y < 0.5) y = 0.5; else if (y > N + 0.5) y = N + 0.5;
var j0 = y | 0; var j1 = j0 + 1;
var s1 = x - i0, s0 = 1 - s1, t1 = y - j0, t0 = 1 - t1;
dst[IX(i, j)] = s0 * (t0 * src[IX(i0, j0)] + t1 * src[IX(i0, j1)]) +
s1 * (t0 * src[IX(i1, j0)] + t1 * src[IX(i1, j1)]);
}
}
}
// 아주 약한 확산 — 종이 섬유로 옆칸에 번지는 정도만. 값이 크면 잉크가 아니라 안개가 된다.
function diffuse(b, k, dt) {
var src = b === 'u' ? u : (b === 'v' ? v : d);
var a = dt * k * N * N;
for (var iter = 0; iter < 2; iter++) {
for (var j = 1; j <= N; j++) {
for (var i = 1; i <= N; i++) {
var idx = IX(i, j);
src[idx] = (src[idx] + a * (src[IX(i - 1, j)] + src[IX(i + 1, j)] + src[IX(i, j - 1)] + src[IX(i, j + 1)])) / (1 + 4 * a);
}
}
}
}
function bounds() {
for (var i = 1; i <= N; i++) {
d[IX(i, 0)] = d[IX(i, 1)] * 0.5; d[IX(i, N + 1)] = d[IX(i, N)] * 0.5;
u[IX(i, 0)] = u[IX(i, 1)] * 0.4; u[IX(i, N + 1)] = u[IX(i, N)] * 0.4;
v[IX(i, 0)] = v[IX(i, 1)] * 0.4; v[IX(i, N + 1)] = v[IX(i, N)] * 0.4;
}
for (var j = 1; j <= N; j++) {
d[IX(0, j)] = d[IX(1, j)] * 0.5; d[IX(N + 1, j)] = d[IX(N, j)] * 0.5;
u[IX(0, j)] = u[IX(1, j)] * 0.4; u[IX(N + 1, j)] = u[IX(N, j)] * 0.4;
v[IX(0, j)] = v[IX(1, j)] * 0.4; v[IX(N + 1, j)] = v[IX(N, j)] * 0.4;
}
}
// 방울 심기 — 밀도 덩어리를 반경 3~4칸 가우시안으로 심고, 살짝 바깥으로 밀어내는 속도를 얹어
// 원이 아니라 젖은 경계처럼 삐뚤빼뚤 자라게 한다.
function bloom(gx, gy, amp, jitter) {
var r = 3.6;
for (var j = -r; j <= r; j++) {
for (var i = -r; i <= r; i++) {
var q = (i * i + j * j) / (r * r);
if (q > 1) continue;
var f = Math.exp(-q * 2.4) * amp;
var ix = Math.round(gx + i), iy = Math.round(gy + j);
if (ix < 1 || ix > N || iy < 1 || iy > N) continue;
var idx = IX(ix, iy);
d[idx] = Math.min(2.4, d[idx] + f);
// 바깥쪽으로 밀어내는 속도 — 종이 섬유를 따라 삐뚤게(jitter)
var len = Math.sqrt(i * i + j * j) || 1;
var nx = i / len, ny = j / len;
var wob = Math.sin(iy * 1.7 + ix * 0.6 + jitter) * 0.4;
u[idx] += (nx + wob) * f * 0.045;
v[idx] += (ny - wob) * f * 0.045;
}
}
}
// 미리보기 — 시드 고정 좌표 세 곳에 2초(60스텝 @30fps)마다 번갈아 새 방울을 심는 결정적 패턴.
var seedSpots = [
{ x: N * 0.32, y: N * 0.38 },
{ x: N * 0.68, y: N * 0.56 },
{ x: N * 0.46, y: N * 0.74 }
];
var seedPeriod = 60; // step 단위(1/30 초 * 60 = 2초)
var lastSeedStep = -999;
var stepCount = 0;
function step(dt) {
if (stepCount - lastSeedStep >= seedPeriod) {
var spot = seedSpots[(stepCount / seedPeriod | 0) % seedSpots.length];
bloom(spot.x, spot.y, 1.7, seedT);
lastSeedStep = stepCount;
}
// 밀도가 진할수록 느리게 번지는 곱셈 감쇠 — decay = 1 - k*density. 진한 코어는 거의 안 움직이고
// 옅은 경계만 계속 퍼진다(단계적으로 보이는 이유).
for (var k = 0; k < SIZE; k++) {
var decay = 1 - Math.min(0.7, d[k] * 0.32);
u[k] *= decay; v[k] *= decay;
}
u0.set(u); v0.set(v); d0.set(d);
advect('u', dt); u0.set(u);
advect('v', dt); v0.set(v);
advect('d', dt);
diffuse('d', 0.0009, dt); // 확산은 아주 낮게 — 이류가 경계를 만드는 주역
bounds();
for (var m = 0; m < SIZE; m++) { d[m] *= 0.9985; u[m] *= 0.985; v[m] *= 0.985; }
seedT += dt;
stepCount++;
}
function render() {
var img = ctx.createImageData(cv.width, cv.height);
var px = img.data;
var cellW = cv.width / N, cellH = cv.height / N;
var pal = [PAL0, PAL1, PAL2];
for (var py = 0; py < cv.height; py++) {
var gy = Math.min(N - 1, (py / cellH) | 0);
for (var pxx = 0; pxx < cv.width; pxx++) {
var gx = Math.min(N - 1, (pxx / cellW) | 0);
var q = d[IX(gx + 1, gy + 1)];
var o = (py * cv.width + pxx) * 4;
if (q <= 0.02) { px[o] = 255; px[o + 1] = 255; px[o + 2] = 255; px[o + 3] = 255; continue; }
var band = q > 0.9 ? 2 : (q > 0.4 ? 1 : 0);
var c = pal[band];
var rr = parseInt(c.slice(1, 3), 16), gg = parseInt(c.slice(3, 5), 16), bb = parseInt(c.slice(5, 7), 16);
var a = Math.min(1, q * 0.85 + 0.15);
px[o] = 255 + (rr - 255) * a; px[o + 1] = 255 + (gg - 255) * a; px[o + 2] = 255 + (bb - 255) * a; px[o + 3] = 255;
}
}
ctx.putImageData(img, 0, 0);
}
function frame() {
step(1 / 30);
render();
raf = requestAnimationFrame(frame);
}
// 실제 사용 — pointerdown 으로 큰 방울, pointermove 로 이어 끄는 작은 방울
field.addEventListener('pointerdown', function (e) {
var r = field.getBoundingClientRect();
var gx = 1 + ((e.clientX - r.left) / r.width * (N - 1));
var gy = 1 + ((e.clientY - r.top) / r.height * (N - 1));
bloom(gx, gy, 2.0, seedT * 3.1);
if (reduce) render();
});
field.addEventListener('pointermove', function (e) {
if (e.pressure === 0 && e.buttons === 0) return;
var r = field.getBoundingClientRect();
var gx = 1 + ((e.clientX - r.left) / r.width * (N - 1));
var gy = 1 + ((e.clientY - r.top) / r.height * (N - 1));
bloom(gx, gy, 0.55, seedT * 3.1);
if (reduce) render();
});
field.addEventListener('keydown', function (e) {
if (e.key !== 'Enter' && e.key !== ' ') return;
e.preventDefault();
bloom(N * 0.5, N * 0.5, 2.0, seedT * 3.1);
if (reduce) render();
});
window.addEventListener('resize', function () { fit(); if (reduce) render(); });
fit();
if (reduce) {
for (var k = 0; k < 40; k++) step(1 / 30);
render();
} else {
raf = requestAnimationFrame(frame);
}// InkBloom — vanilla 판(run/308/demo/04-fluid-ink-bloom/index.html) 의 React 포트.
// 클릭한 자리에 밀도 한 덩어리를 심고 확산은 낮게·이류만 남겨 종이 위 잉크 번짐을 흉내낸다.
// 격자 필드는 ref 상자 안에서만 산다 — React 상태로 두면 리렌더가 매 프레임 일어나 60fps 예산을 못 맞춘다.
import { useEffect, useRef } from "react";
import type { CSSProperties } from "react";
import styles from "./InkBloom.module.scss";
export type InkBloomProps = {
duration?: string;
easing?: string;
color?: string;
};
type Vars = CSSProperties & { "--duration"?: string; "--easing"?: string; "--color"?: string };
type Box<T> = { current: T };
type KeyEvt = { key: string; preventDefault(): void };
type PtrEvt = { clientX: number; clientY: number; pressure?: number; buttons?: number };
const N = 32;
const SIZE = (N + 2) * (N + 2);
function IX(x: number, y: number): number { return x + (N + 2) * y; }
const SEED_SPOTS = [
{ x: N * 0.32, y: N * 0.38 },
{ x: N * 0.68, y: N * 0.56 },
{ x: N * 0.46, y: N * 0.74 },
];
const SEED_PERIOD = 60;
export default function InkBloom({
duration = "2s",
easing = "cubic-bezier(.2, .8, .2, 1)",
color = "#17141a",
}: InkBloomProps) {
const rootRef = useRef<HTMLDivElement | null>(null);
const fieldRef = useRef<HTMLDivElement | null>(null);
const cvRef = useRef<HTMLCanvasElement | null>(null);
const ctxRef = useRef<CanvasRenderingContext2D | null>(null);
const palRef = useRef(["#cdc8d6", "#8f89a3", "#3a3550"]) as Box<string[]>;
const u = useRef(new Float32Array(SIZE)) as Box<Float32Array>;
const v = useRef(new Float32Array(SIZE)) as Box<Float32Array>;
const u0 = useRef(new Float32Array(SIZE)) as Box<Float32Array>;
const v0 = useRef(new Float32Array(SIZE)) as Box<Float32Array>;
const d = useRef(new Float32Array(SIZE)) as Box<Float32Array>;
const d0 = useRef(new Float32Array(SIZE)) as Box<Float32Array>;
const seedTRef = useRef(0) as Box<number>;
const stepCountRef = useRef(0) as Box<number>;
const lastSeedStepRef = useRef(-999) as Box<number>;
const rafRef = useRef(0) as Box<number>;
const vars: Vars = { "--duration": duration, "--easing": easing, "--color": color };
function reduced(): boolean {
return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
}
function fit(): void {
const field = fieldRef.current;
const cv = cvRef.current;
if (!field || !cv) return;
const r = field.getBoundingClientRect();
cv.width = Math.round(r.width);
cv.height = Math.round(r.height);
}
function advect(b: "u" | "v" | "d", dt: number): void {
const src = b === "u" ? u0.current : b === "v" ? v0.current : d0.current;
const dst = b === "u" ? u.current : b === "v" ? v.current : d.current;
const uu = u0.current, vv = v0.current;
for (let j = 1; j <= N; j++) {
for (let i = 1; i <= N; i++) {
let x = i - dt * N * uu[IX(i, j)];
let y = j - dt * N * vv[IX(i, j)];
if (x < 0.5) x = 0.5; else if (x > N + 0.5) x = N + 0.5;
const i0 = x | 0, i1 = i0 + 1;
if (y < 0.5) y = 0.5; else if (y > N + 0.5) y = N + 0.5;
const j0 = y | 0, j1 = j0 + 1;
const s1 = x - i0, s0 = 1 - s1, t1 = y - j0, t0 = 1 - t1;
dst[IX(i, j)] = s0 * (t0 * src[IX(i0, j0)] + t1 * src[IX(i0, j1)]) +
s1 * (t0 * src[IX(i1, j0)] + t1 * src[IX(i1, j1)]);
}
}
}
function diffuse(b: "u" | "v" | "d", k: number, dt: number): void {
const src = b === "u" ? u.current : b === "v" ? v.current : d.current;
const a = dt * k * N * N;
for (let iter = 0; iter < 2; iter++) {
for (let j = 1; j <= N; j++) {
for (let i = 1; i <= N; i++) {
const idx = IX(i, j);
src[idx] = (src[idx] + a * (src[IX(i - 1, j)] + src[IX(i + 1, j)] + src[IX(i, j - 1)] + src[IX(i, j + 1)])) / (1 + 4 * a);
}
}
}
}
function bounds(): void {
const dd = d.current, uu = u.current, vv = v.current;
for (let i = 1; i <= N; i++) {
dd[IX(i, 0)] = dd[IX(i, 1)] * 0.5; dd[IX(i, N + 1)] = dd[IX(i, N)] * 0.5;
uu[IX(i, 0)] = uu[IX(i, 1)] * 0.4; uu[IX(i, N + 1)] = uu[IX(i, N)] * 0.4;
vv[IX(i, 0)] = vv[IX(i, 1)] * 0.4; vv[IX(i, N + 1)] = vv[IX(i, N)] * 0.4;
}
for (let j = 1; j <= N; j++) {
dd[IX(0, j)] = dd[IX(1, j)] * 0.5; dd[IX(N + 1, j)] = dd[IX(N, j)] * 0.5;
uu[IX(0, j)] = uu[IX(1, j)] * 0.4; uu[IX(N + 1, j)] = uu[IX(N, j)] * 0.4;
vv[IX(0, j)] = vv[IX(1, j)] * 0.4; vv[IX(N + 1, j)] = vv[IX(N, j)] * 0.4;
}
}
function bloom(gx: number, gy: number, amp: number, jitter: number): void {
const r = 3.6;
const uu = u.current, vv = v.current, dd = d.current;
for (let j = -r; j <= r; j++) {
for (let i = -r; i <= r; i++) {
const q = (i * i + j * j) / (r * r);
if (q > 1) continue;
const f = Math.exp(-q * 2.4) * amp;
const ix = Math.round(gx + i), iy = Math.round(gy + j);
if (ix < 1 || ix > N || iy < 1 || iy > N) continue;
const idx = IX(ix, iy);
dd[idx] = Math.min(2.4, dd[idx] + f);
const len = Math.sqrt(i * i + j * j) || 1;
const nx = i / len, ny = j / len;
const wob = Math.sin(iy * 1.7 + ix * 0.6 + jitter) * 0.4;
uu[idx] += (nx + wob) * f * 0.045;
vv[idx] += (ny - wob) * f * 0.045;
}
}
}
function step(dt: number): void {
const uu = u.current, vv = v.current, dd = d.current;
if (stepCountRef.current - lastSeedStepRef.current >= SEED_PERIOD) {
const spot = SEED_SPOTS[Math.floor(stepCountRef.current / SEED_PERIOD) % SEED_SPOTS.length];
bloom(spot.x, spot.y, 1.7, seedTRef.current);
lastSeedStepRef.current = stepCountRef.current;
}
for (let k = 0; k < SIZE; k++) {
const decay = 1 - Math.min(0.7, dd[k] * 0.32);
uu[k] *= decay; vv[k] *= decay;
}
u0.current.set(uu); v0.current.set(vv); d0.current.set(dd);
advect("u", dt); u0.current.set(u.current);
advect("v", dt); v0.current.set(v.current);
advect("d", dt);
diffuse("d", 0.0009, dt);
bounds();
const dd2 = d.current, uu2 = u.current, vv2 = v.current;
for (let m = 0; m < SIZE; m++) { dd2[m] *= 0.9985; uu2[m] *= 0.985; vv2[m] *= 0.985; }
seedTRef.current += dt;
stepCountRef.current++;
}
function render(): void {
const ctx = ctxRef.current, cv = cvRef.current;
if (!ctx || !cv) return;
const img = ctx.createImageData(cv.width, cv.height);
const px = img.data;
const cellW = cv.width / N, cellH = cv.height / N;
const pal = palRef.current;
const dd = d.current;
for (let py = 0; py < cv.height; py++) {
const gy = Math.min(N - 1, (py / cellH) | 0);
for (let pxx = 0; pxx < cv.width; pxx++) {
const gx = Math.min(N - 1, (pxx / cellW) | 0);
const q = dd[IX(gx + 1, gy + 1)];
const o = (py * cv.width + pxx) * 4;
if (q <= 0.02) { px[o] = 255; px[o + 1] = 255; px[o + 2] = 255; px[o + 3] = 255; continue; }
const band = q > 0.9 ? 2 : q > 0.4 ? 1 : 0;
const c = pal[band];
const rr = parseInt(c.slice(1, 3), 16), gg = parseInt(c.slice(3, 5), 16), bb = parseInt(c.slice(5, 7), 16);
const a = Math.min(1, q * 0.85 + 0.15);
px[o] = 255 + (rr - 255) * a; px[o + 1] = 255 + (gg - 255) * a; px[o + 2] = 255 + (bb - 255) * a; px[o + 3] = 255;
}
}
ctx.putImageData(img, 0, 0);
}
function frame(): void {
step(1 / 30);
render();
rafRef.current = requestAnimationFrame(frame);
}
function onDown(e: PtrEvt): void {
const field = fieldRef.current;
if (!field) return;
const r = field.getBoundingClientRect();
const gx = 1 + (e.clientX - r.left) / r.width * (N - 1);
const gy = 1 + (e.clientY - r.top) / r.height * (N - 1);
bloom(gx, gy, 2.0, seedTRef.current * 3.1);
if (reduced()) render();
}
function onMove(e: PtrEvt): void {
if ((e.pressure ?? 0) === 0 && (e.buttons ?? 0) === 0) return;
const field = fieldRef.current;
if (!field) return;
const r = field.getBoundingClientRect();
const gx = 1 + (e.clientX - r.left) / r.width * (N - 1);
const gy = 1 + (e.clientY - r.top) / r.height * (N - 1);
bloom(gx, gy, 0.55, seedTRef.current * 3.1);
if (reduced()) render();
}
function onKey(e: KeyEvt): void {
if (e.key !== "Enter" && e.key !== " ") return;
e.preventDefault();
bloom(N * 0.5, N * 0.5, 2.0, seedTRef.current * 3.1);
if (reduced()) render();
}
function onResize(): void { fit(); if (reduced()) render(); }
useEffect(() => {
const cv = cvRef.current;
if (!cv) return;
ctxRef.current = cv.getContext("2d", { willReadFrequently: true });
if (!ctxRef.current) return;
const root = rootRef.current;
if (root) {
const css = getComputedStyle(root);
const p0 = css.getPropertyValue("--dye-0").trim();
const p1 = css.getPropertyValue("--dye-1").trim();
const p2 = css.getPropertyValue("--dye-2").trim();
if (p0 && p1 && p2) palRef.current = [p0, p1, p2];
}
fit();
window.addEventListener("resize", onResize);
if (reduced()) {
if (root) root.classList.add("is-still");
for (let k = 0; k < 40; k++) step(1 / 30);
render();
} else {
rafRef.current = requestAnimationFrame(frame);
}
return () => {
window.removeEventListener("resize", onResize);
cancelAnimationFrame(rafRef.current);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<main className={styles.stage}>
<div className={styles.root} ref={rootRef} style={vars}>
<p className={styles.hint}>
<svg className={styles.ico} viewBox="0 0 256 256" aria-hidden="true"><g fill="currentColor"><path d="M180 208a52 52 0 0 1-104 0c0-44 52-120 52-120s52 76 52 120Z" opacity=".2"/><path d="M133.87 41.42a8 8 0 0 0-11.74 0C107.42 57.11 68 104.61 68 152a60 60 0 0 0 120 0c0-47.39-39.42-94.89-54.13-110.58ZM128 196a44 44 0 0 1-44-44c0-31.68 26.05-68.51 44-90.06 17.95 21.55 44 58.38 44 90.06a44 44 0 0 1-44 44Z"/></g></svg>
<span>종이를 클릭하면 그 자리에 염료 방울이 떨어져 젖은 경계를 그리며 번집니다</span>
</p>
<div
className={styles.field}
ref={fieldRef}
role="application"
aria-label="잉크 번짐 실험판"
tabIndex={0}
onPointerDown={onDown}
onPointerMove={onMove}
onKeyDown={onKey}
>
<canvas className={styles.cv} ref={cvRef}></canvas>
<b className={styles.badge} aria-hidden="true">32<span className={styles.badgeUnit}>격자</span></b>
</div>
</div>
</main>
);
}04 잉크 번짐 — 측정 누적면적 13.76% · 움직인 프레임 23/23GODRICH 원작 · zip 에 SCSS·React 포함 · 유체 시뮬레이션 9종 — 캔버스 연기·잉크 격자 계산 (WebGL 없이)
이 데모가 속한 글
유체 시뮬레이션 9종 — 캔버스 연기·잉크 격자 계산 (WebGL 없이)유체 시뮬레이션은 물이나 연기의 실제 나비에-스토크스 방정식을 그대로 푸는 게 아니라, 격자 위에 놓인 밀도·속도 배열을 매 프레임 이류하고 확산시키고 감쇠시켜 흐름을 흉내 내는 계산이다. 이 편은 라이브러리 없이 이 글의 데모 전부 보기


