Skip to content

Commit 83058d3

Browse files
committed
add FPSDisplay component
1 parent aa6094c commit 83058d3

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/FPSDisplay.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { useEffect, useRef, useState } from 'react'
2+
3+
export const FPSDisplay = () => {
4+
const [fps, setFps] = useState<number>(0)
5+
const frames = useRef<number>(0)
6+
const lastTime = useRef<number>(performance.now())
7+
8+
useEffect(() => {
9+
let animationFrameId: number
10+
11+
const loop = (time: number) => {
12+
frames.current += 1
13+
14+
if (time - lastTime.current >= 1000) {
15+
setFps(frames.current)
16+
frames.current = 0
17+
lastTime.current = time
18+
}
19+
20+
animationFrameId = requestAnimationFrame(loop)
21+
}
22+
23+
animationFrameId = requestAnimationFrame(loop)
24+
25+
return () => cancelAnimationFrame(animationFrameId)
26+
}, [])
27+
28+
return (
29+
<div className="fixed top-2 left-2 bg-black text-green-400 text-sm font-mono px-2 py-1 rounded shadow-md z-50">
30+
FPS: {fps}
31+
</div>
32+
)
33+
}

src/Section.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const SectionContainer = ({ section }: { section: Section }) => {
3333
const overlayStyle = useMemo(() => ({
3434
position: 'absolute' as const,
3535
left: section.position.x,
36-
top: section.position.y,
36+
top: section.position.y + 50,
3737
zIndex: section.position.z_index + 1,
3838
backgroundColor: 'rgba(0, 0, 0, 0.8)',
3939
padding: '10px',

src/main.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { StrictMode } from 'react'
22
import { createRoot } from 'react-dom/client'
33
import './index.css'
44
import { App } from './App'
5+
import { FPSDisplay } from './FPSDisplay'
56

67
createRoot(document.getElementById('root')!).render(
78
<StrictMode>
89
<App />
10+
<FPSDisplay />
911
</StrictMode>,
1012
)

0 commit comments

Comments
 (0)