From b02b28d3e37d66e70de026f368350b8df57fe8c0 Mon Sep 17 00:00:00 2001 From: Leeingnyo Date: Mon, 16 Aug 2021 01:42:07 +0900 Subject: [PATCH 1/6] Implement to preload images when viewer is paged mode --- public/js/reader.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/public/js/reader.js b/public/js/reader.js index f2279728..a2a422a7 100644 --- a/public/js/reader.js +++ b/public/js/reader.js @@ -11,6 +11,7 @@ const readerComponent = () => { lastSavedPage: page, selectedIndex: 0, // 0: not selected; 1: the first page margin: 30, + preloadLookahead: 3, /** * Initialize the component by fetching the page dimensions @@ -52,6 +53,13 @@ const readerComponent = () => { if (savedMargin) { this.margin = savedMargin; } + + // Preload Images + this.preloadLookahead = 3; + const limit = Math.min(page + this.preloadLookahead, this.items.length + 1); + for (let idx = page + 1; idx <= limit; idx++) { + this.preloadImage(this.items[idx - 1].url); + } }) .catch(e => { const errMsg = `Failed to get the page dimensions. ${e}`; @@ -60,6 +68,12 @@ const readerComponent = () => { this.msg = errMsg; }) }, + /** + * Preload an image, which is expected to be cached + */ + preloadImage(url) { + (new Image()).src = url; + }, /** * Handles the `change` event for the page selector */ @@ -111,6 +125,10 @@ const readerComponent = () => { if (newIdx <= 0 || newIdx > this.items.length) return; + if (newIdx + this.preloadLookahead < this.items.length + 1) { + this.preloadImage(this.items[newIdx + this.preloadLookahead - 1].url); + } + this.toPage(newIdx); if (isNext) From d544252e3e54e61012d0f9e3b4c2a539f79db773 Mon Sep 17 00:00:00 2001 From: Leeingnyo Date: Wed, 18 Aug 2021 22:00:39 +0900 Subject: [PATCH 2/6] Add preload lookahead controller --- public/js/reader.js | 6 +++++- src/views/reader.html.ecr | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/public/js/reader.js b/public/js/reader.js index a2a422a7..79943af3 100644 --- a/public/js/reader.js +++ b/public/js/reader.js @@ -55,7 +55,7 @@ const readerComponent = () => { } // Preload Images - this.preloadLookahead = 3; + this.preloadLookahead = +localStorage.getItem('preloadLookahead') ?? 3; const limit = Math.min(page + this.preloadLookahead, this.items.length + 1); for (let idx = page + 1; idx <= limit; idx++) { this.preloadImage(this.items[idx - 1].url); @@ -305,6 +305,10 @@ const readerComponent = () => { marginChanged() { localStorage.setItem('margin', this.margin); this.toPage(this.selectedIndex); + }, + + preloadLookaheadChanged() { + localStorage.setItem('preloadLookahead', this.preloadLookahead); } }; } diff --git a/src/views/reader.html.ecr b/src/views/reader.html.ecr index 6b7bb114..9b4e6930 100644 --- a/src/views/reader.html.ecr +++ b/src/views/reader.html.ecr @@ -98,6 +98,13 @@ +
+ +
+ +
+
+
From 4d1ad8fb383ce0f5ae6910a232980b8fd05b4e6b Mon Sep 17 00:00:00 2001 From: Leeingnyo Date: Wed, 18 Aug 2021 21:22:34 +0900 Subject: [PATCH 3/6] Prevent load images --- src/views/reader.html.ecr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/views/reader.html.ecr b/src/views/reader.html.ecr index 9b4e6930..ff86d175 100644 --- a/src/views/reader.html.ecr +++ b/src/views/reader.html.ecr @@ -21,7 +21,7 @@
-