-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathso_animation.js
executable file
·211 lines (172 loc) · 6.48 KB
/
so_animation.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* @package so
* @object so.animation
* @depends so, so.util, so.dom
* @author Kerem Güneş
* @license The MIT License <https://opensource.org/licenses/MIT>
*/
;(function ($, NULL, TRUE, FALSE) { 'use strict';
var $win = $.win();
var $toStyleName = $.util.toStyleName;
var $easing = ($.ext && $.ext.easing) || {};
var $extend = $.extend, $for = $.for, $forEach = $.forEach, $float = $.float, $now = $.now,
$isNumber = $.isNumber, $isString = $.isString, $isFunction = $.isFunction;
var re_digit = /^[\d.]+/;
var re_scroll = /scroll(?:Top|Left)/;
var re_nonUnitStyles = /(?:(?:fill-?)?opacity|z(?:oom|index)|(?:font-?w|line-?h)eight|column(?:-?count|s))/i;
var opt_fps = 1000 / 60;
var opt_speeds = {fast: 50, slow: 650, ease: 255, normal: 150, default: 325};
// thanks: http://easings.net/ (easeOutQuad)
var fn_easing = function (t,b,c,d) { return -c*(t/=d)*(t-2)+b; };
var fn_runner = $win.requestAnimationFrame || function (fn) { $win.setTimeout(fn, opt_fps); };
/**
* Animation.
* @param {Element} el
* @param {Object} properties
* @param {Int|Function} speed?
* @param {String|Function} easing?
* @param {Function} callback?
*/
function Animation(el, properties, speed, easing, callback) {
var _this = this; // just as minify candy
_this.$dom = $(el);
_this.properties = properties;
// swap arguments
if ($isFunction(speed)) {
callback = speed, speed = NULL;
} else if ($isFunction(easing)) {
callback = easing, easing = NULL;
}
_this.speed = $isNumber(speed) ? speed : opt_speeds[speed] || opt_speeds.default;
_this.easing = easing ? $easing[easing] || fn_easing : NULL;
_this.callback = callback ? function () { callback(_this) } : NULL;
_this.running = _this.stopped = _this.ended = FALSE;
_this.startTime = _this.elapsedTime = 0;
_this.tasks = [];
if (_this.$dom.len()) {
// for stop tool
_this.$dom.set('$animation', _this);
// assign animation tasks
$forEach(properties, function (name, value) {
var scroll, startValue, endValue, diff, style, unit = '';
name = $toStyleName(name);
scroll = re_scroll.test(name);
if (!scroll) {
style = $isString(value)
? _this.$dom.getCssStyles(name)[name] // get original style to catch unit sign
: _this.$dom.getComputedStyles(name)[name];
startValue = $float(style);
endValue = $float(value);
if (!re_nonUnitStyles.test(name)) {
unit = style.remove(re_digit);
}
} else {
startValue = _this.$dom.scroll()[name.slice(6).lower()];
endValue = value;
}
diff = Math.abs(endValue - startValue);
// no need to get excited
if (!diff) return;
_this.tasks.push({
name: name,
scroll: scroll,
startValue: startValue,
endValue: endValue,
reverse: startValue > endValue,
diff: diff,
unit: unit
});
});
}
}
$extend(Animation.prototype, {
/**
* Run.
* @return {this}
*/
run: function () {
var _this = this;
_this.stop(); // stop if running
_this.running = TRUE;
_this.startTime = $now();
!function run() {
if (!_this.$dom.len()) {
// no element(s) to animate
return (_this.running = FALSE, _this.stopped = _this.ended = TRUE);
}
if (!_this.stopped && !_this.ended) {
if (_this.elapsedTime < _this.speed) {
fn_runner(run);
_this.start();
} else {
_this.end();
_this.stop();
}
}
}();
return _this;
},
/**
* Start.
* @return {this}
*/
start: function () {
var _this = this, $dom = _this.$dom, scroll, value;
_this.elapsedTime = $now() - _this.startTime;
$for(_this.tasks, function (task) {
value = fn_easing(_this.elapsedTime, 0.00, task.diff, _this.speed);
value = task.reverse ? task.startValue - value : task.startValue + value;
if (!task.scroll) {
$dom.setStyle(task.name, value.toFixed(9) /* use 'toFixed' for a good percent */
+ task.unit);
} else {
$dom.set(task.name, value.toFixed(0));
}
});
return _this;
},
/**
* End.
* @return {this}
*/
end: function () {
var _this = this, $dom = _this.$dom;
$for(_this.tasks, function (task) {
if (!task.scroll) {
$dom.setStyle(task.name, task.endValue + task.unit);
} else {
$dom.set(task.name, task.endValue);
}
});
_this.ended = TRUE;
if ($isFunction(_this.callback)) {
_this.callback(_this);
}
return _this;
},
/**
* Stop.
* @return {this}
*/
stop: function () {
var _this = this, $dom = _this.$dom;
if (_this.running) {
_this.running = FALSE, _this.stopped = TRUE;
}
// set as null (for stop, animated() etc.)
$dom.set('$animation', NULL);
return _this;
}
});
// shortcut
function initAnimation(el, properties, speed, easing, callback) {
return new Animation(el, properties, speed, easing, callback);
}
// add animation to so
$.animation = {
Animation: initAnimation,
animate: function (el, properties, speed, easing, callback) {
return initAnimation(el, properties, speed, easing, callback).run();
}
};
})(window.so, null, true, false);