简体   繁体   中英

why google maps and charts are not supported by html2canvas in javascript?

Iam doing screenshot using html2canvas with this method:

 makeScreenshot: function (button) {
        html2canvas(document.body, {
            onrendered: function(canvas) {
                new Ext.Window({
                    title: 'Screenshot',
                    width: 800,
                    height: 600,
                    resizable: true,
                    autoScroll: true,
                    preventBodyReset: true,
                    html: '<img src="' + canvas.toDataURL("image/png") + '" height="800"/>'
                }).show();
            }
        });
    }

getting this screenshot (on the image left dialog/window named Screeshot) 在此处输入图片说明

as you can see, google map is not generated, just circles. also a charts ar not in the screenshot. what is wrong?

The map-tiles are images which may not be rendered, because the are located on a different domain.

From the documentation :

Limitations

All the images that the script uses need to reside under the same origin for it to be able to read them without the assistance of a proxy.

Add useCORS: true will solve this issue.

makeScreenshot: function (button) {
    html2canvas(document.body, {
        useCORS: true,
        onrendered: function(canvas) {
            new Ext.Window({
                title: 'Screenshot',
                width: 800,
                height: 600,
                resizable: true,
                autoScroll: true,
                preventBodyReset: true,
                html: '<img src="' + canvas.toDataURL("image/png") + '" height="800"/>'
            }).show();
        }
    });
}

For Google chart i spent all the night to find a solution and finally it was so simple: before calling html2canvas, convert the charts rendered to canvas with canvg(). Charts of Google are svg.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM