301 lines
7.3 KiB
HTML
Executable File
301 lines
7.3 KiB
HTML
Executable File
<html>
|
|
<script>'<base href="BASEPATH">';</script>
|
|
<body>
|
|
<style>
|
|
body {
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
|
|
#photo {
|
|
position: fixed;
|
|
display: inline-block;
|
|
top: 0;
|
|
right: 0;
|
|
bottom: 0;
|
|
left: 0;
|
|
z-index: 0;
|
|
background-repeat: no-repeat;
|
|
background-position: 50% 50%;
|
|
background-size: contain;
|
|
background-color: #222;
|
|
}
|
|
|
|
#placeholder {
|
|
position: absolute;
|
|
left: -1000px;
|
|
display: none;
|
|
}
|
|
|
|
#footer {
|
|
position: absolute;
|
|
bottom: 0px;
|
|
left: 0px;
|
|
right: 0px;
|
|
opacity: 0.6;
|
|
background: linear-gradient(45deg, rgba(16, 16, 16, 1), transparent);
|
|
color: white;
|
|
font-size: 1.5em;
|
|
line-height: 1.5em;
|
|
font-family: Arial;
|
|
box-shadow: 0px -0.25em 1em black;
|
|
text-shadow: 0 1px 0 black;
|
|
z-index: 100;
|
|
display: flex;
|
|
flex-direction: row;
|
|
justify-content: space-between;
|
|
}
|
|
|
|
#info {
|
|
padding: 0.5em 1em;
|
|
}
|
|
|
|
#loading {
|
|
padding: 0.5em 1em;
|
|
}
|
|
|
|
</style>
|
|
<img id="placeholder"></img>
|
|
<div id="photo">
|
|
</div>
|
|
<div id="footer">
|
|
<div id="info">Loading photoset...</div>
|
|
<div id="loading"></div>
|
|
</div>
|
|
<script>
|
|
var base, placeholder, info, photos = [], photoIndex;
|
|
|
|
function shuffle(array) {
|
|
var index = array.length, tmp, random;
|
|
|
|
while (index) {
|
|
random = Math.floor(Math.random() * index);
|
|
index--;
|
|
|
|
// And swap it with the current element.
|
|
tmp = array[index];
|
|
array[index] = array[random];
|
|
array[random] = tmp;
|
|
}
|
|
|
|
return array;
|
|
}
|
|
|
|
var countdown = 15;
|
|
|
|
const days = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ],
|
|
months = [ "January", "February", "March", "April", "May", "June",
|
|
"July", "August", "September", "October", "November", "December" ];
|
|
|
|
let activeFaces = [];
|
|
|
|
function makeFaceBoxes() {
|
|
const el = document.getElementById("photo");
|
|
|
|
let width, height, offsetLeft = 0, offsetTop = 0;
|
|
|
|
/* If photo is wider than viewport, it will be 100% width and < 100% height */
|
|
if (photo.width / photo.height > el.offsetWidth / el.offsetHeight) {
|
|
console.log("A");
|
|
width = 100;
|
|
height = 100 * photo.height / photo.width * el.offsetWidth / el.offsetHeight;
|
|
offsetLeft = 0;
|
|
offsetTop = (100 - height) * 0.5;
|
|
} else {
|
|
console.log("B");
|
|
width = 100 * photo.width / photo.height * el.offsetHeight / el.offsetWidth;
|
|
height = 100;
|
|
offsetLeft = (100 - width) * 0.5;
|
|
offsetTop = 0;
|
|
}
|
|
|
|
|
|
activeFaces.forEach((face) => {
|
|
const box = document.createElement("div");
|
|
box.classList.add("face");
|
|
document.body.appendChild(box);
|
|
box.style.position = "absolute";
|
|
box.style.display = "inline-block";
|
|
box.style.border = "1px solid red";
|
|
box.style.background = "rgba(255, 0, 0, 0.5)";
|
|
box.style.opacity = 0.5;
|
|
box.style.left = offsetLeft + Math.floor(face.left * width) + "%";
|
|
box.style.top = offsetTop + Math.floor(face.top * height) + "%";
|
|
box.style.width = Math.floor((face.right - face.left) * width) + "%";
|
|
box.style.height = Math.floor((face.bottom - face.top) * height) + "%";
|
|
});
|
|
}
|
|
|
|
function loadPhoto(index) {
|
|
const photo = photos[index],
|
|
xml = new XMLHttpRequest(),
|
|
url = base + photo.path /* + "thumbs/scaled/"*/ + photo.filename,
|
|
taken = new Date(photo.taken);
|
|
|
|
document.getElementById("loading").textContent = "0%";
|
|
|
|
xml.onprogress = function (event) {
|
|
var alpha = 0;
|
|
if (event.total) {
|
|
alpha = event.loaded / event.total;
|
|
document.getElementById("loading").textContent = Math.ceil(100 * alpha) + "%";
|
|
} else {
|
|
document.getElementById("loading").textContent = "0%";
|
|
}
|
|
};
|
|
|
|
xml.onload = function(event) {
|
|
info.textContent =
|
|
days[taken.getDay()] + ", " +
|
|
months[taken.getMonth()] + " " +
|
|
taken.getDate() + " " +
|
|
taken.getFullYear();
|
|
document.getElementById("photo").style.backgroundImage = "url(" + encodeURI(url) + ")";
|
|
countdown = 15;
|
|
tick();
|
|
Array.prototype.forEach.call(document.querySelectorAll('.face'), (el) => {
|
|
el.parentElement.removeChild(el);
|
|
});
|
|
window.fetch("api/v1/photos/faces/" + photo.id).then(res => res.json()).then((faces) => {
|
|
activeFaces = faces;
|
|
makeFaceBoxes();
|
|
}).catch(function(error) {
|
|
console.error(error);
|
|
info.textContent += "Unable to obtain face information :(";
|
|
});
|
|
}
|
|
|
|
xml.onerror = function(event) {
|
|
info.textContent = "Error loading photo. Trying next photo.";
|
|
nextPhoto();
|
|
}
|
|
|
|
xml.open("GET", url, true);
|
|
xml.send();
|
|
}
|
|
|
|
function nextPhoto() {
|
|
photoIndex = (photoIndex + 1) % photos.length;
|
|
loadPhoto(photoIndex);
|
|
}
|
|
|
|
var scheduled = false;
|
|
|
|
function tick() {
|
|
if (scheduled) {
|
|
clearTimeout(scheduled);
|
|
}
|
|
document.getElementById("loading").textContent = countdown + "s";
|
|
if (countdown > 0) {
|
|
/* If there is a timer running, then decrement the counter */
|
|
if (scheduled) {
|
|
countdown--;
|
|
}
|
|
scheduled = setTimeout(tick, 1000);
|
|
} else {
|
|
scheduled = null;
|
|
countdown = 15;
|
|
nextPhoto();
|
|
}
|
|
}
|
|
|
|
function schedule() {
|
|
if (scheduled) {
|
|
clearTimeout(scheduled);
|
|
}
|
|
tick();
|
|
}
|
|
|
|
function toggleFullscreen() {
|
|
if (!document.fullscreenElement) {
|
|
document.documentElement.requestFullscreen();
|
|
} else {
|
|
if (document.exitFullscreen) {
|
|
document.exitFullscreen();
|
|
}
|
|
}
|
|
}
|
|
|
|
var paused = false,
|
|
tap = 0;
|
|
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
var tmp = document.querySelector("base");
|
|
if (tmp) {
|
|
base = new URL(tmp.href).pathname.replace(/\/$/, "") + "/"; /* Make sure there is a trailing slash */
|
|
} else {
|
|
base = "/";
|
|
}
|
|
|
|
document.addEventListener("click", function(event) {
|
|
toggleFullscreen();
|
|
var now = new Date().getTime();
|
|
if (tap && (now - tap < 300)) {
|
|
toggleFullscreen();
|
|
tap = 0;
|
|
} else {
|
|
tap = new Date().getTime();
|
|
}
|
|
});
|
|
|
|
document.addEventListener("keydown", function(event) {
|
|
switch (event.keyCode) {
|
|
case 32: /* space */
|
|
paused = !paused;
|
|
if (!paused) {
|
|
tick();
|
|
} else {
|
|
document.getElementById("loading").textContent = "||";
|
|
clearTimeout(scheduled);
|
|
scheduled = null;
|
|
}
|
|
return;
|
|
|
|
case 37: /* left */
|
|
if (photoIndex == 0) {
|
|
photoIndex = photos.length;
|
|
}
|
|
photoIndex--;
|
|
loadPhoto(photoIndex);
|
|
return;
|
|
|
|
case 39: /* right */
|
|
photoIndex = (photoIndex + 1) % photos.length;
|
|
loadPhoto(photoIndex);
|
|
return;
|
|
|
|
case 13: /* enter */
|
|
toggleFullscreen();
|
|
return;
|
|
}
|
|
|
|
console.log(event.keyCode);
|
|
});
|
|
|
|
info = document.getElementById("info");
|
|
|
|
/* Trim off everything up to and including the ? (if its there) */
|
|
var parts = window.location.href.match(/^.*\?(.*)$/);
|
|
if (parts) {
|
|
holiday = parts[1];
|
|
} else {
|
|
holiday = "memorial day";
|
|
}
|
|
window.fetch("api/v1/photos/holiday/" + holiday.replace(/ +/g, "%20"))
|
|
.then(res => res.json()).then(function(data) {
|
|
if (data.items.length) {
|
|
info.textContent = photos.length + " photos found. Shuffling.";
|
|
photos = shuffle(data.items);
|
|
photoIndex = -1;
|
|
nextPhoto();
|
|
} else {
|
|
info.textContent = "No photos found for " + data.holiday + ".";
|
|
}
|
|
}).catch(function(error) {
|
|
console.error(error);
|
|
info.textContent = "Unable to fetch holiday :(";
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|