1
- import Tween from './Tween'
2
- import { add , now , nextId } from './core'
1
+ import Tween , { EVENT_UPDATE , EVENT_RS , EVENT_REPEAT , EVENT_REVERSE , EVENT_COMPLETE } from './Tween'
2
+ import { add , now } from './core'
3
+ import PlaybackPosition from './PlaybackPosition'
3
4
5
+ let _id = 0
4
6
class Timeline extends Tween {
5
7
constructor ( params ) {
6
8
super ( )
7
9
this . _totalDuration = 0
8
10
this . _startTime = now ( )
9
11
this . _tweens = { }
10
- this . _timing = [ ]
11
12
this . _elapsed = 0
12
- this . _id = nextId ( )
13
- this . _labels = { }
13
+ this . _id = _id ++
14
14
this . _defaultParams = params
15
+ this . position = new PlaybackPosition ( )
16
+ this . position . addLabel ( 'afterLast' , this . _totalDuration )
17
+ this . position . addLabel ( 'afterInit' , this . _startTime )
15
18
16
19
return this
17
20
}
18
21
19
- setLabel ( name , value ) {
20
- this . _labels [ name ] = this . parsePosition ( 0 , value , 0 )
22
+ addLabel ( name , offset ) {
23
+ this . position . addLabel ( name , offset )
21
24
return this
22
25
}
23
26
24
- parsePosition ( startTime , input , total ) {
25
- let position = startTime + total
26
-
27
- if ( typeof input === 'string' ) {
28
- for ( let label in this . _labels ) {
29
- if ( input . indexOf ( label ) === 0 ) {
30
- let inp = input . split ( label ) [ 1 ]
31
-
32
- if ( inp . length === 0 || ( inp [ 0 ] === '+' || inp [ 0 ] === '-' ) ) {
33
- position = this . _labels [ label ] + startTime
34
- input = input . replace ( label , '' )
35
- }
36
- }
37
- }
38
-
39
- if ( input . indexOf ( '+' ) === 0 || input . indexOf ( '-' ) === 0 ) {
40
- position += parseFloat ( input )
41
- }
42
- } else if ( typeof input === 'number' ) {
43
- position += input
44
- }
45
-
46
- return Math . max ( 0 , position )
47
- }
48
-
49
27
map ( fn ) {
50
28
for ( let tween in this . _tweens ) {
51
29
let _tween = this . _tweens [ tween ]
@@ -62,7 +40,7 @@ class Timeline extends Tween {
62
40
} )
63
41
return this
64
42
} else if ( typeof tween === 'object' && ! ( tween instanceof Tween ) ) {
65
- tween = new Tween ( tween . from , tween )
43
+ tween = new Tween ( tween . from ) . to ( tween . to , tween )
66
44
}
67
45
68
46
let {
@@ -76,10 +54,12 @@ class Timeline extends Tween {
76
54
}
77
55
}
78
56
79
- tween . _startTime = this . parsePosition ( now ( ) + tween . _delayTime , position , _totalDuration )
80
- this . _timing [ tween . id ] = tween . _startTime
81
- this . _totalDuration = Math . max ( _totalDuration , tween . _duration + tween . _startTime + tween . _delayTime )
57
+ const offset = typeof position === 'number' ? position : this . position . parseLabel ( typeof position !== 'undefined' ? position : 'afterLast' , null )
58
+ tween . _startTime = this . _startTime
59
+ tween . _startTime += offset
60
+ this . _totalDuration = Math . max ( _totalDuration , tween . _startTime + tween . _delayTime + tween . _duration )
82
61
this . _tweens [ tween . id ] = tween
62
+ this . position . setLabel ( 'afterLast' , this . _totalDuration )
83
63
return this
84
64
}
85
65
@@ -88,7 +68,7 @@ class Timeline extends Tween {
88
68
89
69
add ( this )
90
70
91
- return this . emit ( 'restart' )
71
+ return this . emit ( EVENT_RS )
92
72
}
93
73
94
74
easing ( easing ) {
@@ -101,11 +81,7 @@ class Timeline extends Tween {
101
81
102
82
reverse ( ) {
103
83
this . _reversed = ! this . _reversed
104
- this . _timing = this . _timing . reverse ( )
105
- for ( let tween in this . _tweens ) {
106
- this . _tweens [ tween ] . _startTime = this . _timing [ + tween ]
107
- }
108
- return this
84
+ return this . emit ( EVENT_REVERSE )
109
85
}
110
86
111
87
update ( time ) {
@@ -117,7 +93,8 @@ class Timeline extends Tween {
117
93
_startTime,
118
94
_reversed,
119
95
_yoyo,
120
- _repeat
96
+ _repeat,
97
+ _isFinite
121
98
} = this
122
99
123
100
if ( time < _startTime ) {
@@ -142,15 +119,15 @@ class Timeline extends Tween {
142
119
}
143
120
}
144
121
145
- this . emit ( 'update' , elapsed , timing )
122
+ this . emit ( EVENT_UPDATE , elapsed , timing )
146
123
147
124
if ( elapsed === 1 || ( _reversed && elapsed === 0 ) ) {
148
125
if ( _repeat ) {
149
- if ( isFinite ( _repeat ) ) {
126
+ if ( _isFinite ) {
150
127
this . _repeat --
151
128
}
152
129
153
- this . emit ( _reversed ? 'reverse' : 'repeat' )
130
+ this . emit ( _reversed ? EVENT_REVERSE : EVENT_REPEAT )
154
131
155
132
if ( _yoyo ) {
156
133
this . reverse ( )
@@ -169,11 +146,12 @@ class Timeline extends Tween {
169
146
if ( _tween . skip ) {
170
147
_tween . skip = false
171
148
}
149
+ _tween . reassignValues ( )
172
150
}
173
151
174
152
return true
175
153
} else {
176
- this . emit ( 'complete' )
154
+ this . emit ( EVENT_COMPLETE )
177
155
this . _repeat = this . _r
178
156
179
157
for ( let tween in _tweens ) {
0 commit comments