Html2canvas: Not able to get contents of divs with "display:none"

Created on 6 Mar 2016  ·  10Comments  ·  Source: niklasvh/html2canvas

Hi I want to take screen shot of a div which is hidden

this is the code I tried
HTML:

<div` id="visible">
hello there!!
</div>
<div id="hidden" style="display:none">
You can't see me :P
</div>

JS:

var elem1=$("#hidden");
if(!elem1.is(":visible"))
{
elem1.show();elem1.fadeIn(1);elem1.fadeOut(1);
}
html2canvas(elem1).then(function(canvas){

$("body").append(canvas);

});

Error in Browser:
**

Uncaught TypeError: Cannot read property 'length' of nullhtml2canvas @ html2canvas.js:941(anonymous function) @ HtmlPage.html:13

**
My fiddle:https://jsfiddle.net/h39tp0Ly/2/

Please help me out with this

All 10 comments

Html2canvas interprets the display:none style correctly and thus will not render a hidden element. To accomplish what you want, you can absolutely position a visible element off of the page viewport (if the desire is to "render a hidden element").

May be useful.
Thanks !

It works if and only if the div has css which is not "display:none". so i used .show() and .hide() combination to make the element visible and hidden to form Canvas

       onclone: function(document) {
            hiddenDiv = document.getElementById("render");
            hiddenDiv.style.display = 'block';
        }

I found some info on Internet. We can set css to specified div with "display:none" then add option onclone like above. Plz add option "loggin: true" to see what happen

Actually the trick of putting container out of a viewport with position absolute and top/left negative values - does not work in Firefox, it throws NS_ERROR_FAILURE exception

It works though in Chrome

I ran into this issue very recently, and if you want a particular div to be invisible and screenshotted, set the css properties as

height: 0%
overflow:hidden

This doesn't work for me @kchen1025

I ended up generating the canvas immediately after the target div, and then hiding the div immediately after the canvas was loaded. The flicker is barely noticeable.

My code looks like this:

<div id="capture">
  ...
</div>
<div id="end"></div>
html2canvas(document.querySelector("#capture")).then(canvas => {
  $("#end").append(canvas)
  $("#capture").hide()
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

arzyu picture arzyu  ·  3Comments

anthonymejia picture anthonymejia  ·  4Comments

celik75 picture celik75  ·  4Comments

Loki180 picture Loki180  ·  4Comments

dking3876 picture dking3876  ·  4Comments