Skip to content

Commit

Permalink
Merge pull request #423 from alancutter/easingFunction
Browse files Browse the repository at this point in the history
Don't store Javascript functions on AnimationEffectTiming.easing or keyframe.easing
  • Loading branch information
dstoc committed Apr 5, 2016
2 parents b183b98 + 6a2f700 commit 3ee1ec5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
15 changes: 8 additions & 7 deletions src/keyframe-interpolations.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@
function makeInterpolations(propertySpecificKeyframeGroups) {
var interpolations = [];
for (var groupName in propertySpecificKeyframeGroups) {
var group = propertySpecificKeyframeGroups[groupName];
for (var i = 0; i < group.length - 1; i++) {
var startTime = group[i].offset;
var endTime = group[i + 1].offset;
var startValue = group[i].value;
var endValue = group[i + 1].value;
var keyframes = propertySpecificKeyframeGroups[groupName];
for (var i = 0; i < keyframes.length - 1; i++) {
var startTime = keyframes[i].offset;
var endTime = keyframes[i + 1].offset;
var startValue = keyframes[i].value;
var endValue = keyframes[i + 1].value;
var easing = keyframes[i].easing;
if (startTime == endTime) {
if (endTime == 1) {
startValue = endValue;
Expand All @@ -89,7 +90,7 @@
interpolations.push({
startTime: startTime,
endTime: endTime,
easing: group[i].easing,
easing: shared.toTimingFunction(easing ? easing : 'linear'),
property: groupName,
interpolation: scope.propertyInterpolation(groupName, startValue, endValue)
});
Expand Down
4 changes: 0 additions & 4 deletions src/normalize-keyframes.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,17 +239,13 @@
name: 'NotSupportedError',
message: 'add compositing is not supported'
};
} else if (member == 'easing') {
memberValue = shared.toTimingFunction(memberValue);
} else {
memberValue = '' + memberValue;
}
expandShorthandAndAntiAlias(member, memberValue, keyframe);
}
if (keyframe.offset == undefined)
keyframe.offset = null;
if (keyframe.easing == undefined)
keyframe.easing = shared.toTimingFunction('linear');
return keyframe;
});

Expand Down
4 changes: 2 additions & 2 deletions src/timing-utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
function normalizeTimingInput(timingInput, forGroup) {
timingInput = shared.numericTimingToObject(timingInput);
var timing = makeTiming(timingInput, forGroup);
timing._easing = toTimingFunction(timing.easing);
timing._easingFunction = toTimingFunction(timing.easing);
return timing;
}

Expand Down Expand Up @@ -304,7 +304,7 @@
var currentDirectionIsForwards = timing.direction == 'normal' || timing.direction == (currentIterationIsOdd ? 'alternate-reverse' : 'alternate');
var directedTime = currentDirectionIsForwards ? iterationTime : iterationDuration - iterationTime;
var timeFraction = directedTime / iterationDuration;
return iterationDuration * timing.easing(timeFraction);
return iterationDuration * timing._easingFunction(timeFraction);
}

function calculateTimeFraction(activeDuration, localTime, timing) {
Expand Down
14 changes: 13 additions & 1 deletion test/js/keyframes.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ suite('keyframes', function() {
{left: '0px'}
]);
});
assert.equal('' + normalizedKeyframes[0].easing, 'function (x) { return x; }');
assert.equal(normalizedKeyframes[0].easing, 'easy-peasy');
});

test('Normalize keyframes where some properties are given non-string, non-number values.', function() {
Expand Down Expand Up @@ -441,6 +441,18 @@ suite('keyframes', function() {
assert.equal(interpolations[1].property, 'left');
assert.equal(typeof interpolations[1].interpolation, 'function');
});

test('Make interpolations with invalid easing.', function() {
var interpolations;
assert.doesNotThrow(function() {
interpolations = makeInterpolations(makePropertySpecificKeyframeGroups(normalizeKeyframes([
{left: '0px', easing: 'pants and ducks'},
{left: '200px'},
])));
});
assert.equal(interpolations.length, 1);
assert.equal(interpolations[0].easing.toString(), 'function (x) { return x; }');
});
});

suite('keyframe-interpolations - convertEffectInput', function() {
Expand Down
16 changes: 8 additions & 8 deletions test/js/timing-utilities.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
suite('timing-utilities', function() {
test('normalize timing input', function() {
assert.equal(normalizeTimingInput(1).duration, 1);
assert.equal(normalizeTimingInput(1).easing(0.2), 0.2);
assert.equal(normalizeTimingInput(1)._easingFunction(0.2), 0.2);
assert.equal(normalizeTimingInput(undefined).duration, 0);
});
test('calculating active duration', function() {
Expand Down Expand Up @@ -90,13 +90,13 @@ suite('timing-utilities', function() {
});
test('calculating transformed time', function() {
// calculateTransformedTime(currentIteration, iterationDuration, iterationTime, timingInput);
assert.equal(calculateTransformedTime(4, 1000, 200, {easing: function(x) { return x; }, direction: 'normal'}), 200);
assert.equal(calculateTransformedTime(4, 1000, 200, {easing: function(x) { return x; }, direction: 'reverse'}), 800);
assert.closeTo(calculateTransformedTime(4, 1000, 200, {easing: function(x) { return x * x; }, direction: 'reverse'}), 640, 0.0001);
assert.closeTo(calculateTransformedTime(4, 1000, 600, {easing: function(x) { return x * x; }, direction: 'alternate'}), 360, 0.0001);
assert.closeTo(calculateTransformedTime(3, 1000, 600, {easing: function(x) { return x * x; }, direction: 'alternate'}), 160, 0.0001);
assert.closeTo(calculateTransformedTime(4, 1000, 600, {easing: function(x) { return x * x; }, direction: 'alternate-reverse'}), 160, 0.0001);
assert.closeTo(calculateTransformedTime(3, 1000, 600, {easing: function(x) { return x * x; }, direction: 'alternate-reverse'}), 360, 0.0001);
assert.equal(calculateTransformedTime(4, 1000, 200, {_easingFunction: function(x) { return x; }, direction: 'normal'}), 200);
assert.equal(calculateTransformedTime(4, 1000, 200, {_easingFunction: function(x) { return x; }, direction: 'reverse'}), 800);
assert.closeTo(calculateTransformedTime(4, 1000, 200, {_easingFunction: function(x) { return x * x; }, direction: 'reverse'}), 640, 0.0001);
assert.closeTo(calculateTransformedTime(4, 1000, 600, {_easingFunction: function(x) { return x * x; }, direction: 'alternate'}), 360, 0.0001);
assert.closeTo(calculateTransformedTime(3, 1000, 600, {_easingFunction: function(x) { return x * x; }, direction: 'alternate'}), 160, 0.0001);
assert.closeTo(calculateTransformedTime(4, 1000, 600, {_easingFunction: function(x) { return x * x; }, direction: 'alternate-reverse'}), 160, 0.0001);
assert.closeTo(calculateTransformedTime(3, 1000, 600, {_easingFunction: function(x) { return x * x; }, direction: 'alternate-reverse'}), 360, 0.0001);
});
test('EffectTime', function() {
var timing = normalizeTimingInput({duration: 1000, iterations: 4, iterationStart: 0.5, easing: 'linear', direction: 'alternate', delay: 100, fill: 'forwards'});
Expand Down

0 comments on commit 3ee1ec5

Please sign in to comment.