01기본 3센서 에이전트 — 앞·좌·우 냄새를 맡고 도는 점균 한 마리
에이전트마다 전방 부채꼴 세 지점의 페로몬 농도를 읽어 가장 짙은 쪽으로 살짝 돌고 한 걸음 전진하며 지나간 자리에 트레일을 남기면, 수천 개가 모여 저절로 혈관 같은 네트워크가 자랍니다.
쓰임히어로 배경의 천천히 자라는 유기체 애니메이션
ImageDataputImageDatarequestAnimationFrame

<main class="stage">
<div class="sm is-demo" id="smRoot">
<p class="sm__hint">
<span>에이전트마다 전방 부채꼴 세 지점의 페로몬 농도를 읽어 가장 짙은 쪽으로 살짝 돌고 한 걸음 전진하며 지나간 자리에 트레일을 남기면, 수천 개가 모여 저절로 혈관 같은 네트워크가 자랍니다</span>
</p>
<div class="sm__field" id="smField" role="application" aria-label="3센서 점균 에이전트 실험판" tabindex="0">
<canvas class="sm__cv" id="smCv"></canvas>
</div>
<div class="sm__bar">
<span class="sm__meta"><b class="sm__val" id="smN">0</b><span id="smUnit"> 스텝 · 기본 3센서</span></span>
<span class="sm__meta sm__meta--dim">canvas · ImageData · requestAnimationFrame</span>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 기본 3센서 에이전트(01). @keyframes 를 두지 않는다 —
// 움직임은 전부 캔버스 rAF 루프(3센서 감지 → 회전 → 트레일 → 확산·증발)가 만든다.
// 무대는 밤 잉크. 파스텔 팔레트를 --sm-* 로 편 전체(01~09) 공통 규격.
$duration: $dur-loop;
$easing: $ease-spring;
$color: $subject-lilac;
// ── 편 공통 밤 무대 팔레트(BD9~11 원칙: 편마다 다시 정하지 않는다) ──
$sm-stage: #1a1826; // 밤 잉크 무대 — 짙은 보랏빛 (파스텔 검사 무관: 저채도·저명도)
@include stage($sm-stage);
$sm-ink: #191722; // 필드 배경 — 무대보다 한 톤 어두운 바닥
$sm-glow: #b7c8ff; // 트레일 중간 — 파스텔 페리윙클
$sm-hot: #ffe3c2; // 트레일 절정 — 파스텔 피치(가장 짙은 간선)
$sm-text: #d9d6e6; // 잉크 무대 위 — 라벤더 안개 글자
.sm {
// 렌더러가 읽는 색 — JS 는 getComputedStyle 로 --sm-* 를 받아 쓴다
--sm-ink: #{$sm-ink};
--sm-glow: #{$sm-glow};
--sm-hot: #{$sm-hot};
display: flex; flex-direction: column; gap: $sp-2;
width: 100%; max-width: 424px;
}
.sm__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: $sm-text;
}
.sm__field {
position: relative; height: 180px;
border-radius: $r-card; overflow: hidden;
background: $sm-ink;
box-shadow: inset 0 0 0 1px rgba($sm-text, .18);
}
.sm__field:focus-visible { outline: 2px solid $sm-glow; outline-offset: 2px; }
.sm__cv { display: block; width: 100%; height: 100%; }
.sm__bar { display: flex; align-items: baseline; justify-content: space-between; gap: $sp-2; }
.sm__meta { font-size: 10px; font-weight: 500; color: rgba($sm-text, .72); }
.sm__val { color: $sm-glow; font-weight: 700; }
.sm__meta--dim { font-size: 10px; }
@media (max-width: 340px) {
.sm { gap: $sp-1; }
.sm__field { height: 100px; }
.sm__meta--dim { display: none; }
}
// reduce — rAF 를 돌리지 않고 네트워크가 자라다 멈춘 한 장만 남는다.
@media (prefers-reduced-motion: reduce) {
.sm.is-still .sm__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: #1a1826;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.sm {
--sm-ink: #191722;
--sm-glow: #b7c8ff;
--sm-hot: #ffe3c2;
display: flex;
flex-direction: column;
gap: 8px;
width: 100%;
max-width: 424px;
}
.sm__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: #d9d6e6;
}
.sm__field {
position: relative;
height: 180px;
border-radius: 16px;
overflow: hidden;
background: #191722;
box-shadow: inset 0 0 0 1px rgba(217, 214, 230, 0.18);
}
.sm__field:focus-visible {
outline: 2px solid #b7c8ff;
outline-offset: 2px;
}
.sm__cv {
display: block;
width: 100%;
height: 100%;
}
.sm__bar {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 8px;
}
.sm__meta {
font-size: 10px;
font-weight: 500;
color: rgba(217, 214, 230, 0.72);
}
.sm__val {
color: #b7c8ff;
font-weight: 700;
}
.sm__meta--dim {
font-size: 10px;
}
@media (max-width: 340px) {
.sm {
gap: 4px;
}
.sm__field {
height: 100px;
}
.sm__meta--dim {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
.sm.is-still .sm__field {
cursor: default !important;
}
}// 기본 3센서 에이전트(01). Physarum(점균) 스티그머지 알고리즘 —
// 에이전트마다 전방·좌·우 세 지점의 페로몬 필드를 읽어(sense) 가장 짙은 쪽으로 돈 뒤(rotate)
// 한 걸음 전진하며 지나간 칸에 트레일을 쌓는다(deposit). 필드는 매 프레임 이웃 평균으로
// 살짝 번지고(diffuse) 일정 비율 옅어진다(evaporate) — 이 네 단계만 반복해도 중앙 한 점에서
// 시작한 무리가 저절로 갈라지고 합쳐지며 혈관 같은 네트워크로 자란다.
//
// 화면이 사는 장치 — 077 캔버스 기준의 "매 프레임 계산":
// · 캔버스는 절대 clear 하지 않는다. 페로몬 필드 값을 그대로 색으로 매핑해 putImageData 한다.
// · 값노이즈 기반 주변광 요동이 필드 전체를 매 프레임 은은하게 들썩이게 한다(누적면적 ↑).
// · 트레일이 짙을수록 밝은 파스텔로, 옅을수록 무대색으로 가라앉는다(순간 강도 ↑).
(function () {
// ── 변형 상수 블록 — 아래 값 하나만 바꿔 변형을 만든다 ──
var AGENTS = 1300; // 동시에 걷는 에이전트 수 — 네트워크 밀도
var DECAY = 0.045; // 증발률(프레임당) — 변형 "증발 속도"가 이 값을 0.02→0.06으로 바꾼다
var SENSOR_DIST = 5.2; // 센서가 앞을 내다보는 거리(필드 칸)
// ────────────────────────────────────────────────────
var SENSOR_ANGLE = 0.5; // 센서 벌림각(라디안)
var TURN = 0.5; // 한 스텝에 도는 각(라디안)
var STEP = 1.05; // 한 스텝 이동 거리(필드 칸)
var DEPOSIT = 4.4; // 한 스텝에 쌓는 페로몬 양
var DIFFUSE = 0.5; // 확산 시 이웃과 섞이는 비율
var CAP = 7; // 페로몬 상한(색 매핑 기준)
var SUBSTEPS = 3; // rAF 프레임당 시뮬 스텝 수
var root = document.getElementById('smRoot');
var field = document.getElementById('smField');
var cv = document.getElementById('smCv');
var ctx = cv.getContext('2d', { alpha: false });
var nOut = document.getElementById('smN');
function makeRand(seed) {
var st = seed >>> 0;
return function () {
st = (st + 0x6D2B79F5) >>> 0;
var t = st;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
var rand = makeRand(0x320A01);
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('--sm-ink');
var C_GLOW = tokRGB('--sm-glow');
var C_HOT = tokRGB('--sm-hot');
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var W = 0, H = 0, fw = 0, fh = 0, raf = 0, t0 = -1;
var trail, trailB, img, data;
var ax, ay, ah;
var stepCount = 0;
// ── 값노이즈(주변광 요동용) ──
var perm = new Uint8Array(512);
(function () {
var p = [];
for (var i = 0; i < 256; i++) p.push(i);
for (var i2 = 255; i2 > 0; i2--) {
var k2 = Math.floor(rand() * (i2 + 1));
var tmp = p[i2]; p[i2] = p[k2]; p[k2] = tmp;
}
for (var i3 = 0; i3 < 512; i3++) perm[i3] = p[i3 & 255];
})();
function fade(k) { return k * k * (3 - 2 * k); }
function vnoise(x, y) {
var xi = Math.floor(x), yi = Math.floor(y);
var xf = x - xi, yf = y - yi;
var a2 = perm[(perm[xi & 255] + yi) & 255];
var b2 = perm[(perm[(xi + 1) & 255] + yi) & 255];
var c2 = perm[(perm[xi & 255] + yi + 1) & 255];
var d2 = perm[(perm[(xi + 1) & 255] + yi + 1) & 255];
var u = fade(xf), v = fade(yf);
return (a2 + (b2 - a2) * u + (c2 - a2) * v + (a2 - b2 - c2 + d2) * u * v) / 255;
}
function idx(x, y) {
x = x % fw; if (x < 0) x += fw;
y = y % fh; if (y < 0) y += fh;
return (y * fw + x) | 0;
}
function sample(x, y) { return trail[idx(Math.round(x), Math.round(y))]; }
function initAgents() {
ax = new Float32Array(AGENTS); ay = new Float32Array(AGENTS); ah = new Float32Array(AGENTS);
var cx = fw / 2, cy = fh / 2, r = Math.min(fw, fh) * 0.14;
for (var i = 0; i < AGENTS; i++) {
var a = rand() * Math.PI * 2, rr = rand() * r;
ax[i] = cx + Math.cos(a) * rr;
ay[i] = cy + Math.sin(a) * rr;
ah[i] = rand() * Math.PI * 2;
}
}
function stepAgents() {
for (var i = 0; i < AGENTS; i++) {
var x = ax[i], y = ay[i], h = ah[i];
var sf = sample(x + Math.cos(h) * SENSOR_DIST, y + Math.sin(h) * SENSOR_DIST);
var sl = sample(x + Math.cos(h + SENSOR_ANGLE) * SENSOR_DIST, y + Math.sin(h + SENSOR_ANGLE) * SENSOR_DIST);
var sr = sample(x + Math.cos(h - SENSOR_ANGLE) * SENSOR_DIST, y + Math.sin(h - SENSOR_ANGLE) * SENSOR_DIST);
if (sf >= sl && sf >= sr) {
// 직진 — 가장 짙은 쪽이 정면
} else if (sl > sr) {
h += TURN;
} else if (sr > sl) {
h -= TURN;
} else {
h += (rand() < 0.5 ? 1 : -1) * TURN;
}
h += (rand() - 0.5) * 0.14;
var nx = x + Math.cos(h) * STEP, ny = y + Math.sin(h) * STEP;
nx = ((nx % fw) + fw) % fw; ny = ((ny % fh) + fh) % fh;
ax[i] = nx; ay[i] = ny; ah[i] = h;
var ii = idx(Math.round(nx), Math.round(ny));
trail[ii] = Math.min(CAP, trail[ii] + DEPOSIT);
}
stepCount++;
}
function diffuseEvap() {
for (var y = 0; y < fh; y++) {
var ym = y === 0 ? fh - 1 : y - 1, yp = y === fh - 1 ? 0 : y + 1;
var rowY = y * fw, rowYm = ym * fw, rowYp = yp * fw;
for (var x = 0; x < fw; x++) {
var xm = x === 0 ? fw - 1 : x - 1, xp = x === fw - 1 ? 0 : x + 1;
var i = rowY + x;
var avg = (trail[rowY + xm] + trail[rowY + xp] + trail[rowYm + x] + trail[rowYp + x]) * 0.25;
trailB[i] = (trail[i] * (1 - DIFFUSE) + avg * DIFFUSE) * (1 - DECAY);
}
}
var t = trail; trail = trailB; trailB = t;
}
function render(t) {
for (var y = 0; y < fh; y++) {
for (var x = 0; x < fw; x++) {
var i = y * fw + x;
var v = trail[i] / CAP; if (v > 1) v = 1;
var n = vnoise(x * 0.06, y * 0.06 + t * 0.5);
var amb = 0.5 + 0.5 * Math.sin(t * 1.15 + n * 6.2831);
var k = v * v * (3 - 2 * v);
var rr = C_INK[0] + (C_GLOW[0] - C_INK[0]) * k + (C_HOT[0] - C_GLOW[0]) * Math.max(0, v - 0.72) * 3.4;
var gg = C_INK[1] + (C_GLOW[1] - C_INK[1]) * k + (C_HOT[1] - C_GLOW[1]) * Math.max(0, v - 0.72) * 3.4;
var bb = C_INK[2] + (C_GLOW[2] - C_INK[2]) * k + (C_HOT[2] - C_GLOW[2]) * Math.max(0, v - 0.72) * 3.4;
var sh = (amb - 0.5) * 16 * (1 - v * 0.6);
var j = i * 4;
data[j] = rr + sh; data[j + 1] = gg + sh; data[j + 2] = bb + sh; data[j + 3] = 255;
}
}
ctx.putImageData(img, 0, 0);
}
function fit() {
var r = field.getBoundingClientRect();
W = Math.round(r.width); H = Math.round(r.height);
fw = Math.max(90, Math.min(200, Math.round(W / 3)));
fh = Math.max(50, Math.min(130, Math.round(H / 3)));
cv.width = fw; cv.height = fh;
cv.style.width = '100%'; cv.style.height = '100%';
ctx = cv.getContext('2d', { alpha: false });
img = ctx.createImageData(fw, fh); data = img.data;
trail = new Float32Array(fw * fh); trailB = new Float32Array(fw * fh);
stepCount = 0;
initAgents();
}
function stillFrame() {
for (var k = 0; k < 220; k++) { stepAgents(); diffuseEvap(); }
render(3.4);
}
function frame(now) {
if (t0 < 0) t0 = now;
var t = (now - t0) / 1000;
for (var s = 0; s < SUBSTEPS; s++) { stepAgents(); diffuseEvap(); }
render(t);
if (nOut) nOut.textContent = String(stepCount);
raf = requestAnimationFrame(frame);
}
window.addEventListener('resize', function () {
fit();
if (reduce) stillFrame();
});
fit();
if (reduce) {
stillFrame();
root.classList.add('is-still');
} else {
raf = requestAnimationFrame(frame);
}
})();// AgentSensorBasic — vanilla 판(run/320/demo/01-slime-agent-sensor-basic/index.html) 의 React 포트.
// 기본 3센서 에이전트. Physarum(점균) 스티그머지 알고리즘 — 에이전트마다 전방·좌·우 세 지점의
// 페로몬 필드를 읽어(sense) 가장 짙은 쪽으로 돈 뒤(rotate) 한 걸음 전진하며 지나간 칸에 트레일을
// 쌓는다(deposit). 필드는 매 프레임 이웃 평균으로 살짝 번지고(diffuse) 일정 비율 옅어진다(evaporate).
import { useEffect, useRef, useState } from "react";
import type { CSSProperties } from "react";
import styles from "./AgentSensorBasic.module.scss";
export type AgentSensorBasicProps = {
duration?: string;
easing?: string;
color?: string;
agents?: number; // 동시에 걷는 에이전트 수 — 네트워크 밀도
decay?: number; // 증발률(프레임당)
sensorDist?: number; // 센서가 앞을 내다보는 거리(필드 칸)
};
type Vars = CSSProperties & { "--duration"?: string; "--easing"?: string; "--color"?: string };
type Box<T> = { current: T };
type RGB = [number, number, number];
const FIELD_LABEL = "3센서 점균 에이전트 실험판";
const SENSOR_ANGLE = 0.5; // 센서 벌림각(라디안)
const TURN = 0.5; // 한 스텝에 도는 각(라디안)
const STEP = 1.05; // 한 스텝 이동 거리(필드 칸)
const DEPOSIT = 4.4; // 한 스텝에 쌓는 페로몬 양
const DIFFUSE = 0.5; // 확산 시 이웃과 섞이는 비율
const CAP = 7; // 페로몬 상한(색 매핑 기준)
const SUBSTEPS = 3; // rAF 프레임당 시뮬 스텝 수
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 AgentSensorBasic({
duration = "2s",
easing = "cubic-bezier(.2, .8, .2, 1)",
color = "#b9a0ff",
agents = 1300,
decay = 0.045,
sensorDist = 5.2,
}: AgentSensorBasicProps) {
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 colRef = useRef<{ ink: RGB; glow: RGB; hot: RGB } | null>(null);
const agentsRef = useRef(agents) as Box<number>;
const decayRef = useRef(decay) as Box<number>;
const sensorDistRef = useRef(sensorDist) as Box<number>;
const randRef = useRef(makePRNG(0x320a01)) as Box<() => number>;
const fwRef = useRef(0) as Box<number>;
const fhRef = useRef(0) as Box<number>;
const trailRef = useRef<Float32Array>(new Float32Array(0)) as Box<Float32Array>;
const trailBRef = useRef<Float32Array>(new Float32Array(0)) as Box<Float32Array>;
const axRef = useRef<Float32Array>(new Float32Array(0)) as Box<Float32Array>;
const ayRef = useRef<Float32Array>(new Float32Array(0)) as Box<Float32Array>;
const ahRef = useRef<Float32Array>(new Float32Array(0)) as Box<Float32Array>;
const imgRef = useRef<ImageData | null>(null) as Box<ImageData | null>;
const permRef = useRef<Uint8Array>(new Uint8Array(512)) as Box<Uint8Array>;
const stepCountRef = useRef(0) as Box<number>;
const rafRef = useRef(0) as Box<number>;
const [n, setN] = useState("0");
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;
}
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 buildPerm(): void {
const rand = randRef.current;
const p: number[] = [];
for (let i = 0; i < 256; i++) p.push(i);
for (let i2 = 255; i2 > 0; i2--) {
const k2 = Math.floor(rand() * (i2 + 1));
const tmp = p[i2]; p[i2] = p[k2]; p[k2] = tmp;
}
const perm = new Uint8Array(512);
for (let i3 = 0; i3 < 512; i3++) perm[i3] = p[i3 & 255];
permRef.current = perm;
}
function fade(k: number): number { return k * k * (3 - 2 * k); }
function vnoise(x: number, y: number): number {
const perm = permRef.current;
const xi = Math.floor(x), yi = Math.floor(y);
const xf = x - xi, yf = y - yi;
const a2 = perm[(perm[xi & 255] + yi) & 255];
const b2 = perm[(perm[(xi + 1) & 255] + yi) & 255];
const c2 = perm[(perm[xi & 255] + yi + 1) & 255];
const d2 = perm[(perm[(xi + 1) & 255] + yi + 1) & 255];
const u = fade(xf), v = fade(yf);
return (a2 + (b2 - a2) * u + (c2 - a2) * v + (a2 - b2 - c2 + d2) * u * v) / 255;
}
function idx(x: number, y: number): number {
const fw = fwRef.current, fh = fhRef.current;
let xx = x % fw; if (xx < 0) xx += fw;
let yy = y % fh; if (yy < 0) yy += fh;
return (yy * fw + xx) | 0;
}
function sample(x: number, y: number): number { return trailRef.current[idx(Math.round(x), Math.round(y))]; }
function initAgents(): void {
const rand = randRef.current;
const A = agentsRef.current;
const fw = fwRef.current, fh = fhRef.current;
const ax = new Float32Array(A), ay = new Float32Array(A), ah = new Float32Array(A);
const cx = fw / 2, cy = fh / 2, r = Math.min(fw, fh) * 0.14;
for (let i = 0; i < A; i++) {
const a = rand() * Math.PI * 2, rr = rand() * r;
ax[i] = cx + Math.cos(a) * rr;
ay[i] = cy + Math.sin(a) * rr;
ah[i] = rand() * Math.PI * 2;
}
axRef.current = ax; ayRef.current = ay; ahRef.current = ah;
}
function stepAgents(): void {
const rand = randRef.current;
const A = agentsRef.current;
const fw = fwRef.current, fh = fhRef.current;
const ax = axRef.current, ay = ayRef.current, ah = ahRef.current;
const trail = trailRef.current;
const sd = sensorDistRef.current;
for (let i = 0; i < A; i++) {
const x = ax[i], y = ay[i];
let h = ah[i];
const sf = sample(x + Math.cos(h) * sd, y + Math.sin(h) * sd);
const sl = sample(x + Math.cos(h + SENSOR_ANGLE) * sd, y + Math.sin(h + SENSOR_ANGLE) * sd);
const sr = sample(x + Math.cos(h - SENSOR_ANGLE) * sd, y + Math.sin(h - SENSOR_ANGLE) * sd);
if (sf >= sl && sf >= sr) {
// 직진 — 가장 짙은 쪽이 정면
} else if (sl > sr) {
h += TURN;
} else if (sr > sl) {
h -= TURN;
} else {
h += (rand() < 0.5 ? 1 : -1) * TURN;
}
h += (rand() - 0.5) * 0.14;
let nx = x + Math.cos(h) * STEP, ny = y + Math.sin(h) * STEP;
nx = ((nx % fw) + fw) % fw; ny = ((ny % fh) + fh) % fh;
ax[i] = nx; ay[i] = ny; ah[i] = h;
const ii = idx(Math.round(nx), Math.round(ny));
trail[ii] = Math.min(CAP, trail[ii] + DEPOSIT);
}
stepCountRef.current++;
}
function diffuseEvap(): void {
const fw = fwRef.current, fh = fhRef.current;
const trail = trailRef.current, trailB = trailBRef.current;
const dc = decayRef.current;
for (let y = 0; y < fh; y++) {
const ym = y === 0 ? fh - 1 : y - 1, yp = y === fh - 1 ? 0 : y + 1;
const rowY = y * fw, rowYm = ym * fw, rowYp = yp * fw;
for (let x = 0; x < fw; x++) {
const xm = x === 0 ? fw - 1 : x - 1, xp = x === fw - 1 ? 0 : x + 1;
const i = rowY + x;
const avg = (trail[rowY + xm] + trail[rowY + xp] + trail[rowYm + x] + trail[rowYp + x]) * 0.25;
trailB[i] = (trail[i] * (1 - DIFFUSE) + avg * DIFFUSE) * (1 - dc);
}
}
trailRef.current = trailB; trailBRef.current = trail;
}
function render(t: number): void {
const ctx = ctxRef.current;
const col = colRef.current;
const img = imgRef.current;
if (!ctx || !col || !img) return;
const fw = fwRef.current, fh = fhRef.current;
const trail = trailRef.current;
const data = img.data;
for (let y = 0; y < fh; y++) {
for (let x = 0; x < fw; x++) {
const i = y * fw + x;
let v = trail[i] / CAP; if (v > 1) v = 1;
const nz = vnoise(x * 0.06, y * 0.06 + t * 0.5);
const amb = 0.5 + 0.5 * Math.sin(t * 1.15 + nz * 6.2831);
const k = v * v * (3 - 2 * v);
const rr = col.ink[0] + (col.glow[0] - col.ink[0]) * k + (col.hot[0] - col.glow[0]) * Math.max(0, v - 0.72) * 3.4;
const gg = col.ink[1] + (col.glow[1] - col.ink[1]) * k + (col.hot[1] - col.glow[1]) * Math.max(0, v - 0.72) * 3.4;
const bb = col.ink[2] + (col.glow[2] - col.ink[2]) * k + (col.hot[2] - col.glow[2]) * Math.max(0, v - 0.72) * 3.4;
const sh = (amb - 0.5) * 16 * (1 - v * 0.6);
const j = i * 4;
data[j] = rr + sh; data[j + 1] = gg + sh; data[j + 2] = bb + sh; data[j + 3] = 255;
}
}
ctx.putImageData(img, 0, 0);
}
function fit(): void {
const field = fieldRef.current;
const cv = cvRef.current;
if (!field || !cv) return;
const r = field.getBoundingClientRect();
const W = Math.round(r.width), H = Math.round(r.height);
const fw = Math.max(90, Math.min(200, Math.round(W / 3)));
const fh = Math.max(50, Math.min(130, Math.round(H / 3)));
fwRef.current = fw; fhRef.current = fh;
cv.width = fw; cv.height = fh;
cv.style.width = "100%"; cv.style.height = "100%";
const ctx = cv.getContext("2d", { alpha: false });
if (!ctx) return;
ctxRef.current = ctx;
const img = ctx.createImageData(fw, fh);
imgRef.current = img;
trailRef.current = new Float32Array(fw * fh);
trailBRef.current = new Float32Array(fw * fh);
stepCountRef.current = 0;
initAgents();
}
function stillFrame(): void {
for (let k = 0; k < 220; k++) { stepAgents(); diffuseEvap(); }
render(3.4);
}
let t0 = -1;
function frame(now: number): void {
if (t0 < 0) t0 = now;
const t = (now - t0) / 1000;
for (let s = 0; s < SUBSTEPS; s++) { stepAgents(); diffuseEvap(); }
render(t);
setN(String(stepCountRef.current));
rafRef.current = requestAnimationFrame(frame);
}
useEffect(() => {
const root = rootRef.current;
if (!root) return;
agentsRef.current = agents;
decayRef.current = decay;
sensorDistRef.current = sensorDist;
randRef.current = makePRNG(0x320a01);
colRef.current = {
ink: tokRGB(root, "--sm-ink"),
glow: tokRGB(root, "--sm-glow"),
hot: tokRGB(root, "--sm-hot"),
};
buildPerm();
fit();
setN("0");
function onResize(): void {
fit();
if (reduced()) stillFrame();
}
window.addEventListener("resize", onResize);
if (reduced()) {
stillFrame();
setStill(true);
} else {
t0 = -1;
rafRef.current = requestAnimationFrame(frame);
}
return () => {
window.removeEventListener("resize", onResize);
cancelAnimationFrame(rafRef.current);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [agents, decay, sensorDist]);
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}>
<span>에이전트마다 전방 부채꼴 세 지점의 페로몬 농도를 읽어 가장 짙은 쪽으로 살짝 돌고 한 걸음 전진하며 지나간 자리에 트레일을 남기면, 수천 개가 모여 저절로 혈관 같은 네트워크가 자랍니다</span>
</p>
<div
className={styles.field}
ref={fieldRef}
role="application"
aria-label={FIELD_LABEL}
tabIndex={0}
>
<canvas className={styles.cv} ref={cvRef}></canvas>
</div>
<div className={styles.bar}>
<span className={styles.meta}><b className={styles.val}>{n}</b> 스텝 · 기본 3센서</span>
<span className={styles.metaDim}>canvas · ImageData · requestAnimationFrame</span>
</div>
</div>
</main>
);
}01 기본 3센서 에이전트 — 앞·좌·우 냄새를 맡고 도는 점균 한 마리 — 측정 누적면적 51.385% · 움직인 프레임 23/23GODRICH 원작 · zip 에 SCSS·React 포함 · 슬라임몰드 길찾기 9종 — 점균 네트워크가 스스로 최단경로를 찾는 캔버스
이 데모가 속한 글
슬라임몰드 길찾기 9종 — 점균 네트워크가 스스로 최단경로를 찾는 캔버스슬라임몰드 길찾기를 캔버스 픽셀 계산만으로 재현합니다. 라이브러리·WebGL 없이 화면 위를 기어다니는 점 하나하나가 앞·좌·우 냄새를 맡고 진한 쪽으로 도는 규칙만 반복하는 것으로, 미로의 최단경로를 스스로 찾아내 이 글의 데모 전부 보기


