如何将 toDataURL 与图像加载功能一起使用?
function resetDisplayImg(){
var tempImage = new Image();
tempImage.src=document.getElementById("myCanvas").toDataURL("image/png");
tempImage.onload=function(){
context.drawImage(tempImage1,0,0,Canvas_WIDTH,Canvas_HEIGHT);
}
每次
当我调用上面的函数时,我必须调用两次才能正确运行。我知道使用“onload”功能可以解决这个问题,但即使我应用它,问题仍然会发生。我犯了什么错误吗?
function resetDisplayImg(){
var tempImage = new Image();
tempImage.src=document.getElementById("myCanvas").toDataURL("image/png");
tempImage.onload=function(){
context.drawImage(tempImage1,0,0,Canvas_WIDTH,Canvas_HEIGHT);
}
}
Everytime when i call the function above, i have to call two times then it will function correctly. I know with function "onload" can solve this problem but the problem still occur even if i applied it. Is there any mistake that i had made?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
.src
行放在onload
之后BUT 你不应该首先这样做。您可以直接将一个画布绘制到另一个画布上,因此您只需编写:
context.drawImage(document.getElementById("myCanvas"), 0, 0);
即可完成!put the
.src
line after theonload
BUT you shouldnt be doing this in the first place. You are allowed to directly draw one canvas to another, so instead you can just write:
context.drawImage(document.getElementById("myCanvas"), 0, 0);
and be done!