Skip to content

Commit 69990c0

Browse files
committedFeb 20, 2024·
fix(useRafFn): fix fpsLimit option, fix #3481, close #3482
1 parent f7ea105 commit 69990c0

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed
 

‎packages/core/useRafFn/demo.vue

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
<script setup lang="ts">
22
import { ref } from 'vue'
3-
import { useRafFn } from '@vueuse/core'
3+
import { useRafFn } from './index'
44
5+
const fpsLimit = 60
56
const count = ref(0)
6-
const { pause, resume } = useRafFn(() => count.value += 1)
7+
const deltaMs = ref(0)
8+
const { pause, resume } = useRafFn(({ delta }) => {
9+
deltaMs.value = delta
10+
count.value += 1
11+
}, { fpsLimit })
712
</script>
813

914
<template>
10-
<div>Count: {{ count }}</div>
15+
<div font-mono>
16+
Frames: {{ count }}
17+
</div>
18+
<div font-mono>
19+
Delta: {{ deltaMs.toFixed(0) }}ms
20+
</div>
21+
<div font-mono>
22+
FPS Limit: {{ fpsLimit }}
23+
</div>
1124
<button @click="pause">
1225
pause
1326
</button>

‎packages/core/useRafFn/index.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,25 @@ export function useRafFn(fn: (args: UseRafFnCallbackArguments) => void, options:
5555
if (!isActive.value || !window)
5656
return
5757

58-
const delta = timestamp - (previousFrameTimestamp || timestamp)
58+
if (!previousFrameTimestamp)
59+
previousFrameTimestamp = timestamp
60+
61+
const delta = timestamp - previousFrameTimestamp
5962

6063
if (intervalLimit && delta < intervalLimit) {
6164
rafId = window.requestAnimationFrame(loop)
6265
return
6366
}
6467

65-
fn({ delta, timestamp })
66-
6768
previousFrameTimestamp = timestamp
69+
fn({ delta, timestamp })
6870
rafId = window.requestAnimationFrame(loop)
6971
}
7072

7173
function resume() {
7274
if (!isActive.value && window) {
7375
isActive.value = true
76+
previousFrameTimestamp = 0
7477
rafId = window.requestAnimationFrame(loop)
7578
}
7679
}

‎unocss.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default defineConfig({
2626
primary: '#3eaf7c',
2727
},
2828
fontFamily: {
29-
mono: 'var(--vt-font-family-mono)',
29+
mono: 'var(--vp-font-family-mono)',
3030
},
3131
},
3232
transformers: [

0 commit comments

Comments
 (0)
Please sign in to comment.