-
Notifications
You must be signed in to change notification settings - Fork 18
/
Cquence.js
165 lines (144 loc) · 5.15 KB
/
Cquence.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
var Cq;
Cq = function(){
var raf = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || function( f ){ setTimeout( f, 1000/60 ); };
var elem = function( id ){ return document.getElementById( id ); };
// IE checking
var IEVERSION = getInternetExplorerVersion();
var IE8 = IEVERSION === 8;
var IEWTF = IEVERSION < 8 && IEVERSION > -1;
var style = function( e, k, v ){
if( k === 'opacity' ){
if( IE8 ){ // gemene browsers
e.style[ "-ms-filter" ] = "progid:DXImageTransform.Microsoft.Alpha(Opacity=" + Math.floor( v * 100 ) + ")";
e.style.filter = "alpha(opacity=" + Math.floor( v * 100 ) + ")";
} else { // lieve browsers
e.style[k] = v;
}
} else {
e.style[k] = v + "px";
}
};
var combine = function(){
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments
var list = Array.prototype.slice.call( arguments, 0 );
var d = 0;
for( var i=0; i<list.length; i++ ){
d = Math.max( list[i].d, d );
}
return {
d: d,
f: function( t ){
for( var i=0; i<list.length; i++ ){
var last = list[i];
if( last.d > t ){
last.f( t );
} else {
if( !last.done ){
last.f( last.d );
last.done = true;
}
}
}
}
};
};
var sequence = function(){
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments
var timeline = Array.prototype.slice.call( arguments, 0 );
var d = 0;
for( var i=0; i<timeline.length; i++ ){
d = d + timeline[i].d;
}
return {
d: d,
f: function( t ){
var last = null;
var total = 0;
for( var i=0; i<timeline.length; i++ ){
last = timeline[i];
if( total + last.d > t ){
last.f( t - total );
return;
}
if( !last.done ){
last.f( last.d );
last.done = true;
}
total = total + last.d;
}
}
};
};
var animate = function( transform ){
return function( id, dur, begin, fin ){
return {
d: dur,
f: function( t ){
var e = elem( id );
for( var k in begin ){
var bv = begin[k]; // begin waarde
var fv = fin[k]; // finish waarde
var dx = fv - bv; // verschil (afstand)
// p is nu een waarde tussen de 0 en 1 .. en geeft aan hoe ver de animatie is
var p = transform( Math.max( t/dur, 0 ) );
// nieuwe waarde is de factor maal de afstand
var nv = bv + (p * dx);
style( e, k, nv );
}
}
};
};
};
// animation variants
// http://upshots.org/actionscript/jsas-understanding-easing
var linear = animate( function( p ){
return p;
});
var easeIn = animate( function( p ){
return Math.pow( p, 5 );
});
var easeOut = animate( function( p ){
return 1 - Math.pow( 1 - p, 5 );
});
var sleep = function( d ){
return { d: d, f: function(t){} };
};
/* from: http://stackoverflow.com/questions/10964966/detect-ie-version-in-javascript */
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) !== null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}
var timeline = [];
var start = (+new Date()); // unix timestamp
var second = 1000;
var stop = 15 * second;
var render = null;
// gets called recursive by request-animation-frame
var renderloop = function(){
// console.log('1. ', window.render );
var now = (+new Date()) - start; // relatieve tijd vanaf begin animatie
if( now < stop ) raf( renderloop ); // recursion, baby
// console.log('2. ', window.render );
if( window.render ) window.render.f( now );
};
return {
combine: combine,
sequence: sequence,
linear: linear,
animate: animate,
easeIn: easeIn,
easeOut: easeOut,
sleep: sleep,
renderloop: renderloop
};
}();