Real-Time Audio Reactive Frequency EQ Bars Using Web Audio API AnalyserNode & CSS
Real-time audio frequency visualizers feed media audio into an `AudioContext` connected to an `AnalyserNode`. Inside a `requestAnimationFrame` loop, `analyser.getByteFrequencyData()` samples audio bins and dynamically sets CSS height variables (`--bar-h: 84%`) on individual DOM bars.
Step 1: Construct the Equalizer Bar Container
Render a series of vertical pill bars with CSS height properties governed by custom variables.
<div class="audio-eq-widget" id="eqWidget">
<div class="eq-bars-container" id="eqBars">
<div class="eq-bar" style="--val: 10%;"></div>
<div class="eq-bar" style="--val: 25%;"></div>
<div class="eq-bar" style="--val: 45%;"></div>
<div class="eq-bar" style="--val: 70%;"></div>
<div class="eq-bar" style="--val: 90%;"></div>
<div class="eq-bar" style="--val: 60%;"></div>
<div class="eq-bar" style="--val: 35%;"></div>
<div class="eq-bar" style="--val: 15%;"></div>
</div>
<button class="eq-toggle-btn" id="btnToggleEq">Start Equalizer</button>
</div>
Step 2: Style Hardware-Accelerated Scale Transforms
Animate scaleY transforms with transform-origin bottom to achieve smooth 60fps spectrum bars.
.eq-bars-container {
display: flex;
align-items: flex-end;
gap: 6px;
height: 80px;
padding: 12px;
background: #0f172a;
border-radius: 12px;
}
.eq-bar {
flex: 1;
height: 100%;
border-radius: 4px;
background: linear-gradient(180deg, #ec4899 0%, #8b5cf6 50%, #3b82f6 100%);
transform: scaleY(var(--val, 0.1));
transform-origin: bottom;
transition: transform 0.08s ease;
}
Step 3: Sample Frequency Data via AnalyserNode
Extract frequency byte arrays in a requestAnimationFrame loop and update bar transforms.
let audioCtx, analyser, dataArray, animId;
const bars = document.querySelectorAll('.eq-bar');
function startAudio() {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
analyser = audioCtx.createAnalyser();
analyser.fftSize = 32;
analyser.smoothingTimeConstant = 0.8;
// Synthetic demo oscillator
const osc = audioCtx.createOscillator();
osc.type = 'sawtooth';
osc.frequency.setValueAtTime(140, audioCtx.currentTime);
osc.connect(analyser);
analyser.connect(audioCtx.destination);
osc.start();
dataArray = new Uint8Array(analyser.frequencyBinCount);
renderLoop();
}
function renderLoop() {
animId = requestAnimationFrame(renderLoop);
analyser.getByteFrequencyData(dataArray);
bars.forEach((bar, i) => {
const byte = dataArray[i] || 0;
const scale = Math.max(0.08, byte / 255);
bar.style.setProperty('--val', scale);
});
}
document.getElementById('btnToggleEq').addEventListener('click', () => {
if (!audioCtx) startAudio();
});
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
A key lesson from building Web Audio visualizers: always check audioCtx.state === 'suspended' and resume it inside a direct user-initiated click handler. Modern mobile browsers aggressively mute unprompted audio context initialization.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
What is FFT size in Web Audio AnalyserNode?
Fast Fourier Transform (FFT) size defines the frequency resolution. AnalyserNode produces `fftSize / 2` discrete frequency bins. An fftSize of 32 yields 16 frequency bands.
Why use scaleY transforms instead of height style updates?
Updating CSS height triggers expensive DOM layout reflows on every frame. `transform: scaleY()` operates exclusively on the GPU compositor thread without touching layout.
How does smoothingTimeConstant affect equalizer animation?
smoothingTimeConstant ranges from 0 to 1. Higher values (0.8–0.85) provide smooth, gradual bar movements; lower values (0.2–0.4) deliver sharp, twitchy responses.
Why do browsers require a user click to initialize AudioContext?
Browser autoplay policies prevent AudioContext from playing audio until the user interacts with the page (click or keypress), preventing unsolicited audio.
Can the equalizer analyze audio from an HTML5 `
Yes! Use `audioCtx.createMediaElementSource(audioEl)` to route external media streams directly through the AnalyserNode pipeline.