feat(viewer): マウスホイール送り・順次バックグラウンド先行ダウンロード・スムーズ切替遷移を実装

This commit is contained in:
armonica committed 2026-10-01 10:51:30 +09:00
1 parent 9fb915c882
commit 3183924b95
1 file changed
+279 -29
@@ -844,13 +844,21 @@ public static class UnifiedHtmlRenderer
tagsHtml = $"<div class=\"info-tags\">{tagItems}</div>";
}
// 初期表示時は先頭2ページのみ直読み込みし、以降は順次ダウンローダーで1ページ目から順次ロード・表示
var imagesHtml = string.Join("\n", gallery.Images.Select(img =>
{
var imgUrl = Encoder.Encode(img.ImageUrl);
var altText = Encoder.Encode(img.AltText ?? $"{gallery.Title} {img.PageNumber}ページ");
var initialSrc = img.PageNumber <= 2 ? $"src=\"{imgUrl}\"" : "src=\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1'%3E%3C/svg%3E\"";
var loadedClass = img.PageNumber <= 2 ? "" : "";
return $$"""
<div class="scroll-image-wrapper" data-page="{{img.PageNumber}}">
<img src="{{imgUrl}}" loading="lazy" decoding="async" alt="{{altText}}" onload="this.classList.add('loaded')" />
<div class="scroll-image-placeholder">
<span class="placeholder-spinner"></span>
<span>{{img.PageNumber}} / {{totalPages}} を読み込み中...</span>
</div>
<img {{initialSrc}} data-src="{{imgUrl}}" decoding="async" alt="{{altText}}" onload="if(!this.src.startsWith('data:')){ this.classList.add('loaded'); this.parentElement.classList.add('loaded'); }" />
<div class="page-badge">{{img.PageNumber}} / {{totalPages}}</div>
</div>
""";
@@ -977,11 +985,48 @@ public static class UnifiedHtmlRenderer
width: 100%;
display: flex;
justify-content: center;
background: rgba(0, 0, 0, 0.3);
background: rgba(15, 20, 34, 0.6);
border-radius: 12px;
overflow: hidden;
box-shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
min-height: 200px;
min-height: 380px;
transition: min-height 0.3s ease;
}
.scroll-image-wrapper.loaded {
min-height: auto;
}
.scroll-image-placeholder {
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 12px;
color: var(--text-subtle);
font-size: 0.85rem;
pointer-events: none;
transition: opacity 0.3s ease;
}
.placeholder-spinner {
width: 24px;
height: 24px;
border: 2px solid rgba(255, 255, 255, 0.1);
border-top-color: var(--accent-primary);
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.scroll-image-wrapper.loaded .scroll-image-placeholder {
opacity: 0;
visibility: hidden;
}
.scroll-image-wrapper img {
@@ -989,7 +1034,7 @@ public static class UnifiedHtmlRenderer
height: auto;
display: block;
opacity: 0;
transition: opacity 0.3s ease;
transition: opacity 0.35s ease;
}
.scroll-image-wrapper img.loaded {
@@ -1037,6 +1082,7 @@ public static class UnifiedHtmlRenderer
justify-content: center;
position: relative;
user-select: none;
overflow: hidden;
}
.slide-image {
@@ -1044,14 +1090,21 @@ public static class UnifiedHtmlRenderer
max-height: 100%;
object-fit: contain;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.8);
transition: transform 0.15s ease-out;
transition: opacity 0.18s cubic-bezier(0.4, 0, 0.2, 1), transform 0.18s cubic-bezier(0.4, 0, 0.2, 1);
opacity: 1;
transform: scale(1);
}
.slide-image.switching {
opacity: 0;
transform: scale(0.985);
}
.slide-nav-zone {
position: absolute;
top: 0;
bottom: 0;
width: 30%;
width: 32%;
z-index: 10;
cursor: pointer;
display: flex;
@@ -1090,6 +1143,11 @@ public static class UnifiedHtmlRenderer
color: #fff;
font-size: 1.2rem;
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.6);
transition: transform 0.2s ease;
}
.slide-nav-zone:hover .nav-arrow-icon {
transform: scale(1.1);
}
/* サムネイルドロワー & オーバーレイ */
@@ -1410,7 +1468,110 @@ public static class UnifiedHtmlRenderer
const toolbarFullscreenBtn = document.getElementById('toolbarFullscreenBtn');
const thumbItems = document.querySelectorAll('.thumb-item');
// モード切替(縦スクロール ⇔ スライド)
// ==========================================
// 1. 順次先行ダウンロードマネージャー
// 1ページ目から順番にキャッシュロードを進行させ、
// 縦スクロール・スライド双方に即座に反映します。
// ==========================================
class SequentialImageDownloader {
constructor(images) {
this.images = images || [];
this.loadedPages = new Set();
this.loadingPages = new Set();
this.priorityQueue = [];
this.cursor = 0;
this.concurrency = 2; // 通信詰まりを起こさず最高効率で先読み
this.activeCount = 0;
}
start() {
this.pump();
}
prioritize(page) {
if (page < 1 || page > this.images.length) return;
if (this.loadedPages.has(page) || this.loadingPages.has(page)) return;
this.priorityQueue = this.priorityQueue.filter(p => p !== page);
this.priorityQueue.unshift(page);
this.pump();
}
pump() {
while (this.activeCount < this.concurrency) {
let targetPage = null;
if (this.priorityQueue.length > 0) {
targetPage = this.priorityQueue.shift();
} else if (this.cursor < this.images.length) {
targetPage = this.images[this.cursor].pageNumber;
this.cursor++;
}
if (targetPage === null) break;
if (this.loadedPages.has(targetPage) || this.loadingPages.has(targetPage)) {
continue;
}
this.loadPage(targetPage);
}
}
loadPage(page) {
const item = this.images.find(img => img.pageNumber === page);
if (!item) return;
this.loadingPages.add(page);
this.activeCount++;
const preImg = new Image();
preImg.decoding = 'async';
const finalize = (success) => {
this.loadingPages.delete(page);
this.activeCount--;
if (success) {
this.loadedPages.add(page);
this.applyLoadedImage(page, item.imageUrl);
}
this.pump();
};
preImg.onload = () => finalize(true);
preImg.onerror = () => finalize(false);
preImg.src = item.imageUrl;
}
applyLoadedImage(page, url) {
// 縦スクロール要素に反映
const wrapper = document.querySelector(`.scroll-image-wrapper[data-page="${page}"]`);
if (wrapper) {
const img = wrapper.querySelector('img');
if (img && img.getAttribute('src') !== url) {
img.src = url;
}
img?.classList.add('loaded');
wrapper.classList.add('loaded');
}
// スライドモードで現在そのページを表示中の場合
if (isSlideMode && currentPage === page) {
if (currentSlideImage.getAttribute('src') !== url) {
currentSlideImage.src = url;
}
currentSlideImage.classList.remove('switching');
}
}
isLoaded(page) {
return this.loadedPages.has(page);
}
}
const downloader = new SequentialImageDownloader(galleryData.images);
// ==========================================
// 2. モード切替(縦スクロール ⇔ スライド)
// ==========================================
function toggleMode() {
isSlideMode = !isSlideMode;
if (isSlideMode) {
@@ -1428,7 +1589,9 @@ public static class UnifiedHtmlRenderer
}
}
// ページ設定
// ==========================================
// 3. ページ設定&スムーズ切替
// ==========================================
function setPage(page, smoothScroll = true) {
if (page < 1) page = 1;
if (page > totalPages) page = totalPages;
@@ -1444,6 +1607,11 @@ public static class UnifiedHtmlRenderer
item.classList.toggle('active', p === currentPage);
});
// 表示対象とその次後を最優先ダウンロードキューへ
downloader.prioritize(currentPage);
if (currentPage < totalPages) downloader.prioritize(currentPage + 1);
if (currentPage > 1) downloader.prioritize(currentPage - 1);
if (isSlideMode) {
updateSlideImage();
} else if (smoothScroll) {
@@ -1451,29 +1619,30 @@ public static class UnifiedHtmlRenderer
}
}
// スライド画像更新とプリロード
// スライド画像のスムーズ更新
function updateSlideImage() {
if (galleryData.images && galleryData.images[currentPage - 1]) {
const img = galleryData.images[currentPage - 1];
currentSlideImage.src = img.imageUrl;
currentSlideImage.alt = img.altText || `${galleryData.title} ${currentPage}ページ`;
preloadAdjacent();
}
if (!galleryData.images || !galleryData.images[currentPage - 1]) return;
const imgData = galleryData.images[currentPage - 1];
// スムーズな切り替えのため、少しフェードさせる
currentSlideImage.classList.add('switching');
setTimeout(() => {
currentSlideImage.src = imgData.imageUrl;
currentSlideImage.alt = imgData.altText || `${galleryData.title} ${currentPage}ページ`;
// キャッシュ済みなら即座に表示
if (downloader.isLoaded(currentPage) || (currentSlideImage.complete && currentSlideImage.naturalWidth > 0)) {
currentSlideImage.classList.remove('switching');
} else {
currentSlideImage.onload = () => {
currentSlideImage.classList.remove('switching');
};
}
}, 50);
}
function preloadAdjacent() {
if (!galleryData.images) return;
if (currentPage < totalPages) {
const nextImg = new Image();
nextImg.src = galleryData.images[currentPage].imageUrl;
}
if (currentPage > 1) {
const prevImg = new Image();
prevImg.src = galleryData.images[currentPage - 2].imageUrl;
}
}
// 該当ページへのスクロール
// 該当ページへのスムーズスクロール
function scrollToPage(page) {
const target = document.querySelector(`.scroll-image-wrapper[data-page="${page}"]`);
if (target) {
@@ -1490,6 +1659,86 @@ public static class UnifiedHtmlRenderer
}
}
// ==========================================
// 4. マウスホイールによるページ送り(スライドモード)
// ==========================================
let wheelCooldown = false;
let wheelTimer = null;
window.addEventListener('wheel', (e) => {
if (!isSlideMode) return;
if (thumbDrawer.classList.contains('active')) return;
// 微小なスクロールノイズを無視
if (Math.abs(e.deltaY) < 6) return;
e.preventDefault();
if (wheelCooldown) return;
if (e.deltaY > 0) {
// 下スクロール: 次のページへ
setPage(currentPage + 1);
} else if (e.deltaY < 0) {
// 上スクロール: 前のページへ
setPage(currentPage - 1);
}
wheelCooldown = true;
clearTimeout(wheelTimer);
wheelTimer = setTimeout(() => {
wheelCooldown = false;
}, 75); // 75ms スロットルでスムーズに1ページずつ進む
}, { passive: false });
// ==========================================
// 5. スマホ・タブレットのタッチスワイプ操作
// ==========================================
let touchStartX = 0;
let touchStartY = 0;
let touchStartTime = 0;
let isTouching = false;
let lastSwipeTime = 0;
window.addEventListener('touchstart', (e) => {
if (thumbDrawer.classList.contains('active')) return;
if (e.target.closest('header, footer, button, a, input, textarea, .thumb-drawer')) return;
if (e.touches.length > 1) { isTouching = false; return; }
const t = e.touches[0];
touchStartX = t.clientX;
touchStartY = t.clientY;
touchStartTime = Date.now();
isTouching = true;
}, { passive: true });
window.addEventListener('touchmove', (e) => {
if (!isTouching || e.touches.length > 1) return;
const t = e.touches[0];
const diffX = t.clientX - touchStartX;
const diffY = t.clientY - touchStartY;
if (isSlideMode && Math.abs(diffX) > Math.abs(diffY) && Math.abs(diffX) > 10) {
if (e.cancelable) e.preventDefault();
}
}, { passive: false });
window.addEventListener('touchend', (e) => {
if (!isTouching || !e.changedTouches || e.changedTouches.length === 0) return;
isTouching = false;
const t = e.changedTouches[0];
const diffX = t.clientX - touchStartX;
const diffY = t.clientY - touchStartY;
const elapsed = Date.now() - touchStartTime;
if (Math.abs(diffX) >= 40 && Math.abs(diffX) > Math.abs(diffY) * 1.2 && elapsed < 600 && Date.now() - lastSwipeTime >= 60) {
lastSwipeTime = Date.now();
if (diffX < 0) {
setPage(currentPage + 1);
} else {
setPage(currentPage - 1);
}
}
}, { passive: true });
// サムネイルドロワー開閉
function toggleDrawer(open) {
const shouldOpen = open !== undefined ? open : !thumbDrawer.classList.contains('active');
@@ -1604,8 +1853,9 @@ public static class UnifiedHtmlRenderer
}
});
// 初期表示設定
// 初期化:1ページ目を設定し、順次バックグラウンドダウンロードを開始
setPage(1, false);
downloader.start();
</script>
</body>
</html>