Skip to content

useRafFn

Calls a function on every requestAnimationFrame. Returns pause and resume controls. Useful for animation loops and smooth updates.

Basic Usage

vue
<script setup>
import { ref, onMounted } from "vue";
import { useRafFn } from "@baklavue/composables";

const position = ref(0);
const velocity = 100;

const { pause, resume } = useRafFn(({ delta }) => {
  position.value += velocity * (delta / 1000);
});

// Pause when tab is hidden
onMounted(() => {
  document.addEventListener("visibilitychange", () => {
    document.hidden ? pause() : resume();
  });
});
</script>

<template>
  <div :style="{ transform: `translateX(${position}px)` }">
    Animated content
  </div>
</template>

With FPS Limit

Limit the frame rate to reduce CPU usage:

vue
<script setup>
import { useRafFn } from "@baklavue/composables";

const { pause, resume } = useRafFn(
  ({ delta }) => {
    // Update animation
  },
  { fpsLimit: 60 }
);
</script>

API

typescript
useRafFn(
  callback: (args: RafCallbackArgs) => void,
  options?: UseRafFnOptions
): { pause: () => void; resume: () => void; isActive: Ref<boolean> }

Callback Args

  • delta — Time elapsed since last frame in milliseconds
  • timestamp — High-resolution timestamp

Options

PropertyTypeDefaultDescription
immediatebooleantrueStart the loop immediately
fpsLimitnumberundefinedMax FPS (no limit if undefined)

Return Value

PropertyTypeDescription
pause() => voidPause the animation loop
resume() => voidResume the animation loop
isActiveRef<boolean>True when the loop is running