05Multi-species territory — two colored colonies splitting the canvas
Make each species respond only to its own trail and treat the other's as an obstacle — two colored colonies push each other apart into sharply bordered territories.
Best forRivalry/competition concept backgrounds, game lobby screens
ImageDatarequestAnimationFrame

<main class="stage">
<div class="sm is-demo" id="smRoot">
<p class="sm__hint">
<span>Make each species respond only to its own trail and treat the other's as an obstacle — two colored colonies push each other apart into sharply bordered territories</span>
</p>
<div class="sm__field" id="smField" role="application" aria-label="Multi-Species Territory Slime 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"> steps · two-species border</span></span>
<span class="sm__meta sm__meta--dim">canvas · ImageData · requestAnimationFrame</span>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 다종 영역 경쟁(05). @keyframes 를 두지 않는다 —
// 움직임은 전부 캔버스 rAF 루프(두 종 각자 3센서 감지 → 회전 → 자기 트레일 → 확산·증발)가 만든다.
// 무대는 다시 먹(ink) — 01(밤)·02(종이)·03(밀짚)·04(노을) 다음 다섯 번째 순환. 어두운 바닥 위에
// 두 파스텔 종(핑크·민트)이 발광하며 영역 경계를 그린다.
$duration: $dur-loop;
$easing: $ease-spring;
$color: $subject-cream;
// ── 이 항목만의 파스텔 팔레트(어두운 무대 L0.1 미만 + 파스텔 종 L0.9 이상 — BD26 안전) ──
$sm-stage: #1a1620; // 밤무대 먹 — 01 의 밤과 같은 계열
@include stage($sm-stage);
$sm-ink: #1a1620; // 필드 바닥 — 무대와 같은 톤
$sm-a: #ffd6e0; // 종 A — 파스텔 핑크
$sm-b: #d6f5e3; // 종 B — 파스텔 민트
$sm-text: #f3ecdd; // 먹 무대 위 — 크림 글자(대비)
.sm {
--sm-ink: #{$sm-ink};
--sm-a: #{$sm-a};
--sm-b: #{$sm-b};
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-text; outline-offset: 2px; }
.sm__cv { display: block; width: 100%; height: 100%; }
.sm__bar { display: flex; align-items: center; justify-content: space-between; gap: $sp-2; }
.sm__val { color: $sm-a; }
.sm__meta { font-size: 10px; font-weight: 700; color: rgba($sm-text, .72); }
.sm__meta--dim { font-size: 10px; font-weight: 500; color: rgba($sm-text, .72); }
@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 { opacity: 1; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #1a1620;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.sm {
--sm-ink: #1a1620;
--sm-a: #ffd6e0;
--sm-b: #d6f5e3;
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: #f3ecdd;
}
.sm__field {
position: relative;
height: 180px;
border-radius: 16px;
overflow: hidden;
background: #1a1620;
box-shadow: inset 0 0 0 1px rgba(243, 236, 221, 0.18);
}
.sm__field:focus-visible {
outline: 2px solid #f3ecdd;
outline-offset: 2px;
}
.sm__cv {
display: block;
width: 100%;
height: 100%;
}
.sm__bar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.sm__val {
color: #ffd6e0;
}
.sm__meta {
font-size: 10px;
font-weight: 700;
color: rgba(243, 236, 221, 0.72);
}
.sm__meta--dim {
font-size: 10px;
font-weight: 500;
color: rgba(243, 236, 221, 0.72);
}
@media (max-width: 340px) {
.sm {
gap: 4px;
}
.sm__field {
height: 100px;
}
.sm__meta--dim {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
.sm.is-still {
opacity: 1;
}
}// 다종 영역 경쟁(05). Physarum 스티그머지 + 두 페로몬 필드 상호 기피 —
// 종 A·B 는 각자의 트레일 필드만 따로 갖는다. 센싱 값은 "내 종 트레일 - 남의 종 트레일 × 기피가중"
// 이라 자기 흔적은 강화되고 상대 흔적은 장애물처럼 밀어낸다. 두 군집이 중앙에서 만나 서로
// 밀고 밀리며 화면이 뚜렷한 경계선을 가진 두 영역으로 갈라진다.
//
// 화면이 사는 장치 — 077 캔버스 기준의 "매 프레임 계산":
// · 캔버스는 절대 clear 하지 않는다. 두 필드를 합성해 매 프레임 새로 계산해 얹는다.
// · 앰비언트 글로우 레이어(두 옥타브 값노이즈)가 전면을 확산 구조로 흔들어 누적면적을 채운다.
(function () {
// ── 변형 상수 블록 ──
var AGENTS_A = 700, AGENTS_B = 700; // 종마다 개체 수
var DECAY = 0.048; // 증발률
var REPEL_W = 1.7; // 상대 종 트레일 기피 가중치
// ────────────────────────────────────
var SENSOR_ANGLE = 0.5;
var SENSOR_DIST = 5.0;
var TURN = 0.5;
var STEP = 1.05;
var DEPOSIT = 4.2;
var DIFFUSE = 0.5;
var CAP = 7;
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(0x320A05);
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_A = tokRGB('--sm-a');
var C_B = tokRGB('--sm-b');
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var W = 0, H = 0, fw = 0, fh = 0, raf = 0, t0 = -1;
var trailA, trailAB, trailB, trailBB, img, data;
var axA, ayA, ahA, axB, ayB, ahB;
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 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 senseFrom(own, other, x, y) {
var i = clampIdx(x, y);
return own[i] - other[i] * REPEL_W;
}
function initSpecies(count, cx0) {
var ax = new Float32Array(count), ay = new Float32Array(count), ah = new Float32Array(count);
var cx = fw * cx0, cy = fh / 2, r = Math.min(fw, fh) * 0.13;
for (var i = 0; i < count; 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;
}
return [ax, ay, ah];
}
function stepSpecies(ax, ay, ah, count, own, other) {
for (var i = 0; i < count; i++) {
var x = ax[i], y = ay[i], h = ah[i];
var sf = senseFrom(own, other, x + Math.cos(h) * SENSOR_DIST, y + Math.sin(h) * SENSOR_DIST);
var sl = senseFrom(own, other, x + Math.cos(h + SENSOR_ANGLE) * SENSOR_DIST, y + Math.sin(h + SENSOR_ANGLE) * SENSOR_DIST);
var sr = senseFrom(own, other, 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));
own[ii] = Math.min(CAP, own[ii] + DEPOSIT);
}
}
function diffuseEvapOne(field0, field1) {
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 = (field0[rowY + xm] + field0[rowY + xp] + field0[rowYm + x] + field0[rowYp + x]) * 0.25;
field1[i] = (field0[i] * (1 - DIFFUSE) + avg * DIFFUSE) * (1 - DECAY);
}
}
}
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 va = trailA[i] / CAP; if (va > 1) va = 1;
var vb = trailB[i] / CAP; if (vb > 1) vb = 1;
var ka = va * va * (3 - 2 * va);
var kb = vb * vb * (3 - 2 * vb);
var rr = C_INK[0] + (C_A[0] - C_INK[0]) * ka + (C_B[0] - C_INK[0]) * kb;
var gg = C_INK[1] + (C_A[1] - C_INK[1]) * ka + (C_B[1] - C_INK[1]) * kb;
var bb = C_INK[2] + (C_A[2] - C_INK[2]) * ka + (C_B[2] - C_INK[2]) * kb;
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) * 22 + (n2 - 0.5) * 14;
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;
trailA = new Float32Array(fw * fh); trailAB = new Float32Array(fw * fh);
trailB = new Float32Array(fw * fh); trailBB = new Float32Array(fw * fh);
stepCount = 0;
var sa = initSpecies(AGENTS_A, 0.3); axA = sa[0]; ayA = sa[1]; ahA = sa[2];
var sb = initSpecies(AGENTS_B, 0.7); axB = sb[0]; ayB = sb[1]; ahB = sb[2];
}
function stepAll() {
stepSpecies(axA, ayA, ahA, AGENTS_A, trailA, trailB);
stepSpecies(axB, ayB, ahB, AGENTS_B, trailB, trailA);
diffuseEvapOne(trailA, trailAB);
diffuseEvapOne(trailB, trailBB);
var ta = trailA; trailA = trailAB; trailAB = ta;
var tb = trailB; trailB = trailBB; trailBB = tb;
stepCount++;
}
function stillFrame() {
for (var k = 0; k < 260; k++) stepAll();
render(2.6);
}
function frame(now) {
if (t0 < 0) t0 = now;
var t = (now - t0) / 1000;
for (var s = 0; s < SUBSTEPS; s++) stepAll();
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);
}
})();// MultiSpeciesTerritory — vanilla 판(run/320/demo/05-slime-multi-species-territory/index.html) 의 React 포트.
// 다종 영역 경쟁. 두 종(A·B) 이 각자의 트레일 필드만 갖고, 센싱값은 "내 종 - 남의 종 × 기피가중"
// 이라 자기 흔적은 강화되고 상대 흔적은 장애물처럼 밀어낸다 — 화면이 뚜렷한 경계선을 가진 두
// 영역으로 갈라진다.
import { useEffect, useRef, useState } from "react";
import type { CSSProperties } from "react";
import styles from "./MultiSpeciesTerritory.module.scss";
export type MultiSpeciesTerritoryProps = {
duration?: string;
easing?: string;
color?: string;
agentsA?: number;
agentsB?: number;
decay?: number;
repelWeight?: number;
};
type Vars = CSSProperties & { "--duration"?: string; "--easing"?: string; "--color"?: string };
type Box<T> = { current: T };
type RGB = [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.2;
const DIFFUSE = 0.5;
const CAP = 7;
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 MultiSpeciesTerritory({
duration = "2s",
easing = "cubic-bezier(.2, .8, .2, 1)",
color = "#f3ecdd",
agentsA = 700,
agentsB = 700,
decay = 0.048,
repelWeight = 1.7,
}: MultiSpeciesTerritoryProps) {
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; a: RGB; b: RGB } | null>(null);
const agentsARef = useRef(agentsA) as Box<number>;
const agentsBRef = useRef(agentsB) as Box<number>;
const decayRef = useRef(decay) as Box<number>;
const repelRef = useRef(repelWeight) as Box<number>;
const randRef = useRef(makePRNG(0x320a05)) as Box<() => number>;
const fwRef = useRef(0) as Box<number>;
const fhRef = useRef(0) as Box<number>;
const trailARef = useRef<Float32Array>(new Float32Array(0)) as Box<Float32Array>;
const trailABRef = useRef<Float32Array>(new Float32Array(0)) as Box<Float32Array>;
const trailBRef = useRef<Float32Array>(new Float32Array(0)) as Box<Float32Array>;
const trailBBRef = useRef<Float32Array>(new Float32Array(0)) as Box<Float32Array>;
const axARef = useRef<Float32Array>(new Float32Array(0)) as Box<Float32Array>;
const ayARef = useRef<Float32Array>(new Float32Array(0)) as Box<Float32Array>;
const ahARef = useRef<Float32Array>(new Float32Array(0)) as Box<Float32Array>;
const axBRef = useRef<Float32Array>(new Float32Array(0)) as Box<Float32Array>;
const ayBRef = useRef<Float32Array>(new Float32Array(0)) as Box<Float32Array>;
const ahBRef = 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 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 senseFrom(own: Float32Array, other: Float32Array, x: number, y: number): number {
const i = clampIdx(x, y);
return own[i] - other[i] * repelRef.current;
}
function initSpecies(count: number, cx0: number): [Float32Array, Float32Array, Float32Array] {
const rand = randRef.current;
const fw = fwRef.current, fh = fhRef.current;
const ax = new Float32Array(count), ay = new Float32Array(count), ah = new Float32Array(count);
const cx = fw * cx0, cy = fh / 2, r = Math.min(fw, fh) * 0.13;
for (let i = 0; i < count; 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;
}
return [ax, ay, ah];
}
function stepSpecies(
ax: Float32Array, ay: Float32Array, ah: Float32Array, count: number,
own: Float32Array, other: Float32Array,
): void {
const rand = randRef.current;
const fw = fwRef.current, fh = fhRef.current;
for (let i = 0; i < count; i++) {
const x = ax[i], y = ay[i];
let h = ah[i];
const sf = senseFrom(own, other, x + Math.cos(h) * SENSOR_DIST, y + Math.sin(h) * SENSOR_DIST);
const sl = senseFrom(own, other, x + Math.cos(h + SENSOR_ANGLE) * SENSOR_DIST, y + Math.sin(h + SENSOR_ANGLE) * SENSOR_DIST);
const sr = senseFrom(own, other, 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));
own[ii] = Math.min(CAP, own[ii] + DEPOSIT);
}
}
function diffuseEvapOne(field0: Float32Array, field1: Float32Array): void {
const fw = fwRef.current, fh = fhRef.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 = (field0[rowY + xm] + field0[rowY + xp] + field0[rowYm + x] + field0[rowYp + x]) * 0.25;
field1[i] = (field0[i] * (1 - DIFFUSE) + avg * DIFFUSE) * (1 - dc);
}
}
}
function stepAll(): void {
stepSpecies(axARef.current, ayARef.current, ahARef.current, agentsARef.current, trailARef.current, trailBRef.current);
stepSpecies(axBRef.current, ayBRef.current, ahBRef.current, agentsBRef.current, trailBRef.current, trailARef.current);
diffuseEvapOne(trailARef.current, trailABRef.current);
diffuseEvapOne(trailBRef.current, trailBBRef.current);
const ta = trailARef.current; trailARef.current = trailABRef.current; trailABRef.current = ta;
const tb = trailBRef.current; trailBRef.current = trailBBRef.current; trailBBRef.current = tb;
stepCountRef.current++;
}
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 trailA = trailARef.current, trailB = trailBRef.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 va = trailA[i] / CAP; if (va > 1) va = 1;
let vb = trailB[i] / CAP; if (vb > 1) vb = 1;
const ka = va * va * (3 - 2 * va);
const kb = vb * vb * (3 - 2 * vb);
let rr = col.ink[0] + (col.a[0] - col.ink[0]) * ka + (col.b[0] - col.ink[0]) * kb;
let gg = col.ink[1] + (col.a[1] - col.ink[1]) * ka + (col.b[1] - col.ink[1]) * kb;
let bb = col.ink[2] + (col.a[2] - col.ink[2]) * ka + (col.b[2] - col.ink[2]) * kb;
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) * 22 + (n2 - 0.5) * 14;
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;
trailARef.current = new Float32Array(fw * fh);
trailABRef.current = new Float32Array(fw * fh);
trailBRef.current = new Float32Array(fw * fh);
trailBBRef.current = new Float32Array(fw * fh);
stepCountRef.current = 0;
const sa = initSpecies(agentsARef.current, 0.3);
axARef.current = sa[0]; ayARef.current = sa[1]; ahARef.current = sa[2];
const sb = initSpecies(agentsBRef.current, 0.7);
axBRef.current = sb[0]; ayBRef.current = sb[1]; ahBRef.current = sb[2];
}
function stillFrame(): void {
for (let k = 0; k < 260; k++) stepAll();
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++) stepAll();
render(t);
setN(String(stepCountRef.current));
rafRef.current = requestAnimationFrame(frame);
}
useEffect(() => {
const root = rootRef.current;
if (!root) return;
agentsARef.current = agentsA;
agentsBRef.current = agentsB;
decayRef.current = decay;
repelRef.current = repelWeight;
randRef.current = makePRNG(0x320a05);
colRef.current = {
ink: tokRGB(root, "--sm-ink"),
a: tokRGB(root, "--sm-a"),
b: tokRGB(root, "--sm-b"),
};
buildPerm();
fit();
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
}, [agentsA, agentsB, decay, repelWeight]);
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>종(species)마다 자기 트레일에만 반응하고 남의 트레일은 장애물로 취급하게 하면, 두 색 군집이 서로 밀어내며 화면을 경계선 뚜렷한 영역으로 나눕니다</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> 스텝 · 2종 경계선</span>
</span>
<span className={styles.metaDim}>canvas · ImageData · requestAnimationFrame</span>
</div>
</div>
</main>
);
}05 Multi-species territory — two colored colonies splitting the canvas — Measured area 53.258% · 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 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