-
Notifications
You must be signed in to change notification settings - Fork 5
/
BrowserImageSlideshow.html
292 lines (254 loc) · 7.59 KB
/
BrowserImageSlideshow.html
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
<!DOCTYPE html>
<!--
BrowserImageSlideshow
https://github.com/dustymethod/BrowserImageSlideshow
https://obsproject.com/forum/threads/browser-image-slideshow.110157/
-->
<div id="imageContainer"></div>
<style>
*{
border-color: transparent;
background-color: transparent;
}
img {
position: fixed;
object-fit:contain;
width: 100%;
height: 100%;
border-color: transparent;
}
body {
background-color: transparent;
margin: auto;
overflow: hidden;
}
#caption {
height: 30%;
width: 100%;
position: fixed;
bottom: 0;
color: white;
font-size: 7em;
line-height: 80%;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
text-shadow: -2px 2px black, -2px -2px black, 2px -2px black, 2px 2px black, 5px 5px 2px black;
}
</style>
<script src="jquery-3.7.1.min.js"></script>
<script src="images/images.js"></script>
<script src="settings.js"></script>
<script>
// modes
// 0: Random order (default)
// 1: Alphabetical order
// 2: Alphabetical order (start on random image)
// 3: Alphanumeric
// common extensions from here https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types
const imageExtensions = ["apng", "avif", "gif", "jpg", "jpeg", "jfif", "pjpeg", "pjp", "png", "svg", "webp", "bmp", "ico", "cur"];
// setup image src strings
const images = imageNamesStr.split("\n");
images.shift();
images.pop();
for (let i = images.length-1; i >= 0; i--) {
let fileName = images[i];
// remove anything that's not an image
if (!imageExtensions.some(v => fileName.includes("."+v))) {
images.splice(i, 1);
}
}
// setup indexes for shuffling
const indexes = new Array(); // numbers indexing the list of images, used for shuffling
let index = 0; //current position in "indexes" array
for(let i = 0; i < images.length; i++) {
images[i] = "images/" + images[i];
indexes.push(i);
}
// setup img elements
let topImage = document.createElement("img");
let botImage = document.createElement("img");
let imageContainer = document.getElementById("imageContainer");
imageContainer.appendChild(botImage);
imageContainer.appendChild(topImage);
topImage.id = "topImage";
botImage.id = "botImage";
// setup captions
const captionDelimiter = "--";
const captions = new Array();
let caption;
if(captionEnabled) {
caption = document.createElement("div");
caption.id = "caption";
imageContainer.appendChild(caption);
}
let fadeDuration = slideDuration * 0.25;
let slideshowFunc;
let fadeInTop = true; // bool for deciding if top image fades in or out
let isSlideshowPaused = false;
let isSlideshowVisible = true;
let isTransitionInProgress = false; // skip animation if already in progress
function shuffle(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
// Sort list of images alpha numerically
function alphanumericSort() {
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#numeric
const collator = new Intl.Collator(undefined, {
numeric: true
});
images.sort((a, b) => {
return collator.compare(a, b);
});
}
function randomizeIndex(max) {
index = Math.floor(Math.random() * max); // [0, max)
}
// called each interval of setInterval()
function update() {
if (index === images.length-1 && loopSlideshow === false) {
clearInterval(slideshowFunc);
isSlideshowPaused = true;
return;
}
getNextSlide();
}
function generateCaptions() {
captions.length = 0;
images.forEach((fileName) => {
let text = fileName.replace(/^.*[\\/]/, '');
text = text.substr(0, text.lastIndexOf('.')) || text;
text = text.substr(0, text.lastIndexOf(captionDelimiter)) || text;
captions.push(text);
});
}
function setCaption(text) {
caption.innerText = text;
}
function getNextSlide() {
index = index === images.length - 1 ? 0 : index + 1;
if (index === 0 && mode === 0) {
shuffle(indexes);
}
let imageSrc = images[indexes[index]];
fadeInImage(imageSrc);
}
function getPreviousSlide() {
index = index === 0 ? images.length - 1 : index - 1;
let imageSrc = images[indexes[index]];
fadeInImage(imageSrc);
}
function fadeInImage(imageSrc) {
if (fadeInTop) { topImage.src = imageSrc; }
else { botImage.src = imageSrc; }
if (isTransitionInProgress) {
$("#topImage").stop(true, true);
$("#botImage").stop(true, true);
}
isTransitionInProgress = true;
$("#botImage").animate(
{ opacity: fadeInTop ? 0.0 : 1.0 },
{ duration: fadeDuration }
);
$("#topImage").animate(
{ opacity: fadeInTop ? 1.0 : 0.0 },
{
duration: fadeDuration,
done: function() {
isTransitionInProgress = false;
}
}
);
if(captionEnabled) {
setCaption(captions[indexes[index]]);
}
fadeInTop = !fadeInTop;
}
function pause() {
if (isSlideshowPaused) return;
clearInterval(slideshowFunc);
isSlideshowPaused = true;
}
function resume() {
if (!isSlideshowPaused) return;
getNextSlide(); // get next slide without waiting for setInterval delay
slideshowFunc = setInterval(update, slideDuration);
isSlideshowPaused = false;
}
function restart() {
$("#topImage").stop(true, true);
$("#botImage").stop(true, true);
clearInterval(slideshowFunc);
start();
}
// called once when the browser source loads or is restarted via hotkey
function start() {
topImage.style.opacity = "0.0";
botImage.style.opacity = "0.0";
if (images.length > 0) { // if at least 1 image, show the first image
if (mode === 0) { // random mode
shuffle(indexes);
} else if (mode === 2) { // choose random image to start on
randomizeIndex(images.length);
} else if (mode === 3) {
alphanumericSort();
}
if(captionEnabled) {
generateCaptions();
setCaption(captions[indexes[index]]);
}
botImage.src = images[indexes[index]];
$("#botImage").animate(
{ opacity: 1.0 },
{ duration: fadeDuration }
);
}
if (startWithAutoplay && images.length > 1) {
slideshowFunc = setInterval(update, slideDuration);
} else {
isSlideshowPaused = true;
}
}
start();
// handle hotkey events from OBS
document.addEventListener("keydown", (event) => {
if (images.length == 0) return;
let key = "";
try { key = event.key; }
catch (error) {
console.log(error);
return;
}
switch (key) {
case "1": pause(); break;
case "2": resume(); break;
case "3": getNextSlide(); break;
case "4": getPreviousSlide(); break;
case "5": // toggle visibilty
if (isSlideshowVisible) {
imageContainer.style.opacity = 0.0;
isSlideshowVisible = false;
} else {
imageContainer.style.opacity = 1.0;
isSlideshowVisible = true;
}
break;
case "6": // toggle pause
if (isSlideshowPaused) {
resume();
} else {
pause();
}
break;
case "7": restart(); break;
default:
//console.log("Unhandled hotkey: " + key);
break;
}
});
</script>