Hello everybody
I’m confronted to a annoying issue with my Vite / React project and Three.js Fiber.
Here’s the problem :
I have 2 files, one is called App.jsx and the another is Scene.jsx.
When the App.jsx rendering is updated (with new angles of the DeviceOrientation), the animation of my cube stored in my Scene.jsx reset…
Same issue happen with all kinds of new rendering (if I use addEventListener for a “click” per example, so the issue don’t come specifically by the ‘deviceorientation’ event).
Here a gif to illustrate the problem : Screen capture - f24bf397bd9eee80510018dc09495af0 - Gyazo
Here my App.jsx and Scene.jsx codes :
App.jsx :
import { useEffect, useRef, useState } from 'react'
import { Scene } from './components/Scene'
const App = () => {
const [angle, setAngle] = useState(0)
const [leftToRight, setLeftToRight] = useState(0)
const [frontToBack, setFrontToBack] = useState(0)
const eventListenerRef = useRef(null)
useEffect(()=>{
if (window.DeviceOrientationEvent && !eventListenerRef.current){
eventListenerRef.current = e => {
setAngle(e.alpha)
setLeftToRight(e.beta)
setFrontToBack(e.gamma)
}
window.addEventListener('deviceorientation', eventListenerRef.current)
}
return () => {
if (eventListenerRef.current){
window.removeEventListener('deviceorientation', eventListenerRef.current)
eventListenerRef.current = null
}
}
},[])
return (
<>
<div className='angles-infos'>
<h3>angle : {angle}</h3>
<h3>leftToRight : {leftToRight}</h3>
<h3>frontToBack : {frontToBack}</h3>
</div>
<Scene/>
</>
)
}
export default App
Scene.jsx :
import { Canvas, useFrame } from '@react-three/fiber'
import { useRef } from 'react'
export const Scene = () => {
const ref = useRef()
const Cube = ({position, size, color}) => {
useFrame((state, delta)=>{
ref.current.rotation.x += delta
ref.current.rotation.y += delta
ref.current.position.z = Math.sin(state.clock.elapsedTime) * 2
})
return (
<mesh position={position} ref={ref}>
<meshStandardMaterial color={color}/>
<boxGeometry args={size}/>
</mesh>
)
}
return (
<>
<Canvas>
<directionalLight position={[0,0,2]}/>
<Cube position={[0, 0, 0]} size={[1, 1, 1]} color={"orange"}></Cube>
</Canvas>
</>
)
}
Your help would be very appreciated, thanks by advance