简体   繁体   English

如何从此javascript函数返回值

[英]how to return value from this javascript function

I'm working with html2canvas and I have the next function 我正在使用html2canvas ,下一个功能是

hml2canvas($('body'), {
   onrendered: function(canvas) {           
         data = canvas.toDataURL('image/jpeg');
    }
});

Now, I want to return the data value, so I tried this 现在,我想返回数据值,所以我尝试了

html2canvas($('body'), {
    onrendered: (this, imgBase64)         
});

var data;
function imgBase64(canvas) {
     data = canvas.toDataURL('image/jpeg');
}
console.log(data);

but when I see the console I get "undefined". 但是当我看到控制台时,我得到“未定义”。 If I put console.log(data); 如果我把console.log(data); inside the imgBase64 function I get the base64 of the image, but I need the data out of that function to make another work with it. imgBase64函数中,我获得了图像的base64,但是我需要该函数中的data来进行其他处理。 Any ideas? 有任何想法吗? Thanks. 谢谢。

I found this answer so I modified to adapt to my needs. 我找到了这个答案,因此我进行了修改以适应我的需求。

var html2obj = html2canvas($('body'));
var queue  = html2obj.parse();
var canvas = html2obj.render(queue);
var data = canvas.toDataURL('image/jpeg');

this way I have the data to use later. 这样,我就有了以后使用的data

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

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