Skip to content
Advertisement

Trying to display images using JSON file in Javascript

I am trying to display different plane images on a canvas. I have a JSON file that includes the name of cities in an array:

JavaScript

Now, I want to display images on a canvas in a random position on the canvas using random plane images corresponding to the name of the cities, and I’m not sure if I am doing it properly.

Here is how I got the JsonData:

JavaScript

And the code I used to try and display the images on the canvas:

JavaScript

Any type of clarification on how to fix this would be much appreciated

Advertisement

Answer

The complete error you are getting is probably this one:

Uncaught TypeError: Failed to execute ‘drawImage’ on ‘CanvasRenderingContext2D’: No function was found that matched the signature provided.

The problem here is that CanvasRenderingContext2D.drawImage requires an Image object as its first parameter.

Your findImage function currently returns the image path.

You could alter your function like this:

JavaScript
Advertisement