@@ -20,10 +20,10 @@ const float32ArraySupported = typeof Float32Array === 'function'
20
20
const A = ( aA1 , aA2 ) => {
21
21
return 1.0 - 3.0 * aA2 + 3.0 * aA1
22
22
}
23
- function B ( aA1 , aA2 ) {
23
+ const B = ( aA1 , aA2 ) => {
24
24
return 3.0 * aA2 - 6.0 * aA1
25
25
}
26
- function C ( aA1 ) {
26
+ const C = aA1 => {
27
27
return 3.0 * aA1
28
28
}
29
29
@@ -38,7 +38,7 @@ const getSlope = (aT, aA1, aA2) => {
38
38
}
39
39
40
40
const binarySubdivide = ( aX , aA , aB , mX1 , mX2 ) => {
41
- var currentX ,
41
+ let currentX ,
42
42
currentT ,
43
43
i = 0
44
44
do {
@@ -74,19 +74,19 @@ const bezier = (mX1, mY1, mX2, mY2) => {
74
74
}
75
75
76
76
// Precompute samples table
77
- var sampleValues = float32ArraySupported
77
+ const sampleValues = float32ArraySupported
78
78
? new Float32Array ( kSplineTableSize )
79
79
: new Array ( kSplineTableSize )
80
80
if ( mX1 !== mY1 || mX2 !== mY2 ) {
81
- for ( var i = 0 ; i < kSplineTableSize ; ++ i ) {
81
+ for ( let i = 0 ; i < kSplineTableSize ; ++ i ) {
82
82
sampleValues [ i ] = calcBezier ( i * kSampleStepSize , mX1 , mX2 )
83
83
}
84
84
}
85
85
86
86
const getTForX = aX => {
87
- var intervalStart = 0.0
88
- var currentSample = 1
89
- var lastSample = kSplineTableSize - 1
87
+ let intervalStart = 0.0
88
+ let currentSample = 1
89
+ let lastSample = kSplineTableSize - 1
90
90
91
91
for (
92
92
;
@@ -134,21 +134,49 @@ const bezier = (mX1, mY1, mX2, mY2) => {
134
134
}
135
135
}
136
136
137
- // Predefined set of animations. Similar to CSS easing functions
138
- const animations = {
139
- ease : bezier ( 0.25 , 0.1 , 0.25 , 1 ) ,
140
- easeIn : bezier ( 0.42 , 0 , 1 , 1 ) ,
141
- easeOut : bezier ( 0 , 0 , 0.58 , 1 ) ,
142
- easeInOut : bezier ( 0.42 , 0 , 0.58 , 1 ) ,
143
- linear : bezier ( 0 , 0 , 1 , 1 ) ,
137
+ const noop = ( ) => { }
138
+
139
+ const getScheduler = scheduler => {
140
+ if ( ! scheduler ) {
141
+ var canRaf = typeof window !== 'undefined' && window . requestAnimationFrame
142
+ return canRaf ? rafScheduler ( ) : timeoutScheduler ( )
143
+ }
144
+ if ( typeof scheduler . next !== 'function' )
145
+ throw new Error ( 'Scheduler is supposed to have next(cb) function' )
146
+ if ( typeof scheduler . cancel !== 'function' )
147
+ throw new Error ( 'Scheduler is supposed to have cancel(handle) function' )
148
+
149
+ return scheduler
150
+ }
151
+
152
+ const rafScheduler = ( ) => {
153
+ return {
154
+ next : window . requestAnimationFrame . bind ( window ) ,
155
+ cancel : window . cancelAnimationFrame . bind ( window ) ,
156
+ }
157
+ }
158
+
159
+ const timeoutScheduler = ( ) => {
160
+ return {
161
+ next : cb => setTimeout ( cb , 1000 / 60 ) ,
162
+ cancel : id => clearTimeout ( id ) ,
163
+ }
144
164
}
145
165
146
166
export const animate = ( source , target , options ) => {
147
- var start = Object . create ( null )
148
- var diff = Object . create ( null )
167
+ const start = Object . create ( null )
168
+ const diff = Object . create ( null )
169
+ // Predefined set of animations. Similar to CSS easing functions
170
+ const animations = {
171
+ ease : bezier ( 0.25 , 0.1 , 0.25 , 1 ) ,
172
+ easeIn : bezier ( 0.42 , 0 , 1 , 1 ) ,
173
+ easeOut : bezier ( 0 , 0 , 0.58 , 1 ) ,
174
+ easeInOut : bezier ( 0.42 , 0 , 0.58 , 1 ) ,
175
+ linear : bezier ( 0 , 0 , 1 , 1 ) ,
176
+ }
149
177
options = options || { }
150
178
// We let clients specify their own easing function
151
- var easing =
179
+ let easing =
152
180
typeof options . easing === 'function'
153
181
? options . easing
154
182
: animations [ options . easing ]
@@ -161,34 +189,29 @@ export const animate = (source, target, options) => {
161
189
easing = animations . ease
162
190
}
163
191
164
- var step = typeof options . step === 'function' ? options . step : noop
165
- var done = typeof options . done === 'function' ? options . done : noop
192
+ const step = typeof options . step === 'function' ? options . step : noop
193
+ const done = typeof options . done === 'function' ? options . done : noop
166
194
167
- var scheduler = getScheduler ( options . scheduler )
195
+ const scheduler = getScheduler ( options . scheduler )
168
196
169
- var keys = Object . keys ( target )
197
+ const keys = Object . keys ( target )
170
198
keys . forEach ( key => {
171
199
start [ key ] = source [ key ]
172
200
diff [ key ] = target [ key ] - source [ key ]
173
201
} )
174
202
175
- var durationInMs = options . duration || 400
176
- var durationInFrames = Math . max ( 1 , durationInMs * 0.06 ) // 0.06 because 60 frames pers 1,000 ms
177
- var previousAnimationId
178
- var frame = 0
203
+ const durationInMs = options . duration || 400
204
+ const durationInFrames = Math . max ( 1 , durationInMs * 0.06 ) // 0.06 because 60 frames pers 1,000 ms
205
+ let previousAnimationId
206
+ let frame = 0
179
207
180
- previousAnimationId = scheduler . next ( loop )
181
-
182
- return {
183
- cancel : cancel ,
184
- }
185
-
186
- function cancel ( ) {
187
- scheduler . cancel ( previousAnimationId )
188
- previousAnimationId = 0
208
+ const setValues = t => {
209
+ keys . forEach ( function ( key ) {
210
+ source [ key ] = diff [ key ] * t + start [ key ]
211
+ } )
189
212
}
190
213
191
- function loop ( ) {
214
+ const loop = ( ) => {
192
215
var t = easing ( frame / durationInFrames )
193
216
frame += 1
194
217
setValues ( t )
@@ -203,42 +226,12 @@ export const animate = (source, target, options) => {
203
226
}
204
227
}
205
228
206
- function setValues ( t ) {
207
- keys . forEach ( function ( key ) {
208
- source [ key ] = diff [ key ] * t + start [ key ]
209
- } )
210
- }
211
- }
212
-
213
- function noop ( ) { }
214
-
215
- function getScheduler ( scheduler ) {
216
- if ( ! scheduler ) {
217
- var canRaf = typeof window !== 'undefined' && window . requestAnimationFrame
218
- return canRaf ? rafScheduler ( ) : timeoutScheduler ( )
219
- }
220
- if ( typeof scheduler . next !== 'function' )
221
- throw new Error ( 'Scheduler is supposed to have next(cb) function' )
222
- if ( typeof scheduler . cancel !== 'function' )
223
- throw new Error ( 'Scheduler is supposed to have cancel(handle) function' )
224
-
225
- return scheduler
226
- }
229
+ previousAnimationId = scheduler . next ( loop )
227
230
228
- function rafScheduler ( ) {
229
- return {
230
- next : window . requestAnimationFrame . bind ( window ) ,
231
- cancel : window . cancelAnimationFrame . bind ( window ) ,
231
+ const cancel = ( ) => {
232
+ scheduler . cancel ( previousAnimationId )
233
+ previousAnimationId = 0
232
234
}
233
- }
234
235
235
- function timeoutScheduler ( ) {
236
- return {
237
- next : function ( cb ) {
238
- return setTimeout ( cb , 1000 / 60 )
239
- } ,
240
- cancel : function ( id ) {
241
- return clearTimeout ( id )
242
- } ,
243
- }
236
+ return { cancel }
244
237
}
0 commit comments