Skip to content

Hooks

useScrollScene() ​

ts
function useScrollScene(
   elementRef, 
   setup?, 
   options?
): RefObject<ScrollScene | null>;

Defined in: use-scroll-scene.ts:86

Registers a ScrollScene anchored to a DOM element with the nearest ScrollRendererProvider.

The scene is created once the renderer and element are available, and removed (and destroyed) on unmount.

Parameters ​

ParameterTypeDescription
elementRefRefObject<HTMLElement | null>Ref to the DOM element the scene tracks.
setup?ScrollSceneSetup<ScrollScene>Builds the scene's GL content; may return a cleanup function.
options?UseScrollSceneOptionsForwarded to the ScrollScene constructor.

Returns ​

RefObject<ScrollScene | null>

A ref holding the live ScrollScene, or null before creation.

Example ​

ts
function HeroSection() {
  const ref = useRef<HTMLDivElement>(null)

  useScrollScene(ref, ({ gl, scrollScene }) => {
    const program = new Program(gl, {
      vertex,
      fragment,
      uniforms: { ...scrollScene.uniforms }
    })
    new Mesh(gl, { geometry: new Triangle(gl), program }).setParent(
      scrollScene.scene
    )
  })

  return <div ref={ref} className="hero" />
}