-
Notifications
You must be signed in to change notification settings - Fork 0
/
arto-effect.js
688 lines (491 loc) · 24.3 KB
/
arto-effect.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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/**
* Created by ArtoCreative on 25.06.2016.
*/
/* global jQuery:{} */
/* global artoEvents:{} */
var artoEffect;
(function( $, _ ) {
'use strict';
artoEffect = {
// the bunch of artoEffect items
items: [],
// the current request animation frame id
rAFIndex: 0,
// flag used to not call 'requestAnimationFrame' when it's steel running
animation_running: false,
item: function() {
// the computed percent value of the jquery object in the viewport
// - 0 when the top of object enters into the viewport
// - 100 when the bottom of the object goes outside of the viewport
this.percent_value = 0;
// the animation callback function
this.animation_callback = null;
// the jquery object of the artoEffect.item
this.jqueryObj = '';
// optional - a jquery object that wraps the current item. Used in callback
this.wrapper_jquery_obj = undefined;
// a jquery span obj added dynamically added at the top of jqueryObj
this.top_pointer_jquery_obj = '';
// the full outer height of the item
this.full_height = 0;
// the offset top of the top_pointer_jquery_obj
this.offset_top = '';
// the offset top of the top_pointer_jquery_obj and the full_height
this.offset_bottom_top = '';
// the properties registered with the item
this.properties = {};
// the computed properties that probably will be applied by animation callback function over the jquery object
this.computed_item_properties = {};
// flag made 'true' for items having at least one computed property
this.redraw = false;
// top is out of screen
this.top_is_out = false;
// flag used to mark the initialization item
this._is_initialized = false;
// flag used to stop an item to be computed
this.computation_stopped = false;
/**
* - when a new item property is added, it's added as a real property in the item.properties object.
* - if it's already added, the settings of the property are appended
* - the settings for an item property must be added in order of the percents
* - the percent intervals must not be overloaded (ex. 10-30 and 20-40)
* - it doesn't matter how many settings are added to an item property
* - after an adding the space of percentage is full, that means after adding
* ex: add_item_property('opacity', 10, 30, 0, 1, easing)
*
* item.properties.opacity.settings :
* [
* [0, 10, 0, 0, '']
* [10, 30, 0, 1, easing] - property added
* [30, 100, 1, 1, '']
* ]
*
* ex: add_item_property('opacity', 40, 50, 1, 0)
*
* item.properties.opacity.settings :
* [
* [0, 10, 0, 0, '']
* [10, 30, 0, 1, easing] - property added
* [30, 40, 1, 1, '']
* [40, 50, 1, 0, easing] - property added
* [50, 100, 0, 0, '']
* ]
*
* - callable jQuery easing functions:
* swing
* easeInQuad
* easeOutQuad
* easeInOutQuad
* easeInCubic
* easeOutCubic
* easeInOutCubic
* easeInQuart
* easeOutQuart
* easeInOutQuart
* easeInQuint
* easeOutQuint
* easeInOutQuint
* easeInSine
* easeOutSine
* easeInOutSine
* easeInExpo
* easeOutExpo
* easeInOutExpo
* easeInCirc
* easeOutCirc
* easeInOutCirc
* easeInElastic
* easeOutElastic
* easeInOutElastic
* easeInBack
* easeOutBack
* easeInOutBack
* easeInBounce
* easeOutBounce
* easeInOutBounce
*
* @param name string
* @param start_percent numeric
* @param end_percent numeric
* @param start_value numeric
* @param end_value numeric
* @param easing string [optional]
*/
this.add_item_property = function(name, start_percent, end_percent, start_value, end_value, easing) {
if (start_percent >= end_percent) {
return;
}
if (undefined === this.properties[name]) {
this.properties[name] = {
computed_value: '',
settings: []
};
if (0 !== start_percent) {
this.properties[name].settings[this.properties[name].settings.length] = {
start_percent: 0,
end_percent: start_percent,
start_value: start_value,
end_value: start_value,
easing: ''
};
}
this.properties[name].settings[this.properties[name].settings.length] = {
start_percent: start_percent,
end_percent: end_percent,
start_value: start_value,
end_value: end_value,
easing: easing
};
this.properties[name].settings[this.properties[name].settings.length] = {
start_percent: end_percent,
end_percent: 100,
start_value: end_value,
end_value: end_value,
easing: ''
};
} else {
var last_setting = this.properties[name].settings[this.properties[name].settings.length - 1];
if (last_setting.start_percent !== start_percent) {
this.properties[name].settings[this.properties[name].settings.length - 1] = {
start_percent: last_setting.start_percent,
end_percent: start_percent,
start_value: last_setting.end_value,
end_value: last_setting.end_value,
easing: ''
};
this.properties[name].settings[this.properties[name].settings.length] = {
start_percent: start_percent,
end_percent: end_percent,
start_value: start_value,
end_value: end_value,
easing: easing
};
} else {
this.properties[name].settings[this.properties[name].settings.length - 1] = {
start_percent: start_percent,
end_percent: end_percent,
start_value: start_value,
end_value: end_value,
easing: easing
};
}
if (100 !== end_percent) {
this.properties[name].settings[this.properties[name].settings.length] = {
start_percent: end_percent,
end_percent: 100,
start_value: end_value,
end_value: end_value,
easing: ''
};
}
}
};
/**
* remove an item property
*
* @param name {String} The name of the property
* @returns {boolean}
*/
this.remove_item_property = function(name) {
if (undefined === this.properties[name]) {
return false;
}
delete this.properties[name];
return true;
};
},
/**
* - function used to init the artoEffect object
* - it must be called before adding any item
* - the _view_port_interval_index flag is initialized
* - the items list is empty initialized
*/
init: function() {
artoEffect.items = [];
},
/**
* - used to add an item to the item list and initialize it
*
* @param item The item to be added and initialized
*/
add_item: function(item) {
if (item.constructor !== artoEffect.item) {
return;
}
// the item is added in the item list
artoEffect.items.push(item);
// the item is initialized only once when it is added
artoEffect._initialize_item(item);
// for efficiently rendering all items are computed at once, so do not compute item individually
},
/**
* - used to initialize an item
* - an item must be initialized only once
*
* @param item
* @private
*/
_initialize_item: function(item) {
// an item must be initialized only once
if (true === item._is_initialized) {
return;
}
// the item full height is computed
if ( 'undefined' === typeof item.wrapper_jquery_obj) {
item.full_height = item.jqueryObj.outerHeight(true);
} else {
item.full_height = item.wrapper_jquery_obj.height();
}
if (0 === item.full_height) {
return;
}
var new_jquery_obj_reference = jQuery('<div class="arto_effect_pointer" style="height: 0; width: 0">');
new_jquery_obj_reference.insertBefore(item.jqueryObj);
item.top_pointer_jquery_obj = new_jquery_obj_reference;
if ( 'absolute' === item.jqueryObj.css( 'position' ) ) {
item.top_pointer_jquery_obj.css({
position: 'absolute',
top: 0,
left: 0
});
}
item.offset_top = item.top_pointer_jquery_obj.offset().top;
//console.log("initializare " + artoEffect.items.length + " : " + item.top_pointer_jquery_obj.offset().top);
item.offset_bottom_top = item.offset_top + item.full_height;
item.top_is_out = artoEvents.windowPageYOffset > item.offset_top;
// the item is marked as initialized, being ready to be computed
// for efficiently rendering all items are computed at once
item._is_initialized = true;
// maybe it's better to try a request animation frame after every initialization, for computing the already added items
//artoEffect.compute_all_items();
},
/**
* - used to reinitialize all items at the view resolution changing
*
* @param recompute_height boolean True if it's necessary to recompute the item's height [when view port changes]
*/
reinitialize_all_items: function(recompute_height) {
for (var i = artoEffect.items.length - 1; i >= 0; i--) {
artoEffect.reinitialize_item(artoEffect.items[i], recompute_height);
}
},
/**
* - used to reinitialize an item at the view resolution changing
*
* @param item artoEffect.item
* @param recompute_height boolean True if it's necessary to recompute the item height [when view port changes]
* @private
*/
reinitialize_item: function(item, recompute_height) {
// a not initialized item can't be reinitialized
if (false === item._is_initialized) {
return;
}
// prevent the following item computing, till the reinitialization is finished
item._is_initialized = false;
item.offset_top = item.top_pointer_jquery_obj.offset().top;
//console.log("reinitializare " + artoEffect.items.length + " : " + item.top_pointer_jquery_obj.offset().top);
if (true === recompute_height) {
if ( 'undefined' === typeof item.wrapper_jquery_obj) {
item.full_height = item.jqueryObj.outerHeight(true);
} else {
item.full_height = item.wrapper_jquery_obj.height();
}
if (0 === item.full_height) {
return;
}
}
item.offset_bottom_top = item.offset_top + item.full_height;
// Be sure the percent value is also reinitialized
item.percent_value = 0;
item._is_initialized = true;
},
/**
* - used for computing item properties
*
* @param item The item whose properties are computed
* @private
*/
_compute_item_properties: function(item) {
var computed_properties = {},
current_item_property;
for (var property in item.properties) {
if (true === item.properties.hasOwnProperty(property)) {
current_item_property = item.properties[property];
var current_setting,
new_computed_value,
local_computed_value,
easing_step,
easing_computed_value,
easing_division_interval = 1000;
for (var i = 0; i < current_item_property.settings.length; i++) {
current_setting = current_item_property.settings[i];
// the check is done using this form [...) of the interval or the last position 100%
if ((current_setting.start_percent <= item.percent_value && item.percent_value < current_setting.end_percent) ||
(item.percent_value === current_setting.end_percent && 100 === item.percent_value)) {
if (current_setting.start_value === current_setting.end_value) {
new_computed_value = current_setting.start_value;
} else {
// local computed value can have a positive value or a negative value, it depends of the difference end_value - start_value
// for a linear easing function, the new computed value is the start_value + local_computed_value
// if start_value < end_value, the variable local_computed_value is positive
// if start_value > end_value, the variable local_computed_value is negative
local_computed_value = (item.percent_value - current_setting.start_percent) / (current_setting.end_percent - current_setting.start_percent) * (current_setting.end_value - current_setting.start_value);
// if there's specified an easing function, it's applied over the computed_value
if ((undefined === current_setting.easing) || ('' === current_setting.easing)) {
// linear easing function
new_computed_value = current_setting.start_value + local_computed_value;
} else {
// specifying an easing function
easing_step = Math.abs(current_setting.start_value - current_setting.end_value) / easing_division_interval;
if (current_setting.start_value < current_setting.end_value) {
easing_computed_value = current_setting.start_value + jQuery.easing[current_setting.easing](
null,
local_computed_value,
0,
easing_step,
current_setting.end_value - current_setting.start_value) * easing_division_interval;
} else {
easing_computed_value = current_setting.start_value - jQuery.easing[current_setting.easing](
null,
-local_computed_value,
0,
easing_step,
current_setting.start_value - current_setting.end_value) * easing_division_interval;
}
new_computed_value = easing_computed_value;
//console.log(current_setting.easing + ' : ' + easing_step + ' ~ ' + easing_computed_value + ' ~ ' + (current_setting.start_value + computed_value) + ' & ' + current_setting.start_value + ' $ ' + current_setting.end_value);
}
}
// if the existing computed value is different, the new computed value is cached
if (current_item_property.computed_value !== new_computed_value) {
current_item_property.computed_value = new_computed_value;
computed_properties[property] = new_computed_value;
// the item is marked that it has at least one property that need to be redraw
// the animation callback functions are called just for the marked items
item.redraw = true;
}
break;
}
}
}
}
// a plain javascript object is added if there is no computed property
item.computed_item_properties = computed_properties;
},
/**
* - used for computing item
* - the item properties are computed only when the item is in the view port and it is moving
*
* @param item The artoEffect.item to be computed
*/
compute_item: function(item) {
//console.clear();
// the item must be initialized first
if (false === item._is_initialized) {
return;
}
var percent_display_value = 0;
if (artoEvents.windowPageYOffset + artoEvents.windowInnerHeight >= item.offset_top) {
if (artoEvents.windowPageYOffset > item.offset_bottom_top ) {
percent_display_value = 100;
} else {
percent_display_value = (artoEvents.windowPageYOffset + artoEvents.windowInnerHeight - item.offset_top) * 100 / (artoEvents.windowInnerHeight + item.full_height);
}
}
//console.log(window.pageYOffset + ' : ' + item.offset_top + ' : ' + item.offset_bottom_top);
if (item.percent_value !== percent_display_value) {
item.percent_value = percent_display_value;
//console.log(item.percent_value);
artoEffect._compute_item_properties(item);
}
item.top_is_out = artoEvents.windowPageYOffset > item.offset_top;
},
get_current_percent: function(item) {
// the item must be initialized first
if (false === item._is_initialized) {
return;
}
var percent_display_value = 0;
if (artoEvents.windowPageYOffset + artoEvents.windowInnerHeight >= item.offset_top) {
if (artoEvents.windowPageYOffset > item.offset_bottom_top ) {
percent_display_value = 100;
} else {
percent_display_value = (artoEvents.windowPageYOffset + artoEvents.windowInnerHeight - item.offset_top) * 100 / (artoEvents.windowInnerHeight + item.full_height);
}
}
return percent_display_value;
},
get_initial_percent: function( item ) {
// the item must be initialized first
if (false === item._is_initialized) {
return;
}
var percent_display_value = 0;
if (artoEvents.windowInnerHeight >= item.offset_top) {
if (0 > item.offset_bottom_top ) {
percent_display_value = 100;
} else {
percent_display_value = (artoEvents.windowInnerHeight - item.offset_top) * 100 / (artoEvents.windowInnerHeight + item.full_height);
}
}
return percent_display_value;
},
get_top_percent: function( item ) {
return (artoEvents.windowInnerHeight) * 100 / (artoEvents.windowInnerHeight + item.full_height);
},
/**
* - used to request an animation frame for computing all items
* - the flag animation_running is set to false by the last requestAnimationFrame callback (the last animation call),
* so a new call to requestAnimationFrame can be done
*/
compute_all_items: function compute_all_items() {
//artoEffect.animate();
if (false === artoEffect.animation_running) {
artoEffect.rAFIndex = window.requestAnimationFrame( artoEffect._animate_all_items );
}
artoEffect.animation_running = true;
},
/**
* - used to call the existing callback animate functions
*
* @private
*/
_animate_all_items: function _animate_all_items() {
//var start_time = Date.now();
for (var i = 0; i < artoEffect.items.length; i++) {
if ( false === artoEffect.items[i].computation_stopped) {
artoEffect.compute_item(artoEffect.items[i]);
}
}
for (var j = 0; j < artoEffect.items.length; j++) {
if (true === artoEffect.items[j].redraw) {
artoEffect.items[j].animation_callback();
}
}
artoEffect.animation_running = false;
//var end_time = Date.now();
//
//var debug_table = jQuery("#debug_table");
//debug_table.html((end_time - start_time) + ' ms');
},
/** we'll see if it's necessary to make reinitialization just at the view port changing. Now, it's not
* - necessary to be called when the window is being resized
*/
arto_events_resize: function() {
if (0 === artoEffect.items.length) {
return;
}
// this will be applied if it depends just by view port changing
//if (artoEffect._changed_view_port_width()) {
// artoEffect.reinitialize_all_items();
//}
artoEffect.reinitialize_all_items(false);
artoEffect.compute_all_items();
},
log: function log(msg) {
//console.log(msg);
}
};
artoEffect.init();
})( jQuery );