简体   繁体   English

如何使用自定义挂钩访问结构 object 以将背景图像添加到 canvas?

[英]How can I access fabric object using custom hook to add background image to canvas?

So I'm working on this editor where a user can upload an image and edit it on the canvas. To achieve this I'm using React and FabricJS.所以我正在开发这个编辑器,用户可以在其中上传图像并在 canvas 上对其进行编辑。为此,我使用了 React 和 FabricJS。 Besides everything I have tried to make an editor, FabricJS seemed to have what I want.除了我尝试制作编辑器的一切之外,FabricJS 似乎拥有我想要的东西。 And as FabricJS doesn't have native support for react I have been looking for a workaround to make it work with React.由于 FabricJS 没有对 React 的本机支持,我一直在寻找一种解决方法来使其与 React 一起工作。 And I stumbled upon this answer here and trying to use it.我在这里偶然发现了这个答案并尝试使用它。

This custom hook from that answer eliminates another issue which is adding multiple nested canvas wrappers.该答案中的这个自定义挂钩消除了另一个问题,即添加多个嵌套的 canvas 包装器。

Right now it is working fine if I just use a background color but it throws an error when I try to add a background image.现在如果我只使用背景颜色它工作正常但是当我尝试添加背景图像时它会抛出错误。 Seems like the fabric object is not accessible.似乎无法访问结构 object。 And I'm not sure how I can access it.而且我不确定如何访问它。

Here's the error:这是错误:

TypeError: Cannot read properties of undefined (reading 'fromURL')

So I'm posting here if someone has an idea on how to add background image, or different shapes to the canvas using the fabric object but also using this hook as it eliminates nested fabric wrappers.因此,如果有人知道如何使用织物 object 向 canvas 添加背景图像或不同形状,但也使用此钩子,因为它消除了嵌套的织物包装纸,我会在这里发帖。

Here's the CodeSandbox .这是CodeSandbox

Here's the code:这是代码:

import { useCallback, useRef } from "react";
import { fabric } from "fabric";

const useFabric = (onChange) => {

    const fabricRef  = useRef();
    const disposeRef = useRef();

    return useCallback((node) => {
        if (node) {
             fabricRef.current = new fabric.Canvas(node);

             if (onChange) {
                  disposeRef.current = onChange(fabricRef.current);
             }

         } else if (fabricRef.current) {

              fabricRef.current.dispose();

              if (disposeRef.current) {
                   disposeRef.current();
                   disposeRef.current = undefined;
              }
         }
     }, []);

};


export default function App() {

     const ref = useFabric((fabricCanvas) => {

           fabricCanvas.backgroundColor = "green";

           // fabricCanvas.Image.fromURL("https://i.imgur.com/8zvUjul.jpg", function ( img ) {
           //     fabricCanvas.add(img);
           //     fabricCanvas.sendToBack(img);
           // });

          fabricCanvas.renderAll();
     });

     return (
         <div>
             <canvas ref={ref} id="canvas" />
         </div>
     );
}

You're calling .Image.FromUrl to the canvas instance not the fabric object.您正在将.Image.FromUrl调用到 canvas 实例而不是结构 object。

Try尝试

fabric.Image.fromUrl...

instead of代替

fabricCanvas.Image.fromURL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM