index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Nematodal2</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<canvas id="nematodeCanvas"></canvas>
<script src="script.js"></script>
</body>
</html>
style.css
/* styles.css */
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background: #050608;
overflow: hidden;
font-family: system-ui, sans-serif;
}
#slimeCanvas {
display: block;
width: 100%;
height: 100%;
}
script.js
// script.js
const canvas = document.getElementById("nematodeCanvas");
const ctx = canvas.getContext("2d");
// ---------- Canvas sizing ----------
let width, height;
function resize() {
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
}
window.addEventListener("resize", resize);
resize();
// ---------- Simulation state ----------
const trail = ctx.createImageData(width, height);
const worms = [];
// ---------- Tunable configuration ----------
const CONFIG = {
EGGS: 100,
SENSE_DISTANCE: 3,
SENSE_ANGLE: 0.45,
TURN_SPEED: 0.40,
MOVE_SPEED: 1.2,
INVERSE_LENGTH: 0.009,
EXPRESSION: 0.5,
MIN_LIFESPAN: 100,
MAX_LIFESPAN: 20000,
INVERT: false,
WRAP: false,
};
// Organic display labels for the UI
const LABELS = {
EGGS: "Spawning Potential",
SENSE_DISTANCE: "Vision Range",
SENSE_ANGLE: "Perception Arc",
TURN_SPEED: "Agility / Agitation",
MOVE_SPEED: "Kinetic Velocity",
INVERSE_LENGTH: "Memory Decay",
EXPRESSION: "Fractal Bloom",
MIN_LIFESPAN: "Lifespan (Min)",
MAX_LIFESPAN: "Lifespan (Max)",
INVERT: "Microscope Light",
WRAP: "Wrap Canvas",
};
// ---------- Presets ----------
const PRESETS = {
"Default Nematode": {
EGGS: 100, SENSE_DISTANCE: 3, SENSE_ANGLE: 0.45, TURN_SPEED: 0.40, MOVE_SPEED: 1.2,
INVERSE_LENGTH: 0.009, EXPRESSION: 0.5, MIN_LIFESPAN: 100, MAX_LIFESPAN: 20000,
INVERT: false, WRAP: false,
},
"The Web": {
EGGS: 1000, SENSE_DISTANCE: 18, SENSE_ANGLE: 0.45, TURN_SPEED: 0.40, MOVE_SPEED: 1.2,
INVERSE_LENGTH: 0.001, EXPRESSION: 0.5, MIN_LIFESPAN: 10000, MAX_LIFESPAN: 20000,
INVERT: false, WRAP: false,
},
"Embryo (Blastula)": {
EGGS: 300000, SENSE_DISTANCE: 18, SENSE_ANGLE: 0.45, TURN_SPEED: 0.40, MOVE_SPEED: 0.5,
INVERSE_LENGTH: 0.9, EXPRESSION: 0.5, MIN_LIFESPAN: 10000, MAX_LIFESPAN: 20000,
INVERT: false, WRAP: false,
},
"Reactor Core": {
EGGS: 100000, SENSE_DISTANCE: 30, SENSE_ANGLE: 0.45, TURN_SPEED: 0.3, MOVE_SPEED: 1.0,
INVERSE_LENGTH: 0.9, EXPRESSION: 0.9, MIN_LIFESPAN: 100, MAX_LIFESPAN: 900,
INVERT: false, WRAP: false,
},
"Nerve Tissue": {
EGGS: 100000, SENSE_DISTANCE: 30, SENSE_ANGLE: 0.15, TURN_SPEED: 0.3, MOVE_SPEED: 2.0,
INVERSE_LENGTH: 0.1, EXPRESSION: 0.5, MIN_LIFESPAN: 10000, MAX_LIFESPAN: 90000,
INVERT: false, WRAP: false,
},
"The Sprig": {
EGGS: 100000, SENSE_DISTANCE: 30, SENSE_ANGLE: 0.65, TURN_SPEED: 0.9, MOVE_SPEED: 1.0,
INVERSE_LENGTH: 0.1, EXPRESSION: 0.5, MIN_LIFESPAN: 10000, MAX_LIFESPAN: 90000,
INVERT: false, WRAP: false,
},
"Infinite Web": {
EGGS: 5000, SENSE_DISTANCE: 12, SENSE_ANGLE: 0.45, TURN_SPEED: 0.4, MOVE_SPEED: 1.0,
INVERSE_LENGTH: 0.005, EXPRESSION: 0.5, MIN_LIFESPAN: 10000, MAX_LIFESPAN: 20000,
INVERT: false, WRAP: true,
},
"Spongal": {
EGGS: 100000, SENSE_DISTANCE: 256, SENSE_ANGLE: 0.25, TURN_SPEED: 0.2, MOVE_SPEED: 1.0,
INVERSE_LENGTH: 0.9, EXPRESSION: 0.6, MIN_LIFESPAN: 10000, MAX_LIFESPAN: 90000,
INVERT: false, WRAP: false,
},
"Conservation of energy": {
EGGS: 100000, SENSE_DISTANCE: 30, SENSE_ANGLE: 0.15, TURN_SPEED: 0.3, MOVE_SPEED: 1.0,
INVERSE_LENGTH: 0.01, EXPRESSION: 0.5, MIN_LIFESPAN: 999999, MAX_LIFESPAN: 999999,
INVERT: false, WRAP: false,
},
};
// ---------- Color palettes ----------
const PALETTES = {
"Original Gold": { r: 1.0, g: 0.8, b: 0.4 },
"Cyberpunk": { r: 0.1, g: 1.0, b: 0.9 },
"Green Growth": { r: 0.3, g: 1.0, b: 0.1 },
"Blood Red": { r: 1.0, g: 0.1, b: 0.1 },
"Ghostly White": { r: 1.0, g: 1.0, b: 1.0 },
"Deep Sea": { r: 0.1, g: 0.4, b: 1.0 },
};
let currentPalette = PALETTES["Original Gold"];
// ---------- Shared UI styling ----------
const UI_FONT = "monospace";
const BTN_STYLE = {
padding: "6px 12px",
background: "rgba(0, 0, 0, 0.85)",
color: "#fff",
border: "1px solid #555",
cursor: "pointer",
borderRadius: "3px",
fontFamily: UI_FONT,
fontSize: "11px",
textAlign: "left",
width: "100%",
};
function makeButton(label, onClick, extraStyle = {}) {
const btn = document.createElement("button");
btn.innerText = label;
Object.assign(btn.style, BTN_STYLE, extraStyle);
btn.addEventListener("mouseover", () => (btn.style.background = "rgba(50, 50, 50, 0.9)"));
btn.addEventListener("mouseout", () => (btn.style.background = "rgba(0, 0, 0, 0.85)"));
btn.addEventListener("click", onClick);
return btn;
}
const uiInputs = {};
// ---------- UI construction ----------
function createUI() {
createControlsPanel();
createLeftPanel();
canvas.style.filter = CONFIG.INVERT ? "invert(1)" : "invert(0)";
}
function createControlsPanel() {
const container = document.createElement("div");
Object.assign(container.style, {
position: "absolute",
top: "10px",
right: "10px",
display: "flex",
flexDirection: "column",
alignItems: "flex-end",
gap: "6px",
zIndex: "1000",
filter: "invert(1)",
});
const panel = document.createElement("div");
Object.assign(panel.style, {
background: "rgba(0, 0, 0, 0.85)",
color: "#fff",
fontFamily: UI_FONT,
padding: "12px",
borderRadius: "6px",
display: "flex",
flexDirection: "column",
gap: "8px",
fontSize: "12px",
});
const toggleBtn = makeButton("Hide Controls", () => togglePanel(panel, toggleBtn, "Controls"));
Object.assign(toggleBtn.style, { width: "auto", textAlign: "center" });
container.appendChild(toggleBtn);
container.appendChild(panel);
for (const key in CONFIG) {
const row = document.createElement("div");
Object.assign(row.style, {
display: "flex",
justifyContent: "space-between",
alignItems: "center",
gap: "15px",
});
const label = document.createElement("label");
label.innerText = LABELS[key] || key;
label.style.width = "160px";
const input = createConfigInput(key);
uiInputs[key] = input;
row.appendChild(label);
row.appendChild(input);
panel.appendChild(row);
}
const respawnBtn = makeButton("Respawn", initWorms);
Object.assign(respawnBtn.style, {
marginTop: "5px",
background: "#444",
border: "none",
textAlign: "center",
});
panel.appendChild(respawnBtn);
document.body.appendChild(container);
}
function createConfigInput(key) {
const input = document.createElement("input");
if (typeof CONFIG[key] === "boolean") {
input.type = "checkbox";
input.checked = CONFIG[key];
input.style.cursor = "pointer";
input.addEventListener("change", (e) => {
CONFIG[key] = e.target.checked;
if (key === "INVERT") canvas.style.filter = CONFIG[key] ? "invert(1)" : "invert(0)";
if (key === "WRAP") trail.data.fill(0); // clear canvas on topology change
});
} else {
input.type = "text";
input.value = CONFIG[key];
Object.assign(input.style, {
width: "60px",
background: "#222",
color: "#fff",
border: "1px solid #555",
padding: "2px 4px",
borderRadius: "3px",
});
input.addEventListener("input", (e) => {
const val = parseFloat(e.target.value);
if (!isNaN(val)) CONFIG[key] = val;
});
}
return input;
}
function createLeftPanel() {
const container = document.createElement("div");
Object.assign(container.style, {
position: "absolute",
top: "10px",
left: "10px",
display: "flex",
flexDirection: "column",
alignItems: "flex-start",
gap: "6px",
zIndex: "1000",
filter: "invert(1)",
});
const presetsPanel = createSection("Hide Presets", "Presets", buildPresetButtons);
const palettesPanel = createSection("Hide Colors", "Colors", buildPaletteButtons);
palettesPanel.toggleBtn.style.marginTop = "10px";
container.appendChild(presetsPanel.toggleBtn);
container.appendChild(presetsPanel.panel);
container.appendChild(palettesPanel.toggleBtn);
container.appendChild(palettesPanel.panel);
document.body.appendChild(container);
}
function createSection(toggleLabel, name, populate) {
const toggleBtn = makeButton(toggleLabel, null);
Object.assign(toggleBtn.style, { width: "auto", textAlign: "center" });
const panel = document.createElement("div");
panel.style.display = "flex";
panel.style.flexDirection = "column";
panel.style.gap = "6px";
toggleBtn.addEventListener("click", () => {
const hidden = panel.style.display === "none";
panel.style.display = hidden ? "flex" : "none";
toggleBtn.innerText = hidden ? `Hide ${name}` : `Show ${name}`;
});
populate(panel);
return { toggleBtn, panel };
}
function buildPresetButtons(panel) {
for (const name in PRESETS) {
panel.appendChild(makeButton(name, () => applyPreset(name)));
}
}
function buildPaletteButtons(panel) {
for (const name in PALETTES) {
const p = PALETTES[name];
const btn = makeButton(name, () => (currentPalette = PALETTES[name]));
btn.style.borderLeft = `5px solid rgb(${p.r * 255}, ${p.g * 255}, ${p.b * 255})`;
panel.appendChild(btn);
}
}
function togglePanel(panel, btn, name) {
const hidden = panel.style.display === "none";
panel.style.display = hidden ? "flex" : "none";
btn.innerText = hidden ? `Hide ${name}` : `Show ${name}`;
}
// ---------- Presets ----------
function applyPreset(presetName) {
const preset = PRESETS[presetName];
if (!preset) return;
Object.assign(CONFIG, preset);
syncInputsToConfig();
canvas.style.filter = CONFIG.INVERT ? "invert(1)" : "invert(0)";
trail.data.fill(0);
initWorms();
}
function syncInputsToConfig() {
for (const key in CONFIG) {
const input = uiInputs[key];
if (!input) continue;
if (typeof CONFIG[key] === "boolean") input.checked = CONFIG[key];
else input.value = CONFIG[key];
}
}
// ---------- Simulation ----------
const SPAWN_RADIUS_FACTOR = 0.15;
const DEPOSIT_AMOUNT = 220;
const EDGE_MARGIN = 40;
function randRange(min, max) {
return min + Math.random() * (max - min);
}
function initWorms() {
worms.length = 0;
const cx = width / 2;
const cy = height / 2;
const maxRadius = Math.min(width, height) * SPAWN_RADIUS_FACTOR;
for (let i = 0; i < CONFIG.EGGS; i++) {
const angle = Math.random() * Math.PI * 2;
const radius = randRange(0, maxRadius);
worms.push({
x: cx + Math.cos(angle) * radius,
y: cy + Math.sin(angle) * radius,
angle,
age: 0,
life: randRange(CONFIG.MIN_LIFESPAN, CONFIG.MAX_LIFESPAN),
alive: true,
});
}
}
function sampleTrail(x, y) {
if (CONFIG.WRAP) {
x = ((x % width) + width) % width;
y = ((y % height) + height) % height;
} else if (x < 0 || y < 0 || x >= width || y >= height) {
return 0;
}
const ix = Math.floor(x);
const iy = Math.floor(y);
return trail.data[(iy * width + ix) * 4];
}
function sense(worm, offset) {
const angle = worm.angle + offset;
const sx = worm.x + Math.cos(angle) * CONFIG.SENSE_DISTANCE;
const sy = worm.y + Math.sin(angle) * CONFIG.SENSE_DISTANCE;
return sampleTrail(sx, sy);
}
function updateWorms() {
const cx = width / 2;
const cy = height / 2;
for (const w of worms) {
if (!w.alive) continue;
if (++w.age >= w.life) {
w.alive = false;
continue;
}
steerWorm(w, cx, cy);
moveAndDeposit(w);
}
}
function steerWorm(w, cx, cy) {
if (!CONFIG.WRAP && isNearEdge(w)) {
const target = Math.atan2(cy - w.y, cx - w.x);
const diff = Math.atan2(Math.sin(target - w.angle), Math.cos(target - w.angle));
w.angle += diff * CONFIG.TURN_SPEED;
return;
}
const forward = sense(w, 0);
const left = sense(w, -CONFIG.SENSE_ANGLE);
const right = sense(w, CONFIG.SENSE_ANGLE);
const turn = CONFIG.TURN_SPEED;
if (forward > left && forward > right) return;
if (left > right) w.angle -= turn * (0.5 + Math.random());
else if (right > left) w.angle += turn * (0.5 + Math.random());
else w.angle += (Math.random() - 0.5) * turn;
}
function isNearEdge(w) {
return (
w.x < EDGE_MARGIN ||
w.x > width - EDGE_MARGIN ||
w.y < EDGE_MARGIN ||
w.y > height - EDGE_MARGIN
);
}
function moveAndDeposit(w) {
w.x += Math.cos(w.angle) * CONFIG.MOVE_SPEED;
w.y += Math.sin(w.angle) * CONFIG.MOVE_SPEED;
if (CONFIG.WRAP) {
if (w.x < 0) w.x += width;
if (w.x >= width) w.x -= width;
if (w.y < 0) w.y += height;
if (w.y >= height) w.y -= height;
} else {
w.x = Math.max(0, Math.min(width - 1, w.x));
w.y = Math.max(0, Math.min(height - 1, w.y));
}
const ix = Math.floor(w.x);
const iy = Math.floor(w.y);
const idx = (iy * width + ix) * 4;
const p = currentPalette;
trail.data[idx] = Math.min(255, trail.data[idx] + DEPOSIT_AMOUNT * p.r);
trail.data[idx + 1] = Math.min(255, trail.data[idx + 1] + DEPOSIT_AMOUNT * p.g);
trail.data[idx + 2] = Math.min(255, trail.data[idx + 2] + DEPOSIT_AMOUNT * p.b);
trail.data[idx + 3] = 255;
}
function diffuseAndDecay() {
const data = trail.data;
const copy = new Uint8ClampedArray(data);
const decay = CONFIG.INVERSE_LENGTH * 255;
const blur = CONFIG.EXPRESSION;
const invBlur = 1 - blur;
for (let y = 1; y < height - 1; y++) {
const rowIdx = y * width;
const rowAbove = (y - 1) * width;
const rowBelow = (y + 1) * width;
for (let x = 1; x < width - 1; x++) {
const idx = (rowIdx + x) * 4;
let r = (copy[idx] + copy[(rowAbove + x) * 4] + copy[(rowBelow + x) * 4] + copy[(rowIdx + x - 1) * 4] + copy[(rowIdx + x + 1) * 4]) / 5;
let g = (copy[idx + 1] + copy[(rowAbove + x) * 4 + 1] + copy[(rowBelow + x) * 4 + 1] + copy[(rowIdx + x - 1) * 4 + 1] + copy[(rowIdx + x + 1) * 4 + 1]) / 5;
let b = (copy[idx + 2] + copy[(rowAbove + x) * 4 + 2] + copy[(rowBelow + x) * 4 + 2] + copy[(rowIdx + x - 1) * 4 + 2] + copy[(rowIdx + x + 1) * 4 + 2]) / 5;
r = Math.max(0, r - decay);
g = Math.max(0, g - decay);
b = Math.max(0, b - decay);
data[idx] = r * invBlur + data[idx] * blur;
data[idx + 1] = g * invBlur + data[idx + 1] * blur;
data[idx + 2] = b * invBlur + data[idx + 2] * blur;
data[idx + 3] = 255;
}
}
}
function render() {
ctx.putImageData(trail, 0, 0);
}
function loop() {
updateWorms();
diffuseAndDecay();
render();
requestAnimationFrame(loop);
}
createUI();
initWorms();
loop();
Free to mess with.










