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

119
Visualizações
HTML canvas disappears in Chrome after browser window/tab becomes inactive

My canvas element works perfectly fine except when tested in Chrome on a Mac running MacOS 12.2, where the canvas content gets completely wiped out when I

  • minimize Chrome
  • switch to a different tab
  • swipe to another screen
  • place a window on top of Chrome

... even though the canvas tag still remains in the DOM. I was able to reproduce this with some websites using canvas (e.g. https://minexmr.com/poolstats) but not others. Cannot reproduce it in other browsers.

Is there anything I can do to prevent this from happening or is this an issue with Chrome? There appears to be similar complaints from a few years ago.

React code snippet

const canvasRef = useRef(null);

useEffect(() => {
  if (canvasRef && canvasRef.current && canvasRef.current.getContext) {
    const canvas = canvasRef.current;
    const ctx = canvas.getContext('2d');

    if (ctx) {
      ctx.fillStyle = 'red';
      ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    }
  }
}, [])

return (
  <>
    ...
    <canvas ref={canvasRef} width="1000" height="200">
      Fallback text
    </canvas>
    ...
  </>
);

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is caused by https://crbug.com/1268146 where they are trying to free the resources held by canvases in background pages. Note that to reproduce this bug you must have the chrome://flags#enable-experimental-web-platform-features flag on.
The context is supposed to be restored back when the document gains focus again, but obviously this part was failing as the context is just wiped out entirely.
They now hide this feature even more and thus the new Canary doesn't expose this issue anymore.

Given that this is all behind dev flags, few of your users should face it, and it's probably not worth the effort to implement a workaround for it.
But if you really need one, you can create an ImageBitmap in the visibilitychange event and do the restore yourself:

const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "red";
ctx.fillRect(0, 0, 40, 40);
let bmp;
document.onvisibilitychange = async(evt) => {
  if (document.visibilityState === "hidden") {
    bmp = await createImageBitmap(canvas);
  } else {
    ctx.globalCompositeOperation = "copy";
    ctx.drawImage(bmp, 0, 0);
    ctx.globalCompositeOperation = "source-over";
  }
};
<canvas></canvas>

I added a comment on the issue to let them know that the restoring fails in case they come back to it.

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