Initial commit

This commit is contained in:
2026-11-18 11:34:55 +08:00
commit d76faefdec
39 changed files with 778 additions and 0 deletions
+348
View File
@@ -0,0 +1,348 @@
(() => {
"use strict";
/* PHOTOS:START */
const photos = [
{ src: "assets/img/1793460679986.png" },
{ src: "assets/img/1793460702468.png" },
{ src: "assets/img/1793460705640.png" },
{ src: "assets/img/1794318085822.png" },
{ src: "assets/img/1794486619061.png" },
{ src: "assets/img/1794652590020.png" },
{ src: "assets/img/image_1789474692962.jpg" },
{ src: "assets/img/image_1793273216172.jpg" },
{ src: "assets/img/image_1793460850677.jpg" },
{ src: "assets/img/image_1793460854504.jpg" },
{ src: "assets/img/image_1793460860160.jpg" },
{ src: "assets/img/img-1793460564813ce290ec99e90ba59d011a45738b8e829879d3c5d20f9c3373ff293030596368a.jpg" },
{ src: "assets/img/img-1793575563154a3625c1720ceff524ccbcc280bb700fcb6ee741d06c59a9d487681e3b12b0ed3.jpg" },
{ src: "assets/img/img-17935755718269e7e33eea3cacdffae9f9da3cdc3ebb08d1c83fffc1193f1024a40f729b51d72.jpg" },
{ src: "assets/img/img-1794745267039cfe0e8f8044ee103ee9b30c4f41e6379e231a92370e3e019a4b8dce437f407da.jpg" },
{ src: "assets/img/img-179474527214730822a98d45864300ce2876ecd1d830cc1c0687c3ee72efcf9fe999778eb50ce.jpg" },
{ src: "assets/img/img-17947452752440da437582648d25fa580a418dcf366bf4f59231e32ef3e24415d8e10a21331cf.jpg" },
{ src: "assets/img/img-1794745278168db2fb63c6e0e9579bd2dc0023c94cb545052c1baa9f990fe79c878efcb318887.jpg" },
{ src: "assets/img/img-1794745281556231db3fd028ffd3483bd3bf91300ce3bb29e16422b9591c9e5501887ef3c71c5.jpg" },
{ src: "assets/img/img-1794745285566ad8729dd6d8841e909e23641109f8e36ed09668611b2b892215acd02e5e847da.jpg" },
{ src: "assets/img/img-1794745288755ae4f540d0fa3c96bcf2bb25db03094b4836704a966d0a62a5ca2e54ef256fe78.jpg" },
{ src: "assets/img/img-1794745292259e749051d21f787b82b6e3e53ffd7d87b6269b915283c75856cda0743acc2754d.jpg" },
{ src: "assets/img/img-1794745295723a2950baa20b0526046c3ffdab740bd231c89fd740ebed464a0305605ff93b86a.jpg" },
{ src: "assets/img/img-1794745307856d2147457d0c9917d6369b4620d7e7d18b83ba2cee8439b3300c64c4aee64bb52.jpg" },
{ src: "assets/img/RDT_20261114_1838401112059065207923484.jpg" },
{ src: "assets/img/RDT_20261114_1839092334126551755891923.jpg" },
{ src: "assets/img/RDT_20261114_1839131355937088287474941.jpg" },
{ src: "assets/img/Screenshot_2026-10-31-15-31-53-652_com.instagram.android-edit.jpg" },
{ src: "assets/img/VOSR2_upscaled_x2_HW_VVskbYAE60Oo_20261117_212857.png" },
{ src: "assets/img/VOSR2_upscaled_x2_RDT_20261114_1838498_20261117_214156.png" }
];
/* PHOTOS:END */
const scene = document.getElementById("scene");
const shuffleBtn = document.getElementById("shuffleBtn");
if (!scene) return;
const supportsRandom = CSS.supports("width", "random(1px, 10px)");
if (!supportsRandom) {
document.body.classList.add("no-random")
}
const MARGIN = 6;
const MAX_TRIES = 400;
const OVERLAP_RATIO = 0.35;
const promoted = new Map();
let topZ = 1000;
function promote(card) {
const z = ++topZ;
card.style.zIndex = z;
if (card.dataset.src) promoted.set(card.dataset.src, z)
}
const STANDARDS = [{
label: "4 / 3",
value: 4 / 3
}, {
label: "3 / 4",
value: 3 / 4
}, {
label: "1 / 1",
value: 1
}];
function pickStandard(w, h) {
const r = w / h;
return STANDARDS.reduce((best, s) => Math.abs(Math.log(r / s.value)) < Math.abs(Math.log(r / best.value)) ? s : best)
}
function loadImage(src) {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(img);
img.onerror = () => reject(new Error(`Failed to load: ${src}`));
img.src = src
})
}
function rand(min, max) {
return Math.random() * (max - min) + min
}
function intersectionArea(a, b) {
const w = Math.min(a.right, b.right) - Math.max(a.left, b.left);
const h = Math.min(a.bottom, b.bottom) - Math.max(a.top, b.top);
if (w <= 0 || h <= 0) return 0;
return w * h
}
function overlaps(a, b, margin) {
const bExpanded = {
left: b.left - margin,
top: b.top - margin,
right: b.right + margin,
bottom: b.bottom + margin
};
const inter = intersectionArea(a, bExpanded);
if (inter === 0) return !1;
const areaA = (a.right - a.left) * (a.bottom - a.top);
const areaB = (bExpanded.right - bExpanded.left) * (bExpanded.bottom - bExpanded.top);
const ratioA = inter / areaA;
const ratioB = inter / areaB;
return ratioA > OVERLAP_RATIO || ratioB > OVERLAP_RATIO
}
function findFreePosition(w, h, placed, vw, vh) {
const minX = 10;
const maxX = Math.max(minX, vw - w - 10);
const minY = 70;
const maxY = Math.max(minY, vh - h - 10);
for (let i = 0; i < MAX_TRIES; i++) {
const x = rand(minX, maxX);
const y = rand(minY, maxY);
const rect = {
left: x,
top: y,
right: x + w,
bottom: y + h
};
const collides = placed.some(p => overlaps(rect, p, MARGIN));
if (!collides) return rect
}
return null
}
function build() {
scene.innerHTML = "";
const vw = window.innerWidth;
const vh = window.innerHeight;
const root = getComputedStyle(document.documentElement);
const imgW = parseFloat(root.getPropertyValue("--img-w"));
const frameX = 16;
const frameY = 46;
const placed = [];
let skipped = 0;
const shuffled = [...photos].sort(() => Math.random() - 0.5);
shuffled.forEach((photo) => {
const std = pickStandard(photo.width, photo.height);
const imgH = Math.round(imgW / std.value);
const cardW = imgW + frameX;
const cardH = imgH + frameY;
const rect = findFreePosition(cardW, cardH, placed, vw, vh);
if (!rect) {
skipped++;
return
}
placed.push(rect);
const name = photo.src.split("/").pop();
const card = document.createElement("figure");
card.className = "polaroid";
card.dataset.src = photo.src;
card.tabIndex = 0;
card.setAttribute("role", "img");
card.setAttribute("aria-label", `Polaroid: ${name}`);
const promotedZ = promoted.get(photo.src);
if (promotedZ) {
card.style.zIndex = promotedZ;
topZ = Math.max(topZ, promotedZ)
}
card.style.setProperty("--x", `${rect.left}px`);
card.style.setProperty("--y", `${rect.top}px`);
card.style.setProperty("--card-w", `${cardW}px`);
card.style.setProperty("--card-h", `${cardH}px`);
if (!supportsRandom) {
card.style.setProperty("--fallback-angle", `${rand(-14, 14)}deg`)
}
const img = document.createElement("img");
img.src = photo.src;
img.alt = name;
img.loading = "lazy";
img.style.aspectRatio = std.label;
img.onerror = () => {
card.remove()
};
card.append(img);
scene.appendChild(card)
});
if (skipped > 0) {
console.info(`[polaroids] ${skipped} card(s) not placed due to lack of space.`)
}
}
Promise.all(photos.map(photo => loadImage(photo.src).then(img => {
photo.width = img.naturalWidth;
photo.height = img.naturalHeight;
return photo
}).catch(() => null))).then(list => {
const loaded = list.filter(Boolean);
photos.length = 0;
photos.push(...loaded);
build()
});
let dragState = null;
const KEEP_VISIBLE = 40;
scene.addEventListener("pointerover", (e) => {
const card = e.target.closest(".polaroid");
if (!card || card.contains(e.relatedTarget)) return;
promote(card)
});
scene.addEventListener("focusin", (e) => {
const card = e.target.closest(".polaroid");
if (card) promote(card)
});
scene.addEventListener("pointerdown", (e) => {
const card = e.target.closest(".polaroid");
if (!card || (e.button !== undefined && e.button !== 0)) return;
e.preventDefault();
const startX = parseFloat(card.style.getPropertyValue("--x")) || 0;
const startY = parseFloat(card.style.getPropertyValue("--y")) || 0;
dragState = {
card,
pointerId: e.pointerId,
offsetX: e.clientX - startX,
offsetY: e.clientY - startY
};
card.classList.add("dragging");
promote(card);
try {
card.setPointerCapture(e.pointerId)
} catch (_) {}
});
scene.addEventListener("pointermove", (e) => {
if (!dragState || e.pointerId !== dragState.pointerId) return;
const { card, offsetX, offsetY } = dragState;
const cw = card.offsetWidth;
const ch = card.offsetHeight;
const vw = window.innerWidth;
const vh = window.innerHeight;
let nx = e.clientX - offsetX;
let ny = e.clientY - offsetY;
nx = Math.min(Math.max(nx, -(cw - KEEP_VISIBLE)), vw - KEEP_VISIBLE);
ny = Math.min(Math.max(ny, -(ch - KEEP_VISIBLE)), vh - KEEP_VISIBLE);
card.style.setProperty("--x", `${nx}px`);
card.style.setProperty("--y", `${ny}px`)
});
function endDrag(e) {
if (!dragState || e.pointerId !== dragState.pointerId) return;
const { card } = dragState;
card.classList.remove("dragging");
try {
card.releasePointerCapture(e.pointerId)
} catch (_) {}
dragState = null
}
scene.addEventListener("pointerup", endDrag);
scene.addEventListener("pointercancel", endDrag);
let resizeTimer;
window.addEventListener("resize", () => {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(build, 200)
});
if (shuffleBtn) {
shuffleBtn.addEventListener("click", () => {
promoted.clear();
build()
})
}
const THEMES = {
violet: { label: "Violet 紫罗兰", a: "#8357eb", b: "#6a3de0", c: "#a86ff5" },
space: { label: "Space 深空", a: "#0d0b1e", b: "#1b1440", c: "#2a1d52" },
teal: { label: "Teal 青碧", a: "#062a30", b: "#0a3d40", c: "#0e5560" },
sand: { label: "Sand 沙色", a: "#f3e2c7", b: "#f7d9a8", c: "#e8b98a" },
black: { label: "Black 墨黑", a: "#050507", b: "#101018", c: "#16161f" }
};
const THEME_KEY = "photowall-bg";
const settingsBtn = document.getElementById("settingsBtn");
const settingsPanel = document.getElementById("settingsPanel");
const settingsClose = document.getElementById("settingsClose");
const themeList = document.getElementById("themeList");
function applyTheme(key) {
const t = THEMES[key];
if (!t) return;
const rs = document.documentElement.style;
rs.setProperty("--bg-a", t.a);
rs.setProperty("--bg-b", t.b);
rs.setProperty("--bg-c", t.c);
try {
localStorage.setItem(THEME_KEY, key)
} catch (_) {}
if (themeList) {
themeList.querySelectorAll(".theme-option").forEach(btn => {
btn.setAttribute("aria-checked", String(btn.dataset.theme === key))
})
}
}
if (themeList) {
Object.entries(THEMES).forEach(([key, t]) => {
const btn = document.createElement("button");
btn.type = "button";
btn.className = "theme-option";
btn.dataset.theme = key;
btn.setAttribute("role", "radio");
btn.setAttribute("aria-checked", "false");
const dot = document.createElement("span");
dot.className = "dot";
const label = document.createElement("span");
label.className = "label";
label.textContent = t.label;
const swatch = document.createElement("span");
swatch.className = "swatch";
[t.a, t.b, t.c].forEach(color => {
const i = document.createElement("i");
i.style.background = color;
swatch.appendChild(i)
});
btn.append(dot, label, swatch);
btn.addEventListener("click", () => applyTheme(key));
themeList.appendChild(btn)
});
let saved = null;
try {
saved = localStorage.getItem(THEME_KEY)
} catch (_) {}
applyTheme(THEMES[saved] ? saved : "violet")
}
function setPanel(open) {
if (!settingsPanel || !settingsBtn) return;
settingsPanel.classList.toggle("open", open);
settingsBtn.setAttribute("aria-expanded", String(open))
}
if (settingsBtn && settingsPanel) {
settingsBtn.addEventListener("click", () => {
setPanel(!settingsPanel.classList.contains("open"))
});
if (settingsClose) {
settingsClose.addEventListener("click", () => setPanel(false))
}
document.addEventListener("pointerdown", (e) => {
if (!settingsPanel.classList.contains("open")) return;
if (settingsPanel.contains(e.target) || settingsBtn.contains(e.target)) return;
setPanel(false)
});
document.addEventListener("keydown", (e) => {
if (e.key === "Escape") setPanel(false)
})
}
})()