This commit is contained in:
ct
2025-06-15 20:36:04 +08:00
parent b10902f322
commit 5fedb8470b

View File

@@ -37,6 +37,8 @@ const VideoEditor = ({ width, height }) => {
const { setVideoIsPlaying } = useVideoEditorStore(); const { setVideoIsPlaying } = useVideoEditorStore();
const FPS_INTERVAL = 1000 / 30; // 30 FPS
// 🔧 FIX: Keep ref synced with state // 🔧 FIX: Keep ref synced with state
useEffect(() => { useEffect(() => {
timelineElementsRef.current = timelineElements; timelineElementsRef.current = timelineElements;
@@ -322,7 +324,7 @@ const VideoEditor = ({ width, height }) => {
return; return;
} }
let animationId; let intervalId;
let isRunning = true; let isRunning = true;
const animateFrame = () => { const animateFrame = () => {
@@ -337,7 +339,6 @@ const VideoEditor = ({ width, height }) => {
return; return;
} }
if (newTime - lastUpdateRef.current >= 0.05) {
lastUpdateRef.current = newTime; lastUpdateRef.current = newTime;
setCurrentTime(newTime); setCurrentTime(newTime);
updateVideoTimes(newTime); updateVideoTimes(newTime);
@@ -345,29 +346,24 @@ const VideoEditor = ({ width, height }) => {
if (layerRef.current) { if (layerRef.current) {
layerRef.current.batchDraw(); layerRef.current.batchDraw();
} }
}
if (isRunning) {
animationId = requestAnimationFrame(animateFrame);
}
}; };
startTimeRef.current = Date.now() / 1000; startTimeRef.current = Date.now() / 1000;
animationId = requestAnimationFrame(animateFrame); intervalId = setInterval(animateFrame, FPS_INTERVAL);
animationRef.current = { animationRef.current = {
stop: () => { stop: () => {
isRunning = false; isRunning = false;
if (animationId) { if (intervalId) {
cancelAnimationFrame(animationId); clearInterval(intervalId);
} }
}, },
}; };
return () => { return () => {
isRunning = false; isRunning = false;
if (animationId) { if (intervalId) {
cancelAnimationFrame(animationId); clearInterval(intervalId);
} }
}; };
}, [isPlaying, totalDuration, handlePause, updateVideoTimes]); }, [isPlaying, totalDuration, handlePause, updateVideoTimes]);