mixtape/static/js/song-controls.js

59 lines
1.8 KiB
JavaScript
Raw Normal View History

2024-02-16 15:31:30 +01:00
const controls = document.querySelector(".controls");
const song = document.querySelector("iframe.song").contentWindow;
2024-02-17 15:53:36 +01:00
// Autoscroll
2024-02-16 15:31:30 +01:00
var scroll;
2024-12-30 17:13:41 +01:00
var scrollTimeout = 60;
const minTimeout = 10;
const maxTimeout = 120;
const scrollIncrement = 20;
2024-02-16 15:31:30 +01:00
function pageScroll() {
song.scrollBy(0, 1);
2024-12-30 17:13:41 +01:00
scroll = setTimeout(pageScroll, scrollTimeout);
2024-02-16 15:31:30 +01:00
}
2024-12-30 17:13:41 +01:00
function updateScrollSpeed() {
if (controls.querySelector("#autoscroll").classList.contains("active")) {
2024-02-16 15:31:30 +01:00
clearTimeout(scroll);
2024-12-30 17:13:41 +01:00
scroll = setTimeout(pageScroll, scrollTimeout);
2024-02-16 15:31:30 +01:00
}
2024-12-30 17:13:41 +01:00
}
controls.querySelector("#autoscroll-increase").addEventListener("click", () => {
scrollTimeout = Math.max(minTimeout, scrollTimeout - scrollIncrement);
updateScrollSpeed();
});
controls.querySelector("#autoscroll-decrease").addEventListener("click", () => {
scrollTimeout = Math.min(maxTimeout, scrollTimeout + scrollIncrement);
updateScrollSpeed();
});
controls.querySelector("#autoscroll").addEventListener("click", function() {
2024-02-16 15:31:30 +01:00
this.classList.toggle("active");
2024-12-30 17:13:41 +01:00
if (this.classList.contains("active")) {
pageScroll();
} else {
clearTimeout(scroll);
}
2024-02-16 15:31:30 +01:00
});
2024-02-17 15:53:36 +01:00
// Scaling
function pageScale(value) {
if (value === 0) {
song.document.body.style.transform = "scale(1)";
return;
}
const currentScale = parseFloat(song.document.body.style.transform.split("scale(")[1]) || 1;
song.document.body.style.transform = "scale(" + (currentScale + value) + ")";
}
controls.querySelector("#font-size-increase").addEventListener("click", () => pageScale(0.1));
controls.querySelector("#font-size-decrease").addEventListener("click", () => pageScale(-0.1));
controls.querySelector("#font-size-reset").addEventListener("click", () => pageScale(0));
2024-12-30 17:13:41 +01:00
2024-02-16 15:31:30 +01:00
// Display the controls on JS-enabled browsers
window.addEventListener("load", () => controls.classList.remove = "hidden");