【发布时间】:2013-01-30 23:35:19
【问题描述】:
我知道您不应该在 Javascript 中进行阻塞,而且我从来没有因为必须这样做而无法重构。但是我遇到了一些我不知道如何处理回调的东西。我正在尝试将 Downloadify 与 html2canvas 一起使用(这仅适用于 IE,下载数据 URI 在 IE 中不起作用)。您必须指定一个数据函数,以便 Flash 对象知道要下载什么。不幸的是,html2canvas 是异步的。我需要能够等到 onrendered 事件被填写后才能获取数据 URI。
$('#snapshot').downloadify({
filename: function(){
return 'timeline.png';
},
data: function(){
var d = null;
html2canvas($('#timeline'),{
onrendered:function(canvas){
d = canvas.toDataURL();
}
});
//need to be able to block until d isn't null
return d;
},
swf: '../static/bin/downloadify.swf',
downloadImage: '../static/img/camera_icon_32.png?rev=1',
width: 32,
height: 32,
transparent: true,
append: false
});
我愿意接受有关其他方法的建议,但我被困住了。
编辑 - 一些 cmets 似乎需要更多关于 Downloadify 的信息 (https://github.com/dcneiner/Downloadify)。 Downloadify 是一个 Flash 对象,可用于触发浏览器的另存为窗口。 downloadify() 函数只是简单地初始化 Flash 对象并在元素中粘贴<object/> 标记。由于它是一个 Flash 对象,因此您无法从 Javascript 触发事件而不会导致安全违规。
我只在 IE 上使用它来下载 Canvas 元素的图像。在所有其他浏览器中,我可以只使用数据 URI,但 IE 是一朵特殊的花。
【问题讨论】:
-
你不能。但是你不能只从
html2canvas的回调中调用downloadify吗? -
不,Downloadify 是一个 Flash 对象。你不能从外部触发它。
-
当 onRender 被调用时,你能在 downloadify 元素上做一个按钮吗?
-
对不起,我不确定我是否理解你。我的意思是把整个
$('#snapshot').downloadify()东西包裹在onrendered里面。 -
@Terrance 这是一个 Flash 对象,您不能以编程方式与它进行交互。这也是我的第一个想法,我遇到了这个问题:github.com/dcneiner/Downloadify/issues/6
标签: javascript html asynchronous html2canvas downloadify