03Torch — a flame carrying a wind vector on its back
Adding a slow wobble to a steady wind vector sweeps the whole flame to one side — the leaning blaze of a carried torch.
Best forCeremonial torches for game and event pages
fillRectglobalCompositeOperationrequestAnimationFrame

<main class="stage">
<div class="tc is-demo" id="tcRoot">
<p class="tc__hint">
<svg class="tc__ico" viewBox="0 0 256 256" aria-hidden="true"><g fill="currentColor"><path d="M128 24a16 16 0 0 1 16 16v8a80.06 80.06 0 0 1 63.94 63.94h8a16 16 0 0 1 0 32h-8A80.06 80.06 0 0 1 144 207.94v8a16 16 0 0 1-32 0v-8A80.06 80.06 0 0 1 48.06 144h-8a16 16 0 0 1 0-32h8A80.06 80.06 0 0 1 112 48.06v-8A16 16 0 0 1 128 24m-16 57.35V96a16 16 0 0 0 32 0V81.35A48.09 48.09 0 0 0 81.35 112H96a16 16 0 0 0 0-32h14.65M174.65 144H160a16 16 0 0 0 0 32h14.65A48.09 48.09 0 0 0 144 174.65V160a16 16 0 0 0-32 0v14.65A48.09 48.09 0 0 0 81.35 144" opacity=".2"/><path d="M232 128a104 104 1 1 1-104-104a104 104 0 0 1 104 104m-32 0a8 8 0 0 0-8-8h-9.34A56.1 56.1 0 0 0 136 77.34V68a8 8 0 0 0-16 0v9.34A56.1 56.1 0 0 0 77.34 120H68a8 8 0 0 0 0 16h9.34A56.1 56.1 0 0 0 120 179.66V188a8 8 0 0 0 16 0v-8.34A56.1 56.1 0 0 0 178.66 136H192a8 8 0 0 0 8-8m-40 0a32 32 0 1 1-32-32a32 32 0 0 1 32 32m-16 0a16 16 0 1 0-16 16a16 16 0 0 0 16-16"/></g></svg>
<span>A steady wind vector with a slow sine wobble sweeps the whole flame to one side — pointer x is wind strength, y is direction</span>
</p>
<div class="tc__field" id="tcField" role="application" aria-label="Torch wind vector board" tabindex="0">
<canvas class="tc__cv" id="tcCv"></canvas>
</div>
<div class="tc__bar">
<span class="tc__meta">wind <b class="tc__val" id="tcW">0.62</b> · sin-based wobble</span>
<span class="tc__meta tc__meta--dim">requestAnimationFrame · color-temp ladder</span>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 횃불(03). CSS 애니메이션·@keyframes 를 아예 두지 않는다 —
// 움직임은 전부 캔버스 rAF 루프(일정 바람 벡터 + 느린 sin 요동 + 색온도 냉각)가 만든다.
// 무대는 잉크(#241b1d — 어두운 야외). 파스텔 고정 팔레트를 --fire-* 로 편 공통 규격(01~09 동일).
$duration: $dur-loop;
$easing: $ease-spring;
$color: $subject-pink;
@include stage($stage-ink);
// ── 편 공통 파스텔 팔레트(BD9~11 원칙: 편마다 다시 정하지 않는다) ──
$fire-ink: #241b1d; // 배경 — 어두운 실내
$fire-amber: #f0a868; // 불꽃 코어
$fire-gold: #f6cf8a; // 불꽃 팁
$fire-rose: #eca0a8; // 로즈 변형
$fire-ember: #d97a52; // 잔불
$fire-smoke: #c9c2c6; // 연기
.tc {
// 렌더러가 읽는 색 — JS 는 getComputedStyle 로 --fire-* 를 받아 쓴다
--fire-ink: #{$fire-ink};
--fire-amber: #{$fire-amber};
--fire-gold: #{$fire-gold};
--fire-rose: #{$fire-rose};
--fire-ember: #{$fire-ember};
--fire-smoke: #{$fire-smoke};
// 항목 반경 토큰(01~09 공통 — 실사용은 $r-* SCSS 토큰, 표기 참조용)
--radius-sm: 4px;
--radius-md: 10px;
--radius-lg: 20px;
display: flex; flex-direction: column; gap: $sp-2;
width: 100%; max-width: 424px;
}
.tc__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: $fire-smoke; // 잉크 무대 위 — 연기 톤 글자
}
.tc__ico { width: 16px; height: 16px; flex: 0 0 auto; color: $fire-smoke; }
.tc__field {
position: relative; height: 180px;
border-radius: $r-card; overflow: hidden;
background: $fire-ink;
box-shadow: inset 0 0 0 1px rgba($fire-smoke, .18);
cursor: crosshair; touch-action: none;
}
.tc__field:focus-visible { outline: 2px solid $fire-amber; outline-offset: 2px; }
.tc__cv { display: block; width: 100%; height: 100%; }
.tc__bar { display: flex; align-items: baseline; justify-content: space-between; gap: $sp-2; }
.tc__meta { font-size: 10px; font-weight: 500; color: rgba($fire-smoke, .72); }
.tc__val { color: $fire-gold; font-weight: 700; } // 잉크 위 금빛 라벨
.tc__meta--dim { font-size: 10px; }
@media (max-width: 340px) {
.tc { gap: $sp-1; }
.tc__field { height: 100px; }
.tc__meta--dim { display: none; }
}
// reduce — rAF 를 돌리지 않고 한 장만 남는다. 커서 신호만 거둔다.
@media (prefers-reduced-motion: reduce) {
.tc.is-still .tc__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: #17141a;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.tc {
--fire-ink: #241b1d;
--fire-amber: #f0a868;
--fire-gold: #f6cf8a;
--fire-rose: #eca0a8;
--fire-ember: #d97a52;
--fire-smoke: #c9c2c6;
--radius-sm: 4px;
--radius-md: 10px;
--radius-lg: 20px;
display: flex;
flex-direction: column;
gap: 8px;
width: 100%;
max-width: 424px;
}
.tc__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: #c9c2c6;
}
.tc__ico {
width: 16px;
height: 16px;
flex: 0 0 auto;
color: #c9c2c6;
}
.tc__field {
position: relative;
height: 180px;
border-radius: 16px;
overflow: hidden;
background: #241b1d;
box-shadow: inset 0 0 0 1px rgba(201, 194, 198, 0.18);
cursor: crosshair;
touch-action: none;
}
.tc__field:focus-visible {
outline: 2px solid #f0a868;
outline-offset: 2px;
}
.tc__cv {
display: block;
width: 100%;
height: 100%;
}
.tc__bar {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 8px;
}
.tc__meta {
font-size: 10px;
font-weight: 500;
color: rgba(201, 194, 198, 0.72);
}
.tc__val {
color: #f6cf8a;
font-weight: 700;
}
.tc__meta--dim {
font-size: 10px;
}
@media (max-width: 340px) {
.tc {
gap: 4px;
}
.tc__field {
height: 100px;
}
.tc__meta--dim {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
.tc.is-still .tc__field {
cursor: default !important;
}
}// 횃불(03). 불꽃 전체를 기울이는 힘은 두 겹이다:
// 1) 일정한 wind 벡터(WIND_VX, WIND_VY) — 매 프레임 변하지 않는 상수 방향
// 2) 느린 요동 — sin(t * 저주파) 로 벡터 자체가 아주 천천히 흔들려 성화가 "숨쉬며" 쏠린다
// 입자는 연료 k(1→0) 로 gold→amber→ember 색온도 사다리를 타고 부력으로 오르며,
// k 가 낮을수록(위로 갈수록) 바람을 더 많이 받는다 — 근원(횃불 머리)은 곧고 끝은 크게 눕는다.
(function () {
// ── 변형 상수 블록 — 아래 값 하나만 바꿔 변형 3종을 만든다 ──
var WIND_VX = 46; // wind 벡터 x(px/s) — 변형 "wind 세기" 축(20 → 70)
var WIND_VY = -6; // wind 벡터 y(px/s) — 살짝 위로 미는 성분
var WOBBLE = 0.35; // 느린 요동 진폭(라디안) — 변형 "요동 폭" 축
var TONE = 0; // 코어 색 변형 — 0: amber(#f0a868), 1: rose(#eca0a8)
// ────────────────────────────────────────────────────
var root = document.getElementById('tcRoot');
var field = document.getElementById('tcField');
var cv = document.getElementById('tcCv');
var ctx = cv.getContext('2d');
var wOut = document.getElementById('tcW');
// 시드 고정 PRNG — mulberry32. 미리보기가 항상 같은 성화로 살아 움직인다
var SEED = 0x5eed03;
var prngState = SEED >>> 0;
function rand() {
prngState = (prngState + 0x6D2B79F5) >>> 0;
var t = prngState;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
// 색은 CSS 커스텀 프로퍼티(--fire-*)에서 getComputedStyle 로 읽는다 — 파스텔 토큰이 scss 한 곳에
var css = getComputedStyle(root);
function tokRGB(name) {
var s = css.getPropertyValue(name).trim();
var hx = s.length === 4
? s.charAt(1) + s.charAt(1) + s.charAt(2) + s.charAt(2) + s.charAt(3) + s.charAt(3)
: s.slice(1);
return [parseInt(hx.slice(0, 2), 16), parseInt(hx.slice(2, 4), 16), parseInt(hx.slice(4, 6), 16)];
}
var C_INK = tokRGB('--fire-ink');
var C_AMBER = tokRGB('--fire-amber');
var C_GOLD = tokRGB('--fire-gold');
var C_ROSE = tokRGB('--fire-rose');
var C_EMBER = tokRGB('--fire-ember');
var C_SMOKE = tokRGB('--fire-smoke');
var C_CORE = TONE ? C_ROSE : C_AMBER; // 색 변형 — 코어 톤 교체
var TAU = Math.PI * 2;
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var W = 0, H = 0;
var raf = 0, t0 = -1;
var px01 = 0.5, py01 = 0.5; // 가상 wind 세기/방향 포인터(0..1)
var manual = false;
// ── 연료 입자 — 횃불 머리에서 태어나 부력 + wind 벡터로 오르내리며 식는다 ──
var N_PART = 220;
var parts = [];
function spawn(p) {
p.x = (rand() - 0.5) * 8;
p.y = 0;
p.vx = (rand() - 0.5) * 8;
p.vy = -22 - rand() * 30;
p.k = 1;
p.ph = rand() * TAU;
}
for (var i = 0; i < N_PART; i++) {
var p = {}; spawn(p); p.y = -rand() * 46; p.k = rand();
parts.push(p);
}
var K_BURN = 2.1; // 연료 소모율 (1/s)
var STILL_T = 1.55; // reduce 에서 그리는 시각 — 성화가 가장 기운 순간
function draw(tSec, dt) {
// 1) 무대 — 어두운 야외
ctx.globalCompositeOperation = 'source-over';
ctx.fillStyle = 'rgb(' + C_INK[0] + ',' + C_INK[1] + ',' + C_INK[2] + ')';
ctx.fillRect(0, 0, W, H);
var cx = W * 0.5;
var ground = H * 0.90;
var poleTop = ground - 96; // 횃불 자루 윗면 = 머리 밑동
var strength = 0.5 + px01; // 포인터 x — wind 세기 배율(0.5~1.5)
var dirTilt = (py01 - 0.5) * 0.7; // 포인터 y — wind 방향 미세 회전
// ── 두 겹 힘 ──
// 1) 일정한 wind 벡터: (WIND_VX, WIND_VY) * strength, dirTilt 만큼 회전
// 2) 느린 요동: 벡터 각도에 WOBBLE * sin(t * 0.35) 를 더해 성화가 천천히 숨쉬며 쏠린다
var baseAng = Math.atan2(WIND_VY, WIND_VX) + dirTilt;
var wobAng = baseAng + WOBBLE * Math.sin(tSec * 0.35);
var windMag = Math.hypot(WIND_VX, WIND_VY) * strength;
var windVX = Math.cos(wobAng) * windMag;
var windVY = Math.sin(wobAng) * windMag;
// ── 2) 앙비언트 글로우 — 성화는 밤의 벌판을 밝힌다 ──
// 반경별 감쇠 빛 겹 LAYERS 장을 lighter 로 쌓는다. 반경은 R0→RMAX(대각선 끝까지),
// 앞은 촘촘히(밝은 코어)·뒤는 성기게(넓은 은은함). 알파 바닥 0.05 — 가장 바깥 겹도 산다.
// A0 는 살아있는 연료 총합 flick 과 느린 sin 펄스로 매 프레임 재계산 —
// 중심(hx,hy)도 요동 각도를 그대로 받아 그 자리에서 움직인다.
var flick = 0;
for (var f = 0; f < N_PART; f++) flick += parts[f].k;
flick = flick / N_PART;
// 반경 자체가 느리게 부풀었다 줄어든다(breathe) — 겹마다 지름이 진동해 그라디언트 정지점이
// 프레임마다 재배치되므로, 화면 넓은 영역이(중심에서 멀어도) 실제로 값이 바뀐다.
var breathe = (0.5 + 0.5 * flick) * (0.72 + 0.28 * Math.sin(tSec * 1.9));
var hx = cx + windVX * 0.5 + Math.sin(tSec * 1.4) * 6;
var hy = poleTop - 22 + windVY * 0.5 + Math.cos(tSec * 1.1) * 4;
var R0 = Math.min(W, H) * 0.16 * breathe;
var A0 = 0.85 * (0.55 + 0.45 * flick) * (0.62 + 0.38 * Math.sin(tSec * 2.4 + 0.6));
var RMAX = Math.hypot(W, H) * 0.99;
var LAYERS = 15;
var FALLOFF = 0.9;
ctx.globalCompositeOperation = 'lighter';
for (var L = 0; L < LAYERS; L++) {
var Lt = L / (LAYERS - 1); // 0..1
var R = R0 + (RMAX - R0) * Math.pow(Lt, 0.72); // 앞은 촘촘, 뒤는 성기게
var a = Math.max(0.06, A0 * Math.pow(R0 / R, FALLOFF));
var m1 = Lt; // 안쪽 gold → 바깥 코어 톤으로 식는다
var lr = C_GOLD[0] + (C_CORE[0] - C_GOLD[0]) * m1;
var lg = C_GOLD[1] + (C_CORE[1] - C_GOLD[1]) * m1;
var lb = C_GOLD[2] + (C_CORE[2] - C_GOLD[2]) * m1;
var rg = ctx.createRadialGradient(hx, hy, R * 0.04, hx, hy, R);
rg.addColorStop(0, 'rgba(' + (lr | 0) + ',' + (lg | 0) + ',' + (lb | 0) + ',' + a.toFixed(3) + ')');
rg.addColorStop(0.72, 'rgba(' + (lr | 0) + ',' + (lg | 0) + ',' + (lb | 0) + ',' + (a * 0.6).toFixed(3) + ')');
rg.addColorStop(1, 'rgba(0,0,0,0)');
ctx.fillStyle = rg;
ctx.fillRect(hx - R, hy - R, R * 2, R * 2);
}
// ── 잔열 바닥 띠 — 캔버스 전체 폭이 순수 검정으로 죽지 않게, 벌판 전체가
// 은은한 잔열로 미세하게 맥동한다(중심이 좌우로 느리게 흘러 폭 전체가 실제로 변한다).
var hb = ground - 4;
var hbW = W * 0.78;
var hbX = cx + Math.sin(tSec * 0.55) * W * 0.3;
var hbA = 0.12 * (0.6 + 0.4 * flick) * (0.7 + 0.3 * Math.sin(tSec * 1.25 + 2.0));
var hbg = ctx.createRadialGradient(hbX, hb, 2, hbX, hb, hbW);
hbg.addColorStop(0, 'rgba(' + C_EMBER[0] + ',' + C_EMBER[1] + ',' + C_EMBER[2] + ',' + hbA.toFixed(3) + ')');
hbg.addColorStop(1, 'rgba(0,0,0,0)');
ctx.fillStyle = hbg;
ctx.fillRect(0, hb - hbW, W, hbW * 2);
var hb2X = cx - Math.sin(tSec * 0.55) * W * 0.3;
var hbg2 = ctx.createRadialGradient(hb2X, hb, 2, hb2X, hb, hbW * 0.7);
hbg2.addColorStop(0, 'rgba(' + C_ROSE[0] + ',' + C_ROSE[1] + ',' + C_ROSE[2] + ',' + (hbA * 0.7).toFixed(3) + ')');
hbg2.addColorStop(1, 'rgba(0,0,0,0)');
ctx.fillStyle = hbg2;
ctx.fillRect(0, hb - hbW * 0.7, W, hbW * 1.4);
// ── 3) 연료 입자 — 위로 갈수록(k 가 작을수록) 바람을 더 많이 받아 크게 눕는다 ──
for (var j = 0; j < N_PART; j++) {
var q = parts[j];
q.k -= K_BURN * dt;
if (q.k <= 0) { spawn(q); continue; }
var lean = (1 - q.k); // 0(태생) → 1(다 탐) — wind 노출 정도
var sway = Math.sin(tSec * 6.6 + q.ph) * 6 * q.k;
var fx = cx + q.x + q.vx * q.k + sway + windVX * lean * 0.55 * dt * 60;
var fy = poleTop + q.y * q.k + windVY * lean * 0.4 * dt * 60;
q.x += (q.vx + windVX * lean * 0.4) * dt;
q.y += (q.vy + windVY * lean * 0.3) * dt;
var rC, gC, bC, rad;
if (q.k > 0.66) {
rC = C_GOLD[0]; gC = C_GOLD[1]; bC = C_GOLD[2];
rad = 2.3 + 1.5 * (q.k - 0.66) / 0.34;
} else if (q.k > 0.33) {
var m2 = (q.k - 0.33) / 0.33;
rC = C_GOLD[0] + (C_CORE[0] - C_GOLD[0]) * m2;
gC = C_GOLD[1] + (C_CORE[1] - C_GOLD[1]) * m2;
bC = C_GOLD[2] + (C_CORE[2] - C_GOLD[2]) * m2;
rad = 1.8 + 0.5 * m2;
} else {
var m3 = q.k / 0.33;
rC = C_CORE[0] + (C_EMBER[0] - C_CORE[0]) * (1 - m3);
gC = C_CORE[1] + (C_EMBER[1] - C_CORE[1]) * (1 - m3);
bC = C_CORE[2] + (C_EMBER[2] - C_CORE[2]) * (1 - m3);
rad = 1.0 + 0.7 * m3;
}
ctx.fillStyle = 'rgba(' + (rC | 0) + ',' + (gC | 0) + ',' + (bC | 0) + ',' + (0.16 + 0.5 * q.k).toFixed(3) + ')';
ctx.beginPath();
ctx.arc(fx, fy, rad, 0, TAU);
ctx.fill();
}
// ── 4) 횃불 자루·묶음 머리 — 면 계산(source-over) 마감 ──
ctx.globalCompositeOperation = 'source-over';
var poleAng = wobAng * 0.10; // 자루는 살짝만 따라 기운다(단단한 나무)
ctx.save();
ctx.translate(cx, ground);
ctx.rotate(poleAng);
// 자루
ctx.fillStyle = 'rgb(' + ((C_INK[0] + C_EMBER[0] * 0.3) | 0) + ',' + ((C_INK[1] + C_EMBER[1] * 0.3) | 0) + ',' + ((C_INK[2] + C_EMBER[2] * 0.3) | 0) + ')';
ctx.fillRect(-6, -(ground - poleTop), 12, ground - poleTop);
// 묶음 머리(연기 톤 헝겊)
ctx.fillStyle = 'rgba(' + C_SMOKE[0] + ',' + C_SMOKE[1] + ',' + C_SMOKE[2] + ',0.6)';
ctx.beginPath();
ctx.ellipse(0, -(ground - poleTop) - 2, 12, 16, 0, 0, TAU);
ctx.fill();
ctx.restore();
}
function fit() {
var r = field.getBoundingClientRect();
W = r.width; H = r.height;
cv.width = Math.round(W); cv.height = Math.round(H);
wOut.textContent = (0.5 + px01).toFixed(2);
}
function frame(now) {
if (t0 < 0) t0 = now;
var t = (now - t0) / 1000;
var dt = Math.min(0.05, (now - (frame.prev || now)) / 1000);
frame.prev = now;
if (!manual) {
// 미리보기 가상 wind — 시드 고정 Lissajous. x 가 세기, y 가 방향
var ph = t * TAU / 3.6;
px01 = 0.5 + 0.34 * Math.sin(ph + 0.4);
py01 = 0.5 + 0.24 * Math.sin(2 * ph + 0.7);
}
draw(t, dt);
wOut.textContent = (0.5 + px01).toFixed(2);
raf = requestAnimationFrame(frame);
}
field.addEventListener('pointermove', function (e) {
var r = field.getBoundingClientRect();
px01 = Math.min(1, Math.max(0, (e.clientX - r.left) / r.width));
py01 = Math.min(1, Math.max(0, (e.clientY - r.top) / r.height));
if (!manual) { manual = true; root.classList.remove('is-demo'); }
if (reduce) draw(STILL_T, 0.016);
});
field.addEventListener('pointerleave', function () {
if (manual) { px01 = 0.5; py01 = 0.5; }
});
field.addEventListener('keydown', function (e) {
var d = { ArrowLeft: [-0.07, 0], ArrowRight: [0.07, 0], ArrowUp: [0, -0.06], ArrowDown: [0, 0.06] }[e.key];
if (!d) return;
e.preventDefault();
if (!manual) { manual = true; root.classList.remove('is-demo'); }
px01 = Math.max(0, Math.min(1, px01 + d[0]));
py01 = Math.max(0, Math.min(1, py01 + d[1]));
if (reduce) draw(STILL_T, 0.016);
});
window.addEventListener('resize', function () { fit(); if (reduce) draw(STILL_T, 0.016); });
fit();
if (reduce) {
// reduce — rAF 를 돌리지 않고 성화가 가장 기운 한 장만 그린다
draw(STILL_T, 0);
root.classList.add('is-still');
} else {
raf = requestAnimationFrame(frame);
}
})();// TorchWindVector — vanilla 판(run/314/demo/03-fire-torch-wind-vector/index.html) 의 React 포트.
// 일정한 바람 벡터 + 느린 sin 요동으로 성화 전체가 한쪽으로 쏠린다. 입자는 연료 k 로
// gold→amber/rose→ember 색온도 사다리를 타고, k 가 낮을수록 바람을 더 받아 크게 눕는다.
import { useEffect, useRef, useState } from "react";
import type { CSSProperties } from "react";
import styles from "./TorchWindVector.module.scss";
export type TorchWindVectorProps = {
duration?: string;
easing?: string;
color?: string;
wind?: number; // 바람 벡터 x 성분(px/s) — 변형 "바람 세기" 축
wobble?: number; // 느린 요동 진폭(라디안) — 변형 "요동 폭" 축
tone?: 0 | 1; // 코어 색 변형 — 0: amber, 1: rose
};
type Vars = CSSProperties & { "--duration"?: string; "--easing"?: string; "--color"?: string };
// useRef 는 T | null 을 돌려준다 — 처음부터 값이 있고 절대 비지 않는 상자는 이렇게 좁혀 쓴다
type Box<T> = { current: T };
type PointerEvt = { clientX: number; clientY: number };
type KeyEvt = { key: string; preventDefault(): void };
type RGB = [number, number, number];
type Frag = { x: number; y: number; vx: number; vy: number; k: number; ph: number };
type Colors = { ink: RGB; amber: RGB; gold: RGB; rose: RGB; ember: RGB; smoke: RGB };
const TAU = Math.PI * 2;
const FIELD_LABEL = "횃불 바람 벡터 실험판";
const STILL_T = 1.55; // reduce 에서 그리는 시각 — 성화가 가장 기운 순간
const K_BURN = 2.1; // 연료 소모율 (1/s)
const N_PART = 220;
const WIND_VY = -6; // 바람 벡터 y 성분(살짝 위로)
// 시드 고정 PRNG — mulberry32
function makePRNG(seed: number): () => number {
let s = seed >>> 0;
return function () {
s = (s + 0x6d2b79f5) >>> 0;
let t = s;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
export default function TorchWindVector({
duration = "2s",
easing = "cubic-bezier(.2, .8, .2, 1)",
color = "#f0a868",
wind = 46,
wobble = 0.35,
tone = 0,
}: TorchWindVectorProps) {
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 sizeRef = useRef({ w: 0, h: 0 }) as Box<{ w: number; h: number }>;
const colRef = useRef<Colors | null>(null);
const windRef = useRef(wind) as Box<number>;
const wobRef = useRef(wobble) as Box<number>;
const toneRef = useRef(tone) as Box<number>;
const partsRef = useRef<Frag[]>([]) as Box<Frag[]>;
const pxRef = useRef(0.5) as Box<number>;
const pyRef = useRef(0.5) as Box<number>;
const manualRef = useRef(false) as Box<boolean>;
const rafRef = useRef(0) as Box<number>;
const t0Ref = useRef(-1) as Box<number>;
const prevRef = useRef(0) as Box<number>;
const [w, setW] = useState((0.5 + pxRef.current).toFixed(2));
const [still, setStill] = useState(false);
const vars: Vars = { "--duration": duration, "--easing": easing, "--color": color };
function reduced(): boolean {
return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
}
// 색은 CSS 커스텀 프로퍼티(--fire-*)에서 getComputedStyle 로 읽는다 — 파스텔 토큰이 scss 한 곳에
function tokRGB(root: HTMLDivElement, name: string): RGB {
const s = getComputedStyle(root).getPropertyValue(name).trim();
const hx = s.length === 4
? s.charAt(1) + s.charAt(1) + s.charAt(2) + s.charAt(2) + s.charAt(3) + s.charAt(3)
: s.slice(1);
return [parseInt(hx.slice(0, 2), 16), parseInt(hx.slice(2, 4), 16), parseInt(hx.slice(4, 6), 16)];
}
function coreRGB(col: Colors): RGB {
return toneRef.current ? col.rose : col.amber;
}
function buildParts(): void {
const rand = makePRNG(0x5eed03);
const arr: Frag[] = [];
for (let i = 0; i < N_PART; i++) {
arr.push({
x: (rand() - 0.5) * 8,
y: -rand() * 46,
vx: (rand() - 0.5) * 8,
vy: -22 - rand() * 30,
k: rand(),
ph: rand() * TAU,
});
}
partsRef.current = arr;
}
function fit(): void {
const field = fieldRef.current;
const cv = cvRef.current;
if (!field || !cv) return;
const ctx = cv.getContext("2d");
if (!ctx) return;
const r = field.getBoundingClientRect();
sizeRef.current.w = r.width;
sizeRef.current.h = r.height;
cv.width = Math.round(r.width);
cv.height = Math.round(r.height);
ctxRef.current = ctx;
setW((0.5 + pxRef.current).toFixed(2));
}
function spawn(f: Frag, rand: () => number): void {
f.x = (rand() - 0.5) * 8;
f.y = 0;
f.vx = (rand() - 0.5) * 8;
f.vy = -22 - rand() * 30;
f.k = 1;
f.ph = rand() * TAU;
}
function draw(tSec: number, dt: number): void {
const ctx = ctxRef.current;
const col = colRef.current;
const parts = partsRef.current;
if (!ctx || !col || parts.length === 0) return;
const { w: W, h: H } = sizeRef.current;
const rand = makePRNG(0x5eed03 + ((tSec * 61) | 0));
const core = coreRGB(col);
ctx.globalCompositeOperation = "source-over";
ctx.fillStyle = `rgb(${col.ink[0]},${col.ink[1]},${col.ink[2]})`;
ctx.fillRect(0, 0, W, H);
const cx = W * 0.5;
const ground = H * 0.9;
const poleTop = ground - 96;
const strength = 0.5 + pxRef.current;
const dirTilt = (pyRef.current - 0.5) * 0.7;
// 두 겹 힘 — 일정 바람 벡터 + 느린 sin 요동
const baseAng = Math.atan2(WIND_VY, windRef.current) + dirTilt;
const wobAng = baseAng + wobRef.current * Math.sin(tSec * 0.35);
const windMag = Math.hypot(windRef.current, WIND_VY) * strength;
const windVX = Math.cos(wobAng) * windMag;
const windVY = Math.sin(wobAng) * windMag;
let flick = 0;
for (let f = 0; f < parts.length; f++) flick += parts[f].k;
flick = flick / parts.length;
// 앙비언트 글로우 — 반경이 breathe 로 진동하고 알파는 flick·sin 펄스로 매 프레임 재계산.
const breathe = (0.5 + 0.5 * flick) * (0.72 + 0.28 * Math.sin(tSec * 1.9));
const hx = cx + windVX * 0.5 + Math.sin(tSec * 1.4) * 6;
const hy = poleTop - 22 + windVY * 0.5 + Math.cos(tSec * 1.1) * 4;
const R0 = Math.min(W, H) * 0.16 * breathe;
const A0 = 0.85 * (0.55 + 0.45 * flick) * (0.62 + 0.38 * Math.sin(tSec * 2.4 + 0.6));
const RMAX = Math.hypot(W, H) * 0.99;
const LAYERS = 15;
const FALLOFF = 0.9;
ctx.globalCompositeOperation = "lighter";
for (let L = 0; L < LAYERS; L++) {
const Lt = L / (LAYERS - 1);
const R = R0 + (RMAX - R0) * Math.pow(Lt, 0.72);
const a = Math.max(0.06, A0 * Math.pow(R0 / R, FALLOFF));
const m1 = Lt;
const lr = col.gold[0] + (core[0] - col.gold[0]) * m1;
const lg = col.gold[1] + (core[1] - col.gold[1]) * m1;
const lb = col.gold[2] + (core[2] - col.gold[2]) * m1;
const rg = ctx.createRadialGradient(hx, hy, R * 0.04, hx, hy, R);
rg.addColorStop(0, `rgba(${lr | 0},${lg | 0},${lb | 0},${a.toFixed(3)})`);
rg.addColorStop(0.72, `rgba(${lr | 0},${lg | 0},${lb | 0},${(a * 0.6).toFixed(3)})`);
rg.addColorStop(1, "rgba(0,0,0,0)");
ctx.fillStyle = rg;
ctx.fillRect(hx - R, hy - R, R * 2, R * 2);
}
// 잔열 바닥 띠 — 전체 폭이 은은히 맥동(중심 좌우 유동) + 반대 위상 두 번째 점
const hb = ground - 4;
const hbW = W * 0.78;
const hbX = cx + Math.sin(tSec * 0.55) * W * 0.3;
const hbA = 0.12 * (0.6 + 0.4 * flick) * (0.7 + 0.3 * Math.sin(tSec * 1.25 + 2.0));
const hbg = ctx.createRadialGradient(hbX, hb, 2, hbX, hb, hbW);
hbg.addColorStop(0, `rgba(${col.ember[0]},${col.ember[1]},${col.ember[2]},${hbA.toFixed(3)})`);
hbg.addColorStop(1, "rgba(0,0,0,0)");
ctx.fillStyle = hbg;
ctx.fillRect(0, hb - hbW, W, hbW * 2);
const hb2X = cx - Math.sin(tSec * 0.55) * W * 0.3;
const hbg2 = ctx.createRadialGradient(hb2X, hb, 2, hb2X, hb, hbW * 0.7);
hbg2.addColorStop(0, `rgba(${col.rose[0]},${col.rose[1]},${col.rose[2]},${(hbA * 0.7).toFixed(3)})`);
hbg2.addColorStop(1, "rgba(0,0,0,0)");
ctx.fillStyle = hbg2;
ctx.fillRect(0, hb - hbW * 0.7, W, hbW * 1.4);
for (let j = 0; j < parts.length; j++) {
const q = parts[j];
q.k -= K_BURN * dt;
if (q.k <= 0) { spawn(q, rand); continue; }
const lean = 1 - q.k;
const sway = Math.sin(tSec * 6.6 + q.ph) * 6 * q.k;
const fx = cx + q.x + q.vx * q.k + sway + windVX * lean * 0.55 * dt * 60;
const fy = poleTop + q.y * q.k + windVY * lean * 0.4 * dt * 60;
q.x += (q.vx + windVX * lean * 0.4) * dt;
q.y += (q.vy + windVY * lean * 0.3) * dt;
let rC: number, gC: number, bC: number, rad: number;
if (q.k > 0.66) {
rC = col.gold[0]; gC = col.gold[1]; bC = col.gold[2];
rad = 2.3 + 1.5 * ((q.k - 0.66) / 0.34);
} else if (q.k > 0.33) {
const m2 = (q.k - 0.33) / 0.33;
rC = col.gold[0] + (core[0] - col.gold[0]) * m2;
gC = col.gold[1] + (core[1] - col.gold[1]) * m2;
bC = col.gold[2] + (core[2] - col.gold[2]) * m2;
rad = 1.8 + 0.5 * m2;
} else {
const m3 = q.k / 0.33;
rC = core[0] + (col.ember[0] - core[0]) * (1 - m3);
gC = core[1] + (col.ember[1] - core[1]) * (1 - m3);
bC = core[2] + (col.ember[2] - core[2]) * (1 - m3);
rad = 1.0 + 0.7 * m3;
}
ctx.fillStyle = `rgba(${rC | 0},${gC | 0},${bC | 0},${(0.16 + 0.5 * q.k).toFixed(3)})`;
ctx.beginPath();
ctx.arc(fx, fy, rad, 0, TAU);
ctx.fill();
}
// 횃불 자루·묶음 머리 — 면 계산 마감
ctx.globalCompositeOperation = "source-over";
const poleAng = wobAng * 0.1;
ctx.save();
ctx.translate(cx, ground);
ctx.rotate(poleAng);
ctx.fillStyle = `rgb(${(col.ink[0] + col.ember[0] * 0.3) | 0},${(col.ink[1] + col.ember[1] * 0.3) | 0},${(col.ink[2] + col.ember[2] * 0.3) | 0})`;
ctx.fillRect(-6, -(ground - poleTop), 12, ground - poleTop);
ctx.fillStyle = `rgba(${col.smoke[0]},${col.smoke[1]},${col.smoke[2]},0.6)`;
ctx.beginPath();
ctx.ellipse(0, -(ground - poleTop) - 2, 12, 16, 0, 0, TAU);
ctx.fill();
ctx.restore();
}
function frame(now: number): void {
if (t0Ref.current < 0) t0Ref.current = now;
const t = (now - t0Ref.current) / 1000;
const dt = Math.min(0.05, (now - (prevRef.current || now)) / 1000);
prevRef.current = now;
if (!manualRef.current) {
const ph = (t * TAU) / 3.6;
pxRef.current = 0.5 + 0.34 * Math.sin(ph + 0.4);
pyRef.current = 0.5 + 0.24 * Math.sin(2 * ph + 0.7);
}
draw(t, dt);
setW((0.5 + pxRef.current).toFixed(2));
rafRef.current = requestAnimationFrame(frame);
}
function onPoint(e: PointerEvt): void {
const field = fieldRef.current;
if (!field) return;
const r = field.getBoundingClientRect();
pxRef.current = Math.min(1, Math.max(0, (e.clientX - r.left) / r.width));
pyRef.current = Math.min(1, Math.max(0, (e.clientY - r.top) / r.height));
if (!manualRef.current) manualRef.current = true;
if (reduced()) draw(STILL_T, 0.016);
}
function onLeave(): void {
if (manualRef.current) { pxRef.current = 0.5; pyRef.current = 0.5; }
}
function onKeyDown(e: KeyEvt): void {
const deltas: Record<string, [number, number]> = {
ArrowLeft: [-0.07, 0], ArrowRight: [0.07, 0], ArrowUp: [0, -0.06], ArrowDown: [0, 0.06],
};
const d = deltas[e.key];
if (!d) return;
e.preventDefault();
if (!manualRef.current) manualRef.current = true;
pxRef.current = Math.max(0, Math.min(1, pxRef.current + d[0]));
pyRef.current = Math.max(0, Math.min(1, pyRef.current + d[1]));
if (reduced()) draw(STILL_T, 0.016);
}
useEffect(() => {
const root = rootRef.current;
if (!root) return;
windRef.current = wind;
wobRef.current = wobble;
toneRef.current = tone;
colRef.current = {
ink: tokRGB(root, "--fire-ink"),
amber: tokRGB(root, "--fire-amber"),
gold: tokRGB(root, "--fire-gold"),
rose: tokRGB(root, "--fire-rose"),
ember: tokRGB(root, "--fire-ember"),
smoke: tokRGB(root, "--fire-smoke"),
};
buildParts();
fit();
function onResize(): void {
fit();
if (reduced()) draw(STILL_T, 0.016);
}
window.addEventListener("resize", onResize);
if (reduced()) {
draw(STILL_T, 0);
setStill(true);
} else {
rafRef.current = requestAnimationFrame(frame);
}
return () => {
window.removeEventListener("resize", onResize);
cancelAnimationFrame(rafRef.current);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [wind, wobble, tone]);
const rootCls = still ? `${styles.root} ${styles.isStill}` : styles.root;
return (
<main className={styles.stage}>
<div className={rootCls} 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="M128 24a16 16 0 0 1 16 16v8a80.06 80.06 0 0 1 63.94 63.94h8a16 16 0 0 1 0 32h-8A80.06 80.06 0 0 1 144 207.94v8a16 16 0 0 1-32 0v-8A80.06 80.06 0 0 1 48.06 144h-8a16 16 0 0 1 0-32h8A80.06 80.06 0 0 1 112 48.06v-8A16 16 0 0 1 128 24m-16 57.35V96a16 16 0 0 0 32 0V81.35A48.09 48.09 0 0 0 81.35 112H96a16 16 0 0 0 0-32h14.65M174.65 144H160a16 16 0 0 0 0 32h14.65A48.09 48.09 0 0 0 144 174.65V160a16 16 0 0 0-32 0v14.65A48.09 48.09 0 0 0 81.35 144" opacity=".2"/><path d="M232 128a104 104 0 1 1-104-104a104 104 0 0 1 104 104m-32 0a8 8 0 0 0-8-8h-9.34A56.1 56.1 0 0 0 136 77.34V68a8 8 0 0 0-16 0v9.34A56.1 56.1 0 0 0 77.34 120H68a8 8 0 0 0 0 16h9.34A56.1 56.1 0 0 0 120 179.66V188a8 8 0 0 0 16 0v-8.34A56.1 56.1 0 0 0 178.66 136H192a8 8 0 0 0 8-8m-40 0a32 32 0 1 1-32-32a32 32 0 0 1 32 32m-16 0a16 16 0 1 0-16 16a16 16 0 0 0 16-16"/></g></svg>
<span>일정한 바람 벡터와 느린 sin 요동에 성화 전체가 한쪽으로 쏠립니다 — 포인터 x 는 바람 세기, y 는 방향</span>
</p>
<div
className={styles.field}
ref={fieldRef}
role="application"
aria-label={FIELD_LABEL}
tabIndex={0}
onPointerMove={onPoint}
onPointerLeave={onLeave}
onKeyDown={onKeyDown}
>
<canvas className={styles.cv} ref={cvRef}></canvas>
</div>
<div className={styles.bar}>
<span className={styles.meta}>바람 <b className={styles.val}>{w}</b> · 요동 sin 기반</span>
<span className={styles.metaDim}>requestAnimationFrame · 색온도 사다리</span>
</div>
</div>
</main>
);
}03 Torch — a flame carrying a wind vector on its back — Measured area 34.055% · moving frames 23/23Original by GODRICH · SCSS and React ship in the zip · 9 Canvas Fire Animation Scenes, No WebGL
The post this demo lives in
9 Canvas Fire Animation Scenes, No WebGLA canvas fire animation here is a rule, not a sprite — no libraries, no WebGL — canvas particle math only: fuel burns do See all demos in this post
If you like this effect - similar demos
Creative UI08 Strike flash — the whole scene cools off exponentially right after impact9 Lightning Effect Background Scenes, No WebGL
Creative UI04 A predator arrives — a flee vector splitting the flock9 Canvas Boids Algorithm Flocking Birds Simulation
Creative UI01 Lissajous XY scopeWave Interference Simulation: 9 Canvas Scenes