-
Notifications
You must be signed in to change notification settings - Fork 1
/
snap-animation-queue.js
148 lines (130 loc) · 3.84 KB
/
snap-animation-queue.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*!
* Snap Animation Queue
* v0.1.0
* (c) Nov 14, 2016. Mystro Ken <[email protected]>
*/
/**
*
* @constructor
*/
function SnapAnimationQueue() {
this.animationQueue = [];
this._totalDuration = 0;
this._startTime = null;
this._lapsedTime = null;
this._timerForComplete = null;
this.onCompleteCallback = null;
this.defaultOptions = {
easing: mina.easeinout,
delay: 0,
callback: null
};
}
/**
* Adding a new animation to the queue
* @param elem Snap
* @param duration {int}
* @param attrs {object}
* @param easing {null|easing}
* @param delay {null|int}
* @param callbackFn
* @returns {SnapAnimationQueue}
*/
SnapAnimationQueue.prototype.add = function (elem, duration, attrs, easing, delay, callbackFn) {
if(!elem || typeof attrs != 'object') return this;
easing = easing || this.defaultOptions.easing;
var newDelay;
if(typeof delay == 'string')
{
var operator = delay.substring(0,2);
if(operator == '-=') {
newDelay = parseFloat(this._totalDuration) - parseFloat(delay.substring(2));
newDelay = (newDelay < 0) ? 0 : newDelay;
}
else if(operator == '+=') {
newDelay = parseFloat(this._totalDuration) + parseFloat(delay.substring(2));
}
else {
newDelay = parseFloat(delay);
}
}
else if(typeof delay == 'undefined') {
newDelay = this._totalDuration;
}
else {
newDelay = delay;
}
var timeout = window.setTimeout(function(){
elem.animate(attrs, duration, easing, callbackFn);
},newDelay);
this._computeDuration(duration, delay);
this.animationQueue.push({
elem: elem,
duration: duration,
startingTime: newDelay,
timeout: timeout
});
//console.log(this._lapsedTime);
return this;
};
/**
* Compute the total duration of the queue
* @param duration
* @param delay
* @private
*/
SnapAnimationQueue.prototype._computeDuration = function (duration, delay) {
// If it's a first add
if(!this._startTime) {
this._startTime = Date.now();
}
this._lapsedTime = Date.now() - this._startTime;
var newDuration;
if(typeof delay == 'string')
{
var operator = delay.substring(0,2);
if(operator == '-=') {
newDuration = parseFloat(this._totalDuration) - parseFloat(delay.substring(2));
newDuration = (newDuration < 0) ? 0 : newDuration;
newDuration += duration;
}
else if(operator == '+=') {
newDuration = parseFloat(this._totalDuration) + parseFloat(delay.substring(2));
newDuration += duration;
}
else {
newDuration = Math.max(parseFloat(delay)+duration, this._totalDuration);
}
}
else if(typeof delay == 'undefined') {
newDuration = this._totalDuration + duration;
}
else {
newDuration = Math.max(parseFloat(delay)+duration, this._totalDuration);
}
this._totalDuration = newDuration;
var leavingTime = Math.abs(this._totalDuration - this._lapsedTime);
this._setOnCompleteTimer(leavingTime);
};
/**
* Intern setting when animations ended
*/
SnapAnimationQueue.prototype.whenAnimationsEnded = function () {
this.animationQueue = [];
this._totalDuration = 0;
this._startTime = null;
this._lapsedTime = null;
this._timerForComplete = null;
this.onCompleteCallback = null;
};
SnapAnimationQueue.prototype._setOnCompleteTimer = function (delay) {
var self = this;
if(this._timerForComplete) {
window.clearTimeout(this._timerForComplete);
this._timerForComplete= null;
}
this._timerForComplete = window.setTimeout(function(){
if(typeof self.onCompleteCallback == 'function') self.onCompleteCallback(self);
self.whenAnimationsEnded();
}, delay);
};