-
Notifications
You must be signed in to change notification settings - Fork 0
/
slides.js
138 lines (100 loc) · 2.57 KB
/
slides.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
const photos = [
{
src: "images/image1.jpg",
title: "1 / 6",
},
{
src: "images/image2.jpg",
title: "2 / 6",
},
{
src: "images/image3.jpg",
title: "3 / 6",
},
{
src: "images/image4.jpg",
title: "4 / 6",
},
{
src: "images/image5.jpg",
title: "5 / 6",
},
{
src: "images/image6.jpg",
title: "6 / 6",
}
];
console.log(photos.length);
const prevTag = document.querySelector("nav a.prev");
const nextTag = document.querySelector("nav a.next");
const descriptionTag = document.querySelector("header div");
// const imageTag = document.querySelector("div.canvas-holder img");
const canvas = document.querySelector("div.canvas-holder canvas");
const sandbox = new GlslCanvas(canvas);
sandbox.load(frag(photos));
let startIndex = 0;
let endIndex = 0;
// quick way to get time
let timeline = performance.now() - 9999;
const sizer = function (){
const ww = window.innerWidth;
const wh = window.innerHeight;
const s = Math.min(ww, wh);
const dpi = window.devicePixelRatio;
canvas.width = s * 0.6 * dpi;
canvas.height = s * 0.9 * dpi;
canvas.style.width = Math.round(s * 0.6) + "px";
canvas.style.height = Math.round(s * 0.9) + "px";
}
const next = () => {
endIndex = endIndex + 1;
if (endIndex > photos.length - 1) {
endIndex = 0;
}
update();
}
const prev = () => {
endIndex = endIndex - 1;
if(endIndex < 0) {
endIndex = photos.length - 1;
}
update();
}
const update = () => {
descriptionTag.innerHTML = photos[endIndex].title;
// imageTag.setAttribute("src", photos[index].src);
//reset timeline for every single update
timeline = performance.now();
sandbox.setUniform("startIndex", startIndex);
sandbox.setUniform("endIndex", endIndex);
tick();
startIndex = endIndex;
}
const tick = () => {
const diff = (performance.now() - timeline) / 1000; //from past to now
sandbox.setUniform("timeline", diff);
//run tick on every single frame of the page
requestAnimationFrame(tick);
}
//loading texture
const load = () => {
sizer();
tick();
// sandbox.setUniform("image5", "images/image5.jpg");
photos.forEach((photo, index)=>{
sandbox.setUniform(`textures[${index}]`, photo.src);
})
}
load();
//events
nextTag.addEventListener("click",(e) => {
e.preventDefault();
next();
})
prevTag.addEventListener("click", (e) => {
e.preventDefault();
prev();
})
window.addEventListener("resize", function(){
sizer();
})