Voice glow

Ask anything
0:00
Message

Key CSS & implementation tricks

// one lobe: a unit-circle gradient in a stretched space
ctx.save();
ctx.translate(x, h + ry * .06);   // centre just below the bottom edge
ctx.scale(rx, ry);                // circle → ellipse
const g = ctx.createRadialGradient(0, 0, 0, 0, 0, 1);
g.addColorStop(0,   rgba(color, a));
g.addColorStop(.35, rgba(color, a * .75));
g.addColorStop(.7,  rgba(color, a * .28));
g.addColorStop(1,   rgba(color, 0));
ctx.fillStyle = g;
ctx.beginPath(); ctx.arc(0, 0, 1, 0, Math.PI * 2); ctx.fill();
ctx.restore();

const ATTACK = .05, RELEASE = .32;            // seconds
for (let i = 0; i < 5; i++) {
  const target = bands[i];
  const tau = target > level[i] ? ATTACK : RELEASE;
  level[i] += (target - level[i]) * (1 - Math.exp(-dt / tau));
}

gather += ((mode === 'think') - gather) * (1 - Math.exp(-dt / .35));

const beam = .5 + Math.sin(t * 1.7) * p.sweep;          // shared sweep
const x  = w * lerp(home + drift, beam + (i - 2) * .03, gather);
const rx = w * p.spread * lerp(.75 + .45 * L, .72, gather);
const ry = h * p.reach  * lerp(.2 + .8 * L, .78, gather);

const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const ctx = new AudioContext();
const analyser = Object.assign(ctx.createAnalyser(), { fftSize: 1024, smoothingTimeConstant: .5 });
ctx.createMediaStreamSource(stream).connect(analyser);

// five log-spaced bands, 90 Hz – 4 kHz
const edges = [90, 180, 360, 720, 1440, 4000].map(f => Math.round(f / (ctx.sampleRate / 2) * analyser.frequencyBinCount));
analyser.getByteFrequencyData(spectrum);
bands[i] = clamp((avg(spectrum, edges[i], edges[i + 1]) / 255 - .18) * 2.4);

.field { position: relative; overflow: hidden; border-radius: 16px; isolation: isolate; }
.field canvas.glow { position: absolute; inset: 0; width: 100%; height: 100%;
  z-index: -1; filter: blur(10px); pointer-events: none; }

ctx.globalCompositeOperation = 'lighter';           // lobes add, never cover
const a = (dark ? .95 : .5) * Math.min(1, .25 + L); // light surfaces get half