1
1
import clamp from './clamp' ;
2
2
import Ease from './Ease' ;
3
+ import forEachIn from './forEachIn' ;
3
4
import lerp from './lerp' ;
4
5
import round from './round' ;
5
6
@@ -208,34 +209,34 @@ function parseProperties(p) {
208
209
var properties = clone ( p ) ;
209
210
210
211
// Transform some properties if needed.
211
- if ( properties . scale ) {
212
+ if ( typeof properties . scale !== "undefined" ) {
212
213
properties . scaleX = properties . scale ;
213
214
properties . scaleY = properties . scale ;
214
215
delete properties . scale ;
215
216
}
216
217
217
- if ( properties . rotate ) {
218
+ if ( typeof properties . rotate !== "undefined" ) {
218
219
properties . rotateX = properties . rotate ;
219
220
properties . rotateY = properties . rotate ;
220
221
delete properties . rotate ;
221
222
}
222
223
223
- if ( properties . x ) {
224
+ if ( typeof properties . x !== "undefined" ) {
224
225
properties . translateX = properties . x ;
225
226
delete properties . x ;
226
227
}
227
228
228
- if ( properties . y ) {
229
- properties . translateX = properties . x ;
229
+ if ( typeof properties . y !== "undefined" ) {
230
+ properties . translateY = properties . y ;
230
231
delete properties . y ;
231
232
}
232
233
233
- if ( properties . z ) {
234
+ if ( typeof properties . z !== "undefined" ) {
234
235
properties . translateZ = properties . z ;
235
236
delete properties . z ;
236
237
}
237
238
238
- Object . keys ( properties ) . forEach ( function ( key ) {
239
+ forEachIn ( Object . keys ( properties ) ) ( function ( key ) {
239
240
var property = properties [ key ] ;
240
241
var isPropertyArray = Array . isArray ( property ) ;
241
242
var parsedStringProperty = parseStringProperty ( property ) ;
@@ -272,33 +273,68 @@ function parseProperties(p) {
272
273
return properties ;
273
274
}
274
275
276
+ /**
277
+ * Return a translation array from a
278
+ * string (retrieved from style for example)
279
+ * @param {String } transformationString
280
+ * @returns {[x: String, y: String, z: String] }
281
+ */
282
+ function getTranslationArrayFromString ( transformationString ) {
283
+ var translationArray = [ "0" , "0" , "0" ] ;
284
+
285
+ if ( transformationString . indexOf ( 'translate3d' ) !== - 1 ) {
286
+ var translation = transformationString . match ( / t r a n s l a t e 3 d \( ( [ ^ ) ] + ) , ( [ ^ ) ] + ) , ( [ ^ ) ] + ) \) / ) ;
287
+ translationArray [ 0 ] = translation [ 1 ] . trim ( ) ;
288
+ translationArray [ 1 ] = translation [ 2 ] . trim ( ) ;
289
+ translationArray [ 2 ] = translation [ 3 ] . trim ( ) ;
290
+ }
291
+
292
+ return translationArray ;
293
+ }
294
+
275
295
/**
276
296
* Returns the transform value of an element.
297
+ * TODO: Convert units when reading values.
277
298
* @param {HTMLElement|Object } element The animatable element.
278
299
* @param {String } propertyKey The key name of the property to animate.
279
300
* @returns {Number }
280
301
*/
281
302
function getElementTransformValue ( element , propertyKey ) {
282
-
283
- // Typically here I've to try reading
303
+ // Typically we've to try reading
284
304
// the value from the style attribute first,
285
- // if there is no value there, I can call the getComputedStyle then :'(
305
+ // if there is no value there, We should call the getComputedStyle then :'(
286
306
var transformationStyleString = element . style . transform ;
287
- if ( ! transformationStyleString ) {
288
- // It seems like getComputedStyle returns
289
- // either the string "none"
307
+ if ( typeof transformationStyleString === "string" && transformationStyleString . length > 0 ) {
308
+ // We're reading value from element style attribute.
309
+ // If we found the property inside the string, we get value.
310
+
311
+ // If it is a translation, we'll
312
+ // retrieve the value from translate3d prop.
313
+ if ( arrayContains ( [ "translateX" , "translateY" , "translateZ" ] , propertyKey ) ) {
314
+ if ( transformationStyleString . indexOf ( 'translate3d' ) !== - 1 ) {
315
+ var translationArray = getTranslationArrayFromString ( transformationStyleString ) ;
316
+ switch ( propertyKey ) {
317
+ case "translateX" : return parseFloat ( translationArray [ 0 ] ) ;
318
+ case "translateY" : return parseFloat ( translationArray [ 1 ] ) ;
319
+ case "translateZ" : return parseFloat ( translationArray [ 2 ] ) ;
320
+ default : break ;
321
+ }
322
+ }
323
+ }
324
+ else {
325
+ var values = transformationStyleString . match ( new RegExp ( propertyKey + "\(([^)]+)\)" ) ) ;
326
+ return Array . isArray ( values ) && values [ 1 ]
327
+ ? parseStringProperty ( values [ 1 ] . substr ( 1 ) ) [ 0 ]
328
+ : arrayContains ( [ "scale" , "scaleX" , "scaleY" ] , propertyKey ) ? 1 : 0 ;
329
+ }
330
+ }
331
+
332
+ else {
333
+ // Let's call getComputedStyle.
334
+ // the function returns either the string "none"
290
335
// or the computed transformation in the form of a matrix.
291
336
//var computedTransformValue = getComputedStyle(element).getPropertyValue("transform");
292
337
if ( arrayContains ( [ "scale" , "scaleX" , "scaleY" ] , propertyKey ) ) return 1 ;
293
- } else {
294
- // We're reading value from element style attribute.
295
- // If we found the property inside the string, we get value.
296
- // EDIT: (Ignore) Else, we try to look for the group property and we remove the value there.
297
- // Else, we're definitely sure that the value is not set and we return 0.
298
- var values = transformationStyleString . match ( new RegExp ( propertyKey + "\(([^)]+)\)" ) ) ;
299
- return Array . isArray ( values ) && values [ 1 ]
300
- ? parseStringProperty ( values [ 1 ] . substr ( 1 ) ) [ 0 ]
301
- : arrayContains ( [ "scale" , "scaleX" , "scaleY" ] , propertyKey ) ? 1 : 0 ;
302
338
}
303
339
304
340
return 0 ;
@@ -320,6 +356,23 @@ function getElementPropertyValue(element, animationType, propertyKey) {
320
356
}
321
357
}
322
358
359
+ /**
360
+ * Apply a translation to the element.
361
+ * @param {HTMLElement } element
362
+ * @param {String } transformationString
363
+ * @param {Array } translationArray
364
+ */
365
+ function applyTheTranslationArray ( element , transformationString , translationArray ) {
366
+ var regex = / t r a n s l a t e 3 d \( ( [ ^ ) ] + ) \) / ;
367
+ var translationString = "translate3d(" + translationArray [ 0 ] + "," + translationArray [ 1 ] + "," + translationArray [ 2 ] + ")" ;
368
+
369
+ element . style . transform =
370
+ transformationString . indexOf ( 'translate3d' ) !== - 1
371
+ ? transformationString . replace ( regex , translationString )
372
+ : transformationString + " " + translationString
373
+ ;
374
+ }
375
+
323
376
/**
324
377
* Set a value to the transformation property.
325
378
* @param {HTMLElement|Object } element The animatable element.
@@ -332,11 +385,30 @@ function setElementTransformValue(element, propertyKey, value) {
332
385
// If the current transformation property key already exist in the string, we just have to replace its value.
333
386
// Else we create a new string and we add it in the transformation string.
334
387
var transformationString = element . style . transform ;
335
- element . style . transform =
336
- transformationString . indexOf ( propertyKey ) !== - 1
337
- ? transformationString . replace ( new RegExp ( propertyKey + "\(([^)]+)\)" ) , propertyKey + "(" + value )
338
- : transformationString + " " + propertyKey + "(" + value + ")"
339
- ;
388
+
389
+ // If we tryng to do a translation, use translate3d rather.
390
+ if ( arrayContains ( [ "translateX" , "translateY" , "translateZ" ] , propertyKey ) ) {
391
+ // Generate the translation array from the transformation string.
392
+ var translationArray = getTranslationArrayFromString ( transformationString ) ;
393
+ // Fill out the array with the current values.
394
+ switch ( propertyKey ) {
395
+ case "translateX" : translationArray [ 0 ] = value ; break ;
396
+ case "translateY" : translationArray [ 1 ] = value ; break ;
397
+ case "translateZ" : translationArray [ 2 ] = value ; break ;
398
+ default : break ;
399
+ }
400
+ // Apply the translation.
401
+ applyTheTranslationArray ( element , transformationString , translationArray ) ;
402
+ }
403
+
404
+ // We're trying to do other transformation than translation.
405
+ else {
406
+ element . style . transform =
407
+ transformationString . indexOf ( propertyKey ) !== - 1
408
+ ? transformationString . replace ( new RegExp ( propertyKey + "\(([^)]+)\)" ) , propertyKey + "(" + value )
409
+ : transformationString + " " + propertyKey + "(" + value + ")"
410
+ ;
411
+ }
340
412
}
341
413
342
414
/**
@@ -473,7 +545,7 @@ function generateAnimations(elements, params) {
473
545
var properties = parseProperties ( params . p ) ;
474
546
var animations = [ ] ;
475
547
var instanceDuration = 0 ;
476
- elements . forEach ( function ( element , index ) {
548
+ forEachIn ( elements ) ( function ( element , index ) {
477
549
// For each property of each element,
478
550
// we create an animation.
479
551
// EDIT: For more control, I need to group transformation animations into one animation.
@@ -486,7 +558,7 @@ function generateAnimations(elements, params) {
486
558
animationTotalDuration > instanceDuration
487
559
? animationTotalDuration
488
560
: instanceDuration ;
489
- Object . keys ( properties ) . forEach ( function ( key ) {
561
+ forEachIn ( Object . keys ( properties ) ) ( function ( key ) {
490
562
var property = { n : key , v : properties [ key ] } ;
491
563
// Get the animation type.
492
564
var animationType = getAnimationType ( key ) ;
@@ -647,12 +719,7 @@ function animate(params) {
647
719
* @param {Number } time The instance time.
648
720
*/
649
721
function setAnimationsProgress ( time ) {
650
- var i = 0 ;
651
- var animations = instance . a ;
652
- var animationsLength = animations . length ;
653
- while ( i < animationsLength ) {
654
- /** @type {AnimationInstance } */
655
- var animation = animations [ i ] ;
722
+ forEachIn ( instance . a ) ( function ( animation ) {
656
723
var delay = animation . v . D ;
657
724
var duration = animation . v . d ;
658
725
var easing = animation . v . e ;
@@ -678,12 +745,11 @@ function animate(params) {
678
745
) ;
679
746
var startValue = animation . p . v . s ;
680
747
var endValue = animation . p . v . e ;
681
- animation . p . v . c = round ( lerp ( startValue , endValue , eased ) , 4 ) ;
748
+ animation . p . v . c = round ( lerp ( startValue , endValue , eased ) , 3 ) ;
682
749
// Set animation value.
683
750
setElementValue ( animation . el , animation . type , animation . p . n , animation . p . v . c , animation . p . v . u ) ;
684
751
}
685
- i ++ ;
686
- }
752
+ } ) ;
687
753
}
688
754
689
755
/**
0 commit comments