您可以在feact三纤维中的画布中门户吗?

发布于 2025-02-10 10:08:41 字数 2176 浏览 1 评论 0原文

是否可以从一个外部进行一些react-three-fibre元素进入画布?

IE在下面的示例中,这是我第一次尝试将网格放入canvas中,该画布与“目标元素”不是DOM元素”(这是有道理的,鉴于这些不是DOM元素),

import * as React from 'react'
import { Canvas, createPortal } from '@react-three/fiber'

export const Element = ({ parent }) => {
  return createPortal(
    <mesh>
      <boxGeometry args={[1, 1, 1]} />
      <meshStandardMaterial color="red" />
    </mesh>
  , parent);
};

export const MyPage = () => {
  const ref = React.useRef(null);

  return (
    <div>
      <Canvas>
        <group ref={ref} />
      </Canvas>

      <Element parent={ref.current} />
    </div>
  );
};

所以我找到了CreatePortal方法(从react-three-fiber中,它具有非常有限的文档),但是随之而来的是错误,大概在这里,因为createportal方法仅在已经在帆布上下文中时起作用,并且用于在帆布内进行

import * as ReactDOM from 'react-dom'
import * as React from 'react'
import { Canvas } from '@react-three/fiber'

export const Element = ({ parent }) => {
  return ReactDOM.createPortal(
    <mesh>
      <boxGeometry args={[1, 1, 1]} />
      <meshStandardMaterial color="red" />
    </mesh>
  , parent);
};

export const MyPage = () => {
  const ref = React.useRef(null);

  return (
    <div>
      <Canvas>
        <group ref={ref} />
      </Canvas>

      <Element parent={ref.current} />
    </div>
  );
};

车门上面引发了此错误:

react-dom.development.js:22839 Uncaught R3F hooks can only be used within the Canvas component!

有没有办法这样做?

我的希望是能够将项目的其他位置放置在根本上的单个文档大小的帆布元素中。

更新:

&lt; canvas/&gt; 中包裹createPortal的呼叫来使其起作用

export const Element = ({ parent }) => {
   return (
    <Canvas>
      {createPortal(
        <mesh>
          <boxGeometry args={[1, 1, 1]} />
          <meshStandardMaterial color="blue" />
        </mesh>,
        portalTo
      )}
    </Canvas>
  );
};
</code>

我设法通过在第二个在项目中的帆布(并且在DOM中到处都有未使用的实际帆布确实很粗糙)

是否有一种方法可以使createPortal访问该上下文而无需将其渲染在画布内?

Is it possible to portal some react-three-fibre elements into a canvas from outside of one?

I.e. in the following example, which was my first attempt to portal a mesh into a Canvas which errored with "target element is not a DOM element" (which makes sense, given these aren't DOM elements)

import * as React from 'react'
import { Canvas, createPortal } from '@react-three/fiber'

export const Element = ({ parent }) => {
  return createPortal(
    <mesh>
      <boxGeometry args={[1, 1, 1]} />
      <meshStandardMaterial color="red" />
    </mesh>
  , parent);
};

export const MyPage = () => {
  const ref = React.useRef(null);

  return (
    <div>
      <Canvas>
        <group ref={ref} />
      </Canvas>

      <Element parent={ref.current} />
    </div>
  );
};

So I found the createPortal method (which has incredibly limited documentation) from react-three-fiber, but with that it throws errors, presumably here because the createPortal method only works when already inside a Canvas context and is for portaling around inside a Canvas

import * as ReactDOM from 'react-dom'
import * as React from 'react'
import { Canvas } from '@react-three/fiber'

export const Element = ({ parent }) => {
  return ReactDOM.createPortal(
    <mesh>
      <boxGeometry args={[1, 1, 1]} />
      <meshStandardMaterial color="red" />
    </mesh>
  , parent);
};

export const MyPage = () => {
  const ref = React.useRef(null);

  return (
    <div>
      <Canvas>
        <group ref={ref} />
      </Canvas>

      <Element parent={ref.current} />
    </div>
  );
};

the above throws this error:

react-dom.development.js:22839 Uncaught R3F hooks can only be used within the Canvas component!

Is there a way of doing this?

My hope is to be able to portal elements from elsewhere in the project into a single document-sized Canvas element that sits at the root.

update:

I have managed to get this to work by wrapping the call to createPortal in a second <Canvas />

export const Element = ({ parent }) => {
   return (
    <Canvas>
      {createPortal(
        <mesh>
          <boxGeometry args={[1, 1, 1]} />
          <meshStandardMaterial color="blue" />
        </mesh>,
        portalTo
      )}
    </Canvas>
  );
};
</code>

however this is unworkable as three.js lmits the number of canvases in a project (and having unused actual canvases everywhere in the DOM is really gross anyway)

is there a way of giving createPortal access to that context without needing to render it inside a Canvas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡看悲欢离合 2025-02-17 10:08:41

我知道这是一个非常古老的问题。
但是,对于寻求解决方案的人民,您可以使用隧道鼠。
https://github.com/pmndrs/pmndrs/tunnel-rat

I know this is a very old question.
But for the peoples who looks for a solution, you can use Tunnel-rat.
https://github.com/pmndrs/tunnel-rat

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文