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

162
Vistas
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 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