Calculate frame rate per millisecond (FPS), frame rate per second needs to be multiplied by 1000.
| Argument |
Description |
Type |
Promise<number> |
Promise that resolves to frame rate (per millisecond) |
Promise |
| Parameter |
Description |
Type |
Default |
n |
Number of sample frames |
number |
10 |
import { getFrame } from 'ranuts';
const fps = await getFrame();
console.log('Frame rate (per ms):', fps);
console.log('Frame rate (per second):', fps * 1000);
import { getFrame } from 'ranuts';
// Sample 20 frames to calculate average frame rate
const fps = await getFrame(20);
console.log('FPS:', fps * 1000);
import { getFrame } from 'ranuts';
async function monitorPerformance() {
const fps = await getFrame(30);
const fpsPerSecond = fps * 1000;
if (fpsPerSecond < 30) {
console.warn('Low frame rate:', fpsPerSecond);
} else {
console.log('Normal frame rate:', fpsPerSecond);
}
}
import { getFrame } from 'ranuts';
async function checkAnimationPerformance() {
const fps = await getFrame(60);
const fpsPerSecond = fps * 1000;
console.log(`Animation frame rate: ${fpsPerSecond.toFixed(2)} FPS`);
}
- Unit description: Returns frame rate per millisecond, need to multiply by 1000 to get frame rate per second (FPS).
- Sampling method: Uses
requestAnimationFrame for sampling, calculates average interval of multiple frames.
- Async operation: Returns Promise, needs to be handled with
await or .then().
- Use case: Commonly used for performance monitoring, animation performance detection, game frame rate monitoring, etc.