-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (57 loc) · 1.67 KB
/
index.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
/**
* Animate specific functions inside the TweenModules, like TweenLite.to
*
* @param {Function} func - TweenModule.to
* @param {DOMElement} element - DOM Element
* @param {number} duration - Duration
* @param {Object} opts - Paramaters
* @returns
*/
function _animateFunc(func, element, duration, opts) {
opts = Object.assign({}, opts);
var tween;
return new Promise(function(resolve, reject, onCancel) {
opts.onComplete = resolve;
tween = func(element, duration, opts);
onCancel &&
onCancel(function() {
tween.kill();
});
});
}
/**
* Get a wrapper of GSAP Tween
*
* @param {Promise} Promise - Promise framework
* @param {TweenModule} TweenModule - TweenMax or TweenLite
* @returns {Object} GSAP Promisified
*/
function animate(Promise, TweenModule) {
var animateTo = _animateFunc.bind(null, TweenModule.to);
var util = animateTo;
util.to = animateTo;
util.from = _animateFunc.bind(null, TweenModule.from);
util.set = function animateSet(element, params) {
params = Object.assign({}, params);
return new Promise(function(resolve, reject) {
params.onComplete = resolve;
TweenModule.set(element, params);
});
};
util.fromTo = function animateFromTo(element, duration, from, to) {
to = Object.assign({}, to);
var tween;
return new Promise(function(resolve, reject, onCancel) {
to.onComplete = resolve;
tween = TweenModule.fromTo(element, duration, from, to);
onCancel &&
onCancel(function() {
tween.kill();
});
});
};
util.killTweensOf = TweenModule.killTweensOf.bind(TweenModule);
util.all = Promise.all;
return util;
}
module.exports = animate;