Home | History | Annotate | Download | only in src
      1 // Copyright 2014 Google Inc. All rights reserved.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 //     You may obtain a copy of the License at
      6 //
      7 // http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 //     See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 (function(shared, scope, testing) {
     16 
     17   scope.convertEffectInput = function(effectInput) {
     18     var keyframeEffect = shared.normalizeKeyframes(effectInput);
     19     var propertySpecificKeyframeGroups = makePropertySpecificKeyframeGroups(keyframeEffect);
     20     var interpolations = makeInterpolations(propertySpecificKeyframeGroups);
     21     return function(target, fraction) {
     22       if (fraction != null) {
     23         interpolations.filter(function(interpolation) {
     24           return (fraction <= 0 && interpolation.startTime == 0) ||
     25                  (fraction >= 1 && interpolation.endTime == 1) ||
     26                  (fraction >= interpolation.startTime && fraction <= interpolation.endTime);
     27         }).forEach(function(interpolation) {
     28           var offsetFraction = fraction - interpolation.startTime;
     29           var localDuration = interpolation.endTime - interpolation.startTime;
     30           var scaledLocalTime = localDuration == 0 ? 0 : interpolation.easing(offsetFraction / localDuration);
     31           scope.apply(target, interpolation.property, interpolation.interpolation(scaledLocalTime));
     32         });
     33       } else {
     34         for (var property in propertySpecificKeyframeGroups)
     35           if (property != 'offset' && property != 'easing' && property != 'composite')
     36             scope.clear(target, property);
     37       }
     38     };
     39   };
     40 
     41 
     42   function makePropertySpecificKeyframeGroups(keyframeEffect) {
     43     var propertySpecificKeyframeGroups = {};
     44 
     45     for (var i = 0; i < keyframeEffect.length; i++) {
     46       for (var member in keyframeEffect[i]) {
     47         if (member != 'offset' && member != 'easing' && member != 'composite') {
     48           var propertySpecificKeyframe = {
     49             offset: keyframeEffect[i].offset,
     50             easing: keyframeEffect[i].easing,
     51             value: keyframeEffect[i][member]
     52           };
     53           propertySpecificKeyframeGroups[member] = propertySpecificKeyframeGroups[member] || [];
     54           propertySpecificKeyframeGroups[member].push(propertySpecificKeyframe);
     55         }
     56       }
     57     }
     58 
     59     for (var groupName in propertySpecificKeyframeGroups) {
     60       var group = propertySpecificKeyframeGroups[groupName];
     61       if (group[0].offset != 0 || group[group.length - 1].offset != 1) {
     62         throw {
     63           type: DOMException.NOT_SUPPORTED_ERR,
     64           name: 'NotSupportedError',
     65           message: 'Partial keyframes are not supported'
     66         };
     67       }
     68     }
     69     return propertySpecificKeyframeGroups;
     70   }
     71 
     72 
     73   function makeInterpolations(propertySpecificKeyframeGroups) {
     74     var interpolations = [];
     75     for (var groupName in propertySpecificKeyframeGroups) {
     76       var group = propertySpecificKeyframeGroups[groupName];
     77       for (var i = 0; i < group.length - 1; i++) {
     78         var startTime = group[i].offset;
     79         var endTime = group[i + 1].offset;
     80         var startValue = group[i].value;
     81         var endValue = group[i + 1].value;
     82         if (startTime == endTime) {
     83           if (endTime == 1) {
     84             startValue = endValue;
     85           } else {
     86             endValue = startValue;
     87           }
     88         }
     89         interpolations.push({
     90           startTime: startTime,
     91           endTime: endTime,
     92           easing: group[i].easing,
     93           property: groupName,
     94           interpolation: scope.propertyInterpolation(groupName, startValue, endValue)
     95         });
     96       }
     97     }
     98     interpolations.sort(function(leftInterpolation, rightInterpolation) {
     99       return leftInterpolation.startTime - rightInterpolation.startTime;
    100     });
    101     return interpolations;
    102   }
    103 
    104 
    105   if (WEB_ANIMATIONS_TESTING) {
    106     testing.makePropertySpecificKeyframeGroups = makePropertySpecificKeyframeGroups;
    107     testing.makeInterpolations = makeInterpolations;
    108   }
    109 
    110 })(webAnimationsShared, webAnimations1, webAnimationsTesting);
    111