UNPKG

1.66 kBJavaScriptView Raw
1import { useState, useEffect } from 'react';
2/**
3 * Fetch and load an image for programatic use such as in a `<canvas>` element.
4 *
5 * @param imageOrUrl The `HtmlImageElement` or image url to load
6 * @param crossOrigin The `crossorigin` attribute to set
7 *
8 * ```ts
9 * const { image, error } = useImage('/static/kittens.png')
10 * const ref = useRef<HTMLCanvasElement>()
11 *
12 * useEffect(() => {
13 * const ctx = ref.current.getContext('2d')
14 *
15 * if (image) {
16 * ctx.drawImage(image, 0, 0)
17 * }
18 * }, [ref, image])
19 *
20 * return (
21 * <>
22 * {error && "there was a problem loading the image"}
23 * <canvas ref={ref} />
24 * </>
25 * ```
26 */
27export default function useImage(imageOrUrl, crossOrigin) {
28 const [state, setState] = useState({
29 image: null,
30 error: null
31 });
32 useEffect(() => {
33 if (!imageOrUrl) return undefined;
34 let image;
35 if (typeof imageOrUrl === 'string') {
36 image = new Image();
37 if (crossOrigin) image.crossOrigin = crossOrigin;
38 image.src = imageOrUrl;
39 } else {
40 image = imageOrUrl;
41 if (image.complete && image.naturalHeight > 0) {
42 setState({
43 image,
44 error: null
45 });
46 return;
47 }
48 }
49 function onLoad() {
50 setState({
51 image,
52 error: null
53 });
54 }
55 function onError(error) {
56 setState({
57 image,
58 error
59 });
60 }
61 image.addEventListener('load', onLoad);
62 image.addEventListener('error', onError);
63 return () => {
64 image.removeEventListener('load', onLoad);
65 image.removeEventListener('error', onError);
66 };
67 }, [imageOrUrl, crossOrigin]);
68 return state;
69}
\No newline at end of file