简体   繁体   English

在 Canvas 上反应 useRef 得到 null 或 undefined

[英]React useRef on Canvas getting null or undefined

I am trying to reference a canvas DOM element to make the width of it the same as its parent without messing with the scale of the canvas.我正在尝试引用画布 DOM 元素以使其宽度与其父元素相同,而不会弄乱画布的比例。 When doing so I am getting a Uncaught TypeError: _canvasRef$current.getContext is not a function error and am not exactly sure how to fix it.这样做时,我得到一个Uncaught TypeError: _canvasRef$current.getContext is not a function error 并且不完全确定如何修复它。

Ive console logged a few things just to make the app run but the issue is within the useEffect我控制台记录了一些事情只是为了让应用程序运行,但问题在于useEffect

import SignaturePad from 'react-signature-canvas'

const canvasRef = useRef<HTMLCanvasElement>(null)

useEffect(() => {
    const ctx: CanvasRenderingContext2D | null | undefined =
        canvasRef.current?.getContext('2d')
    const ctxContainer = document.getElementById('ctxContainer')

    console.log('ctx: ', ctx)
    // if (ctx && ctxContainer) {
    //     ctx.style.width = '100%'
    //     ctx.style.height = '100%'
    //     ctx.width = ctxContainer.clientWidth
    // }
}, [])

const save = () => {
    console.log('saved!')
}

console.log('canvasRef: ', canvasRef)

return (
    <div id={`ctxContainer`} className={'signatureInput__option-draw'}>
        <SignaturePad
            ref={canvasRef}
            canvasProps={{
                className: 'signatureInput__option-draw__canvas',
            }}
            onEnd={save}
        />
    </div>
)

Try this.尝试这个。

Note that you may still need to do some changes to following code to full fill your full requirement ( such as event handling when the user change the screen sizes. ) The following code will fix the issue you are facing right now.请注意,您可能仍需要对以下代码进行一些更改以完全满足您的全部要求(例如用户更改屏幕尺寸时的事件处理。)以下代码将解决您现在面临的问题。

  useEffect(() => {
    if (canvasRef.current) {
      const canvas = canvasRef.current.getCanvas();
      const ctx = canvas.getContext('2d');

      // do something with the canvas ref
      if (ctx) {
        ctx.canvas.width = window.innerHeight;
        ctx.canvas.height = window.innerHeight;
      }
    }
  }, []);

Try using getCanvas() .尝试使用 getCanvas() 。 i dont think getContext() is provided with the package我不认为 getContext() 是随包一起提供的

function App() {
  const canvasRef = React.createRef(null);

  useEffect(() => {
    const canvas = canvasRef.current;
    console.log(canvas.current);
    const ctx = canvas.getCanvas();
    const ctxContainer = document.getElementById("ctxContainer");

    console.log("ctx: ", ctx);
    if (ctx && ctxContainer) {
      ctx.style.width = "100%";
      ctx.style.height = "100%";
      ctx.width = ctxContainer.clientWidth;
    }
  }, []);

  const save = () => {
    console.log("saved!");
  };

  console.log("canvasRef: ", canvasRef);

  return (
    <div className="App">
      <div id={`ctxContainer`} className={"signatureInput__option-draw"}>
        <SignaturePad
          ref={(ref) => {
            canvasRef.current = ref;
          }}
          canvasProps={{
            className: "signatureInput__option-draw__canvas",
          }}
          onEnd={save}
        />
      </div>
    </div>
  );
}

暂无
暂无

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

相关问题 在 react-js 中的 useRef() 上出现未定义的错误 - getting undefined error on useRef() in react-js 在反应 useRef() 钩子中获取 TypeError - Getting TypeError in react useRef() hook React, getting Uncaught TypeError: Cannot read properties of null (reading "useRef") while using React Router Dom - React, getting Uncaught TypeError: Cannot read properties of null (reading "useRef") while using React Router Dom useRef 数组元素未定义 / null 以一种奇怪的方式 - useRef array element is undefined / null in a weird way 传入 null 和 undefined in useRef 之间的区别 - Difference between passing in null and undefined in useRef React Tooltip 组件与 TypeScript 在我的 useRef 元素上出现“可能未定义”错误,不知道如何修复 - React Tooltip component with TypeScript getting a 'Possibly Undefined' error on my useRef element, not sure how to fix 在 React 功能组件中使用 useRef 时出现无法读取 null 错误的属性“样式” - Getting a Cannot read property 'style' of null Error When Using useRef Within a React Functional Component 反应,得到未捕获的类型错误:无法在 BrowserRouter 读取 null 的属性(读取“useRef”) - React, getting Uncaught TypeError: Cannot read properties of null (reading "useRef") at BrowserRouter React Native - useRef 在 RNCamera 上给出空值 - React Native - useRef is giving null value on RNCamera 与 useEffect 一起使用时,React Hook useRef 为 null - React Hook useRef is null when used with useEffect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM