Vue function that calls given callback inside the RAF loop.
You can optionally specify how many times the callback should
run in a second by specifying the fps
value.
type TFps = number | Ref<number>;
function useRafFn(
callback: Function,
fps?: TFps,
runOnMount?: boolean
): {
isRunning: Ref<boolean>;
start: () => void;
stop: () => void;
}
callback: Function
the function to call inside the Raf loopfps: number | Ref<number>
the amount of times per second that the callback should run. Please note that when a value greater or equal to60
is defined, thefps
logic will be skipped completely therefore the callback will run at full speed. By default the value is set to60
so it will indeed run at full speedrunOnMount: boolean
whether to run the Raf loop on mount,true
by default. Iffalse
, you'll have to start the Raf with thestart
function
isRunning: Ref<boolean>
the Raf statusfalse
when the Raf loop is pausedtrue
when the Raf loop is running
start: Function
the function used for starting the Raf loopstop: Function
the function used for stopping the Raf loop
<template>
<div>
<div>Elapsed: {{ elapsedTime }}</div>
<div><button @click="start">Start loop</button></div>
<div><button @click="stop">Stop loop</button></div>
</div>
</template>
<script lang="ts">
import Vue from 'vue'
import { ref } from '@src/api'
import { useRafFn } from 'vue-use-kit'
export default Vue.extend({
name: 'UseRafFnDemo',
setup() {
const fps = 60
const elapsedTime = ref(0)
const animHandler = elapsedTimeValue => {
elapsedTime.value = Math.ceil(elapsedTimeValue)
}
const { start, stop } = useRafFn(animHandler, fps)
return {
start,
stop,
elapsedTime
}
}
})
</script>