@@ -62,19 +62,31 @@ export class Entity {
62
62
render ( progress , data , globalData ) {
63
63
let subframes = this . states [ data . number ]
64
64
this . container . visible = false
65
+ let start = null
66
+ let end
67
+ // This t is used to animate the interpolation
68
+ let t = 1
65
69
if ( subframes && subframes . length ) {
66
70
let index = 0
67
71
while ( index < subframes . length - 1 && subframes [ index ] . t < progress ) {
68
72
index ++
69
73
}
70
- let start = subframes [ index - 1 ]
71
- let end = subframes [ index ]
72
- // This t is used to animate the interpolation
73
- let t
74
+
75
+ start = subframes [ index - 1 ]
76
+ end = subframes [ index ]
74
77
75
78
if ( ! start ) {
76
- // The start frame must be at the end of the previous turn
77
- var prev = this . states [ data . previous . number ] || [ ]
79
+ // The start frame must be at the end of a previous turn
80
+ let frameNumbers = Object . keys ( this . states )
81
+ let index = frameNumbers . indexOf ( data . number . toString ( ) ) - 1
82
+ // Failsafe
83
+ if ( index === - 1 ) {
84
+ index = frameNumbers . length - 1
85
+ }
86
+ while ( index >= 0 && frameNumbers [ index ] >= data . number ) {
87
+ index --
88
+ }
89
+ let prev = this . states [ frameNumbers [ index ] ] || [ ]
78
90
start = prev [ prev . length - 1 ]
79
91
80
92
// If it didn't exist on the previous turn, don't even animate it
@@ -88,41 +100,53 @@ export class Entity {
88
100
} else {
89
101
t = unlerp ( start . t , end . t , progress )
90
102
}
91
- if ( start ) {
92
- const changed = { }
93
- const state = Object . assign ( { } , this . currentState )
94
-
95
- for ( let property of this . properties ) {
96
- const opts = PROPERTIES [ property ] || PROPERTIES . default
97
- const lerpMethod = opts . lerpMethod
98
- const curve = end . curve [ property ] || ( a => a )
99
- const newValue = lerpMethod ( start [ property ] , end [ property ] , curve ( t ) )
100
- if ( newValue !== this . currentState [ property ] ) {
101
- changed [ property ] = true
102
- state [ property ] = newValue
103
- }
104
- }
105
- this . updateDisplay ( state , changed , globalData , data , progress )
106
- Object . assign ( this . currentState , state )
107
- this . container . visible = this . container . _visible
108
- if ( changed . children ) {
109
- globalData . mustResetTree = true
110
- if ( typeof this . postUpdate === 'function' ) {
111
- globalData . updatedBuffers . push ( this )
112
- }
113
- }
114
- if ( changed . zIndex ) {
115
- globalData . mustResort = true
116
- }
117
- if ( changed . mask ) {
118
- globalData . maskUpdates [ this . id ] = state . mask
103
+ } else {
104
+ // Look for the most recent state, but don't interpolate?
105
+ let frameNumbers = Object . keys ( this . states )
106
+ let index = frameNumbers . length - 1
107
+ while ( index >= 0 && frameNumbers [ index ] > data . number ) {
108
+ index --
109
+ }
110
+ let substates = this . states [ frameNumbers [ index ] ]
111
+
112
+ if ( substates != null ) {
113
+ start = substates [ substates . length - 1 ]
114
+ end = start
115
+ } else {
116
+ Object . assign ( this . currentState , this . defaultState )
117
+ }
118
+ }
119
+ if ( start ) {
120
+ const changed = { }
121
+ const state = Object . assign ( { } , this . currentState )
122
+ for ( let property of this . properties ) {
123
+ const opts = PROPERTIES [ property ] || PROPERTIES . default
124
+ const lerpMethod = opts . lerpMethod
125
+ const curve = end . curve [ property ] || ( a => a )
126
+ const newValue = lerpMethod ( start [ property ] , end [ property ] , curve ( t ) )
127
+ if ( newValue !== this . currentState [ property ] ) {
128
+ changed [ property ] = true
129
+ state [ property ] = newValue
119
130
}
120
- if ( Object . keys ( changed ) . length !== 0 && this . parent ) {
121
- this . parent . notifyChange ( )
131
+ }
132
+ this . updateDisplay ( state , changed , globalData , data , progress )
133
+ Object . assign ( this . currentState , state )
134
+ this . container . visible = this . container . _visible
135
+ if ( changed . children ) {
136
+ globalData . mustResetTree = true
137
+ if ( typeof this . postUpdate === 'function' ) {
138
+ globalData . updatedBuffers . push ( this )
122
139
}
123
140
}
124
- } else {
125
- Object . assign ( this . currentState , this . defaultState )
141
+ if ( changed . zIndex ) {
142
+ globalData . mustResort = true
143
+ }
144
+ if ( changed . mask ) {
145
+ globalData . maskUpdates [ this . id ] = state . mask
146
+ }
147
+ if ( Object . keys ( changed ) . length !== 0 && this . parent ) {
148
+ this . parent . notifyChange ( )
149
+ }
126
150
}
127
151
}
128
152
0 commit comments