我已閱讀所有其他類似的帖子,但我不明白爲什麼它有這個問題。Javascript - canvas - drawImage不起作用
我在頁面上有一個畫布(#canvas)和一個圖像(hero.png),並在正文的末尾加載了一個JS文件。在JS代碼...
這工作:
var game = {};
game.canvas = document.getElementById('canvas');
game.ctx = game.canvas.getContext("2d");
game.draw = function(){
game.ctx.drawImage(game.hero, 200, 200);
}
game.hero = new Image();
game.hero.src = "hero.png";
game.hero.onload = game.draw;
而且這不起作用:出現
var game = {};
game.canvas = document.getElementById('canvas');
game.ctx = game.canvas.getContext("2d");
game.hero = new Image();
game.hero.src = "hero.png";
game.hero.onload = game.draw;
game.draw = function(){
game.ctx.drawImage(game.hero, 200, 200);
}
沒有。控制檯沒有錯誤。爲什麼???
謝謝!
game.hero.onload未定義,請嘗試更改順序。首先定義繪圖函數,然後分配它。 –