|
31 | 31 | <script src="settings.js"></script> |
32 | 32 | <script> |
33 | 33 |
|
34 | | - // modes (defined in settings.js): |
35 | | - // 0: Random order (default) |
36 | | - // 1: Alphabetical order |
37 | | - // 2: Alphabetical order (start on random image) |
38 | | - |
39 | | - // setup image src strings |
40 | | - let images = imageNamesStr.split("\n"); |
41 | | - images.shift(); |
42 | | - images.pop(); |
43 | | - for (let i = images.length-1; i >= 0; i--) { |
44 | | - // remove js, sh, or directory |
45 | | - let remove = images[i].includes(".js") || images[i].includes(".sh") || !images[i].includes(".") || images[i].includes(".git"); |
46 | | - if (remove) { |
47 | | - images.splice(i, 1); |
48 | | - } |
49 | | - } |
50 | | - |
51 | | - // setup indexes for shuffling |
52 | | - let indexes = new Array(); |
53 | | - for(let i = 0; i < images.length; i++) { |
54 | | - images[i] = "images/" + images[i]; |
55 | | - indexes.push(i); |
| 34 | +// modes (defined in settings.js): |
| 35 | +// 0: Random order (default) |
| 36 | +// 1: Alphabetical order |
| 37 | +// 2: Alphabetical order (start on random image) |
| 38 | + |
| 39 | +// setup image src strings |
| 40 | +let images = imageNamesStr.split("\n"); |
| 41 | +images.shift(); |
| 42 | +images.pop(); |
| 43 | +for (let i = images.length-1; i >= 0; i--) { |
| 44 | + // remove js, sh, or directory |
| 45 | + let remove = images[i].includes(".js") || images[i].includes(".sh") || !images[i].includes(".") || images[i].includes(".git"); |
| 46 | + if (remove) { |
| 47 | + images.splice(i, 1); |
56 | 48 | } |
57 | | - |
58 | | - // setup img elements |
59 | | - let topImage = document.createElement("img"); |
60 | | - let botImage = document.createElement("img"); |
61 | | - let imageContainer = document.getElementById("imageContainer"); |
62 | | - imageContainer.appendChild(botImage); |
63 | | - imageContainer.appendChild(topImage); |
64 | | - topImage.id = "topImage"; |
65 | | - botImage.id = "botImage"; |
66 | | - |
67 | | - let fadeDuration = slideDuration * 0.25; |
68 | | - let slideshowFunc; |
69 | | - let index = 0; |
70 | | - let fadeInTop = true; // bool for deciding if top image fades in or out |
71 | | - let isSlideshowPaused = false; |
72 | | - let isSlideshowVisible = true; |
73 | | - let isTransitionInProgress = false; // skip animation if already in progress |
74 | | - |
75 | | - function shuffle(a) { |
76 | | - for (let i = a.length - 1; i > 0; i--) { |
77 | | - const j = Math.floor(Math.random() * (i + 1)); |
78 | | - [a[i], a[j]] = [a[j], a[i]]; |
79 | | - } |
80 | | - return a; |
| 49 | +} |
| 50 | + |
| 51 | +// setup indexes for shuffling |
| 52 | +let indexes = new Array(); |
| 53 | +for(let i = 0; i < images.length; i++) { |
| 54 | + images[i] = "images/" + images[i]; |
| 55 | + indexes.push(i); |
| 56 | +} |
| 57 | + |
| 58 | +// setup img elements |
| 59 | +let topImage = document.createElement("img"); |
| 60 | +let botImage = document.createElement("img"); |
| 61 | +let imageContainer = document.getElementById("imageContainer"); |
| 62 | +imageContainer.appendChild(botImage); |
| 63 | +imageContainer.appendChild(topImage); |
| 64 | +topImage.id = "topImage"; |
| 65 | +botImage.id = "botImage"; |
| 66 | + |
| 67 | +let fadeDuration = slideDuration * 0.25; |
| 68 | +let slideshowFunc; |
| 69 | +let index = 0; |
| 70 | +let fadeInTop = true; // bool for deciding if top image fades in or out |
| 71 | +let isSlideshowPaused = false; |
| 72 | +let isSlideshowVisible = true; |
| 73 | +let isTransitionInProgress = false; // skip animation if already in progress |
| 74 | + |
| 75 | +function shuffle(a) { |
| 76 | + for (let i = a.length - 1; i > 0; i--) { |
| 77 | + const j = Math.floor(Math.random() * (i + 1)); |
| 78 | + [a[i], a[j]] = [a[j], a[i]]; |
81 | 79 | } |
| 80 | + return a; |
| 81 | +} |
82 | 82 |
|
83 | | - function randomizeIndex(max) { |
84 | | - index = Math.floor(Math.random() * max); // [0, max) |
85 | | - } |
| 83 | +function randomizeIndex(max) { |
| 84 | + index = Math.floor(Math.random() * max); // [0, max) |
| 85 | +} |
86 | 86 |
|
87 | | - // called each interval of setInterval() |
88 | | - function update() { |
89 | | - if (index === images.length-1 && stopOnLastImage === true) { |
90 | | - clearInterval(slideshowFunc); |
91 | | - return; |
92 | | - } |
93 | | - getNextSlide(); |
| 87 | +// called each interval of setInterval() |
| 88 | +function update() { |
| 89 | + if (index === images.length-1 && stopOnLastImage === true) { |
| 90 | + clearInterval(slideshowFunc); |
| 91 | + return; |
94 | 92 | } |
| 93 | + getNextSlide(); |
| 94 | +} |
95 | 95 |
|
96 | | - function getNextSlide() { |
97 | | - index = index === images.length - 1 ? 0 : index + 1; |
98 | | - if (index === 0 && mode === 0) { |
99 | | - shuffle(indexes); |
100 | | - } |
101 | | - |
102 | | - let imageSrc = images[indexes[index]]; |
103 | | - fadeInImage(imageSrc); |
| 96 | +function getNextSlide() { |
| 97 | + index = index === images.length - 1 ? 0 : index + 1; |
| 98 | + if (index === 0 && mode === 0) { |
| 99 | + shuffle(indexes); |
104 | 100 | } |
105 | | - |
106 | | - function getPreviousSlide() { |
107 | | - index = index === 0 ? images.length - 1 : index - 1; |
108 | | - let imageSrc = images[indexes[index]]; |
109 | | - fadeInImage(imageSrc); |
| 101 | + |
| 102 | + let imageSrc = images[indexes[index]]; |
| 103 | + fadeInImage(imageSrc); |
| 104 | +} |
| 105 | + |
| 106 | +function getPreviousSlide() { |
| 107 | + index = index === 0 ? images.length - 1 : index - 1; |
| 108 | + let imageSrc = images[indexes[index]]; |
| 109 | + fadeInImage(imageSrc); |
| 110 | +} |
| 111 | + |
| 112 | +function fadeInImage(imageSrc) { |
| 113 | + if (fadeInTop) { topImage.src = imageSrc; } |
| 114 | + else { botImage.src = imageSrc; } |
| 115 | + |
| 116 | + if (isTransitionInProgress) { |
| 117 | + $("#topImage").stop(true, true); |
| 118 | + $("#botImage").stop(true, true); |
110 | 119 | } |
| 120 | + |
| 121 | + isTransitionInProgress = true; |
| 122 | + $("#botImage").animate( |
| 123 | + { opacity: fadeInTop ? 0.0 : 1.0 }, |
| 124 | + { duration: fadeDuration } |
| 125 | + ); |
| 126 | + |
| 127 | + $("#topImage").animate( |
| 128 | + { opacity: fadeInTop ? 1.0 : 0.0 }, |
| 129 | + { |
| 130 | + duration: fadeDuration, |
| 131 | + done: function() { |
| 132 | + isTransitionInProgress = false; |
| 133 | + } |
| 134 | + } |
| 135 | + ); |
| 136 | + |
| 137 | + fadeInTop = !fadeInTop; |
| 138 | +} |
| 139 | + |
| 140 | +function pause() { |
| 141 | + console.log("pause"); |
| 142 | + if (isSlideshowPaused) return; |
| 143 | + clearInterval(slideshowFunc); |
| 144 | + isSlideshowPaused = true; |
| 145 | +} |
| 146 | + |
| 147 | +function resume() { |
| 148 | + if (!isSlideshowPaused) return; |
| 149 | + update(); // trigger a single update to fill setInterval delay |
| 150 | + slideshowFunc = setInterval(update, slideDuration); |
| 151 | + isSlideshowPaused = false; |
| 152 | +} |
| 153 | + |
| 154 | +function restart() { |
| 155 | + $("#topImage").stop(true, true); |
| 156 | + $("#botImage").stop(true, true); |
| 157 | + clearInterval(slideshowFunc); |
| 158 | + start(); |
| 159 | +} |
111 | 160 |
|
112 | | - function fadeInImage(imageSrc) { |
113 | | - if (fadeInTop) { topImage.src = imageSrc; } |
114 | | - else { botImage.src = imageSrc; } |
| 161 | +// called once when the browser source loads or restarts |
| 162 | +function start() { |
| 163 | + topImage.style.opacity = "0.0"; |
| 164 | + botImage.style.opacity = "0.0"; |
| 165 | + |
| 166 | + if (images.length > 0) { // if at least 1 image, show the first image |
115 | 167 |
|
116 | | - if (isTransitionInProgress) { |
117 | | - $("#topImage").stop(true, true); |
118 | | - $("#botImage").stop(true, true); |
| 168 | + if (mode === 0) { // random mode |
| 169 | + shuffle(indexes); |
| 170 | + } else if (mode === 2) { // choose random image to start on |
| 171 | + randomizeIndex(images.length); |
119 | 172 | } |
120 | | - |
121 | | - isTransitionInProgress = true; |
| 173 | + |
| 174 | + botImage.src = images[indexes[index]]; |
122 | 175 | $("#botImage").animate( |
123 | | - { opacity: fadeInTop ? 0.0 : 1.0 }, |
| 176 | + { opacity: 1.0 }, |
124 | 177 | { duration: fadeDuration } |
125 | 178 | ); |
126 | | - |
127 | | - $("#topImage").animate( |
128 | | - { opacity: fadeInTop ? 1.0 : 0.0 }, |
129 | | - { |
130 | | - duration: fadeDuration, |
131 | | - done: function() { |
132 | | - isTransitionInProgress = false; |
133 | | - } |
134 | | - } |
135 | | - ); |
136 | | - |
137 | | - fadeInTop = !fadeInTop; |
138 | 179 | } |
139 | 180 |
|
140 | | - function pause() { |
141 | | - console.log("pause"); |
142 | | - if (isSlideshowPaused) return; |
143 | | - clearInterval(slideshowFunc); |
| 181 | + if (startWithAutoplay && images.length > 1) { |
| 182 | + slideshowFunc = setInterval(update, slideDuration); |
| 183 | + } else { |
144 | 184 | isSlideshowPaused = true; |
145 | 185 | } |
| 186 | +} |
146 | 187 |
|
147 | | - function resume() { |
148 | | - if (!isSlideshowPaused) return; |
149 | | - update(); // trigger a single update to fill setInterval delay |
150 | | - slideshowFunc = setInterval(update, slideDuration); |
151 | | - isSlideshowPaused = false; |
152 | | - } |
| 188 | +start(); |
153 | 189 |
|
154 | | - function restart() { |
155 | | - $("#topImage").stop(true, true); |
156 | | - $("#botImage").stop(true, true); |
157 | | - clearInterval(slideshowFunc); |
158 | | - start(); |
| 190 | +// handle hotkey events from OBS |
| 191 | +document.addEventListener("keydown", (event) => { |
| 192 | + let key = ""; |
| 193 | + try { key = event.key; } |
| 194 | + catch (error) { |
| 195 | + console.log(error); |
| 196 | + return; |
159 | 197 | } |
160 | 198 |
|
161 | | - // called once when the browser source loads or restarts |
162 | | - function start() { |
163 | | - topImage.style.opacity = "0.0"; |
164 | | - botImage.style.opacity = "0.0"; |
165 | | - |
166 | | - if (images.length > 0) { // if at least 1 image, show the first image |
167 | | - |
168 | | - if (mode === 0) { // random mode |
169 | | - shuffle(indexes); |
170 | | - } else if (mode === 2) { // choose random image to start on |
171 | | - randomizeIndex(images.length); |
| 199 | + switch (key) { |
| 200 | + case "1": pause(); break; |
| 201 | + case "2": resume(); break; |
| 202 | + case "3": getNextSlide(); break; |
| 203 | + case "4": getPreviousSlide(); break; |
| 204 | + case "5": // toggle visibilty |
| 205 | + if (isSlideshowVisible) { |
| 206 | + imageContainer.style.opacity = 0.0; |
| 207 | + isSlideshowVisible = false; |
| 208 | + } else { |
| 209 | + imageContainer.style.opacity = 1.0; |
| 210 | + isSlideshowVisible = true; |
172 | 211 | } |
173 | | - |
174 | | - botImage.src = images[indexes[index]]; |
175 | | - $("#botImage").animate( |
176 | | - { opacity: 1.0 }, |
177 | | - { duration: fadeDuration } |
178 | | - ); |
179 | | - } |
180 | | - |
181 | | - if (startWithAutoplay && images.length > 1) { |
182 | | - slideshowFunc = setInterval(update, slideDuration); |
183 | | - } else { |
184 | | - isSlideshowPaused = true; |
185 | | - } |
| 212 | + break; |
| 213 | + case "6": // toggle pause |
| 214 | + if (isSlideshowPaused) { |
| 215 | + resume(); |
| 216 | + } else { |
| 217 | + pause(); |
| 218 | + } |
| 219 | + break; |
| 220 | + case "7": restart(); break; |
| 221 | + default: |
| 222 | + console.log("Unhandled hotkey: " + key); |
| 223 | + break; |
186 | 224 | } |
187 | | - |
188 | | - start(); |
189 | | - |
190 | | - // handle hotkey events from OBS |
191 | | - document.addEventListener("keydown", (event) => { |
192 | | - let key = ""; |
193 | | - try { key = event.key; } |
194 | | - catch (error) { |
195 | | - console.log(error); |
196 | | - return; |
197 | | - } |
198 | | - |
199 | | - switch (key) { |
200 | | - case "1": pause(); break; |
201 | | - case "2": resume(); break; |
202 | | - case "3": getNextSlide(); break; |
203 | | - case "4": getPreviousSlide(); break; |
204 | | - case "5": // toggle visibilty |
205 | | - if (isSlideshowVisible) { |
206 | | - imageContainer.style.opacity = 0.0; |
207 | | - isSlideshowVisible = false; |
208 | | - } else { |
209 | | - imageContainer.style.opacity = 1.0; |
210 | | - isSlideshowVisible = true; |
211 | | - } |
212 | | - break; |
213 | | - case "6": // toggle pause |
214 | | - if (isSlideshowPaused) { |
215 | | - resume(); |
216 | | - } else { |
217 | | - pause(); |
218 | | - } |
219 | | - break; |
220 | | - case "7": restart(); break; |
221 | | - default: |
222 | | - console.log("Unhandled hotkey: " + key); |
223 | | - break; |
224 | | - } |
225 | | - }); |
| 225 | +}); |
226 | 226 |
|
227 | 227 | </script> |
0 commit comments