I want to know can i fire animationFrame when needed. Actually now I has created basic Box component with react-three-fiber and not used useFrame hook into that, but in my browser performance view i see that animation frame fired. But in three js we can fire animationFrame when needed to do animation.
type axis = 'x' | 'y' | 'z'
type rotation = {
state: true | false,
axis: axis,
speed: number
}
export type BoxProps = {
position?: Vector3,
rotation?: Euler,
color?: Color,
geometryArgs?: [width:number, height:number, depth:number],
hoveredColor?: Color,
activeScale?: number
rotate?: rotation
}
import React, { useRef, useState } from "react"
import { BufferGeometry, Color, Material, Mesh, Vector3 } from "three"
import { BoxProps } from "./BoxProps"
import "@react-three/fiber"
export const Box = (props: BoxProps): React.ReactElement => {
const mesh: React.Ref<Mesh<BufferGeometry, Material>> = useRef(null)
const [hovered, setHovered] = useState(false)
const [active, setActive] = useState(false)
const {
geometryArgs = [1,1,1],
position = new Vector3(0,0,0),
color = new Color(0,0,0),
hoveredColor = new Color(0.5,0.5,0.5),
activeScale = 2,
} = props
return (
<mesh
ref={mesh}
position={position}
onClick={():void=>setActive(!active)}
onPointerOver={():void=>setHovered(true)}
onPointerOut={():void=>setHovered(false)}
scale={active ? activeScale : 1}
>
<boxGeometry args={geometryArgs}/>
<meshBasicMaterial color={hovered? hoveredColor: color}/>
</mesh>
)
}
function App() {
return (
<Canvas>
<Box position={new Vector3(-2,0,0)} geometryArgs={[2,2,2]} />
<Box position={new Vector3(2,0,0)} geometryArgs={[2,2,2]} />
</Canvas>
);
}