1 // Copyright 2014 Google Inc. All rights reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 (function(shared, scope, testing) {
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));
34 for (var property in propertySpecificKeyframeGroups)
35 if (property != 'offset' && property != 'easing' && property != 'composite')
36 scope.clear(target, property);
42 function makePropertySpecificKeyframeGroups(keyframeEffect) {
43 var propertySpecificKeyframeGroups = {};
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]
53 propertySpecificKeyframeGroups[member] = propertySpecificKeyframeGroups[member] || [];
54 propertySpecificKeyframeGroups[member].push(propertySpecificKeyframe);
59 for (var groupName in propertySpecificKeyframeGroups) {
60 var group = propertySpecificKeyframeGroups[groupName];
61 if (group[0].offset != 0 || group[group.length - 1].offset != 1) {
63 type: DOMException.NOT_SUPPORTED_ERR,
64 name: 'NotSupportedError',
65 message: 'Partial keyframes are not supported'
69 return propertySpecificKeyframeGroups;
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) {
84 startValue = endValue;
86 endValue = startValue;
92 easing: group[i].easing,
94 interpolation: scope.propertyInterpolation(groupName, startValue, endValue)
98 interpolations.sort(function(leftInterpolation, rightInterpolation) {
99 return leftInterpolation.startTime - rightInterpolation.startTime;
101 return interpolations;
105 if (WEB_ANIMATIONS_TESTING) {
106 testing.makePropertySpecificKeyframeGroups = makePropertySpecificKeyframeGroups;
107 testing.makeInterpolations = makeInterpolations;
110 })(webAnimationsShared, webAnimations1, webAnimationsTesting);