Html2canvas: Strange vertical offset after several renders

Created on 26 May 2020  路  11Comments  路  Source: niklasvh/html2canvas

I am using V1 rc 5, on ubuntu 16.04, and firefox 76.0.1 (64-bit).
I can successfully render my canvas, post the base64 string to my server, and then retrieve it for another page and it all works fine. My first attached screenshot shows the successfully created canvas.
Screenshot from 2020-05-26 13-35-00
However, after several (random) attempts the canvas slips down the page and gets truncated. Its as if something is remaining on the previous canvas? My next screenshot shows the effect:
Screenshot from 2020-05-26 13-37-34
I have spent days on this, trying to work out whether there are any js side effects, or modal creation problems and have run out of ideas!
I watch the console logging and without fail it "renders ok" (ie no errors. I can also see that the code correctly identifies differing heights and widths correctly.
I have also verified this behaviour on Chrome.
Please can someone have a look at this and possibly save my sanity?
Thx Paul
[email protected]

Most helpful comment

I solved all possible problems as follows..

html2canvas(element , {
        scrollX: -window.scrollX,
        scrollY: -window.scrollY,
        windowWidth: document.documentElement.offsetWidth,
        windowHeight: document.documentElement.offsetHeight
})

All 11 comments

Dear Team - is there any chance someone can have a look at this?
Its such an elegant solution but I need it to be reliable please? Could it be something else happening in the browser that it causing the vertical shift?

OK - clearly no-ones priority!
I have tried "removeContainer: true", but that didn't make any difference. The other thing I tried was defining my own canvas (so I could clean up the DOM) and make sure the next invocation was as clean as possible. But that didn't work either. The instructions are a little terse (or perhaps its just me) - when I specify my own canvas do I simply create an object or do I have to prepare it in some way (context?). Perhaps a few more pointers might help me to resolve. OR can you suggest a different tool I could use to create a canvas for my purposes? I dont think the native browser "scrrenshot" functions can be used from js? Well I'm no expert anyway .....

Helllllloooooooooooo.............

Happens to me as well

Can confirm,
happens in a cordova project, sometimes result is correct, sometimes there is a vertical offset.
image
image

reset your scroll to the origin ,maybe could do this

I had the same issue; resetting the scroll with window.scrollTo(0, 0) and/or temporarily setting the canvas to position: fixed before capture fixed this for me.

Hi groenroos, I have tried the scrollTo option and its still not quite right. Could you explain a bit further about how you set the canvas to fixed please? There doesn't appear to be an option for that and I have tried creating my own canvas without success. I am so close and would be delighted to get this sorted!
Cheers

@pcla56 Sure - it's quite simple. My code is something like so (where #stage is the container I want to turn into the canvas):

let stage = document.getElementById("stage");
stage.style.position = 'fixed';

setTimeout(function(){
    html2canvas(stage).then(function(canvas) {
        // ...

        canvas.toBlob(function(blob){
            // ...

            stage.style.position = 'static';
        });
    });
}, 1);

Essentially, I'm setting position: fixed in plain JS just prior to calling the plugin. I noticed I needed the 1ms timeout to wait for the browser to redraw before calling it.

Once I'm done with the canvas (converted to a file etc), I set the position back to static (or whatever it was before).

The side effect is of course that by setting the position to fixed, the page will reflow and elements might move around, but I found this to be a totally acceptable caveat in my use case.

Good luck!

@groenroos It works! Thank you very much. By the way, in some cases it is necessary to set stage.style.left = 0 and stage.style.top = 0.

I solved all possible problems as follows..

html2canvas(element , {
        scrollX: -window.scrollX,
        scrollY: -window.scrollY,
        windowWidth: document.documentElement.offsetWidth,
        windowHeight: document.documentElement.offsetHeight
})

I solved all possible problems as follows..

html2canvas(element , {
        scrollX: -window.scrollX,
        scrollY: -window.scrollY,
        windowWidth: document.documentElement.offsetWidth,
        windowHeight: document.documentElement.offsetHeight
})

Thank you X 100000

Was this page helpful?
0 / 5 - 0 ratings