@@ -7,7 +7,7 @@ export class Animation {
77 [ path : string ] : {
88 startValue : any
99 targetValue : any
10- options : Animation . StartOptions < any >
10+ options : AnimationStartOptions < any >
1111 }
1212 } = { }
1313
@@ -17,14 +17,14 @@ export class Animation {
1717 return Object . keys ( this . ids )
1818 }
1919
20- start < T extends Animation . TargetValue > (
20+ start < T extends AnimationTargetValue > (
2121 path : string | string [ ] ,
2222 targetValue : T ,
23- options : Animation . StartOptions < T > = { } ,
23+ options : AnimationStartOptions < T > = { } ,
2424 delim = '/' ,
2525 ) : ( ) => void {
2626 const startValue = this . cell . getPropByPath < T > ( path )
27- const localOptions = ObjectExt . defaults ( options , Animation . defaultOptions )
27+ const localOptions = ObjectExt . defaults ( options , defaultOptions )
2828 const timing = this . getTiming ( localOptions . timing )
2929 const interpolate = this . getInterp < T > (
3030 localOptions . interp ,
@@ -45,7 +45,9 @@ export class Animation {
4545
4646 if (
4747 dir === 'normal' ||
48+ // alternate 时迭代次数为奇数时正向,偶数时反向
4849 ( dir === 'alternate' && ! isOdd ) ||
50+ // alternate-reverse 时迭代次数为 偶数时正向,奇数时反向
4951 ( dir === 'alternate-reverse' && isOdd )
5052 ) {
5153 return timeFraction
@@ -64,27 +66,28 @@ export class Animation {
6466 }
6567
6668 const iterate = ( ) => {
67- const now = new Date ( ) . getTime ( )
69+ const now = Date . now ( )
6870 if ( startTime === 0 ) {
6971 startTime = now
7072 }
7173
74+ const { duration, iterations, fill } = localOptions
7275 const elapsed = now - startTime
73- const iterationIndex = Math . floor ( elapsed / localOptions . duration )
74- const localTime = elapsed % localOptions . duration
75- const timeFraction = localTime / localOptions . duration
76+ const iterationIndex = Math . floor ( elapsed / duration )
77+ const localTime = elapsed % duration
78+ const timeFraction = localTime / duration
7679
7780 let progress = calculateDirectionProgress ( timeFraction , iterationIndex )
7881
79- if ( iterationIndex < localOptions . iterations ) {
82+ if ( iterationIndex < iterations ) {
8083 this . ids [ key ] = requestAnimationFrame ( iterate )
8184
8285 updateProgressState ( progress )
8386 } else {
8487 // 动画结束
85- if ( localOptions . fill === 'forwards' ) {
88+ if ( fill === 'forwards' ) {
8689 progress = calculateDirectionProgress ( 1 , iterationIndex - 1 )
87- } else if ( localOptions . fill === 'none' ) {
90+ } else if ( fill === 'none' ) {
8891 progress = 0
8992 }
9093 progress = Math . round ( progress )
@@ -109,12 +112,12 @@ export class Animation {
109112 options . start && options . start ( this . getArgs < T > ( key ) )
110113 } , options . delay )
111114
112- return this . stop . bind ( this , path , delim , options )
115+ return this . stop . bind ( this , path , options , delim )
113116 }
114117
115- stop < T extends Animation . TargetValue > (
118+ stop < T extends AnimationTargetValue > (
116119 path : string | string [ ] ,
117- options : Animation . StopOptions < T > = { } ,
120+ options : AnimationStopOptions < T > = { } ,
118121 delim = '/' ,
119122 ) {
120123 const paths = Array . isArray ( path ) ? path : path . split ( delim )
@@ -158,7 +161,7 @@ export class Animation {
158161 return typeof timing === 'string' ? Timing [ timing ] : timing
159162 }
160163
161- private getInterp < T extends Animation . TargetValue > (
164+ private getInterp < T extends AnimationTargetValue > (
162165 interp : Interp . Definition < any > | undefined ,
163166 startValue : T ,
164167 targetValue : T ,
@@ -185,9 +188,9 @@ export class Animation {
185188 )
186189 }
187190
188- private getArgs < T extends Animation . TargetValue > (
191+ private getArgs < T extends AnimationTargetValue > (
189192 key : string ,
190- ) : Animation . CallbackArgs < T > {
193+ ) : AnimationCallbackArgs < T > {
191194 const data = this . cache [ key ]
192195 return {
193196 path : key ,
@@ -198,75 +201,73 @@ export class Animation {
198201 }
199202}
200203
201- export namespace Animation {
202- export interface BaseOptions {
203- delay : number
204- duration : number
205- timing : Timing . Names | Timing . Definition
206- iterations ?: number
207- direction ?: 'normal' | 'reverse' | 'alternate' | 'alternate-reverse'
208- fill ?: 'none' | 'forwards'
209- }
204+ export interface AnimationBaseOptions {
205+ delay : number
206+ duration : number
207+ timing : Timing . Names | Timing . Definition
208+ iterations ?: number
209+ direction ?: 'normal' | 'reverse' | 'alternate' | 'alternate-reverse'
210+ fill ?: 'none' | 'forwards'
211+ }
210212
211- export type TargetValue = string | number | KeyValue < number >
213+ export type AnimationTargetValue = string | number | KeyValue < number >
212214
213- export interface CallbackArgs < T > {
214- cell : Cell
215- path : string
216- startValue : T
217- targetValue : T
218- }
215+ export interface AnimationCallbackArgs < T > {
216+ cell : Cell
217+ path : string
218+ startValue : T
219+ targetValue : T
220+ }
219221
220- export interface ProgressArgs < T > extends CallbackArgs < T > {
221- progress : number
222- currentValue : T
223- }
222+ export interface AnimationProgressArgs < T > extends AnimationCallbackArgs < T > {
223+ progress : number
224+ currentValue : T
225+ }
224226
225- export interface StopArgs < T > extends CallbackArgs < T > {
226- jumpedToEnd ?: boolean
227- }
227+ export interface AnimationStopArgs < T > extends AnimationCallbackArgs < T > {
228+ jumpedToEnd ?: boolean
229+ }
228230
229- export interface StartOptions < T >
230- extends Partial < BaseOptions > ,
231- StopOptions < T > {
232- interp ?: Interp . Definition < any >
233- /**
234- * A function to call when the animation begins.
235- */
236- start ?: ( options : CallbackArgs < T > ) => void
237- /**
238- * A function to be called after each step of the animation, only once per
239- * animated element regardless of the number of animated properties.
240- */
241- progress ?: ( options : ProgressArgs < T > ) => void
242- }
231+ export interface AnimationStartOptions < T >
232+ extends Partial < AnimationBaseOptions > ,
233+ AnimationStopOptions < T > {
234+ interp ?: Interp . Definition < any >
235+ /**
236+ * A function to call when the animation begins.
237+ */
238+ start ?: ( options : AnimationCallbackArgs < T > ) => void
239+ /**
240+ * A function to be called after each step of the animation, only once per
241+ * animated element regardless of the number of animated properties.
242+ */
243+ progress ?: ( options : AnimationProgressArgs < T > ) => void
244+ }
243245
244- export interface StopOptions < T > {
245- /**
246- * A Boolean indicating whether to complete the animation immediately.
247- * Defaults to `false`.
248- */
249- jumpedToEnd ?: boolean
250- /**
251- * A function that is called once the animation completes.
252- */
253- complete ?: ( options : CallbackArgs < T > ) => void
254- /**
255- * A function to be called when the animation stops.
256- */
257- stop ?: ( options : StopArgs < T > ) => void
258- /**
259- * A function to be called when the animation completes or stops.
260- */
261- finish ?: ( options : CallbackArgs < T > ) => void
262- }
246+ export interface AnimationStopOptions < T > {
247+ /**
248+ * A Boolean indicating whether to complete the animation immediately.
249+ * Defaults to `false`.
250+ */
251+ jumpedToEnd ?: boolean
252+ /**
253+ * A function that is called once the animation completes.
254+ */
255+ complete ?: ( options : AnimationCallbackArgs < T > ) => void
256+ /**
257+ * A function to be called when the animation stops.
258+ */
259+ stop ?: ( options : AnimationStopArgs < T > ) => void
260+ /**
261+ * A function to be called when the animation completes or stops.
262+ */
263+ finish ?: ( options : AnimationCallbackArgs < T > ) => void
264+ }
263265
264- export const defaultOptions : BaseOptions = {
265- delay : 10 ,
266- duration : 100 ,
267- timing : 'linear' ,
268- iterations : 1 ,
269- direction : 'normal' ,
270- fill : 'none' ,
271- }
266+ export const defaultOptions : AnimationBaseOptions = {
267+ delay : 10 ,
268+ duration : 100 ,
269+ timing : 'linear' ,
270+ iterations : 1 ,
271+ direction : 'normal' ,
272+ fill : 'none' ,
272273}
0 commit comments