Fix: Skillcheck animation speed inconsistencies by switching to time-… (#66)

* Fix: Skillcheck animation speed inconsistencies by switching to time-based RAF loop

This PR fixes long-standing issues with the skillcheck mini-game animation speed being inconsistent across machines and after long play sessions. Some players reported extremely slow indicator movement, others extremely fast, and many noticed that the speed changed unpredictably the longer they stayed logged in.

The root cause was that the skillcheck relied on a useInterval tick (setInterval-like behavior) and assumed it fired every 1ms. In reality, browser timer clamping and throttling make interval timing highly unpredictable — especially in embedded CEF browsers like FiveM’s NUI.

This PR replaces tick-based animation with a time-based requestAnimationFrame loop using performance.now(), ensuring perfectly consistent animation timing across all hardware and browser states. I have used this method in other NUI based skillcheck scripts to address this same behavior.

Signed-off-by: Senlar <brandonrhue@gmail.com>

* Refactor keyHandler and clean up code

Signed-off-by: Senlar <brandonrhue@gmail.com>

* Refactor keyHandler and cleanup useEffect logic again

Signed-off-by: Senlar <brandonrhue@gmail.com>

---------

Signed-off-by: Senlar <brandonrhue@gmail.com>
This commit is contained in:
Senlar
2026-02-11 09:22:54 -08:00
committed by GitHub
parent e66b6805bc
commit f33c613b6d

View File

@@ -1,6 +1,5 @@
import { useCallback, useEffect, useState } from 'react'; import { useCallback, useEffect, useRef, useState } from 'react';
import type { SkillCheckProps } from '../../typings'; import type { SkillCheckProps } from '../../typings';
import { useInterval } from '@mantine/hooks';
interface Props { interface Props {
angle: number; angle: number;
@@ -11,15 +10,58 @@ interface Props {
handleComplete: (success: boolean) => void; handleComplete: (success: boolean) => void;
} }
const Indicator: React.FC<Props> = ({ angle, offset, multiplier, handleComplete, skillCheck, className }) => { const BASE_DURATION_MS = 2000;
const Indicator: React.FC<Props> = ({
angle,
offset,
multiplier,
handleComplete,
skillCheck,
className,
}) => {
const [indicatorAngle, setIndicatorAngle] = useState(-90); const [indicatorAngle, setIndicatorAngle] = useState(-90);
const [keyPressed, setKeyPressed] = useState<false | string>(false); const [keyPressed, setKeyPressed] = useState<false | string>(false);
const interval = useInterval(
() => const rafIdRef = useRef<number | null>(null);
setIndicatorAngle((prevState) => { const startTimeRef = useRef<number | null>(null);
return (prevState += multiplier); const completedRef = useRef(false);
}),
1 const stopAnimation = () => {
if (rafIdRef.current !== null) {
cancelAnimationFrame(rafIdRef.current);
rafIdRef.current = null;
}
};
const animate = useCallback(
(time: number) => {
if (completedRef.current) return;
if (startTimeRef.current === null) {
startTimeRef.current = time;
}
const elapsed = time - startTimeRef.current;
const speed = Math.max(multiplier || 0, 0.0001);
const duration = BASE_DURATION_MS / speed;
const progress = Math.min(elapsed / duration, 1);
const newAngle = -90 + progress * 360;
setIndicatorAngle(newAngle);
if (newAngle + 90 >= 360) {
completedRef.current = true;
stopAnimation();
handleComplete(false);
return;
}
rafIdRef.current = requestAnimationFrame(animate);
},
[multiplier, handleComplete]
); );
const keyHandler = useCallback( const keyHandler = useCallback(
(e: KeyboardEvent) => { (e: KeyboardEvent) => {
@@ -42,32 +84,43 @@ const Indicator: React.FC<Props> = ({ angle, offset, multiplier, handleComplete,
useEffect(() => { useEffect(() => {
setIndicatorAngle(-90); setIndicatorAngle(-90);
startTimeRef.current = null;
completedRef.current = false;
window.addEventListener('keydown', keyHandler); window.addEventListener('keydown', keyHandler);
interval.start(); rafIdRef.current = requestAnimationFrame(animate);
}, [skillCheck]);
return () => {
stopAnimation();
window.removeEventListener('keydown', keyHandler);
startTimeRef.current = null;
completedRef.current = true;
};
}, [skillCheck, keyHandler, animate]);
useEffect(() => { useEffect(() => {
if (indicatorAngle + 90 >= 360) { if (!keyPressed || completedRef.current) return;
interval.stop();
handleComplete(false);
}
}, [indicatorAngle]);
useEffect(() => {
if (!keyPressed) return;
if (skillCheck.keys && !skillCheck.keys?.includes(keyPressed)) return; if (skillCheck.keys && !skillCheck.keys?.includes(keyPressed)) return;
interval.stop(); stopAnimation();
window.removeEventListener('keydown', keyHandler); window.removeEventListener('keydown', keyHandler);
completedRef.current = true;
if (keyPressed !== skillCheck.key || indicatorAngle < angle || indicatorAngle > angle + offset) if (keyPressed !== skillCheck.key || indicatorAngle < angle || indicatorAngle > angle + offset)
handleComplete(false); handleComplete(false);
else handleComplete(true); else handleComplete(true);
setKeyPressed(false); setKeyPressed(false);
}, [keyPressed]); }, [
keyPressed,
angle,
offset,
indicatorAngle,
skillCheck,
keyHandler,
handleComplete,
]);
return <circle transform={`rotate(${indicatorAngle}, 250, 250)`} className={className} />; return <circle transform={`rotate(${indicatorAngle}, 250, 250)`} className={className} />;
}; };