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

220
Vistas
How can I alternate between a background color and a background image on canvas?

I have this program where I can add lights to my scene (stored in an array lights). When I first start the program, I want everything to be black since I start with my array empty. But when there is a light, I want to change the background to an image, and when there are no lights, I want everything to be black again. I´m trying to call this in my render function:

if(lights.length == 0) {
  canvas.style.background = "#000000 none";
} else {
  canvas.style.background = "transparent url('nebula.png')";
}

But it's not wroking as I intended. Does someone know how to achieve this?

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

0

don't use background properties on canvas.
Use context.fillRect() to set background color
Use context.fillImage() to set background image
Reference:
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillRect <= fillRect()
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage <= fillImage()

Edit:
Example:

const cvs = document.getElementById(' /* your canvas */ ')
const context = cvs.getContext('2d')
// context is the CanvasRenderingContext2D
// use the two commands above to make background.
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