+ 3

html5 canvas problem

I tried to make a flappybird game using html5 canvas on javascript but i show nothing(this is only the background) heres my code: <canvas id="canvas" width="800" height="600" style="border:1px solid #000000"></canvas> <script> canvas = document.getElementById("canvas"); ctx = canvas.getContext("2d"); var bebek1 = new Image(); var bg = new Image(); var pipeNorth = new Image(); var pipeSouth = new Image(); var fg = new Image(); bebek1.src = "images/bebek1.png" bg.src = "images/bg.png" pipeNorth.src = "images/pipeNorth.png" pipeSouth.src = "images/pipeSouth.png" fg.src = "images/fg.png" var gap = 75; var constant = pipeNorth.height+gap; function draw(){ ctx.drawImage(bg,0,0) ctx.drawImage(pipeNorth,100,0) ctx.drawImage(pipeSouth,100,0+constant); } draw(); </script>

21st Feb 2018, 11:26 AM
Deven Valisten
Deven Valisten - avatar
2 Respostas
+ 3
You should put all drawImage() inside a window.onload function to make sure the image has loaded before drawing it to the canvas.
21st Feb 2018, 12:12 PM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 1
function draw(){ ctx.drawImage(bg,0,0) ctx.drawImage(pipeNorth,100,0) ctx.drawImage(pipeSouth,100,0+constant); requestAnimationFrame(draw); } Add requestAnimationFrame(draw); and it must work
3rd May 2019, 4:07 PM
kishor
kishor - avatar