In using html2canvas, I have a stack
of DOM objects (relative positioned div's that contain various things), that I wish to create individual thumbnails for. So if there are ten divs, I will create ten thumbnails.
Some of these objects will be offscreen --each of these divs are in a single, encompassing div called "mainDiv". I iterate through the divs within mainDiv and execute the html2canvas on each of them individually.
For those that are onscreen, this works fine. Those that are offscreen do not -- they come back blank. I created a workaround that scrolls the objects to the top of the mainDiv
, however this is a kludge and visually unappealing.
Is it possible to specify a DOM object that is not visible? Ideally, I'd like to be able to specify a containing div and have html2canvas
ignore the parent visibility, so I can screen capture hidden objects, but barring that, I'd like to be able to screen capture objects that are simply scrolled off screen.
Any thoughts, ideas? Thanks!
---- Here is some example code. Basically, if you had a bunch of divs within a div, iterate through them. I actually do this recursively
so that only one gets processed at a time, with the callback calling the recursive function, so it looks something like this:
function recurser(anIndex, callback) {
if (anIndex == -1) {
callback();
return;
}
$(myDivs[anIndex]).html2canvas({
onrendered : function(canvas) {
var img = canvas.toDataURL();
// store the image in an array, do stuff with it, etc.
recurser(--anIndex, callback);
}
})
}
Once the recursive calls are complete, it executes the callback function, which is a function that will do stuff with the images.
Again, all this works fine as long as the objects are visible within the scrolling div that contains all of the divs in #mainDiv. Once any part of the divs are scrolled off, however, they render black. In fact, if half of two divs are scrolled off (the top half of one, the bottom half of the next), they both render completely black.
Ah now have you tried Div display: none; and Div display: absolute; (or how you'd like your div to appear - maybe z-index)
set 'overflow' as visible for all of its parent elements. After rendered remove it
CSS
JS
I know this is an old question but it seemed kind of interesting. Based on my testing it seemed like only
position: absolute
andposition:fixed
elements that were outside of the viewport caused this issue. I figured out a solution to the problem.What I'm doing is making a clone of the element. Setting the styling of the clone to
left = 0
,top = window.innerHeight
(so that it won't be inside the window) andposition = 'relative'
. Then I append the clone to the body, usehtml2canvas
on the clone, and then remove the clone from the body. This solution works for me in IE, Firefox, and Chrome.I have created a JSBin example here. Here is the key javascript code:
You can use the latest version of html2canvas. This resolves all images pix-elation issues and rendering object issue. I used the version 0.5.0-beta4 by Erik koopmans refer this and called the html2canvas as below: