Screenshot of hidden div using html2canvas

Midhuna picture Midhuna · Dec 16, 2013 · Viewed 26.8k times · Source

How to take screenshots of a hidden div? The code given below is not working for hidden div.

HTML

<div id="mydiv" style="display:none;" >  
 <p>Text</p>
</div>

<div id="canvas" style="display:none;">
<p>Canvas:</p>
</div>

 <div id="image">

  </div>

SCRIPT

<script>
  var elem = $('#printDiv');
        html2canvas(elem, {
            onrendered: function (canvas) {
                var data = canvas.toDataURL('image/png');
                var image = new Image();
                image.src = data;
                document.getElementById('image').appendChild(image);
            }
        });

</script>

It works when we set the style of the div "mydiv" as 'display:block'.

Answer

Trajche Manev picture Trajche Manev · Apr 8, 2014

You can use opacity set to 0. Here is sample example:

<div style="position: absolute; opacity: 0.0;">
    <div id="tempImageThumbnail"></div>
</div>
var tempImageThumb = $('#tempImageThumbnail');
tempImageThumb.append("<div>some text</div>");

html2canvas([tempImageThumb.get(0)], {
    onrendered : function(canvas) {
         document.body.appendChild(canvas);
    }
});