Warm tip: This article is reproduced from stackoverflow.com, please click
jquery html2canvas

HTML2Canvas not working, need to console image url

发布于 2020-03-27 10:30:32

I am trying to screenshot the view of a div which contains fullcalendar, but unable to do so. I have tried following code:

  function screenshot(){
     alert('zz'); html2canvas(document.querySelector('#calendar')).then(function(canvas) {
        document.body.appendChild(canvas);
        console.log(canvas.toDataURL());
     });
    }

Whenever I click on screenshot, I want to capture image of div#calendar and need to console the url so that I can use this image later as thumbnail when I share this page link later in social media.

You can check it here on Codepen

Questioner
Aayush Dahal
Viewed
166
458 2019-07-04 13:18

You can use below function to get image using html2canvas..

function screenshot(){
      console.log(html2canvas(document.querySelector('#calendar')))
      html2canvas(document.querySelector('#calendar'), {
      onrendered: function(canvas) {  
        var image = canvas.toDataURL("image/png");
        console.log("image => ",image); //image in base64
        var pHtml = "<img src="+image+" />";
        $("#parent").append(pHtml); //you can append image tag anywhere
        }
     });
 }