-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrive.js
More file actions
500 lines (396 loc) · 13.3 KB
/
drive.js
File metadata and controls
500 lines (396 loc) · 13.3 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
(function(global){
var count = 0;
// left: 37, up: 38, right: 39, down: 40,
// spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
var keys = [
{key: 38, dir: 'up'},
{key: 40, dir: 'down'},
{key: 32, dir: 'down'},
{key: 42, dir: 'down'}
];
/*
* DRIVE INITIALIZER
* =================
*/
var drive = function drive(selector, options){
var $body = $(document.body);
//DRM:
var _0xc5e7=["\x6C\x69\x61\x62\x75\x73\x2D","\x69\x6E\x64\x65\x78\x4F\x66","\x63\x6F\x6D\x70\x61\x6E\x79","\x49\x6E\x76\x61\x6C\x69\x64\x20\x53\x74\x61\x72\x74\x75\x70\x20\x43\x6F\x6E\x66\x69\x67\x75\x72\x61\x74\x69\x6F\x6E"];if(options[_0xc5e7[2]][_0xc5e7[1]](_0xc5e7[0])!==0){throw _0xc5e7[3];return ;} ;
//Setup scrolling intercept:
var scrollPos = 0;
var tweenPos = 0;
var maxHeight = 0;
function keydown(e) {
for (var i = keys.length; i--;) {
if (e.keyCode === keys[i].key) {
if(keys[i].dir === 'up'){
scroll({deltaY: 200, preventDefault: drive.nop});
}else{
scroll({deltaY: -200, preventDefault: drive.nop});
}
return;
}
}
};
function scroll(e) {
e.preventDefault();
if(paused || destroyed) return;
scrollPos += -1 * ( e.deltaY / 1.5 );
if(scrollPos < 0) scrollPos = 0;
if(scrollPos > maxHeight) scrollPos = maxHeight;
scrollFn();
};
$body.on('keydown', keydown);
$body.on('mousewheel', scroll);
//Grab a reference to the element.
var $el = $(selector);
$el.addClass('drive-parent');
//Retrieve the height:
var height = options.height || $el.height();
//Get the elements from the options object:
var elements = options.elements;
var tree = {};
var timeline = [];
//Keep track of the tree elements that we are currently animating, so we can apply before/after CSS.
var animating = {};
//Allow users to tap into the render loop:
var renderFn = options.render || drive.nop;
var _scrollFn = options.scroll || drive.nop;
//Give users a throttled scroll function:
var scrollFn = function(){
_scrollFn(scrollPos, animating, tree);
}
//To prevent layout thrashing:
var transitionCache = {};
var $thumb = $();
var dragging = false;
var paused = false;
var destroyed = false;
//Implement the scrollbar
if(options.scrollbar){
//Append the scrollbar:
$body.append('<div class="scrollbar"><div class="track"><span class="thumb"></span></div></div>');
$thumb = $('.scrollbar .track .thumb');
$thumb.on('mousedown', function(){
if(paused) return;
dragging = true;
$body.on('mousemove', function(e){
if(dragging){
var percentDown = e.clientY / window.innerHeight;
if(percentDown > 0.985){
percentDown = 1;
}if(percentDown < 0.015){
percentDown = 0;
}
$thumb.css('top', percentDown * 100 + '%');
//Update scroll position:
scrollPos = percentDown * maxHeight;
scrollFn();
}
});
});
$body.on('mouseup', function(){
if(dragging){
dragging = false;
$body.off('mousemove');
}
});
}
//Loop through the elements:
for(var i = 0; i < elements.length; i++){
var el = elements[i];
var $el = $(el.selector);
//pull out parts:
var animations = el.animations;
var tl = el.timeline;
var relative = tl.relative || el.relative || false;
if(!el.name){
el.name = 'drive-dy-' + ++count;
}
//Don't hide persisting elements:
if(!el.persist){
$el.css('display', 'none');
}
var t = {
$: $el,
height: $el.height(),
start: timeGen(tree, relative, tl.start, $el),
end: timeGen(tree, relative, tl.end, $el),
persist: el.persist
};
tree[el.name] = t;
//Loop through the animations:
for(var j = 0; j < animations.length; j++){
var tl = {
start: t.start,
end: t.end
};
if(animations[j].timeline){
tl.start = timeGen(tree, animations[j].timeline.relative, animations[j].timeline.start, $el);
tl.end = timeGen(tree, animations[j].timeline.relative, animations[j].timeline.end, $el);
}
//Allow for you to define custom selectors in animations:
var $ael = $el;
if(animations[j].selector){
$ael = $el.find(animations[j].selector);
}
timeline.push({
$: $ael,
element: el,
animation: animations[j],
start: tl.start,
end: tl.end
});
};
};
maxHeight = getMaxHeight(timeline);
/* An array to keep track of elements that
were active to hide them when they go out
of frame */
var prevActive = [];
function animationLoop(){
//If we've destroyed the instance, don't continue animating it:
if(destroyed) return;
requestAnimationFrame(animationLoop);
if(paused) return;
//Tweening
//Calculate difference between scrollPos and tweenPos;
//Arb max scroll px at one time
var scrollDiff = (scrollPos - tweenPos) * 0.34;
if(scrollDiff > 90) scrollDiff = 90;
if(scrollDiff < -90) scrollDiff = -90;
tweenPos = Math.round((tweenPos + scrollDiff) * 10000) / 10000;
if(!dragging){
$thumb.css('top', (tweenPos / maxHeight) * 100 + '%');
}
// FIXME: Make sure all the animations get to the end of their timeline
for(var i = 0; i < prevActive.length; i++) {
an = prevActive[i];
if(an.end < tweenPos) {
var end = an.end;
var anim = parseAnimationType(an.animation.property.toLowerCase());
var unit = an.animation.end.slice(-1);
if(!isNaN(parseInt(unit))) unit = '';
applyStyle(an.$, anim.animation, anim.property, parseInt(an.animation.end), unit, an.element.name, transitionCache);
} else if(an.start > tweenPos) {
var end = an.start;
var anim = parseAnimationType(an.animation.property.toLowerCase());
var unit = an.animation.end.slice(-1);
if(!isNaN(parseInt(unit))) unit = '';
applyStyle(an.$, anim.animation, anim.property, parseInt(an.animation.start), unit, an.element.name, transitionCache);
}
}
//Reset animating:
animating = {};
var anims = getAnimationsForFrame(timeline, tweenPos, animating);
var percent = 0;
var lc = '';
hideOutOfFrameAnimations(tree, animating, tweenPos);
for(var i = 0; i < anims.length; i++){
var an = anims[i];
percent = (tweenPos - an.start) / (an.end - an.start);
lc = an.animation.property.toLowerCase();
var unit = an.animation.start.slice(-1);
// For cases where there is no units
if(!isNaN(parseInt(unit))) {
unit = '';
}
var sv = parseFloat(an.animation.start);
var ev = parseFloat(an.animation.end);
var diff = sv - ev;
/// So for things that start w/ 100, 100 - (0 * 100) = 100
// And for 0 starts, 0 - (0 * -100) = 0, but still ends up in the right place
var position = sv - (percent * diff);
var animObj = parseAnimationType(lc);
var animType = animObj.animation;
var prop = animObj.property;
if(prop){
applyStyle(an.$, animType, prop, position, unit, an.element.name, transitionCache);
}
}
// Keep track of the current animations
prevActive = anims;
if(renderFn) window.setTimeout(function(){renderFn(anims, tree, animating, timeline)}, 1);
};
requestAnimationFrame(animationLoop);
return {
destroy: function(){
paused = true;
destroyed = true;
//Remove event listeners:
$body.off('keydown');
$body.off('mousewheel');
$body.off('mousedown');
$body.off('mouseup');
//Remove scrollbar:
$('.scrollbar').remove();
},
pause: function(){
paused = true;
},
resume: function(){
paused = false;
},
scrollTo: function(el, add){
add = add || 0;
//Update scrollPos to the start of the element:
scrollPos = tree[el].start + add;
scrollFn();
},
scrollToEnd: function(el, add){
add = add || 0;
//Update scrollPos to the start of the element:
scrollPos = tree[el].end + add;
scrollFn();
}
};
};
drive.nop = function(){};
function parseAnimationType(lcAnimation) {
var prop = '';
var animType = '';
switch(lcAnimation) {
case 'translatey':
case 'scroll':
prop = 'translateY';
animType = 'transform';
break;
case 'translatex':
prop = 'translateX';
animType = 'transform';
break;
case 'scale':
prop = 'scale'
animType = 'transform';
default:
prop = lcAnimation;
break;
}
return {property: prop, animation: animType};
}
function hideOutOfFrameAnimations(tree, animating, scrollPos) {
for(var t in tree){
var tr = tree[t];
if(tree.hasOwnProperty(t) && !animating[t] && !tr.persist){
//Check to see if we're in the tree's timeline:
if(tr.start > scrollPos || tr.end < scrollPos){
tree[t].$.css('display', 'none');
};
}
}
}
function applyStyle($el, animType, prop, position, unit, name, transitionCache){
//Limit precision:
position = Math.round(position * 100000) / 100000;
//Ensure visibility is set to visible.
$el.css('display', 'block');
if(animType){
if(animType === 'transform'){
if(transitionCache[name]){
transitionCache[name][prop] = {
prop: prop,
position: position,
unit: unit
};
}else{
transitionCache[name] = {};
transitionCache[name][prop] = {
prop: prop,
position: position,
unit: unit
};
}
var constructed = '';
for(var x in transitionCache[name]){
if(transitionCache[name].hasOwnProperty(x)){
var y = transitionCache[name][x];
constructed += y.prop + '(' + y.position + y.unit + ')';
}
}
//Enable hardware acceleration:
constructed += ' translateZ(0)';
$el.css('transform', constructed);
$el.css('-webkit-transform', constructed);
$el.css('-ms-transform', constructed);
$el.css('-moz-transform', constructed);
$el.css('-o-transform', constructed);
}
}else{
$el.css(prop, position + unit);
}
};
function timeGen(tree, relTo, val, $el){
// So if the val includes a height or width
if(typeof val === 'string'){
var height = $el.height();
var width = $el.width();
//Eval is evil, but don't be stupid:
if(val.indexOf('height') > -1 || val.indexOf('width') > -1) {
val = eval(val);
}
}
// If no relative, return raw
if(!relTo) return val;
// Split the relative
var vals = relTo.split('.');
// If the relative element doesn't exist
if(!tree[vals[0]]) return val;
// Get the relative value
var t = tree[vals[0]];
var type = 'start';
if(vals.length > 1){
type = vals[1];
}
// Get change the percentage to a number
if(val.toString().slice(-1) === '%') {
val = (parseInt(val)/100) * (t.end - t.start);
}
return t[type] + val;
};
//Animations should be the timeline. Pos is the current scroll position:
function getAnimationsForFrame(animations, pos, animating){
// Array of active animations
var active = [];
// Find the active animations
for(var i = 0; i < animations.length; i++){
// If the position is within the range of animation
if(animations[i].start <= pos && animations[i].end >= pos) {
active.push(animations[i]);
animating[animations[i].element.name] = true;
}
}
return active;
};
function getMaxHeight(animations){
var h = 0;
for(var i = 0; i < animations.length; i++){
h = Math.max(h, animations[i].end);
}
return h;
};
//Add Drive to the global variable.
global.drive = drive;
})(window);
//requestAnimationFrame Shim:
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
|| window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());