426 lines
13 KiB
JavaScript
426 lines
13 KiB
JavaScript
(function () {
|
|
"use strict";
|
|
|
|
/*
|
|
* 图片清单:路径 + 尺寸 + 预计算的 alpha 有效区域(原图像素坐标)。
|
|
* 由 update-photos.js 生成;新增图片后运行 node update-photos.js 更新。
|
|
*/
|
|
var photos = [
|
|
{
|
|
src: "assets/images/1793460705640_cutout.png",
|
|
width: 1440,
|
|
height: 1920,
|
|
bounds: { x: 305, y: 123, w: 996, h: 1797 },
|
|
},
|
|
{
|
|
src: "assets/images/1794486619061_cutout.png",
|
|
width: 1440,
|
|
height: 1920,
|
|
bounds: { x: 467, y: 62, w: 935, h: 1858 },
|
|
},
|
|
{
|
|
src: "assets/images/RDT_20261114_1838401112059065207923484_cutout.png",
|
|
width: 3000,
|
|
height: 2000,
|
|
bounds: { x: 873, y: 8, w: 1445, h: 1992 },
|
|
},
|
|
{
|
|
src: "assets/images/RDT_20261114_1839131355937088287474941_cutout.png",
|
|
width: 3000,
|
|
height: 2000,
|
|
bounds: { x: 1070, y: 124, w: 1479, h: 1876 },
|
|
},
|
|
{
|
|
src: "assets/images/VOSR2_upscaled_x2_HW_VVskbYAE60Oo_20261117_212857_cutout (1).png",
|
|
width: 3840,
|
|
height: 2160,
|
|
bounds: { x: 895, y: 41, w: 2469, h: 2119 },
|
|
},
|
|
{
|
|
src: "assets/images/VOSR2_upscaled_x2_RDT_20261114_1838498_20261117_214156_cutout.png",
|
|
width: 2250,
|
|
height: 4000,
|
|
bounds: { x: 337, y: 302, w: 1768, h: 3698 },
|
|
},
|
|
{
|
|
src: "assets/images/image_1793460860160_cutout.png",
|
|
width: 2000,
|
|
height: 3000,
|
|
bounds: { x: 537, y: 676, w: 1270, h: 2324 },
|
|
},
|
|
{
|
|
src: "assets/images/img_1794745307856d2147457d0c9917d6369b4620d7e7d18b83ba2cee8439b3300c64c4aee64bb5_cutout.png",
|
|
width: 4000,
|
|
height: 6000,
|
|
bounds: { x: 103, y: 681, w: 3823, h: 5319 },
|
|
},
|
|
];
|
|
/* PHOTOS_END */
|
|
|
|
/*
|
|
* 字体注册表:由 update-fonts.js 扫描 assets/font 生成。
|
|
* file 指向 assets/font 下的字体文件;留空时用 stack 回退。
|
|
*/
|
|
/* FONTS_START */
|
|
var FONTS = [
|
|
{ id: "Didot Bold", label: "Didot Bold", file: "assets/font/Didot Bold.otf", stack: "Georgia, \"Times New Roman\", serif" },
|
|
{ id: "Didot Italic", label: "Didot Italic", file: "assets/font/Didot Italic.otf", stack: "Georgia, \"Times New Roman\", serif" },
|
|
{ id: "Didot Title", label: "Didot Title", file: "assets/font/Didot Title.otf", stack: "Georgia, \"Times New Roman\", serif" },
|
|
{ id: "Didot", label: "Didot", file: "assets/font/Didot.otf", stack: "Georgia, \"Times New Roman\", serif" },
|
|
];
|
|
/* FONTS_END */
|
|
|
|
var POSITIONS = [
|
|
{ id: "tl", label: "左上" },
|
|
{ id: "ml", label: "左中" },
|
|
{ id: "bl", label: "左下" },
|
|
{ id: "tc", label: "中上" },
|
|
{ id: "bc", label: "中下" },
|
|
{ id: "tr", label: "右上" },
|
|
{ id: "br", label: "右下" },
|
|
];
|
|
|
|
var DIRECTIONS = [
|
|
{ id: "h", label: "横向" },
|
|
{ id: "v", label: "竖向" },
|
|
{ id: "vr", label: "竖向反转" },
|
|
];
|
|
|
|
function steps(from, to, step, suffix) {
|
|
var list = [];
|
|
for (var n = from; n <= to; n += step) {
|
|
list.push({ id: String(n), label: n + (suffix || "") });
|
|
}
|
|
return list;
|
|
}
|
|
|
|
var SIZES = steps(100, 400, 50, "");
|
|
var SCALES = steps(80, 150, 10, "%");
|
|
|
|
var STORAGE_KEY = "jisoo-album-settings";
|
|
var defaults = {
|
|
text: " ", font: "Didot Title", pos: "tl", dir: "h", size: 100, spacing: 0.02, scale: 100,
|
|
};
|
|
var LIMITS = {
|
|
spacing: { min: -0.1, max: 0.5 },
|
|
};
|
|
|
|
var stage = document.getElementById("stage");
|
|
var statusEl = document.getElementById("status");
|
|
var prevBtn = document.getElementById("prev");
|
|
var nextBtn = document.getElementById("next");
|
|
var titleEl = document.getElementById("title");
|
|
var titleTextEl = document.getElementById("titleText");
|
|
var settingsEl = document.getElementById("settings");
|
|
var settingsBtn = document.getElementById("settingsBtn");
|
|
var settingsClose = document.getElementById("settingsClose");
|
|
var textInput = document.getElementById("textInput");
|
|
var spacingInput = document.getElementById("spacingInput");
|
|
var spacingValue = document.getElementById("spacingValue");
|
|
var fontChoices = document.getElementById("fontChoices");
|
|
var sizeChoices = document.getElementById("sizeChoices");
|
|
var positionChoices = document.getElementById("positionChoices");
|
|
var directionChoices = document.getElementById("directionChoices");
|
|
var scaleChoices = document.getElementById("scaleChoices");
|
|
|
|
var slides = [];
|
|
var current = 0;
|
|
var config = loadConfig();
|
|
|
|
function showStatus(message) {
|
|
statusEl.textContent = message;
|
|
statusEl.classList.add("is-visible");
|
|
}
|
|
|
|
/*
|
|
* 有效区域:高度 = 视口高度的 3/4 × 图片缩放比例,宽度按原比例自适应,
|
|
* 底边贴紧视口底边,水平居中。反推整张 <img> 的尺寸与偏移。
|
|
*/
|
|
function layout() {
|
|
var vw = window.innerWidth;
|
|
var vh = window.innerHeight;
|
|
var targetH = vh * 0.75 * (config.scale / 100);
|
|
|
|
slides.forEach(function (slide) {
|
|
var b = slide.bounds;
|
|
var scale = targetH / b.h;
|
|
var renderW = slide.width * scale;
|
|
var renderH = slide.height * scale;
|
|
var effectiveW = b.w * scale;
|
|
var effectiveLeft = (vw - effectiveW) / 2;
|
|
var effectiveTop = vh - targetH;
|
|
|
|
slide.el.style.width = renderW + "px";
|
|
slide.el.style.height = renderH + "px";
|
|
slide.el.style.left = effectiveLeft - b.x * scale + "px";
|
|
slide.el.style.top = effectiveTop - b.y * scale + "px";
|
|
});
|
|
}
|
|
|
|
function show(index) {
|
|
if (!slides.length) return;
|
|
current = (index + slides.length) % slides.length;
|
|
slides.forEach(function (slide, i) {
|
|
slide.el.classList.toggle("is-active", i === current);
|
|
});
|
|
}
|
|
|
|
function loadImage(photo) {
|
|
return new Promise(function (resolve) {
|
|
var img = new Image();
|
|
img.onload = function () {
|
|
resolve({ photo: photo, img: img });
|
|
};
|
|
img.onerror = function () {
|
|
resolve(null);
|
|
};
|
|
img.src = photo.src;
|
|
});
|
|
}
|
|
|
|
/* -------------------- 设置系统 -------------------- */
|
|
|
|
function clampNumber(value, limit, fallback) {
|
|
var n = Number(value);
|
|
if (!isFinite(n)) return fallback;
|
|
return Math.min(limit.max, Math.max(limit.min, n));
|
|
}
|
|
|
|
/* 吸附到最近的档位,避免读到历史配置中的非档位值 */
|
|
function snapStep(value, options, fallback) {
|
|
var n = Number(value);
|
|
if (!isFinite(n)) return fallback;
|
|
var best = options[0];
|
|
var bestDist = Math.abs(n - Number(best.id));
|
|
options.forEach(function (option) {
|
|
var dist = Math.abs(n - Number(option.id));
|
|
if (dist < bestDist) {
|
|
bestDist = dist;
|
|
best = option;
|
|
}
|
|
});
|
|
return Number(best.id);
|
|
}
|
|
|
|
function loadConfig() {
|
|
var saved = null;
|
|
try {
|
|
saved = JSON.parse(localStorage.getItem(STORAGE_KEY) || "null");
|
|
} catch (e) {
|
|
saved = null;
|
|
}
|
|
if (!saved || typeof saved !== "object") saved = {};
|
|
|
|
var savedFont = typeof saved.font === "string" ? saved.font : defaults.font;
|
|
var fontExists = FONTS.some(function (f) { return f.id === savedFont; });
|
|
if (!fontExists) savedFont = defaults.font;
|
|
|
|
return {
|
|
text: typeof saved.text === "string" ? saved.text : defaults.text,
|
|
font: savedFont,
|
|
pos: typeof saved.pos === "string" ? saved.pos : defaults.pos,
|
|
dir: typeof saved.dir === "string" ? saved.dir : defaults.dir,
|
|
size: snapStep(saved.size, SIZES, defaults.size),
|
|
spacing: clampNumber(saved.spacing, LIMITS.spacing, defaults.spacing),
|
|
scale: snapStep(saved.scale, SCALES, defaults.scale),
|
|
};
|
|
}
|
|
|
|
function saveConfig() {
|
|
try {
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(config));
|
|
} catch (e) {
|
|
/* file:// 下部分浏览器禁用存储,忽略即可,不影响本次会话 */
|
|
}
|
|
}
|
|
|
|
function findFont(id) {
|
|
for (var i = 0; i < FONTS.length; i++) {
|
|
if (FONTS[i].id === id) return FONTS[i];
|
|
}
|
|
return FONTS[0];
|
|
}
|
|
|
|
function fontFormat(file) {
|
|
var ext = file.split(".").pop().toLowerCase();
|
|
if (ext === "woff2") return "woff2";
|
|
if (ext === "woff") return "woff";
|
|
if (ext === "otf") return "opentype";
|
|
return "truetype";
|
|
}
|
|
|
|
function injectFontFaces() {
|
|
var rules = "";
|
|
FONTS.forEach(function (font) {
|
|
if (font.file) {
|
|
rules += '@font-face{font-family:"' + font.id + '";src:url("' + font.file +
|
|
'") format("' + fontFormat(font.file) + '");font-display:swap;}\n';
|
|
}
|
|
});
|
|
if (!rules) return;
|
|
var style = document.createElement("style");
|
|
style.textContent = rules;
|
|
document.head.appendChild(style);
|
|
}
|
|
|
|
function applyConfig() {
|
|
var font = findFont(config.font);
|
|
titleTextEl.textContent = config.text;
|
|
titleTextEl.style.fontFamily = '"' + font.id + '", ' + font.stack;
|
|
titleTextEl.style.fontSize = config.size + "px";
|
|
titleTextEl.style.letterSpacing = config.spacing + "em";
|
|
titleEl.setAttribute("data-pos", config.pos);
|
|
titleTextEl.setAttribute("data-dir", config.dir);
|
|
}
|
|
|
|
function renderChoices(container, name, options, currentValue) {
|
|
container.innerHTML = "";
|
|
options.forEach(function (option) {
|
|
var label = document.createElement("label");
|
|
label.className = "choice";
|
|
|
|
var input = document.createElement("input");
|
|
input.type = "radio";
|
|
input.name = name;
|
|
input.value = option.id;
|
|
input.checked = option.id === currentValue;
|
|
|
|
var dot = document.createElement("span");
|
|
dot.className = "dot";
|
|
|
|
var cap = document.createElement("span");
|
|
cap.className = "cap";
|
|
cap.textContent = option.label;
|
|
|
|
label.appendChild(input);
|
|
label.appendChild(dot);
|
|
label.appendChild(cap);
|
|
container.appendChild(label);
|
|
});
|
|
}
|
|
|
|
function syncPanel() {
|
|
textInput.value = config.text;
|
|
spacingInput.value = config.spacing;
|
|
spacingValue.textContent = Number(config.spacing).toFixed(2) + " em";
|
|
renderChoices(fontChoices, "font", FONTS.map(function (f) {
|
|
return { id: f.id, label: f.label };
|
|
}), config.font);
|
|
renderChoices(sizeChoices, "size", SIZES, String(config.size));
|
|
renderChoices(positionChoices, "position", POSITIONS, config.pos);
|
|
renderChoices(directionChoices, "direction", DIRECTIONS, config.dir);
|
|
renderChoices(scaleChoices, "scale", SCALES, String(config.scale));
|
|
}
|
|
|
|
function openSettings() {
|
|
syncPanel();
|
|
settingsEl.hidden = false;
|
|
settingsClose.focus();
|
|
}
|
|
|
|
function closeSettings() {
|
|
settingsEl.hidden = true;
|
|
settingsBtn.focus();
|
|
}
|
|
|
|
function initSettings() {
|
|
injectFontFaces();
|
|
applyConfig();
|
|
syncPanel();
|
|
|
|
settingsBtn.addEventListener("click", openSettings);
|
|
settingsClose.addEventListener("click", closeSettings);
|
|
settingsEl.querySelector(".settings-backdrop").addEventListener("click", closeSettings);
|
|
|
|
textInput.addEventListener("input", function () {
|
|
config.text = textInput.value;
|
|
applyConfig();
|
|
saveConfig();
|
|
});
|
|
|
|
spacingInput.addEventListener("input", function () {
|
|
config.spacing = clampNumber(spacingInput.value, LIMITS.spacing, defaults.spacing);
|
|
spacingValue.textContent = config.spacing.toFixed(2) + " em";
|
|
applyConfig();
|
|
saveConfig();
|
|
});
|
|
|
|
[
|
|
{ el: fontChoices, key: "font", numeric: false },
|
|
{ el: sizeChoices, key: "size", numeric: true },
|
|
{ el: positionChoices, key: "pos", numeric: false },
|
|
{ el: directionChoices, key: "dir", numeric: false },
|
|
{ el: scaleChoices, key: "scale", numeric: true },
|
|
].forEach(function (group) {
|
|
group.el.addEventListener("change", function (e) {
|
|
if (e.target && e.target.type === "radio") {
|
|
config[group.key] = group.numeric ? Number(e.target.value) : e.target.value;
|
|
applyConfig();
|
|
if (group.key === "scale") layout();
|
|
saveConfig();
|
|
}
|
|
});
|
|
});
|
|
|
|
window.addEventListener("keydown", function (e) {
|
|
if (e.key === "Escape" && !settingsEl.hidden) {
|
|
closeSettings();
|
|
return;
|
|
}
|
|
if (!settingsEl.hidden) return;
|
|
var tag = document.activeElement && document.activeElement.tagName;
|
|
if (tag === "INPUT" || tag === "TEXTAREA") return;
|
|
if (e.key === "ArrowLeft") show(current - 1);
|
|
if (e.key === "ArrowRight") show(current + 1);
|
|
});
|
|
}
|
|
|
|
function init() {
|
|
if (!photos.length) {
|
|
showStatus("图片数组为空:请运行 update-photos.bat 生成。");
|
|
return;
|
|
}
|
|
|
|
Promise.all(
|
|
photos.map(function (photo) {
|
|
return loadImage(photo);
|
|
})
|
|
).then(function (loaded) {
|
|
var valid = loaded.filter(Boolean);
|
|
|
|
if (!valid.length) {
|
|
showStatus("图片加载失败:请检查 assets/images 目录与路径。");
|
|
return;
|
|
}
|
|
|
|
valid.forEach(function (item) {
|
|
var img = item.img;
|
|
var photo = item.photo;
|
|
img.className = "slide-img";
|
|
img.alt = "JISOO album visual";
|
|
stage.appendChild(img);
|
|
slides.push({
|
|
el: img,
|
|
width: img.naturalWidth || photo.width,
|
|
height: img.naturalHeight || photo.height,
|
|
bounds: photo.bounds,
|
|
});
|
|
});
|
|
|
|
layout();
|
|
show(0);
|
|
|
|
prevBtn.addEventListener("click", function () {
|
|
show(current - 1);
|
|
});
|
|
nextBtn.addEventListener("click", function () {
|
|
show(current + 1);
|
|
});
|
|
window.addEventListener("resize", layout);
|
|
});
|
|
}
|
|
|
|
initSettings();
|
|
init();
|
|
})();
|