Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
Convert 2 or more SVGs to PNG in JavaScript

I'm using the below code to convert 1 SVG to PNG. I convert the SVG to canvas to PNG.

getDownloadURL = async () => {
          // Logic to convert SVG to PNG

          const svg = document.querySelector('#widget svg'),
            canvas = new OffscreenCanvas(svg.clientWidth*3, svg.clientHeight*3),
            svgString = new XMLSerializer().serializeToString(svg),
            ctx = canvas.getContext('2d'),
            v = await Canvg.fromString(ctx, svgString, presets.offscreen());
          await v.render();
          const blob = await canvas.convertToBlob(),
            pngUrl = URL.createObjectURL(blob);

          return pngUrl;
}

download = filename => {
          const a = document.createElement('a');
          a.download = filename;
          getDownloadURL().then(url => {
            a.href = url;
            document.body.appendChild(a);
            a.click();
            a.remove();
          });
        };

If I try to convert more than 1 SVG by replacing const svg = document.querySelector('#widget svg') to const svg = document.querySelector('#widget') because #widget element has 2 SVGs, the resultant PNG has 2 SVGs overlapping each other.

How can I fix this overlapping issue? It would be great if I can render the content of element #widget as it is on PNG.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda