Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
Adding Pictures onto Canvas

I am creating a game using HTML and JavaScript, and I've created assets for the game. (PNG files)

The problem is, I want to add the image onto the canvas, but I didn't know how. So I watched some YouTube Videos (about 5) and tried to do what the person on YouTube did.

Didn't work. I used PNG, and the guy on YouTube also used PNG. I copied his EXACT code, but it doesn't work for ME, not him.

Here is my code (part of it, because the others are just plain HTMl):

    <script>
        let canvEl = document.getElementById('game');
        let c = canvEl.getContext("2d");

        // Draw Sky

        c.fillStyle = "rgb(22, 203, 235)"
        c.fillRect(0,0,w,h)

        // Draw Ground

        c.drawImage('dirt_block.png', 100, 100, 18, 18);
    </script>

Then, after I loaded, all I saw was plain blue. (They sky) The dirt block should be brown, but I don't spot a dot of brown anywhere.

So I went into the console, and it displayed an error message:

game.html:75 Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLCanvasElement or HTMLImageElement or HTMLVideoElement or ImageBitmap or OffscreenCanvas or SVGImageElement or VideoFrame)'.

Please help, because errors make my guts hurt. lol

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!