summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-ui/src/components/topologies/map/elements/ImageComponent.js
blob: fdae53f2e2b050c5291bb2faaba149063e3ffbd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import PropTypes from 'prop-types'
import React, { useEffect, useState } from 'react'
import { Image } from 'react-konva'

const imageCaches = {}

function ImageComponent({ src, x, y, width, height, opacity }) {
    const [image, setImage] = useState(null)

    useEffect(() => {
        if (imageCaches[src]) {
            setImage(imageCaches[src])
            return
        }

        const image = new window.Image()
        image.src = src
        image.onload = () => {
            setImage(image)
            imageCaches[src] = image
        }
    }, [src])

    // eslint-disable-next-line jsx-a11y/alt-text
    return <Image image={image} x={x} y={y} width={width} height={height} opacity={opacity} />
}

ImageComponent.propTypes = {
    src: PropTypes.string.isRequired,
    x: PropTypes.number.isRequired,
    y: PropTypes.number.isRequired,
    width: PropTypes.number.isRequired,
    height: PropTypes.number.isRequired,
    opacity: PropTypes.number.isRequired,
}

export default ImageComponent