File size: 428 Bytes
1f122c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { useState, useEffect } from "react"

export function useCanvasImage(url: string): HTMLImageElement | null {
  const [imageSrc, setImageSrc] = useState<HTMLImageElement | null>(null);

  useEffect(() => {
    const img = new Image();

    img.onload = () => {
      setImageSrc(img);
    };

    img.src = url;
    
    return () => {
      img.onload = null;
      img.src = '';
    };
  }, [url]);

  return imageSrc;
}