melpomene/melpomene.js

620 lines
14 KiB
JavaScript
Raw Normal View History

2023-05-21 21:07:40 +02:00
/* Melpomene webcomic reader JS */
2023-05-26 19:32:31 +02:00
/* Version 1.0.0_RC1 */
2023-05-19 18:15:34 +02:00
/* CC-BY-NC-SA : https://git.aribaud.net/caribaud/melpomene/ */
"use strict";
// ============
// CONTROLS
// ============
const MOVE_NEXT = "ArrowRight";
const MOVE_BACK = "ArrowLeft";
const TOGGLE_FULLSCREEN = "F";
const TOGGLE_PROGRESSBAR = "P";
const TOGGLE_VIEW_MODE = "V";
// ========================
// NAVIGATION CONSTANTS
// ========================
const MOUSEWHELL_MIN_DELAY = 50;
const DELAY_BEFORE_HIDDING_CONTROLS = 4000;
// ====================
// STATES CONSTANTS
// ====================
const MELPOMENE_VERSION = "1.0.0_UNSTABLE";
const READER_FRAME = document.getElementById("melpomene");
const READER_CONTENT_FRAME = document.getElementById("melpomene-content-frame");
const READER_PAGES = document.getElementById("melpomene-pages");
const FOCUS_OVERLAY_HEIGHT = document.getElementById("melpomene-focus");
const FOCUS_OVERLAY_WIDTH = document.getElementById("melpomene-focus-col");
const HELP_CONTROLS = document.getElementById("melpomene-help-menu");
const PROGRESS_BAR_CONTAINER = document.getElementById("melpomene-progress-container");
const PROGRESS_BAR = document.getElementById("melpomene-progress-bar");
const PROGRESS_BAR_PAGES = document.getElementById("melpomene-progress-sections");
const VERSION_DISPLAY = document.getElementById("melpomene-version");
2023-06-11 09:51:47 +02:00
// ====================
// INDEX CONSTANTS
// ====================
const ZOOM_PAGE_INDEX = 0;
const ZOOM_WIDTH_INDEX = 1;
const ZOOM_HEIGHT_INDEX = 2;
const ZOOM_X_INDEX = 3;
const ZOOM_Y_INDEX = 4;
// ===========================
// STATES GLOBAL VARIABLES
// ===========================
2023-06-11 09:51:47 +02:00
let PAGES_ZOOMS;
// The variable ZOOMS can either be defined by another JS file or contructed at init
2023-06-05 00:20:32 +02:00
if (typeof PAGES_ZOOMS === "undefined")
{
PAGES_ZOOMS = null;
}
2023-06-11 09:51:47 +02:00
let CURRENT_ZOOM = 0;
let CURRENT_PAGE = 1;
let CURRENT_WIDTH = 0;
let CURRENT_HEIGHT = 0;
let CURRENT_X = 0;
let CURRENT_Y = 0;
2023-04-15 13:12:49 +02:00
2023-06-11 09:51:47 +02:00
let IS_PAGE_MODE = false;
let MOUSEWHELL_WAIT = false;
2023-04-16 17:43:37 +02:00
2023-04-16 21:35:32 +02:00
// =============
// UTILITIES
// =============
2023-04-15 13:12:49 +02:00
2023-06-05 00:17:23 +02:00
// Dimensions utilites
// -------------------
function getPagesCount()
{
return READER_PAGES.childElementCount;
}
function pageOriginalHeight(pageNumber)
{
return READER_PAGES.children[pageNumber - 1].naturalHeight;
}
function pageOriginalWidth(pageNumber)
{
return READER_PAGES.children[pageNumber - 1].naturalWidth;
}
function readerFrameRatio()
{
return READER_CONTENT_FRAME.clientWidth / READER_CONTENT_FRAME.clientHeight;
}
function pageMaxHeight()
{
let maxHeight = 0;
2023-06-05 00:40:25 +02:00
for (let pageIdx = 0; pageIdx < READER_PAGES.children.length; pageIdx += 1)
2023-06-05 00:17:23 +02:00
{
2023-06-05 00:40:25 +02:00
if (READER_PAGES.children[pageIdx].naturalHeight > maxHeight)
2023-06-05 00:17:23 +02:00
{
2023-06-05 00:40:25 +02:00
maxHeight = READER_PAGES.children[pageIdx].naturalHeight;
2023-06-05 00:17:23 +02:00
}
}
return maxHeight;
}
function pageVerticalOffset(pageNumber)
{
2023-06-05 00:40:25 +02:00
return (pageMaxHeight() - pageOriginalHeight(pageNumber)) / 2;
2023-06-05 00:17:23 +02:00
}
function previousPagesWidth(pageNumber)
{
// The width of all previous pages relative to the provided index
let totalWidth = 0;
for (let idx = 0; idx < pageNumber - 1; idx += 1)
{
totalWidth += READER_PAGES.children[idx].naturalWidth;
}
return totalWidth;
}
// Zooms utilites
// --------------
function globalZoomScale()
{
if (READER_PAGES.dataset.globalZoomScale !== undefined)
{
return parseFloat(READER_PAGES.dataset.globalZoomScale);
}
return 1.0;
}
function globalZoomOffsetX()
{
if (READER_PAGES.dataset.globalZoomOffset !== undefined)
{
2023-06-05 00:20:32 +02:00
return parseFloat(READER_PAGES.dataset.globalZoomOffset.split(",")[0]);
}
return 0.0;
}
function globalZoomOffsetY()
{
if (READER_PAGES.dataset.globalZoomOffset !== undefined)
{
2023-06-05 00:20:32 +02:00
return parseFloat(READER_PAGES.dataset.globalZoomOffset.split(",")[1]);
}
return 0.0;
}
function loadZoomsFromImgTagsIfRequired()
{
// Zooms may be defined by another JS file
if (PAGES_ZOOMS === null)
{
PAGES_ZOOMS = [];
// parse the data-zooms of each img and and the page number info
for (let idx = 0; idx < READER_PAGES.children.length; idx += 1)
{
const zoomsRawData = READER_PAGES.children[idx].dataset.zooms;
2023-06-05 00:20:32 +02:00
// ";" separates zooms data, "," separates values
// We add the page number (adding 1 because of indexing)
const zooms = zoomsRawData.split(";").map(
2023-06-05 00:40:25 +02:00
(zoom) => [idx + 1].concat(
2023-06-05 00:20:32 +02:00
zoom.split(",").map(
2023-06-05 00:40:25 +02:00
(value) => parseFloat(value)
)
)
);
PAGES_ZOOMS = PAGES_ZOOMS.concat(zooms);
}
}
}
function getFirstZoomOfPage(pageNumber)
{
for (let zoomIdx = 0; zoomIdx < PAGES_ZOOMS.length; zoomIdx += 1)
{
if (PAGES_ZOOMS[zoomIdx][0] === pageNumber)
{
return zoomIdx;
2023-04-15 13:12:49 +02:00
}
}
2023-06-05 00:40:25 +02:00
return undefined;
2023-04-15 13:12:49 +02:00
}
function getZoomCountForPage(pageNumber)
{
2023-06-05 00:40:25 +02:00
return PAGES_ZOOMS.filter((zoom) => zoom[0] === pageNumber).length;
2023-04-17 19:33:41 +02:00
}
function getCurrentZoomIndexForPage()
{
2023-06-05 00:40:25 +02:00
const previousZoomsCount = PAGES_ZOOMS.filter((zoom) => zoom[0] < CURRENT_PAGE).length;
return CURRENT_ZOOM - previousZoomsCount + 1;
2023-04-17 19:33:41 +02:00
}
function getReadingProgressPercent()
{
const progressPerPage = 1 / getPagesCount();
if (IS_PAGE_MODE)
{
return 100 * progressPerPage * CURRENT_PAGE;
2023-04-17 19:33:41 +02:00
}
const progressPerZoom = progressPerPage / getZoomCountForPage(CURRENT_PAGE);
2023-06-05 00:40:25 +02:00
let readingProgress = (CURRENT_PAGE - 1) * progressPerPage;
readingProgress += getCurrentZoomIndexForPage() * progressPerZoom;
return 100 * readingProgress;
2023-04-17 19:33:41 +02:00
}
function updateProgressBar()
{
2023-06-05 00:40:25 +02:00
PROGRESS_BAR.style.width = `${getReadingProgressPercent()}%`;
2023-04-17 19:33:41 +02:00
}
2023-04-15 13:12:49 +02:00
// =========
// ACTIONS
// =========
2023-06-05 00:17:23 +02:00
function updateFocusByWidth(width)
{
2023-06-05 00:17:23 +02:00
FOCUS_OVERLAY_WIDTH.style.width = `${width / READER_CONTENT_FRAME.clientWidth * 100}%`;
FOCUS_OVERLAY_HEIGHT.style.height = "100%";
2023-04-15 13:12:49 +02:00
}
2023-06-05 00:17:23 +02:00
function updateFocusByHeight(height)
{
FOCUS_OVERLAY_WIDTH.style.width = "100%";
FOCUS_OVERLAY_HEIGHT.style.height = `${height / READER_CONTENT_FRAME.clientHeight * 100}%`;
}
2023-04-15 13:12:49 +02:00
function moveReaderDisplayToArea(pageNumber, oWidth, oHeight, oPosx, oPosy)
{
// Keep original values for registering
let width = oWidth;
let height = oHeight;
let posx = oPosx;
let posy = oPosy;
// Apply global offsets before scales if we are displaying a zoom
// Pages display uses width & height = 0
if (width !== 0 || height !== 0)
{
width = width * globalZoomScale();
height = height * globalZoomScale();
posx = (posx + globalZoomOffsetX()) * globalZoomScale();
posy = (posy + globalZoomOffsetY()) * globalZoomScale();
}
// reduce width if offset sent us outside of page
if (posx < 0)
{
width = width + posx;
posx = 0;
}
2023-06-04 21:38:56 +02:00
2023-06-05 00:40:25 +02:00
if (posx + width > pageOriginalWidth(pageNumber))
{
width = pageOriginalWidth(pageNumber) - posx;
}
// reduce height if offset sent us outside of page
if (posy < 0)
{
height = height + posy;
posy = 0;
}
2023-06-04 21:38:56 +02:00
2023-06-05 00:40:25 +02:00
if (posy + height > pageOriginalHeight(pageNumber))
{
height = pageOriginalHeight(pageNumber) - posy;
}
2023-05-25 21:31:22 +02:00
// Align the top-left corner of the frame with the page
2023-06-05 00:16:02 +02:00
READER_PAGES.style.transform = `translate(-${previousPagesWidth(pageNumber)}px, -${pageVerticalOffset(pageNumber)}px)`;
2023-05-22 00:19:12 +02:00
// Then move so the top-left point of the zoom match the frame top-left
2023-06-05 00:16:02 +02:00
READER_PAGES.style.transform = `translate(${-posx}px, ${-posy}px) ${READER_PAGES.style.transform}`;
2023-05-22 00:19:12 +02:00
// Then, scale so the zoom would fit the frame, and center the zoom
if (width === 0)
{
width = pageOriginalWidth(pageNumber);
2023-04-15 13:12:49 +02:00
}
if (height === 0)
{
height = pageOriginalHeight(pageNumber);
2023-04-15 13:12:49 +02:00
}
const zoomRatio = width / height;
if (readerFrameRatio() > zoomRatio)
{
2023-05-22 00:19:12 +02:00
// Frame wider than zoom => scale so heights are the same, offset on x
const zoomToFrameScaleFactor = READER_CONTENT_FRAME.clientHeight / height;
2023-06-05 00:16:02 +02:00
READER_PAGES.style.transform = `scale(${zoomToFrameScaleFactor}) ${READER_PAGES.style.transform}`;
const scaledWidth = width * zoomToFrameScaleFactor;
const offset = (READER_CONTENT_FRAME.clientWidth - scaledWidth) / 2;
2023-06-05 00:16:02 +02:00
READER_PAGES.style.transform = `translateX(${offset}px) ${READER_PAGES.style.transform}`;
updateFocusByWidth(scaledWidth);
2023-04-15 13:12:49 +02:00
}
2023-06-04 21:38:56 +02:00
else
{
// Frame narower than zoom => scale so left/right match, offset on y
const zoomToFrameScaleFactor = READER_CONTENT_FRAME.clientWidth / width;
2023-06-05 00:16:02 +02:00
READER_PAGES.style.transform = `scale(${zoomToFrameScaleFactor}) ${READER_PAGES.style.transform}`;
const scaledHeight = height * zoomToFrameScaleFactor;
const offset = (READER_CONTENT_FRAME.clientHeight - scaledHeight) / 2;
2023-06-05 00:16:02 +02:00
READER_PAGES.style.transform = `translateY(${offset}px) ${READER_PAGES.style.transform}"`;
updateFocusByHeight(scaledHeight);
}
// Use values before global offset / scale
CURRENT_PAGE = pageNumber;
CURRENT_WIDTH = oWidth;
CURRENT_HEIGHT = oHeight;
CURRENT_X = oPosx;
CURRENT_Y = oPosy;
2023-04-15 13:12:49 +02:00
}
function refreshReaderDisplay()
{
2023-06-05 00:40:25 +02:00
moveReaderDisplayToArea(
CURRENT_PAGE,
CURRENT_WIDTH,
CURRENT_HEIGHT,
CURRENT_X,
CURRENT_Y
);
2023-04-17 20:45:46 +02:00
}
2023-04-15 13:12:49 +02:00
function moveReaderDisplayToPage(pageNumber)
{
moveReaderDisplayToArea(pageNumber, 0, 0, 0, 0);
2023-04-15 13:12:49 +02:00
}
2023-06-11 09:51:47 +02:00
function moveReaderDisplayToZoom(zoomIdx)
{
2023-06-05 00:40:25 +02:00
moveReaderDisplayToArea(
2023-06-11 09:51:47 +02:00
PAGES_ZOOMS[zoomIdx][ZOOM_PAGE_INDEX],
PAGES_ZOOMS[zoomIdx][ZOOM_WIDTH_INDEX],
PAGES_ZOOMS[zoomIdx][ZOOM_HEIGHT_INDEX],
PAGES_ZOOMS[zoomIdx][ZOOM_X_INDEX],
PAGES_ZOOMS[zoomIdx][ZOOM_Y_INDEX]
2023-06-05 00:40:25 +02:00
);
2023-06-11 09:51:47 +02:00
CURRENT_ZOOM = zoomIdx;
2023-04-15 13:12:49 +02:00
}
function toggleViewMode()
{
if (IS_PAGE_MODE)
{
2023-06-05 00:16:02 +02:00
if (CURRENT_ZOOM === null)
{
2023-06-05 00:16:02 +02:00
moveReaderDisplayToZoom(getFirstZoomOfPage(CURRENT_PAGE));
2023-04-17 20:59:32 +02:00
}
2023-06-04 21:38:56 +02:00
else
{
2023-06-05 00:16:02 +02:00
moveReaderDisplayToZoom(CURRENT_ZOOM);
}
2023-06-04 21:38:56 +02:00
IS_PAGE_MODE = false;
2023-04-17 20:59:32 +02:00
}
2023-06-04 21:38:56 +02:00
else
{
moveReaderDisplayToPage(CURRENT_PAGE);
IS_PAGE_MODE = true;
}
updateProgressBar();
2023-04-17 20:59:32 +02:00
}
function moveReader(toNext)
{
if (IS_PAGE_MODE)
{
if (toNext && CURRENT_PAGE < getPagesCount())
{
moveReaderDisplayToPage(CURRENT_PAGE + 1);
CURRENT_ZOOM = null;
2023-04-15 13:12:49 +02:00
}
else if (!toNext && CURRENT_PAGE > 1)
{
moveReaderDisplayToPage(CURRENT_PAGE - 1);
CURRENT_ZOOM = null;
2023-04-15 13:12:49 +02:00
}
2023-06-05 00:16:02 +02:00
updateProgressBar();
}
2023-06-04 21:38:56 +02:00
2023-06-05 00:16:02 +02:00
// Zoom mode
else
{
if (toNext && CURRENT_ZOOM < PAGES_ZOOMS.length - 1)
{
moveReaderDisplayToZoom(CURRENT_ZOOM + 1);
2023-04-17 20:59:32 +02:00
}
else if (!toNext && CURRENT_ZOOM > 0)
{
moveReaderDisplayToZoom(CURRENT_ZOOM - 1);
2023-04-15 13:12:49 +02:00
}
2023-06-05 00:16:02 +02:00
updateProgressBar();
}
2023-04-15 13:12:49 +02:00
}
2023-06-05 00:17:23 +02:00
function initReader()
{
VERSION_DISPLAY.innerText = VERSION_DISPLAY.innerText.replace("Unknown version", MELPOMENE_VERSION);
loadZoomsFromImgTagsIfRequired();
moveReaderDisplayToZoom(0);
// Smoothly show pictures when they intersect with the viewport
const visibilityObserver = new IntersectionObserver(
(entries, _observer) =>
{
entries.forEach((entry) =>
{
if (entry.isIntersecting)
{
entry.target.style.opacity = 1;
entry.target.style.visibility = "visible";
}
else
{
entry.target.style.opacity = 0;
entry.target.style.visibility = "hidden";
}
});
},
2023-06-05 00:40:25 +02:00
{
root: READER_CONTENT_FRAME,
rootMargin: "-10px"
}
2023-06-05 00:17:23 +02:00
);
2023-06-05 00:40:25 +02:00
for (let pageIndex = 0; pageIndex < READER_PAGES.children.length; pageIndex += 1)
2023-06-05 00:17:23 +02:00
{
2023-06-05 00:40:25 +02:00
const img = READER_PAGES.children[pageIndex];
2023-06-05 00:17:23 +02:00
visibilityObserver.observe(img);
PROGRESS_BAR_PAGES.appendChild(document.createElement("div"));
}
READER_PAGES.style.display = "flex";
setTimeout(
2023-06-05 00:40:25 +02:00
() => { READER_PAGES.hidden = false; },
2023-06-05 00:17:23 +02:00
"300"
);
setTimeout(
() =>
{
HELP_CONTROLS.style.opacity = null;
HELP_CONTROLS.style.transform = null;
},
DELAY_BEFORE_HIDDING_CONTROLS
);
}
2023-04-16 21:35:32 +02:00
// =============
// CALLBACKS
// =============
function handleKeyPress(key)
{
if (key === MOVE_NEXT)
{
moveReader(true);
2023-04-15 13:12:49 +02:00
}
else if (key === MOVE_BACK)
{
moveReader(false);
2023-04-15 13:12:49 +02:00
}
else if (key.toUpperCase() === TOGGLE_FULLSCREEN)
{
if (document.fullscreenElement === null)
{
2023-04-16 17:43:37 +02:00
READER_FRAME.requestFullscreen();
}
2023-06-04 21:38:56 +02:00
else
{
document.exitFullscreen();
}
2023-04-16 17:43:37 +02:00
}
else if (key.toUpperCase() === TOGGLE_PROGRESSBAR)
{
if (PROGRESS_BAR_CONTAINER.hidden === true)
{
PROGRESS_BAR_CONTAINER.hidden = false;
2023-04-17 20:45:46 +02:00
}
2023-06-04 21:38:56 +02:00
else
{
PROGRESS_BAR_CONTAINER.hidden = true;
}
2023-06-04 21:38:56 +02:00
2023-04-17 20:45:46 +02:00
refreshReaderDisplay();
}
2023-04-16 17:43:37 +02:00
else if (key.toUpperCase() === TOGGLE_VIEW_MODE)
{
toggleViewMode();
}
2023-04-16 17:43:37 +02:00
}
2023-04-15 13:12:49 +02:00
function handleMouseWhell(event)
{
2023-05-26 23:32:11 +02:00
// Only handle scroll event if the target is the nav controls
// to avoid preventing page scrolling.
// Do disable page scrolling when we do prev/next, though
2023-06-05 00:16:02 +02:00
if (!READER_FRAME.contains(event.target))
{
return;
2023-05-26 23:32:11 +02:00
}
event.preventDefault();
event.stopPropagation();
if (MOUSEWHELL_WAIT)
{
return;
2023-04-16 17:43:37 +02:00
}
2023-06-04 21:38:56 +02:00
2023-06-05 00:16:02 +02:00
MOUSEWHELL_WAIT = true;
setTimeout(
() => { MOUSEWHELL_WAIT = false; },
MOUSEWHELL_MIN_DELAY
);
if (event.deltaY > 0)
{
moveReader(true, false);
}
else
{
moveReader(false, false);
2023-04-16 17:43:37 +02:00
}
2023-04-15 13:12:49 +02:00
}
// ======
// INIT
// ======
window.addEventListener(
"load",
2023-06-04 21:37:56 +02:00
(_event) => { initReader(); }
);
addEventListener(
"resize",
2023-06-04 21:37:56 +02:00
(_event) => { refreshReaderDisplay(); }
);
addEventListener(
"keydown",
(event) =>
{
2023-06-04 21:37:56 +02:00
handleKeyPress(event.key, event.shiftKey);
}
);
2023-04-16 17:43:37 +02:00
addEventListener(
"wheel",
2023-06-04 21:37:56 +02:00
(event) => { handleMouseWhell(event); },
{ passive: false }
);