03Multi-food network — optimizing connections like a rail map
Scatter several food points and the slime sorts them into thick trunk routes and thin branch lines by itself — an efficient radial transport map.
Best forNetwork diagrams, logistics/transit content heroes
ImageDatarequestAnimationFrame

<main class="stage">
<div class="sm is-demo" id="smRoot">
<p class="sm__hint">
<span>Scatter several food points and the slime sorts them into thick trunk routes and thin branch lines by itself — an efficient radial transport map</span>
</p>
<div class="sm__field" id="smField" role="application" aria-label="Multi-Food Slime Network Demo" 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"> agents · radial transport network</span></span>
<span class="sm__meta sm__meta--dim">canvas · ImageData · requestAnimationFrame</span>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 다중 먹이 네트워크(03). @keyframes 를 두지 않는다 —
// 움직임은 전부 캔버스 rAF 루프(먹이점 6개 바이어스 + 3센서 감지 → 회전 → 트레일 → 확산·증발)가 만든다.
// 무대는 볕든 밀짚색 — 01(밤)·02(종이) 다음 세 번째 순환.
$duration: $dur-loop;
$easing: $ease-spring;
$color: $subject-cream;
// ── 이 항목만의 파스텔 팔레트(밝은 계열, L 0.82 이상 — BD26 안전) ──
$sm-stage: #faf0d0; // 볕든 밀짚색 무대
@include stage($sm-stage);
$sm-floor: #faf0d0; // 필드 바닥 — 무대와 같은 톤
$sm-branch: #f7e6bf; // 가는 지선 — 바닥보다 한 톤 진한 밀짚
$sm-trunk: #f8ddaa; // 굵은 간선 — 더 진한 골든
$sm-food: #ffc4ad; // 먹이 맥동 — 파스텔 코랄(L 0.839)
$sm-text: #3a2f1a; // 볕든 무대 위 — 짙은 흙색 글자(대비)
.sm {
--sm-floor: #{$sm-floor};
--sm-trunk: #{$sm-trunk};
--sm-branch: #{$sm-branch};
--sm-food: #{$sm-food};
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-floor;
box-shadow: inset 0 0 0 1px rgba($sm-text, .18);
}
.sm__field:focus-visible { outline: 2px solid $sm-text; 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-text; 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: #faf0d0;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.sm {
--sm-floor: #faf0d0;
--sm-trunk: #f8ddaa;
--sm-branch: #f7e6bf;
--sm-food: #ffc4ad;
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: #3a2f1a;
}
.sm__field {
position: relative;
height: 180px;
border-radius: 16px;
overflow: hidden;
background: #faf0d0;
box-shadow: inset 0 0 0 1px rgba(58, 47, 26, 0.18);
}
.sm__field:focus-visible {
outline: 2px solid #3a2f1a;
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(58, 47, 26, 0.72);
}
.sm__val {
color: #3a2f1a;
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;
}
}// 다중 먹이 네트워크(03). Physarum 스티그머지 + 먹이점 6개 —
// 화면에 먹이 앵커 6개를 흩뿌리고, 에이전트 센싱값에 "가장 가까운 먹이로 갈수록 유리"한
// 가우시안 바이어스를 트레일 위에 얹는다. 여러 먹이를 잇는 지름길은 왕복이 잦아 굵은 간선으로
// 남고, 외곽 먹이 하나로만 가는 가지는 가늘게 남는다 — 변형 "에이전트 수"(4천→1만2천)를
// 반영해 01 보다 개체 수를 크게 늘려(2200) 네트워크 밀도를 높였다.
//
// 화면이 사는 장치 — 077 캔버스 기준의 "매 프레임 계산":
// · 캔버스는 절대 clear 하지 않는다. 필드 전체를 매 프레임 새로 계산해 얹는다.
// · 값노이즈 기반 주변광 요동이 바닥·간선을 가리지 않고 함께 흔들어 누적면적을 채운다.
// · 먹이점 6개가 각자 다른 위상으로 맥동해 화면 전역이 살아 있다.
(function () {
// ── 변형 상수 블록 ──
var AGENTS = 2200; // 변형 "에이전트 수"(4천→1만2천)를 반영 — 01(1300)보다 밀도 상향
var DECAY = 0.038; // 증발률 — 간선/지선 구분이 살도록 01보다 살짝 느리게
var FOOD_W = 2.0; // 먹이향이 센서 판단에 끼치는 가중치
// ────────────────────────────────────
var SENSOR_ANGLE = 0.5;
var SENSOR_DIST = 5.0;
var TURN = 0.5;
var STEP = 1.05;
var DEPOSIT = 4.0;
var DIFFUSE = 0.5;
var CAP = 8;
var SUBSTEPS = 3;
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(0x320A03);
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_FLOOR = tokRGB('--sm-floor');
var C_TRUNK = tokRGB('--sm-trunk');
var C_BRANCH = tokRGB('--sm-branch');
var C_FOOD = tokRGB('--sm-food');
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, food, foodMax, img, data;
var ax, ay, ah;
var stepCount = 0;
var foods = []; // [x, y, phase]
// ── 값노이즈(주변광 요동용) ──
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 clampIdx(x, y) {
x = Math.max(0, Math.min(fw - 1, Math.round(x)));
y = Math.max(0, Math.min(fh - 1, Math.round(y)));
return y * fw + x;
}
function buildFoods() {
foods = [];
var n = 6;
var cx = fw / 2, cy = fh / 2, r = Math.min(fw, fh) * 0.42;
for (var i = 0; i < n; i++) {
var a = (i / n) * Math.PI * 2 + rand() * 0.5;
var rr = r * (0.7 + rand() * 0.3);
foods.push([cx + Math.cos(a) * rr, cy + Math.sin(a) * rr, rand() * Math.PI * 2]);
}
food = new Float32Array(fw * fh);
for (var y = 0; y < fh; y++) {
for (var x = 0; x < fw; x++) {
var best = 0;
for (var p = 0; p < foods.length; p++) {
var dx = x - foods[p][0], dy = y - foods[p][1];
var d2b = dx * dx + dy * dy;
var v = Math.exp(-d2b / (fw * 0.9));
if (v > best) best = v;
}
food[y * fw + x] = best;
}
}
}
function sense(x, y) {
var i = clampIdx(x, y);
return trail[i] + food[i] * FOOD_W * CAP;
}
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.12;
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 = sense(x + Math.cos(h) * SENSOR_DIST, y + Math.sin(h) * SENSOR_DIST);
var sl = sense(x + Math.cos(h + SENSOR_ANGLE) * SENSOR_DIST, y + Math.sin(h + SENSOR_ANGLE) * SENSOR_DIST);
var sr = sense(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 j = i * 4;
var v = trail[i] / CAP; if (v > 1) v = 1;
// 앰비언트 글로우 레이어 — 전면 확산 구조(314 기법): 두 옥타브 값노이즈로
// 필드 전체를 크게 흔들어 간선·지선 구분과 별개로 누적면적을 화면 전역으로 채운다.
var n1 = vnoise(x * 0.09, y * 0.09 + t * 0.9);
var n2 = vnoise(x * 0.03 + 40, y * 0.03 + t * 0.6);
var sh = (n1 - 0.5) * 28 + (n2 - 0.5) * 18;
// 굵기 단계 — 옅은 트레일은 지선(branch) 색, 짙은 트레일은 간선(trunk) 색
var kBranch = Math.min(1, v * 2.2);
var kTrunk = Math.max(0, v - 0.45) * 1.8;
var rr = C_FLOOR[0] + (C_BRANCH[0] - C_FLOOR[0]) * kBranch + (C_TRUNK[0] - C_BRANCH[0]) * kTrunk;
var gg = C_FLOOR[1] + (C_BRANCH[1] - C_FLOOR[1]) * kBranch + (C_TRUNK[1] - C_BRANCH[1]) * kTrunk;
var bb = C_FLOOR[2] + (C_BRANCH[2] - C_FLOOR[2]) * kBranch + (C_TRUNK[2] - C_BRANCH[2]) * kTrunk;
for (var p = 0; p < foods.length; p++) {
var dx = x - foods[p][0], dy = y - foods[p][1];
var d2c = dx * dx + dy * dy;
var pulse = 0.55 + 0.45 * Math.sin(t * 2.4 + foods[p][2]);
var glow = Math.exp(-d2c / 20) * pulse;
rr += (C_FOOD[0] - rr) * glow; gg += (C_FOOD[1] - gg) * glow; bb += (C_FOOD[2] - bb) * glow;
}
rr += sh; gg += sh; bb += sh;
data[j] = rr; data[j + 1] = gg; data[j + 2] = bb; 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(96, Math.min(200, Math.round(W / 3)));
fh = Math.max(52, Math.min(120, 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;
buildFoods();
initAgents();
}
function stillFrame() {
for (var k = 0; k < 260; k++) { stepAgents(); diffuseEvap(); }
render(2.6);
}
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(AGENTS);
raf = requestAnimationFrame(frame);
}
window.addEventListener('resize', function () {
fit();
if (reduce) stillFrame();
});
fit();
if (reduce) {
stillFrame();
root.classList.add('is-still');
} else {
raf = requestAnimationFrame(frame);
}
})();// NetworkFoodSource — vanilla 판(run/320/demo/03-slime-network-food-source/index.html) 의 React 포트.
// 다중 먹이 네트워크. Physarum 스티그머지 + 먹이점 6개 — 화면에 먹이 앵커 6개를 흩뿌리고,
// 센싱값에 "가까운 먹이로 갈수록 유리"한 가우시안 바이어스를 트레일 위에 얹는다. 여러 먹이를
// 잇는 지름길은 왕복이 잦아 굵은 간선(trunk)으로 남고, 외곽 먹이 하나로만 가는 가지는 가는
// 지선(branch)으로 남는다.
import { useEffect, useRef, useState } from "react";
import type { CSSProperties } from "react";
import styles from "./NetworkFoodSource.module.scss";
export type NetworkFoodSourceProps = {
duration?: string;
easing?: string;
color?: string;
agents?: number; // 01(1300)보다 밀도 상향
decay?: number; // 증발률 — 간선/지선 구분이 살도록 01보다 느리게
foodWeight?: number; // 먹이향이 센서 판단에 끼치는 가중치
};
type Vars = CSSProperties & { "--duration"?: string; "--easing"?: string; "--color"?: string };
type Box<T> = { current: T };
type RGB = [number, number, number];
type Food = [number, number, number];
const FIELD_LABEL = "다중 먹이 점균 네트워크 실험판";
const SENSOR_ANGLE = 0.5;
const SENSOR_DIST = 5.0;
const TURN = 0.5;
const STEP = 1.05;
const DEPOSIT = 4.0;
const DIFFUSE = 0.5;
const CAP = 8;
const SUBSTEPS = 3;
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 NetworkFoodSource({
duration = "2s",
easing = "cubic-bezier(.2, .8, .2, 1)",
color = "#f8ddaa",
agents = 2200,
decay = 0.038,
foodWeight = 2.0,
}: NetworkFoodSourceProps) {
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<{ floor: RGB; trunk: RGB; branch: RGB; food: RGB } | null>(null);
const agentsRef = useRef(agents) as Box<number>;
const decayRef = useRef(decay) as Box<number>;
const foodWRef = useRef(foodWeight) as Box<number>;
const randRef = useRef(makePRNG(0x320a03)) 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 foodFieldRef = useRef<Float32Array>(new Float32Array(0)) as Box<Float32Array>;
const foodsRef = useRef<Food[]>([]) as Box<Food[]>;
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 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 clampIdx(x: number, y: number): number {
const fw = fwRef.current, fh = fhRef.current;
const xx = Math.max(0, Math.min(fw - 1, Math.round(x)));
const yy = Math.max(0, Math.min(fh - 1, Math.round(y)));
return yy * fw + xx;
}
function buildFoods(): void {
const rand = randRef.current;
const fw = fwRef.current, fh = fhRef.current;
const foods: Food[] = [];
const nF = 6;
const cx = fw / 2, cy = fh / 2, r = Math.min(fw, fh) * 0.42;
for (let i = 0; i < nF; i++) {
const a = (i / nF) * Math.PI * 2 + rand() * 0.5;
const rr = r * (0.7 + rand() * 0.3);
foods.push([cx + Math.cos(a) * rr, cy + Math.sin(a) * rr, rand() * Math.PI * 2]);
}
foodsRef.current = foods;
const food = new Float32Array(fw * fh);
for (let y = 0; y < fh; y++) {
for (let x = 0; x < fw; x++) {
let best = 0;
for (let p = 0; p < foods.length; p++) {
const dx = x - foods[p][0], dy = y - foods[p][1];
const d2b = dx * dx + dy * dy;
const v = Math.exp(-d2b / (fw * 0.9));
if (v > best) best = v;
}
food[y * fw + x] = best;
}
}
foodFieldRef.current = food;
}
function sense(x: number, y: number): number {
const i = clampIdx(x, y);
return trailRef.current[i] + foodFieldRef.current[i] * foodWRef.current * CAP;
}
function initAgents(): void {
const rand = randRef.current;
const fw = fwRef.current, fh = fhRef.current;
const A = agentsRef.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.12;
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;
for (let i = 0; i < A; i++) {
const x = ax[i], y = ay[i];
let h = ah[i];
const sf = sense(x + Math.cos(h) * SENSOR_DIST, y + Math.sin(h) * SENSOR_DIST);
const sl = sense(x + Math.cos(h + SENSOR_ANGLE) * SENSOR_DIST, y + Math.sin(h + SENSOR_ANGLE) * SENSOR_DIST);
const sr = sense(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;
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);
}
}
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 foods = foodsRef.current;
const data = img.data;
for (let y = 0; y < fh; y++) {
for (let x = 0; x < fw; x++) {
const i = y * fw + x;
const j = i * 4;
let v = trail[i] / CAP; if (v > 1) v = 1;
const n1 = vnoise(x * 0.09, y * 0.09 + t * 0.9);
const n2 = vnoise(x * 0.03 + 40, y * 0.03 + t * 0.6);
const sh = (n1 - 0.5) * 28 + (n2 - 0.5) * 18;
const kBranch = Math.min(1, v * 2.2);
const kTrunk = Math.max(0, v - 0.45) * 1.8;
let rr = col.floor[0] + (col.branch[0] - col.floor[0]) * kBranch + (col.trunk[0] - col.branch[0]) * kTrunk;
let gg = col.floor[1] + (col.branch[1] - col.floor[1]) * kBranch + (col.trunk[1] - col.branch[1]) * kTrunk;
let bb = col.floor[2] + (col.branch[2] - col.floor[2]) * kBranch + (col.trunk[2] - col.branch[2]) * kTrunk;
for (let p = 0; p < foods.length; p++) {
const dx = x - foods[p][0], dy = y - foods[p][1];
const d2c = dx * dx + dy * dy;
const pulse = 0.55 + 0.45 * Math.sin(t * 2.4 + foods[p][2]);
const glow = Math.exp(-d2c / 20) * pulse;
rr += (col.food[0] - rr) * glow; gg += (col.food[1] - gg) * glow; bb += (col.food[2] - bb) * glow;
}
rr += sh; gg += sh; bb += sh;
data[j] = rr; data[j + 1] = gg; data[j + 2] = bb; 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(96, Math.min(200, Math.round(W / 3)));
const fh = Math.max(52, Math.min(120, 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);
buildFoods();
initAgents();
}
function stillFrame(): void {
for (let k = 0; k < 260; k++) { stepAgents(); diffuseEvap(); }
render(2.6);
}
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(agentsRef.current));
rafRef.current = requestAnimationFrame(frame);
}
useEffect(() => {
const root = rootRef.current;
if (!root) return;
agentsRef.current = agents;
decayRef.current = decay;
foodWRef.current = foodWeight;
randRef.current = makePRNG(0x320a03);
colRef.current = {
floor: tokRGB(root, "--sm-floor"),
trunk: tokRGB(root, "--sm-trunk"),
branch: tokRGB(root, "--sm-branch"),
food: tokRGB(root, "--sm-food"),
};
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, foodWeight]);
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> 에이전트 · 방사형 수송망</span>
<span className={styles.metaDim}>canvas · ImageData · requestAnimationFrame</span>
</div>
</div>
</main>
);
}03 Multi-food network — optimizing connections like a rail map — Measured area 36.237% · moving frames 23/23Original by GODRICH · SCSS and React ship in the zip · Slime Mold Simulation, 9 Ways (No Library)
The post this demo lives in
Slime Mold Simulation, 9 Ways (No Library)No library, no WebGL — just thousands of dots sniffing front, left, right and turning toward the strongest scent, and a See all demos in this post
If you like this effect - similar demos
Creative UI01 Spot array — dots rise where reaction wins9 Reaction Diffusion Simulations, No Library
Creative UI01 One sphere — the first 3D a minimal marcher makes9 Ray Marching Scenes in Canvas (No WebGL)
Creative UI01 Summed-sine surface — a Gerstner wave field9 Canvas Wave Animation Scenes: Gerstner Field