Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!