Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: inline views #1784

Merged
merged 5 commits into from
Jan 16, 2024
Merged

feat: inline views #1784

merged 5 commits into from
Jan 16, 2024

Conversation

drcmda
Copy link
Member

@drcmda drcmda commented Jan 15, 2024

  • no refs
  • no double definitions in and out the canvas
  • no index counting
  • canvas straight inside the dom, where you want it to appear
  • fully dynamic, views can pop in and out any time in any number
  • doesn't render views that are offscreen or invisible
  • fully backwards compatible to the old api

Demo: inline views

View

Views use gl.scissor to cut the viewport into segments. You tie a view to a tracking div which then controls the position and bounds of the viewport. This allows you to have multiple views with a single, performant canvas. These views will follow their tracking elements, scroll along, resize, etc.

It is advisable to re-connect the event system to a parent that contains both the canvas and the html content.
This ensures that both are accessible/selectable and even allows you to mount controls or other deeper
integrations into your view.

Note that @react-three/fiber newer than ^8.1.0 is required for View to work correctly if the
canvas/react three fiber root is not fullscreen. A warning will be logged if drei is used with older
versions of @react-three/fiber.

export type ViewProps = {
  /** Root element type, default: div */
  as?: string
  /** CSS id prop */
  id?: string
  /** CSS classname prop */
  className?: string
  /** CSS style prop */
  style?: React.CSSProperties
  /** If the view is visible or not, default: true */
  visible?: boolean
  /** Views take over the render loop, optional render index (1 by default) */
  index?: number
  /** If you know your view is always at the same place set this to 1 to avoid needless getBoundingClientRect overhead */
  frames?: number
  /** The scene to render, if you leave this undefined it will render the default scene */
  children?: React.ReactNode
  /** The tracking element, the view will be cut according to its whereabouts
   * @deprecated
   */
  track: React.MutableRefObject<HTMLElement>
}

export type ViewportProps = { Port: () => React.ReactNode } & React.ForwardRefExoticComponent<
  ViewProps & React.RefAttributes<HTMLElement | THREE.Group>
>

You can define as many views as you like, directly mix them into your dom graph, right where you want them to appear. Use View.Port inside the canvas to output them. The canvas should ideally fill the entire screen with absolute positioning, underneath HTML or on top of it, as you prefer.

return (
  <main ref={container}>
    <h1>Html content here</h1>
    <View style={{ width: 200, height: 200 }}>
      <mesh geometry={foo} />
      <OrbitControls />
    </View>
    <View className="canvas-view">
      <mesh geometry={bar} />
      <CameraControls />
    </View>
    <Canvas eventSource={container}>
      <View.Port />
    </Canvas>
  </main>
)

Copy link

vercel bot commented Jan 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
drei ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 16, 2024 1:07pm

Copy link

codesandbox-ci bot commented Jan 15, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit ab65834:

Sandbox Source
vigorous-wildflower-kxkptz Configuration
Ground reflections and video textures Configuration
arc-x-pmndrs-colors Configuration
View tracking (forked) PR

@drcmda drcmda merged commit c965762 into master Jan 16, 2024
2 checks passed
Copy link

🎉 This PR is included in version 9.94.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants