Note that there are some explanatory texts on larger screens.

plurals
  1. POHTML 5 Canvas Image Rendering
    primarykey
    data
    text
    <p>First time poster here but definitely not a first time reader.</p> <p>My question is aimed directly at this portion of code I have. I am currently learning how HTML 5 canvases work and am designing my own RPG style game for a University project. After looking around I found some good tutorials on this guys blog, I have followed his code and triple checked it but images are now showing up.</p> <p>I tried putting an alert() before and after when the image is called to the canvas under drawMap(). It works before the image is drawn but not after, leading me to believe it is something to do with my image rendering. Can someone double check my code and see what is going on? It's driving me insane!</p> <pre><code>&lt;canvas id="game-viewport" width="760" height="440"&gt;&lt;/canvas&gt; &lt;script&gt; window.onload = init; var map = Array([0,0],[0,0],[0,0],[0,0]); var tileSize = 40; tileTypes = Array("grass.png"); tileImage = new Array(); var loaded = 0; var loadTimer; function loadImage(){ for(i = 0; i &lt; tileTypes.length; i++){ tileImage[i] = new Image(); tileImage[i].src = "./game/lib/icons/own_icons/" + tileTypes[i]; tileImage[i].onload = function(){ loaded++; } } } function loadAll(){ if(loaded == tileTypes.length){ clearInterval(loadTimer); drawMap(); } } function drawMap(){ var mapX = 80; var mapY = 10; for(i = 0; i &lt; map.length; i++){ for(j = 0; j &lt; map[i].length; j++){ var drawTile = map[i][j]; var xPos = (i - j) * tileSize; var yPos = (i + j) * tileSize; ctx.drawImage(tileImage[drawTile], xPos, yPos); } } } function init(){ var canvas = document.getElementById('game-viewport') var ctx = canvas.getContext('2d'); loadImage(); loadTimer = setInterval(loadAll, 100); } &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload