Coder Social home page Coder Social logo

Comments (6)

drcmda avatar drcmda commented on May 22, 2024 2

useThree is context based, it only works inside canvas. But it goes deeper, generally the real three canvas isn't created in line with the Canvas component. Just because Canvas has rendered and the parent gets its useEffect ping doesn't mean that the three js stuff is ready yet. React decides when it will render, and it seems to do it async by default. So the only safe way is to pass the camera up when it becomes available.

But you still have multiple options here.

  1. Canvas has a onCreated event that gives you the cam (+ state)
function Parent() {
  return <Child onCreated={({ camera }) => console.log(camera)} />
}

function Child(props) {
  return <Canvas {...props} />
}
  1. Canvas can render a function child that gives you the cam (+ state)
<Canvas>
  {({ camera }) => ... }
  1. Create and manage your own camera: https://github.com/drcmda/react-three-fiber#heads-up-display-rendering-multiple-scenes

from react-three-fiber.

drcmda avatar drcmda commented on May 22, 2024

See #34

A reconciler renders JSX into a target. react-dom renders into the dom, react-three-fiber renders into a threejs scene. You can't mount a div into a scene.

from react-three-fiber.

AndrewRayCode avatar AndrewRayCode commented on May 22, 2024

Makes sense. I was able to do it with react-three-renderer but I don't think that uses a customer reconciler internally.

from react-three-fiber.

giulioz avatar giulioz commented on May 22, 2024

May it be possible to use useThree() hook outside of Canvas elements?
I need to have a ref to the camera for using with DOM components outside of canvas, and the only way I found to do so is using a component inside which passes the camera to parent with an event, but it's ugly.

A solution may be adding something like domChildren prop to , to have them excluded by the renderer but still be able to access useThree().

from react-three-fiber.

linonetwo avatar linonetwo commented on May 22, 2024

Can CSS2DRenderer in THREE fits in this situation?

from react-three-fiber.

drcmda avatar drcmda commented on May 22, 2024

yes, you can exchange the renderer: https://github.com/drcmda/react-three-fiber/blob/master/readme.md#switching-the-default-renderer

from react-three-fiber.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.