LoeiFy / Recordum

GitHub issues blog
MIT License
33 stars 6 forks source link

使用 canvas 的 drawImage 方法时候必须确保 image 已经加载完成 #3

Closed LoeiFy closed 8 years ago

LoeiFy commented 9 years ago

image 通过 ajax 方式加载,加载完成调用 drawImage 方法,chrome 完成 canvas 图片,但是 firefox 却 canvas 空白,Google 一下 Try calling the drawImage function inside the image's load function to ensure that the image is actually loaded before trying to draw it.

image.onload = function() {
    // context.drawImage...
}

但这样做可能再次打开页面,drawImage 不会发生,原因是 image 可能不触发 onload,而是 complete,所以

image.onload = function() {
    // context.drawImage...
}
if (image.complete) image.onload();