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

161
Visualizações
React broken state from drawImage

React beginner here, I'm getting this error 'Uncaught DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The HTMLImageElement provided is in the 'broken' state.' i have been stuck with this for few hours now.

That error is showing on drawImage which is inside if statement. Is there a way to tell it if there is an image then go on and draw it ( ctx.drawImage(img, 0, 0)), but if there isnt any image then dont give any error(dont show any image either), something like what Optional chaining (?.) does ?

English is not my mother language so could be mistakes.

my code:

 
     const CameraCanvas = useRef<any>();
     const CameraImage = useRef<any>();
 
  const updatePreviewUrl = () => {
  if (currentCam) {
  let curSnapshotUrl = state.snapshot?.snapshotUrl.split("?ts=")[0];
  let newSnapshotUrl =
    `api/cam/${....}/` +
    `${curCam.ident}/snapsht`;
  if (curSnapshotUrl ....) {
    setState({
    ...
    });
  }
}
  };

    const updateCanvas = () => {
    updatePreviewUrl();
    const canvas = CameraCanvas.current;
    const ctx = canvas.getContext("2d");

    const img = CameraImage.current;
    ctx.rect(0, 0, canvas.width, canvas.height);
    ctx.fill();
    if (currentCam) {
      ctx.drawImage(img, 0, 0);
  
    }
  };
 
 
 useEffect(() => {
        updateCanvas();
        const img = CameraImage.current;
        img.onload = updateCanvas;

        return () => {
          img.onload = null;
        };
      });

about 4 years ago · Santiago Trujillo
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