-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbase.js
74 lines (63 loc) · 1.85 KB
/
base.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
var TweenMax = require('gsap')
var assign = require('object-assign')
module.exports = function(Promise) {
Promise.config({
cancellation: true
})
function animateFunc(func, element, duration, opts) {
opts = assign({}, opts)
var tween
return new Promise(function(resolve, reject, onCancel) {
opts.onComplete = resolve
tween = func(element, duration, opts)
onCancel && onCancel(function(){
tween.kill()
})
})
}
var animateTo = animateFunc.bind(null, TweenMax.to)
var util = animateTo
util.to = animateTo
util.from = animateFunc.bind(null, TweenMax.from)
util.set = function animateSet(element, params) {
params = assign({}, params)
return new Promise(function(resolve, reject) {
params.onComplete = resolve
TweenMax.set(element, params)
})
}
util.fromTo = function animateFromTo(element, duration, from, to) {
to = assign({}, to)
var tween
return new Promise(function(resolve, reject, onCancel) {
to.onComplete = resolve
tween = TweenMax.fromTo(element, duration, from, to)
onCancel && onCancel(function(){
tween.kill()
})
})
}
;['staggerTo', 'staggerFrom'].forEach(function(fn) {
var tweenFunc = TweenMax[fn]
var tweens
util[fn] = function(element, duration, from, stagger) {
return new Promise(function(resolve, reject, onCancel) {
tweens = tweenFunc(element, duration, from, stagger, resolve)
onCancel && onCancel(function(){
tweens.forEach( function (tween) { tween.kill() })
})
})
}
})
util.staggerFromTo = function staggerFromTo(element, duration, from, to, stagger, position) {
var tweens
return new Promise(function(resolve, reject, onCancel) {
tweens = TweenMax.staggerFromTo(element, duration, from, to, stagger, resolve)
onCancel && onCancel(function(){
tweens.forEach( function (tween) { tween.kill() })
})
})
}
util.all = Promise.all
return util
}