03Crest foam — whitecaps on the peaks
Cells where summed height crosses a fraction of the amplitude are painted pale cream as breaking whitecaps, and stronger wind lowers that threshold so the foam multiplies.
Best forSurfing and marine-sports content backgrounds
thresholdImageDatawind

<main class="stage">
<div class="cf" id="cf">
<p class="cf__hint">
<svg class="cf__ico" viewBox="0 0 256 256" aria-hidden="true"><g fill="currentColor"><path d="M16 120c24-32 48-32 72 0s48 32 72 0 48-32 72 0" opacity=".25"/><path d="M16 168c24-32 48-32 72 0s48 32 72 0 48-32 72 0" opacity=".15"/><path d="M228.92 49.09a112 112 0 1 0 14 84.91a8 8 0 0 0-15.5-4a96 96 0 1 1-12-72.91A8 8 0 0 1 216 71.63V40a8 8 0 0 1-8-8h-31.63a8 8 0 0 1-5.65-13.09ZM64 104a12 12 0 1 1 12 12a12 12 0 0 1-12-12m40 64a12 12 0 1 1 12 12a12 12 0 0 1-12-12m72-16a16 16 0 1 1 16 16a16 16 0 0 1-16-16" opacity=".9"/></g></svg>
<span>Only cells whose summed height crosses an amplitude threshold break into cream — stronger wind means more whitecaps</span>
</p>
<div class="cf__field" id="field" role="application" aria-label="Crest foam playground" tabindex="0">
<canvas class="cf__cv" id="cv"></canvas>
<b class="cf__badge" aria-hidden="true"><span class="cf__badgeNum" id="thrVal">threshold 72%</span><span class="cf__badgeUnit" id="windVal">wind 1.0</span></b>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 크레스트 거품 — 억제된 바탕 램프 위에 문턱을 넘은 칸만 크림 whitecap. 움직임은 전부 캔버스 rAF 안에 있다.
// @keyframes 를 두지 않는다(077 방식). 색은 전부 토큰 또는 mix() 로컬 변수(BD26) — 헥스 리터럴 금지.
// 캔버스가 쓰는 색은 커스텀 프로퍼티로 내려보낸다 — 값은 이 파일 한 곳에만 둔다.
$cf-base0: mix($subject-blue, $stage-paper, 40%); // 바탕 마루쪽 — 파랑(억제)
$cf-base1: mix($subject-blue-deep, $stage-paper, 46%); // 바탕 중간 — 파랑
$cf-deep: mix($subject-blue-deep, $stage-paper, 62%); // 가장 깊은 골 — 진한 파랑
$cf-foam: mix($subject-cream, $stage-paper, 92%); // whitecap — 크림
$cf-foam2: $subject-cream; // whitecap 꼭대기 — 밝은 크림
$duration: $dur-loop;
$easing: $ease-spring;
$color: $cf-foam;
@include stage($stage-paper);
.cf {
--base0: #{$cf-base0};
--base1: #{$cf-base1};
--deep: #{$cf-deep};
--foam: #{$cf-foam};
--foam2: #{$cf-foam2};
display: flex; flex-direction: column; gap: $sp-2;
width: 100%; max-width: 424px;
}
.cf__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;
}
.cf__ico { width: 16px; height: 16px; flex: 0 0 auto; color: $subject-blue; }
.cf__field {
position: relative; height: 208px;
border-radius: $r-card; overflow: hidden;
background: $cf-deep;
box-shadow: inset 0 0 0 1px rgba($subject-ink, .16);
cursor: crosshair; touch-action: none;
}
.cf__field:focus-visible { outline: 2px solid $subject-blue; outline-offset: 2px; }
.cf__cv { display: block; width: 100%; height: 100%; }
.cf__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;
}
.cf__badgeNum { font-size: 13px; color: $stage-yellow; }
.cf__badgeUnit { font-weight: 700; }
@media (max-width: 340px) {
.cf { gap: $sp-1; }
.cf__field { height: 124px; }
.cf__badge { font-size: 10px; padding: 0 $sp-1; }
.cf__badgeNum { font-size: 11px; }
.cf__hint { font-size: 10px; }
}
// reduce — 루프가 아예 안 돈다. 거품이 박힌 정적 한 장이 남으므로
// 끌 수 있다는 신호(커서)만 거둔다.
@media (prefers-reduced-motion: reduce) {
.cf.is-still .cf__field { cursor: default !important; }
.cf.is-still .cf__badge { opacity: .92 !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);
}
.cf {
--base0: rgb(67.3725490196%, 77.0980392157%, 98.5882352941%);
--base1: rgb(55.8039215686%, 68.2509803922%, 95.8509803922%);
--deep: rgb(40.431372549%, 57.2078431373%, 94.4078431373%);
--foam: rgb(100%, 97.1137254902%, 90.9803921569%);
--foam2: #fff7e6;
display: flex;
flex-direction: column;
gap: 8px;
width: 100%;
max-width: 424px;
}
.cf__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;
}
.cf__ico {
width: 16px;
height: 16px;
flex: 0 0 auto;
color: #2f6df6;
}
.cf__field {
position: relative;
height: 208px;
border-radius: 16px;
overflow: hidden;
background: rgb(40.431372549%, 57.2078431373%, 94.4078431373%);
box-shadow: inset 0 0 0 1px rgba(23, 20, 26, 0.16);
cursor: crosshair;
touch-action: none;
}
.cf__field:focus-visible {
outline: 2px solid #2f6df6;
outline-offset: 2px;
}
.cf__cv {
display: block;
width: 100%;
height: 100%;
}
.cf__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;
}
.cf__badgeNum {
font-size: 13px;
color: #ffd23f;
}
.cf__badgeUnit {
font-weight: 700;
}
@media (max-width: 340px) {
.cf {
gap: 4px;
}
.cf__field {
height: 124px;
}
.cf__badge {
font-size: 10px;
padding: 0 4px;
}
.cf__badgeNum {
font-size: 11px;
}
.cf__hint {
font-size: 10px;
}
}
@media (prefers-reduced-motion: reduce) {
.cf.is-still .cf__field {
cursor: default !important;
}
.cf.is-still .cf__badge {
opacity: 0.92 !important;
}
}// 크레스트 foam — 01 과 같은 2D 파도장(정현파 6개 mulberry32(7) + Gerstner 처짐)이지만
// 바탕 램프를 낮게 억제해(거의 단색 파랑 — 거품이 주인공) 물밑에 묻어두고,
// 합성높이 > 실효진폭×threshold 인 칸만 smoothstep 가장자리로 크림색 whitecap 을 얹는다.
// 문턱은 wind 세기에 반비례해 0.82 → 0.55 로 낮아진다 — 바람이 세질수록 거품이 늘어난다.
var root = document.getElementById('cf');
var field = document.getElementById('field');
var cv = document.getElementById('cv');
var thrVal = document.getElementById('thrVal');
var windVal = document.getElementById('windVal');
var ctx = cv.getContext('2d');
var css = getComputedStyle(root);
function v(name) { return css.getPropertyValue(name).trim(); }
var BASE0 = v('--base0'), BASE1 = v('--base1'); // 억제된 바탕 램프 — 파랑 (foam 아닌 물)
var DEEP = v('--deep'); // 가장 낮은 골 — 진한 파랑
var FOAM = v('--foam'); // whitecap — 크림
var FOAM2 = v('--foam2'); // whitecap 꼭대기 — 밝은 크림
var COLS = 110; // 수면 격자 가로 칸 수(01 과 동일)
var STEP = 3; // 격자 한 칸이 렌더 버퍼에서 차지하는 픽셀
var NW = 6; // 겹칠 정현파 개수
var Q = 0.42; // Gerstner 처짐 계수
var FOAM_EDGE = 0.16; // smoothstep 가장자리 폭(threshold 비율 단위)
function mulberry32(a) {
return function () {
a |= 0; a = (a + 0x6D2B79F5) | 0;
var t = Math.imul(a ^ (a >>> 15), 1 | a);
var u = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((u ^ (u >>> 14)) >>> 0) / 4294967296;
};
}
var rng = mulberry32(7);
var WAVES = [];
(function () {
var lam = 34;
for (var i = 0; i < NW; i++) {
var ang = rng() * Math.PI * 2;
WAVES.push({
lam: lam,
freq: (Math.PI * 2) / lam,
dirX: Math.cos(ang), dirY: Math.sin(ang),
amp: 0.58 / (i * 0.62 + 1),
spd: 0.55 + rng() * 0.25,
ph: rng() * Math.PI * 2
});
lam *= 0.55;
}
})();
for (var w = 0; w < NW; w++) {
WAVES[w].om = WAVES[w].spd * 3.6 * Math.sqrt(1 / WAVES[w].lam) * 6;
}
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var W = 0, H = 0, dpr = 1, ROWS = 0;
var hgt = null;
var buf = null, bctx = null, out = null;
var RW = 0, RH = 0;
var raf = 0, auto = true, nowSec = 0;
var wind = 1.0, windT = 1.0;
var windAng = 0.5, windAngT = 0.5;
var lastPX = -1, lastPY = -1, lastPT = 0;
function hex2rgb(s) {
var pm = /^\s*rgb\(\s*([\d.]+)%\s*,\s*([\d.]+)%\s*,\s*([\d.]+)%\s*\)\s*$/.exec(s);
if (pm) return [Math.round(pm[1] * 2.55), Math.round(pm[2] * 2.55), Math.round(pm[3] * 2.55)];
s = s.replace('#', '');
if (s.length === 3) s = s[0] + s[0] + s[1] + s[1] + s[2] + s[2];
var n = parseInt(s, 16);
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
}
var RB0 = hex2rgb(BASE0), RB1 = hex2rgb(BASE1);
var RDEEP = hex2rgb(DEEP);
var RF = hex2rgb(FOAM), RF2 = hex2rgb(FOAM2);
function fit() {
var r = field.getBoundingClientRect();
dpr = window.devicePixelRatio || 1;
W = r.width; H = r.height;
cv.width = Math.round(W * dpr);
cv.height = Math.round(H * dpr);
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ROWS = Math.max(40, Math.round(COLS * H / W));
hgt = new Float32Array(COLS * ROWS);
RW = COLS * STEP; RH = ROWS * STEP;
buf = document.createElement('canvas');
buf.width = RW; buf.height = RH;
bctx = buf.getContext('2d');
out = bctx.createImageData(RW, RH);
}
// 파도장 높이 합성 — height(x,y,t) = Σ amp·sin(dot(k·dir,(x,y)) + t·ω + φ) (01 과 같은 뿌리)
function fieldcalc(t, wstr, wang) {
var i = 0;
for (var y = 0; y < ROWS; y++) {
for (var x = 0; x < COLS; x++, i++) {
var h = 0;
for (var w2 = 0; w2 < NW; w2++) {
var WV = WAVES[w2];
var da = wang * 0.5;
var dx = WV.dirX * Math.cos(da) - WV.dirY * Math.sin(da);
var dy = WV.dirX * Math.sin(da) + WV.dirY * Math.cos(da);
var ph = (x * dx + y * dy) * WV.freq * 0.62 + t * WV.om + WV.ph;
h += WV.amp * wstr * Math.sin(ph);
}
hgt[i] = h;
}
}
}
function smoothstep(a, b, x) {
var t = (x - a) / (b - a);
if (t < 0) t = 0; else if (t > 1) t = 1;
return t * t * (3 - 2 * t);
}
// 렌더 — 억제된 바탕(골 DEEP → BASE0 → BASE1, 마루 직전까지만) 위에
// 문턱을 넘은 칸을 크림 whitecap 으로. 가장자리는 smoothstep 로 부드럽게, 꼭대기는 FOAM2 로 더 밝게.
function render(thr) {
var o = out.data;
// 실효 진폭 — 현재 바람까지 반영한 이 파도장의 실제 스케일. 문턱은 이 배율로 작동한다.
var eff = 0;
for (var i = 0; i < NW; i++) eff += WAVES[i].amp;
eff *= wind;
var cut = eff * thr;
for (var y = 0; y < ROWS; y++) {
var row = y * COLS;
for (var x = 0; x < COLS; x++) {
var idx = row + x;
var h = hgt[idx];
// 억제된 바탕 램프 — h 를 0.55 배로 눌러 물색이 거의 단색에 가깝게(거품이 주인공)
var u = h * 0.55 * 1.24 + 0.5;
if (u < 0) u = 0; else if (u > 1) u = 1;
var r, g, b;
if (u > 0.5) {
var k = (u - 0.5) * 2;
r = RB1[0] * (1 - k) + RB0[0] * k;
g = RB1[1] * (1 - k) + RB0[1] * k;
b = RB1[2] * (1 - k) + RB0[2] * k;
} else {
var k2 = u * 2;
r = RDEEP[0] * (1 - k2) + RB0[0] * k2;
g = RDEEP[1] * (1 - k2) + RB0[1] * k2;
b = RDEEP[2] * (1 - k2) + RB0[2] * k2;
}
// whitecap — h 가 cut 을 넘으면 크림. 가장자리 smoothstep(폭 FOAM_EDGE·eff), 꼭대기는 FOAM2 로 두 단
var f = smoothstep(cut - FOAM_EDGE * eff, cut + FOAM_EDGE * eff, h);
if (f > 0) {
var top = smoothstep(cut + FOAM_EDGE * eff * 2.2, cut + eff * 0.42, h);
var fr = RF[0] * (1 - top) + RF2[0] * top;
var fg = RF[1] * (1 - top) + RF2[1] * top;
var fb = RF[2] * (1 - top) + RF2[2] * top;
r = r * (1 - f) + fr * f;
g = g * (1 - f) + fg * f;
b = b * (1 - f) + fb * f;
}
for (var sy = 0; sy < STEP; sy++) {
var base = ((y * STEP + sy) * RW + x * STEP) * 4;
for (var sx = 0; sx < STEP; sx++, base += 4) {
o[base] = r;
o[base + 1] = g;
o[base + 2] = b;
o[base + 3] = 255;
}
}
}
}
bctx.putImageData(out, 0, 0);
ctx.drawImage(buf, 0, 0, W, H);
}
// 문턱은 바람에 반비례 — 0.82(약한 wind) → 0.55(강한 wind)
function thresholdFor(w) {
return 0.82 - 0.27 * Math.max(0, Math.min(1, (w - 0.35) / (2.1 - 0.35)));
}
function frame(now) {
nowSec = now / 1000;
if (auto) {
var th = nowSec * 0.31;
windT = 1.05 + Math.sin(th * 0.9 + 0.4) * 0.55 + Math.sin(th * 2.3) * 0.12;
windAngT = 0.5 + Math.sin(th * 0.42) * 0.85;
}
wind += (windT - wind) * 0.05;
windAng += (windAngT - windAng) * 0.05;
fieldcalc(nowSec, wind, windAng);
var thr = thresholdFor(wind);
render(thr);
thrVal.textContent = 'threshold ' + Math.round(thr * 100) + '%';
windVal.textContent = 'wind ' + wind.toFixed(2);
raf = requestAnimationFrame(frame);
}
function still() {
fieldcalc(7.3, 1.35, 0.5);
render(thresholdFor(1.35));
}
function localXY(e) {
var r = field.getBoundingClientRect();
return [e.clientX - r.left, e.clientY - r.top];
}
field.addEventListener('pointermove', function (e) {
var p = localXY(e);
var t = performance.now();
if (lastPX >= 0 && t > lastPT) {
var vx = p[0] - lastPX, vy = p[1] - lastPY;
var sp2 = Math.sqrt(vx * vx + vy * vy);
if (sp2 > 0.5) {
windT = Math.max(0.35, Math.min(2.1, 0.9 + sp2 * 0.02));
windAngT = Math.atan2(vy, vx) * 0.16;
}
}
lastPX = p[0]; lastPY = p[1]; lastPT = t;
auto = false;
});
field.addEventListener('pointerleave', function () {
auto = true;
lastPX = -1;
});
field.addEventListener('keydown', function (e) {
var d = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }[e.key];
if (!d) return;
e.preventDefault();
auto = false;
windT = Math.max(0.35, Math.min(2.1, windT + d[0] * 0.18));
windAngT = windAngT + d[1] * 0.1;
if (reduce) {
fieldcalc(nowSec || 3.1, windT, windAngT);
render(thresholdFor(windT));
}
});
window.addEventListener('resize', function () { fit(); if (reduce) still(); });
fit();
if (reduce) {
root.classList.add('is-still');
still();
} else {
raf = requestAnimationFrame(frame);
}// CrestFoam — vanilla 판(run/313/demo/03-ocean-crest-foam/index.html) 의 React 포트.
// 01 과 같은 2D 파도장(정현파 6개, 시드 7)을 억제된 파랑 바탕 위에 그리고,
// 합성높이 > 실효진폭×문턱 인 칸만 smoothstep 가장자리로 크림 whitecap 을 얹는다.
// 문턱은 바람 세기에 반비례(0.82→0.55). 움직임은 캔버스 rAF 루프가 전부이므로 @keyframes 는 없다.
import { useEffect, useRef } from "react";
import type { CSSProperties } from "react";
import styles from "./CrestFoam.module.scss";
export type CrestFoamProps = {
duration?: string;
easing?: string;
color?: string;
};
type Vars = CSSProperties & { "--duration"?: string; "--easing"?: string; "--color"?: string };
type Box<T> = { current: T };
type PointerMotionEvent = { clientX: number; clientY: number };
type RGB = [number, number, number];
const COLS = 110; // 수면 격자 가로 칸 수
const STEP = 3; // 격자 한 칸이 렌더 버퍼에서 차지하는 픽셀
const NW = 6; // 겹칠 정현파 개수
const FOAM_EDGE = 0.16; // smoothstep 가장자리 폭(문턱 비율 단위)
// 결정적 난수 mulberry32(7)
function mulberry32(a: number): () => number {
return function () {
a |= 0; a = (a + 0x6D2B79F5) | 0;
const t = Math.imul(a ^ (a >>> 15), 1 | a);
const u = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((u ^ (u >>> 14)) >>> 0) / 4294967296;
};
}
type Wave = {
lam: number; freq: number; dirX: number; dirY: number;
amp: number; spd: number; ph: number; om: number;
};
function buildWaves(): Wave[] {
const rng = mulberry32(7);
const out: Wave[] = [];
let lam = 34;
for (let i = 0; i < NW; i++) {
const ang = rng() * Math.PI * 2;
const wv: Wave = {
lam,
freq: (Math.PI * 2) / lam,
dirX: Math.cos(ang),
dirY: Math.sin(ang),
amp: 0.58 / (i * 0.62 + 1),
spd: 0.55 + rng() * 0.25,
ph: rng() * Math.PI * 2,
om: 0,
};
wv.om = wv.spd * 3.6 * Math.sqrt(1 / wv.lam) * 6;
out.push(wv);
lam *= 0.55;
}
return out;
}
function hex2rgb(s: string): RGB {
// sass 가 mix() 값을 rgb(r%, g%, b%) 로 내보낸다 — 헥스와 둘 다 읽는다
const pm = /^\s*rgb\(\s*([\d.]+)%\s*,\s*([\d.]+)%\s*,\s*([\d.]+)%\s*\)\s*$/.exec(s);
if (pm) return [Math.round(+pm[1] * 2.55), Math.round(+pm[2] * 2.55), Math.round(+pm[3] * 2.55)];
let t = s.replace("#", "");
if (t.length === 3) t = t[0] + t[0] + t[1] + t[1] + t[2] + t[2];
const n = parseInt(t, 16);
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
}
function smoothstep(a: number, b: number, x: number): number {
let t = (x - a) / (b - a);
if (t < 0) t = 0; else if (t > 1) t = 1;
return t * t * (3 - 2 * t);
}
// 문턱은 바람에 반비례 — 0.82(약한 바람) → 0.55(강한 바람)
function thresholdFor(w: number): number {
return 0.82 - 0.27 * Math.max(0, Math.min(1, (w - 0.35) / (2.1 - 0.35)));
}
type St = {
rows: number; rw: number; rh: number;
hgt: Float32Array | null;
out: ImageData | null;
bctx: CanvasRenderingContext2D | null;
buf: HTMLCanvasElement | null;
ampSum: number;
auto: boolean;
wind: number; windT: number;
windAng: number; windAngT: number;
lastPX: number; lastPY: number; lastPT: number;
rb0: RGB | null; rb1: RGB | null; rdeep: RGB | null;
rf: RGB | null; rf2: RGB | null;
};
export default function CrestFoam({
duration = "2s", // vanilla $duration: $dur-loop
easing = "cubic-bezier(.2, .8, .2, 1)", // vanilla $easing: $ease-spring
color = "#fff7e6", // vanilla $color: $cf-foam (근사값)
}: CrestFoamProps) {
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 thrValRef = useRef<HTMLSpanElement | null>(null);
const windValRef = useRef<HTMLSpanElement | null>(null);
const wavesRef = useRef<Wave[] | null>(null);
const sizeRef = useRef({ w: 0, h: 0 }) as Box<{ w: number; h: number }>;
const rafRef = useRef(0) as Box<number>;
const stillRef = useRef(false) as Box<boolean>;
const stRef = useRef({
rows: 0, rw: 0, rh: 0,
hgt: null,
out: null,
bctx: null,
buf: null,
ampSum: 0,
auto: true,
wind: 1.0, windT: 1.0,
windAng: 0.5, windAngT: 0.5,
lastPX: -1, lastPY: -1, lastPT: 0,
rb0: null, rb1: null, rdeep: null, rf: null, rf2: null,
} as St) as Box<St>;
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, cv = cvRef.current;
if (!field || !cv) return;
const r = field.getBoundingClientRect();
const dpr = window.devicePixelRatio || 1;
sizeRef.current.w = r.width;
sizeRef.current.h = r.height;
cv.width = Math.round(r.width * dpr);
cv.height = Math.round(r.height * dpr);
const ctx = ctxRef.current;
if (ctx) ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
const st = stRef.current;
st.rows = Math.max(40, Math.round(COLS * r.height / r.width));
st.hgt = new Float32Array(COLS * st.rows);
st.rw = COLS * STEP;
st.rh = st.rows * STEP;
const buf = document.createElement("canvas");
buf.width = st.rw; buf.height = st.rh;
st.buf = buf;
st.bctx = buf.getContext("2d");
if (st.bctx) st.out = st.bctx.createImageData(st.rw, st.rh);
}
// 파도장 높이 합성 — height(x,y,t) = Σ amp·sin(dot(k·dir,(x,y)) + t·ω + φ)
function fieldcalc(t: number, wstr: number, wang: number): void {
const st = stRef.current;
const WV = wavesRef.current;
if (!st.hgt || !WV) return;
let i = 0;
for (let y = 0; y < st.rows; y++) {
for (let x = 0; x < COLS; x++, i++) {
let h = 0;
for (let k = 0; k < NW; k++) {
const wv = WV[k];
const da = wang * 0.5;
const dx = wv.dirX * Math.cos(da) - wv.dirY * Math.sin(da);
const dy = wv.dirX * Math.sin(da) + wv.dirY * Math.cos(da);
const ph = (x * dx + y * dy) * wv.freq * 0.62 + t * wv.om + wv.ph;
h += wv.amp * wstr * Math.sin(ph);
}
st.hgt[i] = h;
}
}
}
// 렌더 — 억제된 바탕 램프 위에 문턱을 넘은 칸만 크림 whitecap(smoothstep 가장자리)
function render(thr: number): void {
const st = stRef.current;
const ctx = ctxRef.current;
if (!st.out || !st.bctx || !st.buf || !ctx || !st.hgt) return;
if (!st.rb0 || !st.rb1 || !st.rdeep || !st.rf || !st.rf2) return;
const o = st.out.data;
const RB0 = st.rb0, RB1 = st.rb1, RDEEP = st.rdeep, RF = st.rf, RF2 = st.rf2;
const eff = st.ampSum * st.wind; // 실효 진폭 — 현재 바람까지 반영한 파도장 스케일
const cut = eff * thr;
for (let y = 0; y < st.rows; y++) {
const row = y * COLS;
for (let x = 0; x < COLS; x++) {
const idx = row + x;
const h = st.hgt[idx];
let u = h * 0.55 * 1.24 + 0.5;
if (u < 0) u = 0; else if (u > 1) u = 1;
let r: number, g: number, b: number;
if (u > 0.5) {
const k = (u - 0.5) * 2;
r = RB1[0] * (1 - k) + RB0[0] * k;
g = RB1[1] * (1 - k) + RB0[1] * k;
b = RB1[2] * (1 - k) + RB0[2] * k;
} else {
const k2 = u * 2;
r = RDEEP[0] * (1 - k2) + RB0[0] * k2;
g = RDEEP[1] * (1 - k2) + RB0[1] * k2;
b = RDEEP[2] * (1 - k2) + RB0[2] * k2;
}
const f = smoothstep(cut - FOAM_EDGE * eff, cut + FOAM_EDGE * eff, h);
if (f > 0) {
const top = smoothstep(cut + FOAM_EDGE * eff * 2.2, cut + eff * 0.42, h);
const fr = RF[0] * (1 - top) + RF2[0] * top;
const fg = RF[1] * (1 - top) + RF2[1] * top;
const fb = RF[2] * (1 - top) + RF2[2] * top;
r = r * (1 - f) + fr * f;
g = g * (1 - f) + fg * f;
b = b * (1 - f) + fb * f;
}
for (let sy = 0; sy < STEP; sy++) {
let base = ((y * STEP + sy) * st.rw + x * STEP) * 4;
for (let sx = 0; sx < STEP; sx++, base += 4) {
o[base] = r;
o[base + 1] = g;
o[base + 2] = b;
o[base + 3] = 255;
}
}
}
}
st.bctx.putImageData(st.out, 0, 0);
ctx.drawImage(st.buf, 0, 0, sizeRef.current.w, sizeRef.current.h);
}
function frame(now: number): void {
const st = stRef.current;
const sec = now / 1000;
if (st.auto) {
const th = sec * 0.31;
st.windT = 1.05 + Math.sin(th * 0.9 + 0.4) * 0.55 + Math.sin(th * 2.3) * 0.12;
st.windAngT = 0.5 + Math.sin(th * 0.42) * 0.85;
}
st.wind += (st.windT - st.wind) * 0.05;
st.windAng += (st.windAngT - st.windAng) * 0.05;
fieldcalc(sec, st.wind, st.windAng);
const thr = thresholdFor(st.wind);
render(thr);
if (thrValRef.current) thrValRef.current.textContent = "문턱 " + Math.round(thr * 100) + "%";
if (windValRef.current) windValRef.current.textContent = "바람 " + st.wind.toFixed(2);
rafRef.current = requestAnimationFrame(frame);
}
function onPointerMove(e: PointerMotionEvent): void {
const field = fieldRef.current;
if (!field) return;
const r = field.getBoundingClientRect();
const px = e.clientX - r.left, py = e.clientY - r.top;
const t = performance.now();
const st = stRef.current;
if (st.lastPX >= 0 && t > st.lastPT) {
const vx = px - st.lastPX, vy = py - st.lastPY;
const sp = Math.sqrt(vx * vx + vy * vy);
if (sp > 0.5) {
st.windT = Math.max(0.35, Math.min(2.1, 0.9 + sp * 0.02));
st.windAngT = Math.atan2(vy, vx) * 0.16;
}
}
st.lastPX = px; st.lastPY = py; st.lastPT = t;
st.auto = false;
}
function onPointerLeave(): void {
stRef.current.auto = true;
stRef.current.lastPX = -1;
}
function onResize(): void {
fit();
if (reduced()) {
fieldcalc(7.3, 1.35, 0.5);
render(thresholdFor(1.35));
}
}
useEffect(() => {
const cv = cvRef.current;
const root = rootRef.current;
if (!cv || !root) return;
ctxRef.current = cv.getContext("2d");
if (!ctxRef.current) return;
wavesRef.current = buildWaves();
let sum = 0;
for (let i = 0; i < NW; i++) sum += wavesRef.current[i].amp;
stRef.current.ampSum = sum;
const cs = getComputedStyle(root);
const tok = (name: string) => cs.getPropertyValue(name).trim();
const st = stRef.current;
st.rb0 = hex2rgb(tok("--base0"));
st.rb1 = hex2rgb(tok("--base1"));
st.rdeep = hex2rgb(tok("--deep"));
st.rf = hex2rgb(tok("--foam"));
st.rf2 = hex2rgb(tok("--foam2"));
fit();
window.addEventListener("resize", onResize);
if (reduced()) {
stillRef.current = true;
fieldcalc(7.3, 1.35, 0.5);
render(thresholdFor(1.35));
} else {
rafRef.current = requestAnimationFrame(frame);
}
return () => {
window.removeEventListener("resize", onResize);
cancelAnimationFrame(rafRef.current);
};
}, []);
const cls = [styles.root, stillRef.current ? styles.isStill : ""].filter(Boolean).join(" ");
return (
<main className={styles.stage}>
<div className={cls} 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="M16 120c24-32 48-32 72 0s48 32 72 0 48-32 72 0" opacity=".25"/><path d="M16 168c24-32 48-32 72 0s48 32 72 0 48-32 72 0" opacity=".15"/><path d="M228.92 49.09a112 112 0 1 0 14 84.91a8 8 0 0 0-15.5-4a96 96 0 1 1-12-72.91A8 8 0 0 1 216 71.63V40a8 8 0 0 1-8-8h-31.63a8 8 0 0 1-5.65-13.09ZM64 104a12 12 0 1 1 12 12a12 12 0 0 1-12-12m40 64a12 12 0 1 1 12 12a12 12 0 0 1-12-12m72-16a16 16 0 1 1 16 16a16 16 0 0 1-16-16" opacity=".9"/></g></svg>
<span>합성 높이가 진폭의 문턱을 넘는 칸만 크림색으로 부서뜨립니다 — 바람이 세질수록 흰 꼭대기가 늘어요</span>
</p>
<div className={styles.field} ref={fieldRef} onPointerMove={onPointerMove} onPointerLeave={onPointerLeave}>
<canvas className={styles.cv} ref={cvRef}></canvas>
<b className={styles.badge} aria-hidden="true"><span className={styles.badgeNum} ref={thrValRef}>문턱 72%</span><span className={styles.badgeUnit} ref={windValRef}>바람 1.0</span></b>
</div>
</div>
</main>
);
}03 Crest foam — whitecaps on the peaks — Measured area 60.08% · moving frames 23/23Original by GODRICH · SCSS and React ship in the zip · 9 Canvas Wave Animation Scenes: Gerstner Field
The post this demo lives in
9 Canvas Wave Animation Scenes: Gerstner FieldA canvas wave animation is not a looping photo — every grid cell has its wave height recomputed in real time as a gerstn See all demos in this post
If you like this effect - similar demos
Creative UI03 Maze — stripes kink into corridors in the middle ground9 Reaction Diffusion Simulations, No Library
Creative UI01 One sphere — the first 3D a minimal marcher makes9 Ray Marching Scenes in Canvas (No WebGL)
Creative UI04 Afterglow diffusion — the bolt's halo bleeding through a pixel buffer9 Lightning Effect Background Scenes, No WebGL