I am trying to make flappy bird but when I try to load the background on my canvas it just does nothing
var cvs = document.getElementById("canvas"); var ctx = cvs.getContext("2d"); // load img var bird = new Image(); bird.src = "img/bird.png"; var bg = new Image(); bg.src = "img/bg.png"; var fg = new Image(); fg.src = "img/fg.png"; var pipeNoord = new Image(); pipeNoord.src = "img/pipeNorth.png"; var pipeSouth = new Image(); pipeSouth.src = "img/pipeSouth.png"; // draw images window.onLoad = function(){ ctx.drawImage(bg, 0, 0); }
I would suggest you move the whole image loading code into the onload
handler. Like so, you can make sure that everything is loaded before you actually call drawImage
. In addition, as @Chris G mentioned, it's window.onload
, not window.onLoad
:
// draw images window.onload = function(){ var cvs = document.getElementById("canvas"); var ctx = cvs.getContext("2d"); // load img var bird = new Image(); bird.src = "img/bird.png"; var bg = new Image(); bg.src = "img/bg.png"; var fg = new Image(); fg.src = "img/fg.png"; var pipeNoord = new Image(); pipeNoord.src = "img/pipeNorth.png"; var pipeSouth = new Image(); pipeSouth.src = "img/pipeSouth.png"; ctx.drawImage(bg, 0, 0); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.