Skip to content

useRafFn ​

Category
Export Size
452 B
Last Changed
6 months ago

Call function on every requestAnimationFrame. With controls of pausing and resuming.

Demo ​

Frames: 0
Delta: 0ms
FPS Limit: 60

Usage ​

ts
import { 
useRafFn
} from '@vueuse/core'
import {
shallowRef
} from 'vue'
const
count
=
shallowRef
(0)
const {
pause
,
resume
} =
useRafFn
(() => {
count
.
value
++
console
.
log
(
count
.
value
)
})

Type Declarations ​

Show Type Declarations
ts
export interface UseRafFnCallbackArguments {
  /**
   * Time elapsed between this and the last frame.
   */
  
delta
: number
/** * Time elapsed since the creation of the web page. See {@link https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp#the_time_origin Time origin}. */
timestamp
:
DOMHighResTimeStamp
} export interface UseRafFnOptions extends ConfigurableWindow { /** * Start the requestAnimationFrame loop immediately on creation * * @default true */
immediate
?: boolean
/** * The maximum frame per second to execute the function. * Set to `null` to disable the limit. * * @default null */
fpsLimit
?:
MaybeRefOrGetter
<number | null>
/** * After the requestAnimationFrame loop executed once, it will be automatically stopped. * * @default false */
once
?: boolean
} /** * Call function on every `requestAnimationFrame`. With controls of pausing and resuming. * * @see https://vueuse.org/useRafFn * @param fn * @param options */ export declare function
useRafFn
(
fn
: (
args
: UseRafFnCallbackArguments) => void,
options
?: UseRafFnOptions,
):
Pausable

Source ​

Source • Demo • Docs

Contributors ​

Anthony Fu
Robin
Anthony Fu
Vida Xie
Nemanja Malesija
SerKo
IlyaL
btea
OrbisK
max.lee
Hayk Khachatryan
Dan Rose
Enzo Innocenzi
Jelf
he yifan
Alex Kozack
Jacob Clevenger
Scott Bedard
Antério Vieira

Changelog ​

8ce0d - fix: resolve reactive null fpsLimit not being handled (#5284)
3041e - feat: add once option (#4583)
4759f - feat: allow framerate to be a reactive value (#4409)
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
69990 - fix: fix fpsLimit option, fix #3481, close #3482

Released under the MIT License.