Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda