Skip to content

Hooks

useScrollImage() ​

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

Defined in: use-scroll-image.ts:59

Registers a ScrollImage scene with the nearest ScrollRendererProvider: the referenced <img> is uploaded to a GPU texture and exposed to your programs via the u_image / u_imageSize uniforms, alongside the standard ScrollScene uniforms.

Pass a ref to an <img> element, or a ref to any element plus an image in options.

Parameters ​

ParameterTypeDescription
elementRefRefObject<HTMLElement | null>Ref to the tracked element - an <img> unless options.image is set.
setup?ScrollSceneSetup<ScrollImage>Builds the scene's GL content; may return a cleanup function.
options?UseScrollImageOptionsForwarded to the ScrollImage constructor.

Returns ​

RefObject<ScrollImage | null>

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

Example ​

ts
function ClothImage({ src }: { src: string }) {
  const ref = useRef<HTMLImageElement>(null)

  useScrollImage(ref, ({ gl, scrollScene }) => {
    const program = new Program(gl, {
      vertex,
      fragment,
      uniforms: { ...scrollScene.uniforms }
    })
    new Mesh(gl, {
      geometry: new Plane(gl, { widthSegments: 30, heightSegments: 22 }),
      program
    }).setParent(scrollScene.scene)
  })

  return <img ref={ref} src={src} alt="" />
}