123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- 'use strict';
- (() => {
- function Show(parent, files, delay) {
- this.index = 0;
- this.elapsed = 0;
- this.playing = true;
- this.list = [];
- this.preloaded = {};
- this.updated = Date.now();
- this.delay = !isNaN(delay) ? delay : 3000;
- this.parent = parent;
- this.parent.innerHTML = '';
- this.parent.appendChild(document.createElement('img'));
- (() => {
- this.parse(files);
- window.requestAnimationFrame(() => { this.render(); });
- window.addEventListener('keyup', (e) => {
- if (!e.keyCode) return;
- if (e.keyCode === 32) { this.toggle(); }
- else if (e.keyCode === 37) { this.prev(); }
- else if (e.keyCode === 39) { this.next(); }
- }, false);
- })();
- };
- Show.prototype.toggle = function() { this.playing = !this.playing; };
- Show.prototype.next = function() {
- if (++this.index >= this.list.length) this.index = 0;
- this.elapsed = 0;
- };
- Show.prototype.prev = function() {
- if (--this.index < 0) this.index = this.list.length-1;
- this.elapsed = 0;
- };
- Show.prototype.format = function(f, a) {
- f = f.replaceAll('%s', a);
- for (let start = f.indexOf('%'); start != -1 && f.length > start+3 && f[start+3] === 's'; start = f.indexOf('%')) {
- let c = String(f[start+1]);
- let w = parseInt(f[start+2]);
- let r = (String(a).length >= w) ? a : (new Array(w).join(c)).concat(a).slice(-w);
- f = f.replace(f.slice(start, start+4), r);
- }
- return f
- };
- Show.prototype.generate = function(idx) {
- let o = this.list[idx];
- let el;
- if (o.file.endsWith('.mp4') || o.file.endsWith('.webm') || o.file.endsWith('.ogg')) {
- el = document.createElement('video');
- el.muted = true;
- el.loop = true;
- } else {
- el = document.createElement('img');
- }
- el.src = o.file;
- el.addEventListener('error', () => { this.list.splice(this.list.indexOf(o), 1); }, {once: true});
- return el;
- }
- Show.prototype.preload = function() {
- let partial = Math.max(Math.trunc(.1 * this.list.length), 1);
- let position = partial > this.index ? this.list.length - Math.abs(partial - this.index) : this.index - partial;
- for (let i = 0; i <= partial*2; i++) {
- let l = (position+i)%this.list.length;
- let o = this.list[l];
- if (!this.preloaded.hasOwnProperty(o.file)) {
- this.preloaded[o.file] = this.generate(l);
- setTimeout(() => { delete this.preloaded[o.file]; }, 2*(o.delay ? o.delay : this.delay));
- }
- }
- }
- Show.prototype.add = function(file, delay) {
- let o = {file: file, delay: delay};
- this.list.push(o);
- if (o.file.endsWith('.mp4') || o.file.endsWith('.webm') || o.file.endsWith('.ogg')) {
- let el = document.createElement('video');
- el.addEventListener('canplay', () => { o.delay = (isNaN(o.delay) ? 1 : o.delay) * Math.trunc(el.duration * 1000); }, {once: true});
- el.src = o.file;
- }
- }
- Show.prototype.parse = function(files) {
- if (!(files instanceof Array)) return;
- files.map((o) => {
- if (typeof o === 'string') {
- this.add(o);
- } else if (o instanceof Object && !(o instanceof Array) && typeof o.file !== 'undefined' && typeof o.file === 'string') {
- if (o.file.indexOf('%') > -1 && typeof o.range !== 'undefined' && o.range instanceof Array) {
- if (o.range.length === 2 && !(isNaN(o.range[2]) && isNaN(o.range[1])) && parseInt(o.range[0]) < parseInt(o.range[1])) {
- for (let i = parseInt(o.range[0]); i <= parseInt(o.range[1]); i++) {
- this.add(this.format(o.file, i), o.delay);
- }
- } else {
- for (let i of o.range) {
- this.add(this.format(o.file, i), o.delay);
- }
- }
- } else {
- this.add(o.file, o.delay);
- }
- }
- });
- };
- Show.prototype.render = function() {
- if (!this.list.length) return;
- let d = Date.now();
- let el = this.parent.firstChild;
- let vid = (el.tagName == 'VIDEO') ? 'video' : 'natural';
- this.parent.firstChild.className = (el[vid+'Width'] / el[vid+'Height'] < this.parent.clientWidth / this.parent.clientHeight) ? 'fillheight' : 'fillwidth';
- let o = this.list[this.index];
- if (this.current != this.index) {
- this.current = this.index;
- this.preload();
- let el = this.preloaded[o.file];
- let vid = (el.tagName == 'VIDEO') ? 'video' : 'natural';
- if (el.tagName !== this.parent.firstChild.tagName) this.parent.replaceChild(el.cloneNode(), this.parent.firstChild);
- if (vid === 'video') {
- if (this.parent.firstChild.src !== el.src) this.parent.firstChild.src = el.src;
- this.parent.firstChild.currentTime = 0;
- this.parent.firstChild.loop = true;
- this.parent.firstChild.muted = true;
- this.parent.firstChild.play();
- } else {
- this.parent.firstChild.src = el.src;
- }
- }
- if (this.playing && document.hasFocus()) this.elapsed += (d - this.updated);
- if (this.elapsed >= (o.delay | this.delay)) this.next();
- s.updated = d;
- window.requestAnimationFrame(() => { this.render(); });
- }
- window.Show = Show;
- })();
|