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

276
Visualizações
html2canvas to convert div to canvas and then to image

I want to convert a div to canvas and then eventually to image. So I am using the html2canvas library. I have tried downloading image, opening image in new window, and appending to a div, but none of them has worked.

$(document).ready(function (e) {
  $("#final_download").click(function() {
      html2canvas($("#final_image_div")[0]).then(function (canvas) {
          console.log(canvas);
          var myImage = canvas.toDataURL("image/png");
          // download the generated image
          downloadURI(myImage, "final_image.png");
          // Open the image in a new window
          window.open(myImage , "_blank");
          // display it in div
          $("#bottom").append(myImage);
      });
  });
});
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.3.3/html2canvas.min.js"></script>

<div id="final_image_div">abcdef</div>
<img src="images/download.jpg" id="final_download">
<div class="bottom"></div>

Any idea as to what I am doing wrong here?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I tried to run your code and it was just lacking a dowloadURL function (there is no such thing in Vanilla JS).

I simply added the following code and then everything worked as expected :

function downloadURI(uri, name) {
    var link = document.createElement("a");
    link.download = name;
    link.href = uri;
    link.click();
}

FYI I found this function here

about 4 years ago · Juan Pablo Isaza Relatório
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