我已经阅读了所有其他类似的帖子,但我不明白为什么我有这个问题。
我在页面上有一个画布(#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);
}
什么都没有出现。控制台没有错误。为什么???
谢谢!