@@ -39,6 +39,7 @@ export const KeyFramePreviewer = () => {
3939 const originalAngle = Math . atan2 ( originalPos . x , originalPos . z ) ;
4040 return { radius, originalAngle}
4141 } , [ keyFrames ] )
42+ const timeRatio = timeRate / frameRate ;
4243
4344 const KeyFrameLerper = ( startState : Record < string , any > , endState :Record < string , any > , alpha :number , useCamera = true ) => {
4445 const startVizState = startState [ "visual" ]
@@ -127,6 +128,18 @@ export const KeyFramePreviewer = () => {
127128 invalidate ( ) ;
128129 }
129130 }
131+ if ( useTime ) {
132+ //This is currently incongruent with the export. This uses the timestep at the first keyframe as the intital progress
133+ //whereas export uses the current timestep at time of export. If the first keyframe is not on the first frame export will look different than preview.
134+ //Will come back to this
135+ const animProg = keyFrames ?. get ( keyFrameList [ 0 ] ) . time
136+ const { dataShape} = useGlobalStore . getState ( )
137+ const timeFrames = dataShape [ dataShape . length - 3 ]
138+ const dt = 1 / timeFrames
139+ let newProg = dt * Math . floor ( currentFrame * timeRatio ) + animProg ;
140+ newProg = loopTime ? newProg - Math . floor ( newProg ) : Math . min ( newProg , 1 ) ;
141+ usePlotStore . setState ( { animProg :newProg } )
142+ }
130143 if ( orbit ) {
131144 const angle = ( currentFrame / ( frames + 1 ) ) * deg2rad ( orbitDeg ) ;
132145 const newAngle = originalAngle + ( orbitDir ? - angle : angle ) ;
@@ -140,11 +153,13 @@ export const KeyFramePreviewer = () => {
140153
141154 // PREVIEW ANIMATION
142155 useEffect ( ( ) => {
143- if ( ! keyFrames || isAnimating . current ) return ;
156+ if ( ! keyFrames || ! previewKeyFrames ) {
157+ isAnimating . current = false ;
158+ return ;
159+ }
144160 const { animProg, setAnimProg} = usePlotStore . getState ( )
145161 originalAnimProg . current = animProg
146162 const keyFrameList = Array . from ( keyFrames . keys ( ) ) . sort ( ( a , b ) => a - b )
147- const timeRatio = timeRate / frameRate ;
148163 const { dataShape} = useGlobalStore . getState ( )
149164 const timeFrames = dataShape [ dataShape . length - 3 ]
150165 const dt = 1 / timeFrames
@@ -155,17 +170,13 @@ export const KeyFramePreviewer = () => {
155170 if ( frame > frames ) {
156171 clearInterval ( intervalRef . current as NodeJS . Timeout ) ;
157172 isAnimating . current = false ;
173+ useImageExportStore . setState ( { previewKeyFrames :false } )
158174 setAnimProg ( originalAnimProg . current )
159175 return ;
160176 }
161177 useImageExportStore . getState ( ) . setCurrentFrame ( frame )
162178 const startFrame = keyFrameList [ keyFrameIdx ] ;
163179 if ( keyFrameIdx + 1 < keyFrameList . length ) {
164- if ( useTime ) {
165- let newProg = dt * Math . floor ( frame * timeRatio ) + animProg ;
166- newProg = loopTime ? newProg - Math . floor ( newProg ) : Math . min ( newProg , 1 ) ;
167- setAnimProg ( newProg ) ;
168- }
169180 const endFrame = keyFrameList [ keyFrameIdx + 1 ] ;
170181 const thisFrames = endFrame - startFrame ;
171182 const alpha = Math . max ( frame - startFrame , 0 ) / thisFrames ;
@@ -183,6 +194,11 @@ export const KeyFramePreviewer = () => {
183194 invalidate ( ) ;
184195 }
185196 }
197+ if ( useTime ) {
198+ let newProg = dt * Math . floor ( frame * timeRatio ) + animProg ;
199+ newProg = loopTime ? newProg - Math . floor ( newProg ) : Math . min ( newProg , 1 ) ;
200+ setAnimProg ( newProg ) ;
201+ }
186202 if ( orbit ) {
187203 const angle = ( frame / ( frames + 1 ) ) * deg2rad ( orbitDeg ) ;
188204 const newAngle = originalAngle + ( orbitDir ? - angle : angle ) ;
0 commit comments