summaryrefslogtreecommitdiff
path: root/src/components/app/map/elements/ImageComponent.js
blob: 486296eacdffb1fb9250be361d5b7a667caae710 (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
38
39
40
41
42
43
44
45
46
47
48
import PropTypes from "prop-types";
import React from "react";
import {Image} from "react-konva";

class ImageComponent extends React.Component {
    static imageCaches = {};
    static 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,
    };

    state = {
        image: null
    };

    componentDidMount() {
        if (ImageComponent.imageCaches[this.props.src]) {
            this.setState({image: ImageComponent.imageCaches[this.props.src]});
            return;
        }

        const image = new window.Image();
        image.src = this.props.src;
        image.onload = () => {
            this.setState({image});
            ImageComponent.imageCaches[this.props.src] = image;
        }
    }

    render() {
        return (
            <Image
                image={this.state.image}
                x={this.props.x}
                y={this.props.y}
                width={this.props.width}
                height={this.props.height}
                opacity={this.props.opacity}
            />
        )
    }
}

export default ImageComponent;