useRafLoop

A React Hook that helps to run a function on every frame using requestAnimationFrame.

Demo

Source
RAF
Interval

Usage

See API for more details.

Source

Click links below to view source on GitHub.

API

const controls = useRaf(callback, options)

Callback

A function that will be called on every frame.

export interface UseRafLoopCallbackArgs {
  /**
   * The time elapsed since the last frame
   */
  delta: number
  /**
   * The timestamp of the current frame
   */
  timestamp: DOMHighResTimeStamp
}

export type UseRafLoopCallback = (args: UseRafLoopCallbackArgs) => void

Options

export interface UseRafLoopOptions {
  /**
   * The maximum fps limit
   *
   * @defaultValue undefined
   */
  fpsLimit?: number
  /**
   * Whether to start the interval immediately on mounted
   *
   * @defaultValue true
   */
  immediate?: boolean
  /**
   * Whether to execute the callback immediately before the interval starts
   *
   * @defaultValue false
   */
  immediateCallback?: boolean
}