Skip to content

Commit

Permalink
refactor(live2d): refactor nextStep function to use useCallback for b…
Browse files Browse the repository at this point in the history
…etter performance
  • Loading branch information
dnaroma committed Feb 1, 2025
1 parent 6c0a6b3 commit 2fe97aa
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/pages/storyreader-live2d/StoryReaderLive2DCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React, {
forwardRef,
useImperativeHandle,
useEffect,
useCallback,
} from "react";
import { useTranslation } from "react-i18next";
import {
Expand Down Expand Up @@ -99,6 +100,20 @@ const StoryReaderLive2DCanvas: React.FC<{
const [textAnimation, setTextAnimation] = useState(true);
const [autoplay, setAutoplay] = useState(false);

const nextStep = useCallback(() => {
if (!playing && !autoplayWaiting && scenarioStep !== -1) {
setPlaying(true);
stage.current?.controller
.step_until_checkpoint(scenarioStep)
.then((current) => {
setScenarioStep(current);
setPlaying(false);
});
} else {
stage.current?.controller.animate.abort();
}
}, [autoplayWaiting, playing, scenarioStep]);

// change canvas size
useLayoutEffect(() => {
const update_stage_size = () => {
Expand Down Expand Up @@ -132,7 +147,7 @@ const StoryReaderLive2DCanvas: React.FC<{
nextStep();
});
}
}, [autoplay, playing]);
}, [autoplay, loadStatus, nextStep, playing]);

//DEBUG
/*
Expand Down Expand Up @@ -215,20 +230,6 @@ const StoryReaderLive2DCanvas: React.FC<{
}
};

const nextStep = () => {
if (!playing && !autoplayWaiting && scenarioStep !== -1) {
setPlaying(true);
stage.current?.controller
.step_until_checkpoint(scenarioStep)
.then((current) => {
setScenarioStep(current);
setPlaying(false);
});
} else {
stage.current?.controller.animate.abort();
}
};

const handleBgmVolumeChange = (
event: Event,
newBgmVolume: number | number[]
Expand Down

0 comments on commit 2fe97aa

Please sign in to comment.