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

218
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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