forked from codeflix12/Glitch-Slider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgbShiftSlider.js
519 lines (377 loc) · 17.9 KB
/
rgbShiftSlider.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
/*!
* rbgShiftSlider : little plugin to create slides with rgba glitch transition
* (c) 2019 Hadrien Mongouachon
* MIT Licensed.
*
* Author URI: http://hmongouachon.com
* Plugin URI: https://github.com/hmongouachon/GlitchrbgShiftSlider
* Version: 1.0.0
*/
;
(function() {
window.rbgShiftSlider = function(options) {
///////////////////////////////
// OPTIONS
///////////////////////////////
options = options || {};
options.slideImages = options.hasOwnProperty('slideImages') ? options.slideImages : [];
options.stageWidth = options.hasOwnProperty('stageWidth') ? options.stageWidth : 1920;
options.stageHeight = options.hasOwnProperty('stageHeight') ? options.stageHeight : 1080;
options.displacementImage = options.hasOwnProperty('displacementImage') ? options.displacementImage : '';
options.fullScreen = options.hasOwnProperty('fullScreen') ? options.fullScreen : true;
options.transitionDuration = options.hasOwnProperty('transitionDuration') ? options.transitionDuration : 0.25;
options.transitionGhostDuration = options.hasOwnProperty('transitionGhostDuration') ? options.transitionGhostDuration : 0.25;
options.transitionFilterIntensity = options.hasOwnProperty('transitionFilterIntensity') ? options.transitionFilterIntensity : 350;
options.transitioSpriteIntensity = options.hasOwnProperty('transitioSpriteIntensity') ? options.transitioSpriteIntensity : 2;
options.mouseDispIntensity = options.hasOwnProperty('mouseDispIntensity') ? options.mouseDispIntensity : 3;
options.nav = options.hasOwnProperty('nav') ? options.nav : true;
options.navElement = options.hasOwnProperty('navElement') ? options.navElement : '.scene-nav';
options.interactive = options.hasOwnProperty('interactive') ? options.interactive : true;
options.autoPlay = options.hasOwnProperty('autoPlay') ? options.autoPlay : true;
options.autoPlaySpeed = options.hasOwnProperty('autoPlaySpeed') ? options.autoPlaySpeed : 3000;
///////////////////////////////
// METHODS
// create 3 containers, set rgb colors with filterMatrix
// each container will have the same background img, same filters and displacement img
// for transition between slides, a fake object named "ghostEl" will be animate with easing by gsap
// the x value of this object will be use to create transition effect and fake user gesture for slide transition
///////////////////////////////
///////////////////////////////
// VARS
///////////////////////////////
var canvas = document.getElementById("rbgShiftSlider");
// remove pixi message in console
PIXI.utils.skipHello();
var stage = new PIXI.Container();
var renderer = PIXI.autoDetectRenderer(options.stageWidth, options.stageHeight, {
transparent: true
}); // transparent: true
var render;
var slidesContainer = new PIXI.Container();
var bgs = [],
texture_bg = [],
containers = [],
channelsContainer = [],
displacementFilters = [],
displacementSprites = [];
var redChannelFilter, greenChannelFilter, blueChannelFilter;
var containerRed, containerGreen, containerBlue;
const ghostEl = {
x: 0,
y: 0,
};
var posx = 0,
posy = 0;
var node_xp = 0,
node_yp = 0;
var rafId_gestureMove, rafId_transition;
var baseTimeline;
// slide index
var currentIndex = 0;
var isPlaying = false;
// autoplay
var interval, autoplay;
///////////////////////////////
// Build pixi scene
///////////////////////////////
function build_scene() {
// append render to canvas
canvas.appendChild(renderer.view);
// Add children containers to the stage = canvas
stage.addChild(slidesContainer);
// Fit renderer to the screen
if (options.fullScreen === true) {
renderer.view.style.objectFit = 'cover';
renderer.view.style.width = '100%';
renderer.view.style.height = '100%';
renderer.view.style.top = '50%';
renderer.view.style.left = '50%';
renderer.view.style.webkitTransform = 'translate( -50%, -50% ) scale(1.2)';
renderer.view.style.transform = 'translate( -50%, -50% ) scale(1.2)';
} else {
renderer.view.style.maxWidth = '100%';
renderer.view.style.top = '50%';
renderer.view.style.left = '50%';
renderer.view.style.webkitTransform = 'translate( -50%, -50% )';
renderer.view.style.transform = 'translate( -50%, -50% )';
}
render = new PIXI.ticker.Ticker();
render.autoStart = true;
render.add(function(delta) {
renderer.render(stage);
});
slidesContainer.interactive = true;
}
///////////////////////////////
// Build rgb containers
///////////////////////////////
function build_RGBcontainers() {
redChannelFilter = new PIXI.filters.ColorMatrixFilter();
redChannelFilter.matrix = [
1, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 1, 0
];
greenChannelFilter = new PIXI.filters.ColorMatrixFilter();
greenChannelFilter.matrix = [
0, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 1, 0
];
blueChannelFilter = new PIXI.filters.ColorMatrixFilter();
blueChannelFilter.matrix = [
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0
];
channelsContainer.push(redChannelFilter, greenChannelFilter, blueChannelFilter);
// CONTAINERS
containerRed = new PIXI.Container();
containerRed.position.x = 0;
containerGreen = new PIXI.Container();
containerGreen.position.x = 0;
containerBlue = new PIXI.Container();
containerBlue.position.x = 0;
containers.push(containerRed, containerGreen, containerBlue);
// set texture for each background (used later for slide transition)
for (var i = 0; i < options.slideImages.length; ++i) {
texture_bg[i] = new PIXI.Texture.fromImage(options.slideImages[i]);
}
// set displacement filter and displacement sprite for each container
for (var i = 0, len = containers.length; i < len; i++) {
slidesContainer.addChild(containers[i]);
texture = new PIXI.Texture.fromImage(options.displacementImage);
// push sprites & filters to array
displacementSprites.push(new PIXI.Sprite(texture));
displacementFilters.push(new PIXI.filters.DisplacementFilter(displacementSprites[i]));
// set first image texture background and push to array
var bg = new PIXI.Sprite(texture_bg[0]); //new PIXI.Sprite(texture2);
bgs.push(bg);
bgs[i].width = renderer.view.width;
bgs[i].height = renderer.view.height;
bgs[i].anchor.set(0.5)
bgs[i].x = renderer.view.width / 2;
bgs[i].y = renderer.view.height / 2;
bgs[i].alpha = 0;
// add bg array + displacement sprites array to container
containers[i].addChild(displacementSprites[i], bgs[i]);
// addchannel container filter array + displacement filter array to container
containers[i].filters = [displacementFilters[i], channelsContainer[i]];
// init x y value
displacementFilters[i].scale.x = 0;
displacementFilters[i].scale.y = 0;
// set autofit
displacementFilters[i].autoFit = true;
}
// add different anchor value to each displacementSprite
displacementSprites[0].anchor.set(0.0);
displacementSprites[1].anchor.set(0.5);
displacementSprites[2].anchor.set(0.3);
// add blend mode
// containers[0].filters[1].blendMode = PIXI.BLEND_MODES.ADD;
containers[1].filters[1].blendMode = PIXI.BLEND_MODES.ADD;
containers[2].filters[1].blendMode = PIXI.BLEND_MODES.ADD;
}
///////////////////////////////
// Next slide transition
///////////////////////////////
function next_slide(next) {
// init ghost x value
TweenMax.set(ghostEl, {
x: 0,
ease: Power0.easeOut,
});
// init basetimeline
baseTimeline = new TimelineMax({
onStart: function() {
isPlaying = true;
// fake user gesture from left to right
TweenMax
.to(ghostEl, options.transitionGhostDuration , {
x: screen.width,
ease: Power0.easeOut,
})
},
onComplete: function() {
// update current index
currentIndex = next;
isPlaying = false;
if (options.interactive === true) {
// init mouse gesture
gestureEffect();
}
},
onUpdate: function() {
// make transition displacement effect
node_xp += ((ghostEl.x - node_xp) / 3);
node_yp += ((ghostEl.x - node_yp) / 3);
for (var i = 0, len = containers.length; i < len; i++) {
displacementFilters[i].scale.x = Math.atan(node_xp - (displacementSprites[i].x)) * (baseTimeline.progress() * options.transitionFilterIntensity);
displacementSprites[i].position.x = node_yp * (baseTimeline.progress() * options.transitionSpriteIntensity);
}
// console.log(ghostEl.x)
}
});
// baseTimeline.clear();
// if (baseTimeline.isActive()) {
// return;
// }
baseTimeline
// hide all 3 containers backgrounds
.to([bgs[0], bgs[1], bgs[2]], options.transitionDuration, {
alpha: 0,
ease: Power2.easeOut
}, options.transitionDuration)
// add fn for container bg texture update
.add(updateTextureBgs, options.transitionDuration);
function updateTextureBgs() {
for (var i = 0; i < options.slideImages.length; ++i) {
if (i == next) {
for (var j = 0, len = containers.length; j < len; j++) {
// update texture
bgs[j].texture = texture_bg[i];
// show background with new texture
baseTimeline
.to(bgs[j], options.transitionDuration, {
alpha: 1,
ease: Power2.easeOut
}, options.transitionDuration);
}
}
}
}
};
///////////////////////////////
// gesture effect
///////////////////////////////
function gestureEffect() {
// re init animation
cancelAnimationFrame(rafId_transition);
// make sure basetimeline is not running
if (baseTimeline.isActive()) {
return;
}
// reinit x/y value for sprites and filters
for (var i = 0, len = containers.length; i < len; i++) {
displacementSprites[i].x = 0;
displacementSprites[i].y = 0;
displacementFilters[i].scale.x = 0;
displacementFilters[i].scale.y = 0;
}
// add mouse / touch event
slidesContainer
.on('mousemove', onPointerMove)
.on('touchmove', onPointerMove);
function onPointerMove(eventData) {
// get mouse value
posx = eventData.data.global.x;
posy = eventData.data.global.y;
}
// use raf for smooth sprites / filters animation
ticker();
function ticker() {
rafId_gestureMove = requestAnimationFrame(ticker);
// make sure transition is done
if (ghostEl.x >= screen.width) {
// get new mouse positions with dumping intensity ( between [1-10] : 1 is faster)
node_xp += ((posx - node_xp) / options.mouseDispIntensity);
node_yp += ((posy - node_yp) / options.mouseDispIntensity);
for (var i = 0, len = containers.length; i < len; i++) {
// update disp scale x / y values
displacementFilters[i].scale.x = (node_xp - (displacementSprites[i].x));
displacementFilters[i].scale.y = (node_yp - (displacementSprites[i].y));
// update sprite x / y values
displacementSprites[i].position.x = node_xp;
displacementSprites[i].position.y = node_yp;
}
} else {
cancelAnimationFrame(rafId_gestureMove);
}
}
};
///////////////////////////////
// navigation
///////////////////////////////
if(options.nav === true) {
var nav = document.querySelectorAll(options.navElement);
for (var i = 0; i < nav.length; i++) {
var navItem = nav[i];
navItem.onclick = function(event) {
// Make sure the previous transition has ended
if (isPlaying) {
return false;
}
if (this.getAttribute('data-nav') === 'next') {
if (currentIndex >= 0 && currentIndex < options.slideImages.length - 1) {
next_slide(currentIndex + 1);
} else {
next_slide(0);
}
if(options.autoPlay === true) {
// re init autoplay
clearInterval(interval);
autoplay();
}
} else {
if (currentIndex > 0 && currentIndex < options.slideImages.length) {
next_slide(currentIndex - 1);
} else {
next_slide(options.slideImages.length - 1);
}
if(options.autoPlay === true) {
// re init autoplay
clearInterval(interval);
autoplay();
}
}
return false;
}
}
}
///////////////////////////////
// autoplay
///////////////////////////////
function autoplay(){
interval = setInterval(function() {
// if (isPlaying) {
// return false;
// }
currentIndex = currentIndex + 1;
if (currentIndex === options.slideImages.length) {
currentIndex = 0;
next_slide(currentIndex);
} else {
next_slide(currentIndex);
}
// // clear after x seconds
// if (Date.now() - started > 15000) {
// // pause it
// // clearInterval(interval);
// }
// else {
// // the thing to do every 100ms
// }
}, options.autoPlaySpeed); // default 3000ms
};
///////////////////////////////
// init slider
///////////////////////////////
function init_slider() {
build_scene();
build_RGBcontainers();
if (options.autoPlay === true) {
var started = Date.now();
currentIndex = 0;
next_slide(currentIndex);
autoplay();
} else {
next_slide(0);
}
};
// let's go
init_slider();
};
})();