【问题标题】:Cannot get image to load into Konva-React, Getting TypeError: react-konva webpack ...Image is not a constructor无法将图像加载到 Konva-React 中,出现 TypeError: react-konva webpack ...图像不是构造函数
【发布时间】:2020-08-22 04:56:47
【问题描述】:

我正在尝试使用 Konva-React 使用 fillpatternImage 在形状对象中设置图像。

我尝试了几种不同的方法,但显然我需要使用 Image 对象,而不是用于 fillPatternImage 的 Konva Image。

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

所以我正在尝试这个....

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 
            />
        };

但是得到一个错误提示“TypeError: react_konva__WEBPACK_IMPORTED_MODULE_1__.Image is not a constructor”

任何关于如何将这个或其他填充模式图像解析为 konva 形状的想法将不胜感激......

编辑。正如 Lavrton 所说,我从 React-Konva 导入了 Image,这导致了访问全局变量的问题。于是我把Image()改成了window.Image()

仍然没有加载到我的 Shape 对象中,所以为了让它完全工作,我最终这样做了..

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}
        />
    );
}

然后使用上面的函数从我的道具映射..


    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>

    );

【问题讨论】:

    标签: reactjs image react-konva


    【解决方案1】:

    就像你从 react-konva 导入 Image 组件一样:

    import { Image } from 'react-konva';
    

    这种导入会覆盖全局 Image 对象。

    要解决此问题,您可以:

    1. 重命名导入
    import { Image as KonvaImage } from 'react-konva';
    
    1. 或从全局访问Image
    var imageObj = new window.Image();
    
    1. 或从文档创建图像:
    var imageObj = document.createElement('img');
    

    【讨论】:

    • 谢谢你,#2 为我做了。我还必须更改其他一些东西才能使其正常工作,我也会将其包含在原始问题中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 2018-09-19
    • 1970-01-01
    • 2019-06-28
    相关资源
    最近更新 更多