Skip to content

Commit

Permalink
feat(Float): add autoInvalidate prop for on-demand rendering (#2034)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronClaes authored Jul 22, 2024
1 parent 7d3c546 commit 690f7a0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4350,6 +4350,16 @@ This component makes its contents float or hover.
</Float>
```

If you have your frameloop set to `demand`, you can set `autoInvalidate` to `true`. This will ensure the animation will render while it is enabled.

```js
<Canvas frameloop="demand">
<Float autoInvalidate>
<mesh />
</Float>
</Canvas>
```

#### Stage

[![](https://img.shields.io/badge/-storybook-%23ff69b4)](https://drei.pmnd.rs/?path=/story/staging-stage--stage-st)
Expand Down
5 changes: 5 additions & 0 deletions src/core/Float.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type FloatProps = JSX.IntrinsicElements['group'] & {
floatIntensity?: number
children?: React.ReactNode
floatingRange?: [number?, number?]
autoInvalidate?: boolean
}

export const Float: ForwardRefComponent<FloatProps, THREE.Group> = /* @__PURE__ */ React.forwardRef<
Expand All @@ -24,6 +25,7 @@ export const Float: ForwardRefComponent<FloatProps, THREE.Group> = /* @__PURE__
rotationIntensity = 1,
floatIntensity = 1,
floatingRange = [-0.1, 0.1],
autoInvalidate = false,
...props
},
forwardRef
Expand All @@ -33,6 +35,9 @@ export const Float: ForwardRefComponent<FloatProps, THREE.Group> = /* @__PURE__
const offset = React.useRef(Math.random() * 10000)
useFrame((state) => {
if (!enabled || speed === 0) return

if (autoInvalidate) state.invalidate()

const t = offset.current + state.clock.getElapsedTime()
ref.current.rotation.x = (Math.cos((t / 4) * speed) / 8) * rotationIntensity
ref.current.rotation.y = (Math.sin((t / 4) * speed) / 8) * rotationIntensity
Expand Down

0 comments on commit 690f7a0

Please sign in to comment.