// NextXus Federation — Cognitive Circuitry License // Animation: Synaptic Firing // License: Token Bundle (https://keywebster.gumroad.com/l/olhsf) // Own the marrow. (function () { "use strict"; var canvas = document.getElementById("canvas-theta"); var ctx = canvas.getContext("2d"); var stage = canvas.parentElement; var reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches; var NCLUSTERS = 4, PER = 10; // 40 neurons in organic clusters var neurons = [], edges = [], signals = [], frame = 0, nextFire = 0; var running = false, rafId = null; function resize() { canvas.width = stage.clientWidth; canvas.height = stage.clientHeight; } function seed() { neurons = []; edges = []; signals = []; var w = canvas.width, h = canvas.height, i, j, c; for (c = 0; c < NCLUSTERS; c++) { var cx = w * (0.16 + 0.68 * Math.random()); var cy = h * (0.2 + 0.6 * Math.random()); for (i = 0; i < PER; i++) { var ang = Math.random() * Math.PI * 2; var rad = Math.random() * Math.min(w, h) * 0.16; neurons.push({ x: Math.max(16, Math.min(w - 16, cx + Math.cos(ang) * rad * 1.4)), y: Math.max(16, Math.min(h - 16, cy + Math.sin(ang) * rad)), cluster: c, flash: 0, phase: Math.random() * Math.PI * 2 }); } } var seen = {}; function link(a, b) { var lo = Math.min(a, b), hi = Math.max(a, b), key = lo + "-" + hi; if (seen[key] || lo === hi) return; seen[key] = true; var mx = (neurons[lo].x + neurons[hi].x) / 2; var my = (neurons[lo].y + neurons[hi].y) / 2; var ex = neurons[hi].x - neurons[lo].x, ey = neurons[hi].y - neurons[lo].y; var len = Math.sqrt(ex * ex + ey * ey) || 1; var off = (Math.random() - 0.5) * 70; edges.push({ a: lo, b: hi, cx: mx - (ey / len) * off, cy: my + (ex / len) * off }); } // dendrites within clusters: 2-3 nearest same-cluster neighbours for (i = 0; i < neurons.length; i++) { var dists = []; for (j = 0; j < neurons.length; j++) { if (j === i || neurons[j].cluster !== neurons[i].cluster) continue; var dx = neurons[i].x - neurons[j].x, dy = neurons[i].y - neurons[j].y; dists.push({ j: j, d: dx * dx + dy * dy }); } dists.sort(function (p, q) { return p.d - q.d; }); var take = 2 + (Math.random() < 0.5 ? 1 : 0); for (var k = 0; k < take && k < dists.length; k++) link(i, dists[k].j); } // occasional cross-cluster chains for (c = 0; c < 5; c++) { link((Math.random() * neurons.length) | 0, (Math.random() * neurons.length) | 0); } nextFire = performance.now() + 600; } function fire(idx, depth, delay) { neurons[idx].flash = 1; var spawned = 0; for (var i = 0; i < edges.length && spawned < 3; i++) { var e = edges[i]; if (e.a !== idx && e.b !== idx) continue; if (Math.random() < 0.3) continue; signals.push({ e: e, from: idx, t: -delay, depth: depth }); spawned++; } } function bez(e, from, t) { var p0 = neurons[from === e.a ? e.a : e.b]; var p1 = neurons[from === e.a ? e.b : e.a]; var mt = 1 - t; return { x: mt * mt * p0.x + 2 * mt * t * e.cx + t * t * p1.x, y: mt * mt * p0.y + 2 * mt * t * e.cy + t * t * p1.y }; } function step() { frame++; var i, now = performance.now(); if (now >= nextFire) { // FIRING EVENT every 1-3 seconds fire((Math.random() * neurons.length) | 0, 2, 0); nextFire = now + 1000 + Math.random() * 2000; } for (i = 0; i < neurons.length; i++) neurons[i].flash *= 0.93; for (i = signals.length - 1; i >= 0; i--) { var s = signals[i]; s.t += 0.025; if (s.t >= 1) { var target = s.from === s.e.a ? s.e.b : s.e.a; neurons[target].flash = 1; // cascade-flare at ~150ms delay (0.225 at 60fps signal speed) if (s.depth > 0 && Math.random() < 0.7) fire(target, s.depth - 1, 0.225); signals.splice(i, 1); } } } function draw() { var w = canvas.width, h = canvas.height, i; ctx.fillStyle = "#0a0a0f"; ctx.fillRect(0, 0, w, h); ctx.strokeStyle = "rgba(150,150,160,0.22)"; // thin gray dendrites at rest ctx.lineWidth = 0.8; for (i = 0; i < edges.length; i++) { var e = edges[i]; ctx.beginPath(); ctx.moveTo(neurons[e.a].x, neurons[e.a].y); ctx.quadraticCurveTo(e.cx, e.cy, neurons[e.b].x, neurons[e.b].y); ctx.stroke(); } for (i = 0; i < neurons.length; i++) { var n = neurons[i]; var breathe = 0.1 + 0.07 * Math.sin(frame * 0.02 + n.phase); ctx.fillStyle = "rgba(212,175,55," + breathe.toFixed(3) + ")"; ctx.beginPath(); ctx.arc(n.x, n.y, 7.5, 0, Math.PI * 2); ctx.fill(); ctx.fillStyle = "#8a6d1f"; ctx.beginPath(); ctx.arc(n.x, n.y, 3.4, 0, Math.PI * 2); ctx.fill(); if (n.flash > 0.04) { var f = n.flash; ctx.fillStyle = "rgba(255,245,200," + (f * 0.9).toFixed(3) + ")"; ctx.beginPath(); ctx.arc(n.x, n.y, 3.4 + 2.6 * f, 0, Math.PI * 2); ctx.fill(); ctx.strokeStyle = "rgba(255,255,255," + (f * 0.5).toFixed(3) + ")"; ctx.lineWidth = 1.2; ctx.beginPath(); ctx.arc(n.x, n.y, 5 + 16 * (1 - f), 0, Math.PI * 2); ctx.stroke(); } } ctx.fillStyle = "#06b6d4"; for (i = 0; i < signals.length; i++) { var s = signals[i]; if (s.t < 0 || s.t > 1) continue; var p = bez(s.e, s.from, s.t); ctx.beginPath(); ctx.arc(p.x, p.y, 2.5, 0, Math.PI * 2); ctx.fill(); } } function warm() { fire(0, 2, 0); for (var s = 0; s < 25; s++) step(); } function loop() { if (!running) return; step(); draw(); rafId = requestAnimationFrame(loop); } function start() { if (running || reduced) return; running = true; nextFire = performance.now() + 800; rafId = requestAnimationFrame(loop); } function stop() { running = false; if (rafId) cancelAnimationFrame(rafId); } window.addEventListener("resize", function () { resize(); seed(); warm(); draw(); }); resize(); seed(); warm(); draw(); if (!reduced) { if ("IntersectionObserver" in window) { new IntersectionObserver(function (entries) { entries.forEach(function (e) { if (e.isIntersecting) start(); else stop(); }); }).observe(canvas); } else { start(); } } })();