I got the following error:
Uncaught TypeError: Failed to execute 'drawImage' in 'CanvasRenderingContext2D': the supplied value is not of type '(HTMLImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap)'
I saw a link to the same error, but the implementation was different from the mines. This is a game, and it displays some images, but still gives this error. Here is my code:
This is the line where chrome indicates an error:
for (row = 0; row < numRows; row++) { for (col = 0; col < numCols; col++) { ctx.drawImage(resources.get(rowImages[row]), col * 101, row * 83); } }
This is ctx.drawImage(resources.get(rowImages[row]), col * 101, row * 83) .
This is the full render() function. Images are contained in the following array:
function render() { var rowImages = [ 'images/water-block.png',
resources - a separate resources.js file that creates a cache for images, the code should be in case this helps:
(function() { var resourceCache = {}; var loading = []; var readyCallbacks = []; function load(urlOrArr) { if(urlOrArr instanceof Array) { urlOrArr.forEach(function(url) { _load(url); }); } else { _load(urlOrArr); } } function _load(url) { if(resourceCache[url]) { return resourceCache[url]; } else { var img = new Image(); img.src = url; img.onload = function() { resourceCache[url] = img; if(isReady()) { readyCallbacks.forEach(function(func) { func(); }); } }; resourceCache[url] = false; } } function get(url) { return resourceCache[url]; } function isReady() { var ready = true; for(var k in resourceCache) { if(resourceCache.hasOwnProperty(k) && !resourceCache[k]) { ready = false; } } return ready; } function onReady(func) { readyCallbacks.push(func); } window.resources = { load: load, get: get, onReady: onReady, isReady: isReady }; })();
Chrome also lists two other sections with the same error:
var main = function () { var now = Date.now(); var delta = now - then; update(delta / 1000); render(); then = now;
Error in rendering rendering ();
and in the last line of my file, which calls main () as follows:
// Let play this game! var then = Date.now(); reset(); main();