-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(viewer): remove renderer version and load time (#407)
- Loading branch information
Showing
3 changed files
with
50 additions
and
118 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
import { useCallback, useEffect, useState, useRef } from 'react'; | ||
import { useAppDispatch, useAppSelector } from '../store/hooks'; | ||
import DotLottieNew from './dotlottie-new'; | ||
import { DotLottiePlayer, DotLottieCommonPlayer } from '@dotlottie/react-player'; | ||
import { DotLottie } from '@lottiefiles/dotlottie-react'; | ||
import { | ||
DotLottieReact, | ||
setWasmUrl as setDotLottieWasmUrl, | ||
DotLottie, | ||
RenderEvent, | ||
} from '@lottiefiles/dotlottie-react'; | ||
import dotLottieWebPkg from '@lottiefiles/dotlottie-react/node_modules/@lottiefiles/dotlottie-web/package.json'; | ||
import { Range, getTrackBackground } from 'react-range'; | ||
import dotLottieWasmUrl from '../../../../packages/web/src/core/dotlottie-player.wasm?url'; | ||
import { | ||
setActiveAnimationId, | ||
setAnimations, | ||
setCurrentFrame, | ||
setCurrentState, | ||
setLoadTimeDotLottie, | ||
setLoadTimeLottieWeb, | ||
setLoop, | ||
setMarkers, | ||
setThemes, | ||
|
@@ -22,7 +25,7 @@ import { ImLoop } from 'react-icons/im'; | |
import { GiNextButton, GiPreviousButton } from 'react-icons/gi'; | ||
import LoadTime from './load-time'; | ||
|
||
let startTime = performance.now(); | ||
setDotLottieWasmUrl(dotLottieWasmUrl); | ||
|
||
export default function Players() { | ||
const lottieWebRef = useRef<DotLottieCommonPlayer | null>(null); | ||
|
@@ -39,7 +42,6 @@ export default function Players() { | |
const activeAnimationId = useAppSelector((state) => state.viewer.activeAnimationId); | ||
const activeThemeId = useAppSelector((state) => state.viewer.activeThemeId); | ||
const isJson = useAppSelector((state) => state.viewer.isJson); | ||
const loadTime = useAppSelector((state) => state.viewer.loadTime); | ||
const animations = useAppSelector((state) => state.viewer.animations); | ||
const segment = useAppSelector((state) => state.viewer.segment); | ||
const useFrameInterpolation = useAppSelector((state) => state.viewer.useFrameInterpolation); | ||
|
@@ -48,8 +50,7 @@ export default function Players() { | |
|
||
const onLoad = useCallback(() => { | ||
dispatch(setTotalFrames(dotLottie?.totalFrames)); | ||
const endTime = performance.now(); | ||
dispatch(setLoadTimeDotLottie(endTime - startTime)); | ||
|
||
if (!src.endsWith('.json') && !src.startsWith('data:application/json')) { | ||
if (!activeAnimationId) { | ||
dispatch(setActiveAnimationId(dotLottie?.manifest?.animations?.[0]?.id || '')); | ||
|
@@ -60,15 +61,29 @@ export default function Players() { | |
} | ||
}, [src, dotLottie, dispatch, activeAnimationId]); | ||
|
||
const onFrame = useCallback(() => { | ||
dispatch(setCurrentFrame(dotLottie?.currentFrame || 0)); | ||
}, [dispatch, dotLottie]); | ||
const onRender = useCallback( | ||
({ currentFrame }: RenderEvent) => { | ||
dispatch(setCurrentFrame(currentFrame)); | ||
}, | ||
[dispatch, dotLottie], | ||
); | ||
|
||
const onPlay = useCallback(() => { | ||
dispatch(setCurrentState('playing')); | ||
}, [dispatch]); | ||
|
||
const onStop = useCallback(() => { | ||
dispatch(setCurrentState('stopped')); | ||
}, [dispatch]); | ||
|
||
const onPause = useCallback(() => { | ||
dispatch(setCurrentState('paused')); | ||
}, [dispatch]); | ||
|
||
const getNext = useCallback(() => { | ||
const currentIndex = animations.indexOf(activeAnimationId); | ||
if (currentIndex === -1) return undefined; // or handle error | ||
if (currentIndex === -1) return undefined; | ||
|
||
// Use modulus to wrap around | ||
const nextIndex = (currentIndex + 1) % animations.length; | ||
return animations[nextIndex]; | ||
}, [animations, activeAnimationId]); | ||
|
@@ -77,119 +92,65 @@ export default function Players() { | |
const currentIndex = animations.indexOf(activeAnimationId); | ||
if (currentIndex === -1) return undefined; // or handle error | ||
|
||
// Use modulus to wrap around. Adding `emotions.length` before subtracting to avoid negative index | ||
const prevIndex = (currentIndex - 1 + animations.length) % animations.length; | ||
return animations[prevIndex]; | ||
}, [animations, activeAnimationId]); | ||
|
||
useEffect(() => { | ||
if (!dotLottie) return; | ||
dotLottie.addEventListener('load', onLoad); | ||
dotLottie.addEventListener('frame', onFrame); | ||
dotLottie.addEventListener('render', onRender); | ||
dotLottie.addEventListener('complete', onStop); | ||
dotLottie.addEventListener('stop', onStop); | ||
dotLottie.addEventListener('play', onPlay); | ||
dotLottie.addEventListener('pause', onPause); | ||
|
||
return () => { | ||
dotLottie.removeEventListener('load', onLoad); | ||
dotLottie.removeEventListener('frame', onFrame); | ||
dotLottie.removeEventListener('render', onRender); | ||
dotLottie.removeEventListener('complete', onStop); | ||
dotLottie.removeEventListener('stop', onStop); | ||
dotLottie.removeEventListener('play', onPlay); | ||
dotLottie.removeEventListener('pause', onPause); | ||
}; | ||
}, [onLoad, dotLottie, onFrame]); | ||
|
||
useEffect(() => { | ||
if (!dotLottie) return; | ||
if (segment[0] && segment[1]) { | ||
dotLottie.setSegment(segment[0], segment[1]); | ||
} | ||
}, [segment, dotLottie]); | ||
|
||
useEffect(() => { | ||
dotLottie?.setMarker(activeMarker); | ||
}, [activeMarker]); | ||
|
||
useEffect(() => { | ||
if (!dotLottie) return; | ||
dotLottie.setUseFrameInterpolation(useFrameInterpolation); | ||
}, [dotLottie, useFrameInterpolation]); | ||
|
||
const addEventListeners = useCallback( | ||
(player: DotLottie) => { | ||
player.addEventListener('complete', () => { | ||
dispatch(setCurrentState('stopped')); | ||
}); | ||
player.addEventListener('stop', () => { | ||
dispatch(setCurrentState('stopped')); | ||
}); | ||
player.addEventListener('play', () => { | ||
dispatch(setCurrentState('playing')); | ||
}); | ||
player.addEventListener('pause', () => { | ||
dispatch(setCurrentState('paused')); | ||
}); | ||
}, | ||
[dispatch], | ||
); | ||
|
||
useEffect(() => { | ||
if (!dotLottie) return; | ||
startTime = performance.now(); | ||
dotLottie?.loadAnimation(activeAnimationId); | ||
}, [activeAnimationId, dotLottie]); | ||
|
||
useEffect(() => { | ||
if (!dotLottie) return; | ||
dotLottie?.setTheme(activeThemeId); | ||
}, [activeThemeId, dotLottie]); | ||
}, [dotLottie, onLoad, onRender, onStop, onPlay, onPause]); | ||
|
||
useEffect(() => { | ||
if (!dotLottie) return; | ||
dispatch(setAnimations(dotLottie?.manifest?.animations?.map((item) => item.id) || [])); | ||
dispatch(setThemes(dotLottie?.manifest?.themes || [])); | ||
}, [dotLottie, dispatch]); | ||
|
||
useEffect(() => { | ||
startTime = performance.now(); | ||
}, [src, dotLottie]); | ||
|
||
return ( | ||
<> | ||
<div className="h-full flex-grow flex justify-between items-center flex-col gap-4"> | ||
<div className="flex justify-center h-full"> | ||
<div className="flex flex-col dotlottie-player"> | ||
<LoadTime | ||
version={dotLottieWebPkg.version} | ||
rendererVersion="[email protected]" | ||
className="mb-4" | ||
title="dotLottie Web" | ||
loadTime={parseFloat(loadTime.dotLottie.toFixed(2))} | ||
/> | ||
<LoadTime version={dotLottieWebPkg.version} className="mb-4" title="dotLottie Web" /> | ||
<div className="flex justify-center items-center p-4 flex-grow"> | ||
<div style={{ width: '350px', height: '350px' }}> | ||
<DotLottieNew | ||
<DotLottieReact | ||
backgroundColor={backgroundColor} | ||
width={350} | ||
height={350} | ||
autoplay={autoplay} | ||
useFrameInterpolation={true} | ||
useFrameInterpolation={useFrameInterpolation} | ||
loop={loop} | ||
mode={mode} | ||
speed={speed} | ||
dotLottieRefCallback={(ref) => { | ||
if (ref) { | ||
addEventListeners(ref); | ||
setDotLottie(ref); | ||
} | ||
}} | ||
themeId={activeThemeId} | ||
animationId={activeAnimationId} | ||
segment={segment as [number, number]} | ||
marker={activeMarker} | ||
dotLottieRefCallback={setDotLottie} | ||
src={src} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
{isJson ? ( | ||
<div className="flex flex-col lottie-web"> | ||
<LoadTime | ||
version="v5.12.2" | ||
rendererVersion="[email protected]" | ||
className="mb-4" | ||
title="Lottie Web" | ||
loadTime={parseFloat(loadTime.lottieWeb.toFixed(2))} | ||
/> | ||
<LoadTime version="v5.12.2" className="mb-4" title="Lottie Web" /> | ||
<div className="flex justify-center items-center p-4 flex-grow"> | ||
<div style={{ width: '350px', height: '350px' }}> | ||
<DotLottiePlayer | ||
|
@@ -200,12 +161,6 @@ export default function Players() { | |
autoplay={autoplay} | ||
loop={loop} | ||
speed={speed} | ||
onEvent={(event) => { | ||
if (event === 'ready') { | ||
const endTime = performance.now(); | ||
dispatch(setLoadTimeLottieWeb(endTime - startTime)); | ||
} | ||
}} | ||
src={src} | ||
/> | ||
</div> | ||
|