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

Viewport size inaccurate inside <View/> #137

Open
bryndsey opened this issue May 30, 2023 · 0 comments
Open

Viewport size inaccurate inside <View/> #137

bryndsey opened this issue May 30, 2023 · 0 comments

Comments

@bryndsey
Copy link

I'm trying to use viewport values inside of useFrame to position items relative to the size of the viewport. However, when resizing the browser window, the value will rapidly switch between the correct value and something that is not correct.

To reproduce, start from the base template and update the Dog component in Example.jsx to the following:

export function Dog(props) {
  const { scene } = useGLTF('/dog.glb')
  const ref = useRef(null)

  useFrame((state) => {
    if (ref.current === null) return

    ref.current.position.x = state.viewport.width / 4
  })

  return <primitive object={scene} {...props} ref={ref} />
}

Then, run the app, and when resizing the browser window, you can see the dog jitter back and forth between the expected position and somewhere else:

dog-jitter.mov

You can also log viewport.width to see that the values reported there are inaccurate at times. The same behavior can also be observed using useThree((state) => state.viewport) and using the result.

I also experimented with structuring the code such that I was using drei's built-in version of View as opposed to the wrapped version and it still occurred there, so that seems to rule out tunnel-rat as the issue. I also tried it out in a non-Next.js context, and the issue doesn't seem to occur (see sandbox). While it could be an issue with drei's underlying View implementation, since this template was the only place I've seen the issue, I figured I'd start here.

I can work around the issue by measuring the tracked div's size myself using something like react-use-measure, passing the sizing values to the canvas content, and then using state.viewport.getCurrentViewport(cameraRef.current, viewportTarget, bounds). This provides the correct viewport sizing, but has a number of drawbacks (managing extra refs, passing bounds arounds, extra arguments needed for getCurrentViewport), so not ideal.

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

No branches or pull requests

1 participant