简体   繁体   中英

html2canvas.js not capturing image for dynamically generated content

I am using vivagraphs to generate dynamic svg element but when I click capture button, no nodes and edges are shown.

This is the script:

$(document).ready(function() {
//var testdiv = document.getElementById("testdiv");
$('#btn').click(function(){
    html2canvas($("#graph1"), {
        onrendered: function(canvas) {
            var myImage = canvas.toDataURL("img/png");
            window.open(myImage);
        }
    });
});

While I inspect for elements svg is shown after rendering graph but snapshot does not contain nodes and edges.

Is there an alternative for html2canvas or can I fix this issue?

if you want to save the image from canvas to some image format here is some help for you. hope this will help you out.

$(document).ready(function() {
$('#btn').click(function(){
    html2canvas(document.getElementById('graph1'), {
            onrendered: function(canvas) {
                var cs = new CanvasSaver('save_img.php',canvas,'myimage')
            }
        });
    });
});

here CanvasSaver() function define is here below which take three parameters one is a php file which process image from RAW date to some image format. i'll write the code of 'save_img.php' belwo this script part and save that file in your root directory.

function CanvasSaver(url, cnvs, fname) {

    this.url = url;

    if(!cnvs || !url) return;
    fname = fname || 'picture';

    var data = cnvs.toDataURL("image/png");
    data = data.substr(data.indexOf(',') + 1).toString();

    var dataInput = document.createElement("input") ;
    dataInput.setAttribute("name", 'imgdata') ;
    dataInput.setAttribute("value", data);
    dataInput.setAttribute("type", "hidden");

    var nameInput = document.createElement("input") ;
    nameInput.setAttribute("name", 'name') ;
    nameInput.setAttribute("value", fname + '.jpg');

    var myForm = document.createElement("form");
    myForm.method = 'post';
    myForm.action = url;
    myForm.appendChild(dataInput);
    myForm.appendChild(nameInput);

    document.body.appendChild(myForm) ;
    myForm.submit() ;
    document.body.removeChild(myForm) ;

}

in above script whatever the image formate you want to save from browser give that image extension in this function above script

nameInput.setAttribute("value", fname + '.jpg');

now here is the code for your 'save_img.php' and save it in your root directory.

<?php
    # we are a PNG image
    header('Content-type: image/png');

    # we are an attachment (eg download), and we have a name
    header('Content-Disposition: attachment; filename="' . $_POST['name'] .'"');

    #capture, replace any spaces w/ plusses, and decode
    $encoded = $_POST['imgdata'];
    $encoded = str_replace(' ', '+', $encoded);
    $decoded = base64_decode($encoded);

    #write decoded data
    echo $decoded;
?>

您可能使用lib的beta版本,goto在html2canvas的github页面上发布并下载稳定的alpha版本

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