Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

144
Vistas
I Can't draw an image using drawImage method in html5 canvas

I am trying to create dino cloud game. but I encouter a problem .When I try to draw the image of the dinosaur in the canvas using drawImage method , the image is not drawn . Can you help me please.

const canvas = document.querySelector(".canvas");
canvas.height = 400;
canvas.width = 800;

const ctx = canvas.getContext("2d");

class Dino {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.width = (88 * this.height) / 94;
    this.height = 70;
  }
  draw() {
    let image = new Image();
    image.src = "imgs/player/idle/dino-idle-1.png";
    ctx.drawImage(image, this.x, this.y, this.width, this.height);
  }}
  
const dino = new Dino(20, 200);
dino.draw();

I tried to draw dino using window.onload but it is not working

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to draw an image on canvas only after the image is fully loaded (For example, using the onload trigger on the image).

Example below:

const canvas = document.querySelector(".canvas");
canvas.height = 400;
canvas.width = 800;

const ctx = canvas.getContext("2d");

class Dino {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.height = 70;
    this.width = (88 * this.height) / 94;
  }
  draw() {
    const image = new Image();
    image.onload = () => ctx.drawImage(image, this.x, this.y, this.width, this.height);
    image.src = "https://www.fillmurray.com/600/400";
  }}
  
const dino = new Dino(20, 200);
dino.draw();
<canvas class="canvas"></canvas>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda