Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
Setting a SVG fill using Javascript

I am trying to change the color of an SVG image using audio from the microphone.

I have got the logic written, the color is updated on the SVG element, but nothing changes visually.

window.onload = function () {
  if (navigator.getUserMedia) {
    navigator.getUserMedia(
      { audio: true },
      (stream) => {
        var context = new AudioContext();
        var src = context.createMediaStreamSource(stream);
        var analyser = context.createAnalyser();

        var svg = document.querySelector("#rangoli");

        src.connect(analyser);
        analyser.connect(context.destination);

        analyser.fftSize = 256;

        var bufferLength = analyser.frequencyBinCount;

        var dataArray = new Uint8Array(bufferLength);

        function renderFrame() {
          requestAnimationFrame(renderFrame);

          analyser.getByteFrequencyData(dataArray);

          for (var i = 0; i < bufferLength; i++) {
            barHeight = dataArray[i] / 2;
            var r = barHeight + 25 * (i / bufferLength);
            var g = 250 * (i / bufferLength);
            var b = 50;

            svg.setAttribute("fill", `rgb(${r}, ${g}, ${b})`);
          }
        }

        renderFrame();
      },
      () => console.log("Error")
    );
  }
};

Here is the SVG updating the fill value: SVG fill updating

Here is a CodePen demo: https://codepen.io/GR8z/pen/qBjopZG

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda