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

124
Vistas
No image width, canvas / constructor javascript

Hello everyone this is my problem :

I am currently creating a game following a tutorial, but I have a problem in my constructor.

I'm using a constructor to draw some platform. I'm trying to draw an image on my platform but it doesnt work.

The image appears but not its width (things I need to have to make the project works correctly). But after a reload, the width is correct, and the project is working.

So I assume that I have to declare my image somewhere (maybe with an onload) but I tried a lot of things without it ever working.

Here is my code :

class Platform {  
  constructor({x, y, image}) {
    this.position = {
      x: x,
      y: y
    }
    
    this.image = image
    this.width = image.width
    this.height = image.height

  }

  draw() {
    canvas.drawImage(this.image, this.position.x, this.position.y)
  }
}

const image = new Image()
image.src = platform

console.log(image)
console.log(image.width)

const player = new Player()
const platforms = [new Platform({
  x: 200,
  y: 100,
  image: image
}), new Platform({
  x: 500, y: 200, image: image})]

const keys = {
  right: {
    pressed: false
  },
  left: {
    pressed: false
  }
}


let scrollOffset = 0


function animate() {
  requestAnimationFrame(animate)
  canvas.clearRect(0, 0, getCanvas.width, getCanvas.height)
  platforms.forEach((platform) => {
    platform.draw()
  })

So as you can see, on the first load, I have no width : 0 width

But, if I hits reload, there it is : 580 width

Im someone had an idea for me...

Thanks in advance and sorry for my english

about 4 years ago · Juan Pablo Isaza
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