forked from pleasetrythisathome/react.animate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreact.animate.js
107 lines (88 loc) · 2.93 KB
/
react.animate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['underscore', 'react', 'ease'], factory);
} else {
// Browser globals
root.amdWeb = factory(root._, root.React, root.Ease);
}
}(this, function (_, React, Ease) {
var requestAnimationFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame;
var animator = function() {
var date = (new Date).getTime();
var alpha = (date - this.startTime) / this.duration;
alpha = alpha > 1 ? 1 : alpha;
var newState = {};
var easeFunc = Ease[this.ease];
for (var i in this.startState) {
newState[i] = this.startState[i] + (this.endState[i] - this.startState[i]) * easeFunc(alpha);
}
this.component.setState(newState);
if (alpha >= 1) {
for (var i in this.startState) {
delete this.component._reactAnimations[i];
}
this.callback();
} else
this.animation = requestAnimationFrame(animator.bind(this));
};
var removeAnimatorProperty = function(animator, property) {
if (animator.startState[property] === undefined)
return;
delete animator.startState[property];
delete animator.endState[property];
if (_.keys(animator.startState).length < 1) {
animator.callback();
}
};
React.Animate = {
animate: function() {
// Default parameters
var anim = {
startTime: (new Date()).getTime(),
animation: null,
startState: {},
endState: {},
duration: 500,
ease: "cubic-in-out",
callback: _.identity,
component: this,
};
// Parameter parsing
var argIter = 0;
if (_.isObject(arguments[argIter])) {
anim.endState = arguments[argIter++];
} else {
anim.endState = _.object([arguments[argIter],
arguments[argIter + 1]]);
argIter += 2;
}
if (_.isNumber(arguments[argIter])) {
anim.duration = arguments[argIter++];
}
if (_.isString(arguments[argIter])) {
anim.ease = arguments[argIter++];
}
if (_.isFunction(arguments[argIter])) {
anim.callback = arguments[argIter++];
}
// Save the animation object on the animated component
if (!this._reactAnimations)
this._reactAnimations = {};
for (var i in anim.endState) {
anim.startState[i] = (this.state[i] !== undefined ?
this.state[i] : this.endState[i]);
// Stop currently running animation for a given property
if (this._reactAnimations[i] !== undefined)
removeAnimatorProperty(this._reactAnimations[i], i);
this._reactAnimations[i] = anim;
}
// Kick the animation
anim.animator = requestAnimationFrame(animator.bind(anim));
}
};
return React;
}));