laundry-room-rave/laundry-room.js

59 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

2020-04-12 05:31:55 +00:00
var E = (function () {
const images = Array.of.apply([], document.getElementsByTagName("img"));
const main = document.getElementsByTagName("main")[0];
const overlays = images.filter(e => e.classList.contains("ov"));
const wms = images.filter(e => e.classList.contains("wm"));
const shark = images.find(e => e.classList.contains("shark"));
const BG_W = 1300;
const BG_H = 688;
var strobeIdx = ~~(Math.random() * 999);
const nOverlays = overlays.length;
function strobe() {
if (Math.random() < 0.5) {
strobeIdx++;
} else {
strobeIdx--;
}
strobeIdx = (strobeIdx + nOverlays) % nOverlays;
for (var i = 0; i < nOverlays; i++) {
overlays[i].style.visibility = (i == strobeIdx) ? "visible" : "hidden";
}
}
var time0 = +new Date;
function elapsed() { return (new Date - time0) / 1000; }
var lastStrobe = -Infinity;
var lastShake = wms.map(_ => Math.random() * 0.2);
function animate() {
requestAnimationFrame(animate);
var t = elapsed();
if (t - lastStrobe > 0.5) {
lastStrobe = t;
strobe();
}
for (var i = 0; i < wms.length; i++) {
if (t - lastShake[i] > 0.1) {
const dx = Math.random() * 0.2;
const dy = Math.random() * 0.2;
wms[i].style.transform = "translate(" + dx + "vw," + dy + "vw)";
lastShake[i] = t;
}
}
const shDy = 2 * Math.sin(elapsed() * 4.7);
const shRot = Math.sin(elapsed() * 7) * 3;
shark.style.transform = "translate(0," + shDy + "vw) rotate(" + shRot + "deg)";
}
animate();
return { main, overlays, shark, wms, strobe, animate };
})();