forked from fgnass/spin.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spin.js
295 lines (268 loc) · 8.3 KB
/
spin.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//fgnass.github.com/spin.js
(function(window, document, undefined) {
/**
* Copyright (c) 2011 Felix Gnass [fgnass at neteye dot de]
* Licensed under the MIT license
*
* Unfortunately uglify.js doesn't provide an option to de-duplicate strings
* or to use string-based property access. Hence we have to manually define
* some string constants in order to keep file-size below our 3K limit, as
* one of the design goals was to create a script that is smaller than an
* animated GIF.
*/
var width = 'width',
length = 'length',
radius = 'radius',
lines = 'lines',
trail = 'trail',
color = 'color',
opacity = 'opacity',
speed = 'speed',
shadow = 'shadow',
style = 'style',
height = 'height',
left = 'left',
top = 'top',
px = 'px',
childNodes = 'childNodes',
firstChild = 'firstChild',
parentNode = 'parentNode',
position = 'position',
relative = 'relative',
absolute = 'absolute',
animation = 'animation',
transform = 'transform',
Origin = 'Origin',
Timeout = 'Timeout',
coord = 'coord',
black = '#000',
styleSheets = style + 'Sheets',
prefixes = "webkit0Moz0ms0O".split(0), /* Vendor prefixes, separated by zeros */
animations = {}, /* Animation rules keyed by their name */
useCssAnimations;
/**
*
*/
function eachPair(args, it) {
var end = ~~((args[length]-1)/2);
for (var i = 1; i <= end; i++) {
it(args[i*2-1], args[i*2]);
}
}
/**
* Utility function to create elements. If no tag name is given, a DIV is created.
*/
function createEl(tag) {
var el = document.createElement(tag || 'div');
eachPair(arguments, function(prop, val) {
el[prop] = val;
});
return el;
}
function ins(parent, child1, child2) {
if(child2 && !child2[parentNode]) ins(parent, child2);
parent.insertBefore(child1, child2||null);
return parent;
}
/**
* Insert a new stylesheet to hold the @keyframe or VML rules.
*/
ins(document.getElementsByTagName('head')[0], createEl(style));
var sheet = document[styleSheets][document[styleSheets][length] - 1];
/**
* Creates an opacity keyframe animation rule.
*/
function addAnimation(to, end) {
var name = [opacity, end, ~~(to*100)].join('-'),
dest = '{' + opacity + ':' + to + '}',
i;
if (!animations[name]) {
for (i=0; i<prefixes[length]; i++) {
try {
sheet.insertRule('@' +
(prefixes[i] && '-'+prefixes[i].toLowerCase() +'-' || '') +
'keyframes ' + name + '{0%{' + opacity + ':1}' +
end + '%' + dest + 'to' + dest + '}', sheet.cssRules[length]);
}
catch (err) {
}
}
animations[name] = 1;
}
return name;
}
/**
* Tries various vendor prefixes and returns the first supported property.
**/
function vendor(el, prop) {
var s = el[style],
pp,
i;
if(s[prop] !== undefined) return prop;
prop = prop.charAt(0).toUpperCase() + prop.slice(1);
for(i=0; i<prefixes[length]; i++) {
pp = prefixes[i]+prop;
if(s[pp] !== undefined) return pp;
}
}
/**
* Sets multiple style properties at once.
*/
function css(el) {
eachPair(arguments, function(n, val) {
el[style][vendor(el, n)||n] = val;
});
return el;
}
/**
* Fills in default values. The values are passed as argument pairs rather
* than as object in order to save some extra bytes.
*/
function defaults(obj) {
eachPair(arguments, function(prop, val) {
if (obj[prop] === undefined) obj[prop] = val;
});
return obj;
}
/** The constructor */
var Spinner = function Spinner(o) {
this.opts = defaults(o || {},
lines, 12,
trail, 100,
length, 7,
width, 5,
radius, 10,
color, black,
opacity, 1/4,
speed, 1);
},
proto = Spinner.prototype = {
spin: function(target) {
var self = this,
el = self.el = self[lines](self.opts);
if (target) {
ins(target, css(el,
left, ~~(target.offsetWidth/2) + px,
top, ~~(target.offsetHeight/2) + px
), target[firstChild]);
}
if (!useCssAnimations) {
// No CSS animation support, use setTimeout() instead
var o = self.opts,
i = 0,
f = 20/o[speed],
ostep = (1-o[opacity])/(f*o[trail] / 100),
astep = f/o[lines];
(function anim() {
i++;
for (var s=o[lines]; s; s--) {
var alpha = Math.max(1-(i+s*astep)%f * ostep, o[opacity]);
self[opacity](el, o[lines]-s, alpha, o);
}
self[Timeout] = self.el && window['set'+Timeout](anim, 50);
})();
}
return self;
},
stop: function() {
var self = this,
el = self.el;
window['clear'+Timeout](self[Timeout]);
if (el[parentNode]) el[parentNode].removeChild(el);
self.el = undefined;
return self;
}
};
proto[lines] = function(o) {
var el = css(createEl(), position, relative),
animationName = addAnimation(o[opacity], o[trail]),
i = 0,
seg;
function fill(color, shadow) {
return css(createEl(),
position, absolute,
width, (o[length]+o[width]) + px,
height, o[width] + px,
'background', color,
'boxShadow', shadow,
transform + Origin, left,
transform, 'rotate(' + ~~(360/o[lines]*i) + 'deg) translate(' + o[radius]+px +',0)',
'borderRadius', '100em'
);
}
for (; i < o[lines]; i++) {
seg = css(createEl(),
position, absolute,
top, 1+~(o[width]/2) + px,
transform, 'translate3d(0,0,0)',
animation, animationName + ' ' + 1/o[speed] + 's linear infinite ' + (1/o[lines]/o[speed]*i - 1/o[speed]) + 's'
);
if (o[shadow]) ins(seg, css(fill(black, '0 0 4px ' + black), top, 2+px));
ins(el, ins(seg, fill(o[color], '0 0 1px rgba(0,0,0,.1)')));
}
return el;
};
proto[opacity] = function(el, i, val) {
el[childNodes][i][style][opacity] = val;
};
///////////////////////////////////////////////////////////////////////////////
// VML rendering for IE
///////////////////////////////////////////////////////////////////////////////
var behavior = 'behavior',
URL_VML = 'url(#default#VML)',
tag = 'group0roundrect0fill0stroke'.split(0);
/**
* Check and init VML support
*/
(function() {
var s = css(createEl(tag[0]), behavior, URL_VML),
i;
if (!vendor(s, transform) && s.adj) {
// VML support detected. Insert CSS rules for group, shape and stroke.
for (i=0; i < tag[length]; i++) {
sheet.addRule(tag[i], behavior + ':' + URL_VML);
}
proto[lines] = function() {
var o = this.opts,
r = o[length]+o[width],
s = 2*r;
function grp() {
return css(createEl(tag[0], coord+'size', s +' '+s, coord+Origin, -r + ' ' + -r), width, s, height, s);
}
var g = grp(),
margin = ~(o[length]+o[radius]+o[width])+px,
i;
function seg(i, dx, filter) {
ins(g,
ins(css(grp(), 'rotation', 360 / o[lines] * i + 'deg', left, ~~dx),
ins(css(createEl(tag[1], 'arcsize', 1), width, r, height, o[width], left, o[radius], top, -o[width]/2, 'filter', filter),
createEl(tag[2], color, o[color], opacity, o[opacity]),
createEl(tag[3], opacity, 0) // transparent stroke to fix color bleeding upon opacity change
)
)
);
}
if (o[shadow]) {
for (i = 1; i <= o[lines]; i++) {
seg(i, -2, 'progid:DXImage'+transform+'.Microsoft.Blur(pixel'+radius+'=2,make'+shadow+'=1,'+shadow+opacity+'=.3)');
}
}
for (i = 1; i <= o[lines]; i++) {
seg(i);
}
return ins(css(createEl(),
'margin', margin + ' 0 0 ' + margin,
position, relative
), g);
};
proto[opacity] = function(el, i, val, o) {
o = o[shadow] && o[lines] || 0;
el[firstChild][childNodes][i+o][firstChild][firstChild][opacity] = val;
};
}
else {
useCssAnimations = vendor(s, animation);
}
})();
window.Spinner = Spinner;
})(window, document);