# Audio spectrum waves

> Layered horizontal waves that vibrate like a music visualizer and fade softly towards the edges.

- Category: [Backgrounds](https://library.aimran.es/components/patterns)
- Built with: HTML + CSS
- Live editor: https://library.aimran.es/blocks/pt-spectrum
- Library: AIMRAN Library (https://library.aimran.es), free, no sign-up, by Aimran Oukham

## Where to use it

These waves fit anything with sound: the homepage of a podcast, the release page of a new single, a voice assistant’s landing page or a DJ’s booking page. They move on their own and don’t react to real audio, which keeps them simple and predictable. An animated sound wave background drawn on canvas with a little JavaScript.

The number of lines follows the density slider: two or three look elegant, ten feel busy and energetic. Put the headline above or below the middle band, since that’s where the waves swing the most.

## Code

### HTML

```html
<div class="pt-spectrum"><canvas></canvas></div>
```

### CSS

```css
.pt-spectrum {
  --c1: #0071e3;
  --c2: #8b5cf6;
  --c3: #14b8a6;
  --k: 1;
  /* velocidad */
  --d: 1;
  /* densidad / escala */
  --o: 1;
  /* opacidad */
  --bg: #05060a;
  --fg: #ffffff;
  --blend: screen;
  min-height: 100vh;
}

.pt-spectrum {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg);
  container-type: size;
  isolation: isolate;
}

.pt-spectrum>* {
  opacity: var(--o);
}

.pt-spectrum canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}
```

### JavaScript

```js
(function (el) {
const cv=el.querySelector("canvas"),x=cv.getContext("2d");let W=0,H=0,raf=0;
const fit=()=>{const r=Math.min(window.devicePixelRatio||1,2);W=el.clientWidth;H=el.clientHeight;cv.width=W*r;cv.height=H*r;x.setTransform(r,0,0,r,0,0)};
const ro=new ResizeObserver(fit);ro.observe(el);fit();
const V=()=>{const s=getComputedStyle(el),g=(n)=>s.getPropertyValue(n).trim();return{c1:g("--c1"),c2:g("--c2"),c3:g("--c3"),bg:g("--bg"),k:+g("--k")||1,d:+g("--d")||1}};
// Movimiento reducido: se calculan de golpe unos fotogramas (con tiempo simulado) y la escena se queda quieta
const RM=matchMedia("(prefers-reduced-motion: reduce)").matches;let RF=0;
const requestAnimationFrame=(f)=>{if(!RM)return window.requestAnimationFrame(f);if(++RF<400)f(RF*60);return 0};
let t=0;const loop=()=>{const v=V();t+=.016*v.k;x.clearRect(0,0,W,H);const g=x.createLinearGradient(0,0,W,0);g.addColorStop(0,v.c1);g.addColorStop(.5,v.c2);g.addColorStop(1,v.c3);
const L=Math.max(2,Math.min(14,Math.round(6*v.d)));x.strokeStyle=g;x.shadowColor=v.c2;x.shadowBlur=14;
for(let j=0;j<L;j++){x.beginPath();const ph=j*.7;for(let px=0;px<=W;px+=4){const u=px/W,env=Math.pow(Math.sin(Math.PI*u),2);
const y=H/2+env*H*.3*(Math.sin(u*9+t*2+ph)*.6+Math.sin(u*23-t*3+ph*2)*.25)*Math.sin(t*.8+j*.5);px===0?x.moveTo(px,y):x.lineTo(px,y)}
x.globalAlpha=.2+.8*(1-j/L);x.lineWidth=j===0?2.6:1.3;x.stroke()}x.globalAlpha=1;x.shadowBlur=0;raf=requestAnimationFrame(loop)};loop();
return()=>{cancelAnimationFrame(raf);ro.disconnect()};
})(document.querySelector(".pt-spectrum"));
```

### React

```tsx
// @ts-nocheck
import { useEffect, useRef } from "react";

const css = `
.pt-spectrum {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg);
  container-type: size;
  isolation: isolate;
}

.pt-spectrum>* {
  opacity: var(--o);
}

.pt-spectrum canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

`;

export default function PatternSpectrum() {
  const ref = useRef(null);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
const cv=el.querySelector("canvas"),x=cv.getContext("2d");let W=0,H=0,raf=0;
const fit=()=>{const r=Math.min(window.devicePixelRatio||1,2);W=el.clientWidth;H=el.clientHeight;cv.width=W*r;cv.height=H*r;x.setTransform(r,0,0,r,0,0)};
const ro=new ResizeObserver(fit);ro.observe(el);fit();
const V=()=>{const s=getComputedStyle(el),g=(n)=>s.getPropertyValue(n).trim();return{c1:g("--c1"),c2:g("--c2"),c3:g("--c3"),bg:g("--bg"),k:+g("--k")||1,d:+g("--d")||1}};
// Movimiento reducido: se calculan de golpe unos fotogramas (con tiempo simulado) y la escena se queda quieta
const RM=matchMedia("(prefers-reduced-motion: reduce)").matches;let RF=0;
const requestAnimationFrame=(f)=>{if(!RM)return window.requestAnimationFrame(f);if(++RF<400)f(RF*60);return 0};
let t=0;const loop=()=>{const v=V();t+=.016*v.k;x.clearRect(0,0,W,H);const g=x.createLinearGradient(0,0,W,0);g.addColorStop(0,v.c1);g.addColorStop(.5,v.c2);g.addColorStop(1,v.c3);
const L=Math.max(2,Math.min(14,Math.round(6*v.d)));x.strokeStyle=g;x.shadowColor=v.c2;x.shadowBlur=14;
for(let j=0;j<L;j++){x.beginPath();const ph=j*.7;for(let px=0;px<=W;px+=4){const u=px/W,env=Math.pow(Math.sin(Math.PI*u),2);
const y=H/2+env*H*.3*(Math.sin(u*9+t*2+ph)*.6+Math.sin(u*23-t*3+ph*2)*.25)*Math.sin(t*.8+j*.5);px===0?x.moveTo(px,y):x.lineTo(px,y)}
x.globalAlpha=.2+.8*(1-j/L);x.lineWidth=j===0?2.6:1.3;x.stroke()}x.globalAlpha=1;x.shadowBlur=0;raf=requestAnimationFrame(loop)};loop();
return()=>{cancelAnimationFrame(raf);ro.disconnect()};
  }, []);
  return (
    <>
      <style>{css}</style>
      <div
        ref={ref}
        className="pt-spectrum"
        style={{
          "--c1": "#0071e3",
          "--c2": "#8b5cf6",
          "--c3": "#14b8a6",
          "--k": 1,
          "--d": 1,
          "--o": 1,
          "--bg": "#05060a",
          "--fg": "#ffffff",
          "--blend": "screen",
          minHeight: "100vh",
        }}
      >
        <canvas></canvas>
      </div>
    </>
  );
}
```

## Prompt for AI assistants

Paste this into ChatGPT, Claude, Cursor, v0 or Lovable to adapt the block to your stack (Tailwind, Vue, Svelte…):

````text
I want to add the “Audio spectrum waves” block (Backgrounds) from AIMRAN Library to my project.

## What it is
Layered horizontal waves that vibrate like a music visualizer and fade softly towards the edges.

## Chosen settings
- Color 1: #0071e3
- Color 2: #8b5cf6
- Color 3: #14b8a6
- Speed: 1x (from 0.2 to 3x)
- Density / scale: 1x (from 0.5 to 2x)
- Opacity: 1 (from 0.2 to 1)

## What I need
1. Add it to my project with the same look and animation as the reference code: same sizes, colours, radii, shadows, timings and easing.
2. It is built with plain HTML and CSS (the reference wraps it in a React component with its CSS). If my project uses something else (Vue, Svelte, Astro, plain HTML…), translate it without losing any behaviour. If I use Tailwind, turn the styles into classes; if not, keep the CSS in its own file.
3. Expose the settings above as props (or CSS variables) with those values as defaults.
4. Make it work well on phones: nothing that depends only on hover, touch targets of at least 44 px and no overflow on 320 px screens.
5. Make it look right in light and dark mode.
6. Keep it accessible: semantic HTML, roles and aria-* where needed, visible focus and keyboard support.
7. Respect prefers-reduced-motion: with reduced motion, no big animations but the same end result.

## Reference code
```tsx
// @ts-nocheck
import { useEffect, useRef } from "react";

const css = `
.pt-spectrum {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg);
  container-type: size;
  isolation: isolate;
}

