gpt4 book ai didi

javascript - HTML5 Canvas Base64 错误

转载 作者:行者123 更新时间:2023-12-03 09:57:27 24 4
gpt4 key购买 nike

知道为什么这段代码不起作用吗? :\

演示 -> https://jsfiddle.net/vgmFN/89/

 <script>
var imgObj1 = new Image();
var img = new Image();var a1=0;
var can = document.getElementById('canvas1');
var ctx = can.getContext('2d');
imgObj1.onload = function() {
ctx.drawImage(imgObj1,1,1);a1 = 1;
}
imgObj1.src = 'favicon.ico';
if (a1 == 1) {
console.log(can.toDataURL())
img.src = can.toDataURL();
document.body.appendChild(img);
a1=0;
}
</script>

最佳答案

您使用 imgObj1.onloadimgObj1 提供了加载时间,但还必须通过添加 img 为 img 提供加载时间.onload

https://jsfiddle.net/m1erickson/tf0y1pL9/

enter image description here

顺便说一句,如果您格式化代码,则更容易发现错误。 ;-)

var can = document.getElementById('canvas1');
var ctx = can.getContext('2d');

// create and load the first image
var imgObj1 = new Image();
imgObj1.onload = function() {

ctx.drawImage(imgObj1,1,1);

// create and load the canvas as an image
var img = new Image();
img.onload=function(){
document.body.appendChild(img);
}
img.src = can.toDataURL();

}
imgObj1.src = '/favicon.ico';

关于javascript - HTML5 Canvas Base64 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30685556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com