Pause rendering whilst out of frame #769
Replies: 6 comments 8 replies
-
@marklundin you can do that: import { useInView } from 'react-intersection-observer'
const DisableRender = () => useFrame(() => null, 1000)
function App() {
const { ref, inView } = useInView()
return (
<div ref={ref} className="canvasContainer">
<Canvas>
{!inView && <DisableRender />} to detect if the canvas is out is probably a user-land thing, this would be too overarching as a r3f feature imo. just throw an intersection observer around the canvas and you basically have it. |
Beta Was this translation helpful? Give feedback.
-
yes, invalidateFrameloop disables the automatic loop, there's none. you need to trigger each frame manually via invalidate(). the code above lets the loop stand and takes over rendering with a null function if the camvas is out. the loop is harmless, and in theory this would also be safe for physics if you have them. |
Beta Was this translation helpful? Give feedback.
-
Is settings i.e. would this also work? import { useInView } from 'react-intersection-observer'
function App() {
const { ref, inView } = useInView()
return (
<div ref={ref} className="canvasContainer">
<Canvas invalidateFrameloop={!inView}> |
Beta Was this translation helpful? Give feedback.
-
Changing Canvas' frameloop doesn't seem to change anything. Only the first passed value matters. |
Beta Was this translation helpful? Give feedback.
-
If someone also needs it. Here's what I used :
|
Beta Was this translation helpful? Give feedback.
-
In case someone come accross this in 2023, for me const DisableRender = () => useFrame(() => null, 1000) didn't work, so I ended up switching between frameloop const { ref, inView } = useInView()
return (
<div ref={ref} className="glContainer">
<Canvas frameloop={inView ? 'always' : 'never'} /> |
Beta Was this translation helpful? Give feedback.
-
This may have already been discussed, but it would a nice feature to be able to pause/defer the render whilst the canvas is outside of view. I understand there may be a use case for keeping
useFrame
running, due to physics etc, but it would be great to stop issuing draw commands whilst the canvas is out of frame. Not sure if this fits in well with the overall architecture, but just a thoughtBeta Was this translation helpful? Give feedback.
All reactions