.pt-spectrum>* {
  opacity: var(--o);
}

.pt-spectrum canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

`;

export default function PatternSpectrum() {
  const ref = useRef(null);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
const cv=el.querySelector("canvas"),x=cv.getContext("2d");let W=0,H=0,raf=0;
const fit=()=>{const r=Math.min(window.devicePixelRatio||1,2);W=el.clientWidth;H=el.clientHeight;cv.width=W*r;cv.height=H*r;x.setTransform(r,0,0,r,0,0)};
const ro=new ResizeObserver(fit);ro.observe(el);fit();
const V=()=>{const s=getComputedStyle(el),g=(n)=>s.getPropertyValue(n).trim();return{c1:g("--c1"),c2:g("--c2"),c3:g("--c3"),bg:g("--bg"),k:+g("--k")||1,d:+g("--d")||1}};
// Movimiento reducido: se calculan de golpe unos fotogramas (con tiempo simulado) y la escena se queda quieta
const RM=matchMedia("(prefers-reduced-motion: reduce)").matches;let RF=0;
const requestAnimationFrame=(f)=>{if(!RM)return window.requestAnimationFrame(f);if(++RF<400)f(RF*60);return 0};
let t=0;const loop=()=>{const v=V();t+=.016*v.k;x.clearRect(0,0,W,H);const g=x.createLinearGradient(0,0,W,0);g.addColorStop(0,v.c1);g.addColorStop(.5,v.c2);g.addColorStop(1,v.c3);
const L=Math.max(2,Math.min(14,Math.round(6*v.d)));x.strokeStyle=g;x.shadowColor=v.c2;x.shadowBlur=14;
for(let j=0;j<L;j++){x.beginPath();const ph=j*.7;for(let px=0;px<=W;px+=4){const u=px/W,env=Math.pow(Math.sin(Math.PI*u),2);
const y=H/2+env*H*.3*(Math.sin(u*9+t*2+ph)*.6+Math.sin(u*23-t*3+ph*2)*.25)*Math.sin(t*.8+j*.5);px===0?x.moveTo(px,y):x.lineTo(px,y)}
x.globalAlpha=.2+.8*(1-j/L);x.lineWidth=j===0?2.6:1.3;x.stroke()}x.globalAlpha=1;x.shadowBlur=0;raf=requestAnimationFrame(loop)};loop();
return()=>{cancelAnimationFrame(raf);ro.disconnect()};
  }, []);
  return (
    <>
      <style>{css}</style>
      <div
        ref={ref}
        className="pt-spectrum"
        style={{
          "--c1": "#0071e3",
          "--c2": "#8b5cf6",
          "--c3": "#14b8a6",
          "--k": 1,
          "--d": 1,
          "--o": 1,
          "--bg": "#05060a",
          "--fg": "#ffffff",
          "--blend": "screen",
          minHeight: "100vh",
        }}
      >
        <canvas></canvas>
      </div>
    </>
  );
}
```

Give me the complete final code, ready to paste, and tell me in one line where to put it in my project.
````
