Edward Han
Edward Han

Reputation: 13

Cannot get image to load into Konva-React, Getting TypeError: react-konva webpack ...Image is not a constructor

I am trying to use Konva-React to set image within shape objects using fillpatternImage.

I've tried a few different ways but apparently I need to use an Image object, not a Konva Image for fillPatternImage.

Why I cannot use Konva.Image() in Konva.Shape.fillPatternImage(imageObj)?

So I am trying this....

const GridView = (props) => {

    const placeRectImages = props.places.map(({ ...otherProps }, index) => {
        const corsProxy = 'https://cors-anywhere.herokuapp.com/' + otherProps.default_image_url
        var imageObj = new Image();  /error happens here....
        imageObj.src = corsProxy
        imageObj.onload = function () {

            var canvas = document.createElement("canvas"),
                canvasContext = canvas.getContext("2d");
            canvasContext.drawImage(imageObj, 0, 0, 30, 30);
            var dataURL = canvas.toDataURL();

            return <Circle
                x={20 * index}
                y={20 * index / 2}
                width={50}
                height={50}
                shadowBlur={5}
                fillPatternImage={dataURL} // would try passing imageObj if dataURL didn't work 
            />
        };

But get an error that says "TypeError: react_konva__WEBPACK_IMPORTED_MODULE_1__.Image is not a constructor"

Any ideas on how to resolve this or otherwise fillpatternimage into a konva shape would be appreciated...

EDIT. As Lavrton stated below, I imported Image from React-Konva which caused the problem accessing the global variable. So I changed Image() to window.Image()

Still wasn't loading into my Shape object so to get it to work completely, I ended up doing this..

const PlaceCircle = (url, index) => {
    let timage
    const [image, setImage] = useState(null)
    useEffect(() => {
        loadImage();

    }, []);
    const loadImage = () => {
        timage = new window.Image();
        timage.src = url;
        timage.addEventListener("load", handleLoad);
    }
    const handleLoad = () => {
        setImage(timage);
    }
    return (
        <Circle
            x={20 * index}
            y={20 * index / 2}
            width={50}
            height={50}
            shadowBlur={5}
            fillPatternImage={image}
        />
    );
}

and then using the above function to map from my props..


    return (

        <Stage width={window.innerWidth} height={window.innerHeight}>
            <Layer>
                {props.places.map(({ ...otherProps }, index) => {
                    const corsProxy = otherProps.default_image_url
                    return PlaceCircle(corsProxy, index)
                })}
            </Layer>
        </Stage>

    );

Upvotes: 1

Views: 1393

Answers (1)

lavrton
lavrton

Reputation: 20373

Like like you imported Image component from react-konva:

import { Image } from 'react-konva';

Such import overwrites the global Image object.

To fix the issue you can:

  1. Rename import
import { Image as KonvaImage } from 'react-konva';
  1. Or access Image from global:
var imageObj = new window.Image();
  1. Or create image from document:
var imageObj = document.createElement('img');

Upvotes: 1

Related Questions