简体   繁体   English

html2canvas.js无法捕获动态生成内容的图像

[英]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. 我正在使用vivagraphs生成动态svg元素,但是当我单击捕获按钮时,未显示任何节点和边缘。

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. 虽然我检查了svg元素,但在渲染图形后显示了svg,但快照不包含节点和边。

Is there an alternative for html2canvas or can I fix this issue? html2canvas是否有替代方法,还是可以解决此问题?

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. 这里的CanvasSaver()函数定义在下面,它带有三个参数,一个是php文件,该文件将图像从RAW日期转换为某种图像格式。 i'll write the code of 'save_img.php' belwo this script part and save that file in your root directory. 我将在此脚本部分中写上“ save_img.php”的代码,并将该文件保存在您的根目录中。

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. 现在,这里是您的“ save_img.php”的代码,并将其保存在您的根目录中。

<?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版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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