7
7
from './core' ;
8
8
import Easing from './Easing' ;
9
9
import Interpolation from './Interpolation' ;
10
- import cloneTween from './clone' ;
11
10
import joinToString from './joinToString' ;
12
11
import toNumber from './toNumber' ;
12
+ import SubTween from './SubTween' ;
13
13
14
14
// Credits:
15
15
// @jkroso for string parse library
@@ -19,6 +19,8 @@ const Number_Match_RegEx = /\s+|([A-Za-z?().,{}:""\[\]#]+)|([-+\/*%]+=)?([-+*\/%
19
19
class Tween {
20
20
constructor ( object = { } , instate ) {
21
21
22
+ this . isJoinToString = typeof object === "string" && Number_Match_RegEx . test ( object ) ;
23
+ object = this . isJoinToString ? object . match ( Number_Match_RegEx ) . map ( toNumber ) : object ;
22
24
this . object = object ;
23
25
this . _valuesStart = Tween . createEmptyConst ( object ) ;
24
26
this . _valuesEnd = Tween . createEmptyConst ( object ) ;
@@ -200,7 +202,7 @@ class Tween {
200
202
} ;
201
203
this . _valuesEnd = _vE ;
202
204
} else {
203
- this . _valuesEnd = properties ;
205
+ this . _valuesEnd = this . isJoinToString ? SubTween ( this . object , properties . match ( Number_Match_RegEx ) . map ( toNumber ) ) : properties ;
204
206
}
205
207
206
208
if ( typeof duration === "number" ) {
@@ -233,61 +235,22 @@ class Tween {
233
235
234
236
for ( let property in _valuesEnd ) {
235
237
238
+ property = object [ + property ] !== undefined ? + property : property ;
239
+
236
240
if ( typeof _valuesEnd [ property ] === "object" ) {
237
- if ( Array . isArray ( _valuesEnd [ property ] ) ) {
238
- if ( typeof object [ property ] === "number" ) {
239
- this . _valuesEnd [ property ] = [ object [ property ] ] . concat ( _valuesEnd [ property ] ) ;
240
- } else {
241
- let clonedTween = cloneTween ( this , {
242
- object : object [ property ] ,
243
- _valuesEnd : _valuesEnd [ property ] ,
244
- _events : undefined
245
- } )
246
- . start ( )
247
- . stop ( ) ;
248
-
249
- this . _valuesEnd [ property ] = clonedTween ;
250
- }
251
- } else {
252
- let clonedTween = cloneTween ( this , {
253
- object : object [ property ] ,
254
- _valuesEnd : _valuesEnd [ property ] ,
255
- _events : undefined
256
- } )
257
- . start ( )
258
- . stop ( ) ;
259
-
260
- this . _valuesStart [ property ] = 1 ;
261
- this . _valuesEnd [ property ] = clonedTween ;
262
- }
241
+
242
+ this . _valuesEnd [ property ] = SubTween ( object [ property ] , _valuesEnd [ property ] ) ;
243
+
263
244
} else if ( typeof _valuesEnd [ property ] === "string" && typeof object [ property ] === "string" && Number_Match_RegEx . test ( object [ property ] ) && Number_Match_RegEx . test ( _valuesEnd [ property ] ) ) {
264
245
265
246
let __get__Start = object [ property ] . match ( Number_Match_RegEx ) ;
266
247
__get__Start = __get__Start . map ( toNumber ) ;
267
248
let __get__End = _valuesEnd [ property ] . match ( Number_Match_RegEx ) ;
268
249
__get__End = __get__End . map ( toNumber ) ;
269
- let clonedTween = cloneTween ( this , {
270
- object : __get__Start ,
271
- _valuesEnd : __get__End ,
272
- _events : { }
273
- } )
274
- . start ( )
275
- . stop ( ) ;
276
-
277
- clonedTween . join = true ; // For string tweening
278
- this . _valuesStart [ property ] = 1 ;
279
- this . _valuesEnd [ property ] = clonedTween ;
280
250
281
- }
282
-
283
- // If value presented as function,
284
- // we should convert to value again by passing function
285
- if ( typeof object [ property ] === "function" ) {
286
- object [ property ] = this . object [ property ] = object [ property ] ( this ) ;
287
- }
251
+ this . _valuesEnd [ property ] = SubTween ( __get__Start , __get__End ) ;
252
+ this . _valuesEnd [ property ] . join = true ;
288
253
289
- if ( typeof _valuesEnd [ property ] === "function" ) {
290
- this . _valuesEnd [ property ] = _valuesEnd [ property ] ( this ) ;
291
254
}
292
255
293
256
// If `to()` specifies a property that doesn't exist in the source object,
@@ -410,7 +373,8 @@ class Tween {
410
373
_duration,
411
374
_valuesStart,
412
375
_valuesEnd,
413
- object
376
+ object,
377
+ isJoinToString
414
378
} = this ;
415
379
416
380
let property ;
@@ -436,6 +400,20 @@ class Tween {
436
400
437
401
value = _easingFunction ( elapsed ) ;
438
402
403
+ if ( typeof _valuesEnd === "function" ) {
404
+
405
+ let get = _valuesEnd ( value ) ;
406
+
407
+ if ( isJoinToString ) {
408
+
409
+ get = joinToString ( get ) ;
410
+
411
+ }
412
+
413
+ object = get ;
414
+
415
+ } else {
416
+
439
417
for ( property in _valuesEnd ) {
440
418
441
419
// Don't update properties that do not exist in the source object
@@ -446,19 +424,17 @@ class Tween {
446
424
let start = _valuesStart [ property ] ;
447
425
let end = _valuesEnd [ property ] ;
448
426
449
- if ( end instanceof Tween ) {
427
+ if ( typeof end === "function" ) {
450
428
451
- let getValue = end . get ( time ) ;
429
+ let get = end ( value ) ;
452
430
453
- if ( end . join ) {
431
+ if ( end . join ) {
454
432
455
- object [ property ] = joinToString ( getValue ) ;
433
+ get = joinToString ( get ) ;
456
434
457
- } else {
458
-
459
- object [ property ] = getValue ;
435
+ }
460
436
461
- }
437
+ object [ property ] = get ;
462
438
463
439
} else if ( Array . isArray ( end ) ) {
464
440
@@ -482,6 +458,8 @@ class Tween {
482
458
483
459
}
484
460
461
+ }
462
+
485
463
this . emit ( 'update' , object , value , elapsed ) ;
486
464
487
465
if ( elapsed === 1 || ( _reversed && elapsed === 0 ) ) {
0 commit comments