Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | /**
* @fileoverview FPS Counter Component
* @description Displays real-time frames per second
* @module components/FPSCounter
*/
import { useState, useEffect, useRef } from 'react';
import './FPSCounter.css';
/**
* FPS Counter Component
*/
function FPSCounter() {
const [fps, setFps] = useState(0);
const [isVisible, setIsVisible] = useState(true);
const frameCountRef = useRef(0);
const lastTimeRef = useRef(performance.now());
useEffect(() => {
let animationId;
const updateFPS = () => {
frameCountRef.current++;
const now = performance.now();
const delta = now - lastTimeRef.current;
if (delta >= 1000) {
setFps(Math.round((frameCountRef.current * 1000) / delta));
frameCountRef.current = 0;
lastTimeRef.current = now;
}
animationId = requestAnimationFrame(updateFPS);
};
animationId = requestAnimationFrame(updateFPS);
return () => cancelAnimationFrame(animationId);
}, []);
if (!isVisible) return null;
const fpsColor = fps >= 50 ? '#10b981' : fps >= 30 ? '#f59e0b' : '#ef4444';
return (
<div className="fps-counter" onClick={() => setIsVisible(false)} title="Click to hide">
<span className="fps-value" style={{ color: fpsColor }}>{fps}</span>
<span className="fps-label">FPS</span>
</div>
);
}
export default FPSCounter;
|