useImage
API
import useImage from '@restart/hooks/useImage'
useImage
(imageOrUrl?: string | HTMLImageElement | null | undefined, crossOrigin?: "anonymous" | "use-credentials" | string) => objectFetch and load an image for programatic use such as in a
<canvas>
element.Parameters
imageOrUrl?
string | HTMLImageElement | null | undefinedThe
HtmlImageElement
or image url to loadcrossOrigin?
"anonymous" | "use-credentials" | stringThe
crossorigin
attribute to setconst { image, error } = useImage('/static/kittens.png')const ref = useRef<HTMLCanvasElement>()useEffect(() => {const ctx = ref.current.getContext('2d')if (image) {ctx.drawImage(image, 0, 0)}}, [ref, image])return (<>{error && "there was a problem loading the image"}<canvas ref={ref} /></>
Return Value object