Skip to content

Three.js frame updates

This guide is for the ThreeCanvas entries that take a Three.js scene and camera: the portable entry and the Web GPU entries. For renderer selection and React Three Fiber, see Three.js & 3D.

Keep frameloop="demand" (the default) for diagrams and models that spend most of their time still. Changing the surface props or its size can schedule a redraw. But mutating an existing Three.js object does not change the identity of the scene prop.

If a control only turns or moves a part, make the mutation and call the surface ref’s invalidate():

tsx
import { Button } from '@hozo/core'import { ThreeCanvas, type ThreeCanvasHandle } from '@hozo/three'import { useRef } from 'react'import type { Object3D, PerspectiveCamera, Scene } from 'three'export function AdjustableModel({  scene,  camera,  part,}: {  scene: Scene  camera: PerspectiveCamera  part: Object3D}) {  const surface = useRef<ThreeCanvasHandle>(null)  return (    <>      <ThreeCanvas        ref={surface}        scene={scene}        camera={camera}        width={320}        height={180}        accessibilityLabel="Adjustable product model"      />      <Button        onPress={() => {          part.rotation.y += Math.PI / 12          surface.current?.invalidate()        }}      >        Turn model      </Button>    </>  )}

Bump the revision prop after a batch of changes when the accessible objects may have changed: for example, showing or hiding a part, changing its accessible name, or changing which objects the camera layer includes. This gives the surface a React update in which to refresh the controls as well as the drawing. Do not increment it on every animation frame.

Use frameloop="always" for continuous motion. onFrame receives a frame object; destructure it as onFrame={({ delta }) => …}. The delta field is elapsed seconds since the previous frame, so a rotation of one radian per second adds delta to the angle. Change the Three objects directly rather than driving the frame clock through React state.

Classic WebGL and WebGPU surfaces submit their continuous frames to the renderer directly. The portable path still projects the scene on the CPU, so start with demand mode for large scenes, and measure your workload before keeping it in continuous mode.