const [apng, setApng] = React.useState(null);
const [error, setError] = React.useState(null);
+ const [frameInfo, setFrameInfo] = React.useState('');
const [loading, setLoading] = React.useState(true);
const [player, setPlayer] = React.useState(null);
React.useEffect(async () => {
if (loading || !canvas.current) return;
- setPlayer(await createPlayer(apng, canvas.current));
+ setFrameInfo(`1/${apng.frames.length}`);
+ const p = await createPlayer(apng, canvas.current);
+ setPlayer(p);
+ const updateFrame = (number) => {
+ setFrameInfo(`${number + 1}/${apng.frames.length}`);
+ };
+ p.on('frame', updateFrame);
+ return () => {
+ p.off('frame', updateFrame);
+ };
}, [apng, canvas.current, loading]);
- const play = React.useCallback(() => {
- if (player) player.play();
- }, [player]);
-
- const pause = React.useCallback(() => {
- if (player) player.pause();
- }, [player]);
-
const stop = React.useCallback(() => {
if (player) player.stop();
}, [player]);
+ const toggle = React.useCallback(() => {
+ if (!player) return;
+ if (player.paused) {
+ player.play();
+ } else {
+ player.pause();
+ }
+ }, [player]);
+
const nextFrame = React.useCallback(() => {
if (player) player.renderNextFrame();
}, [player]);
<div className="screen">
<canvas ref={canvas} width={apng.width} height={apng.height} />
</div>
+ <span className="ms-auto">{frameInfo}</span>
<div className="button-bar controls">
<Button
onClick={stop}
<Icon.STOP title="" />
</Button>
<Button
- onClick={play}
- title={t('button.play')}
+ onClick={toggle}
+ title={t('button.playPause')}
variant="outline-secondary"
>
<Icon.PLAY title="" />
- </Button>
- <Button
- onClick={pause}
- title={t('button.pause')}
- variant="outline-secondary"
- >
+ {' '}
<Icon.PAUSE title="" />
</Button>
<Button