/* ========================================================= 聖恋 クリスマスストーリー(仮) 特設サイト — script.js ----------------------------------------------------------- 目次 (Table of contents) 1. Mobile in-page nav toggle 2. Opening splash 3. Sticky nav shadow + scroll progress bar 4. Scroll reveal (.reveal fade/slide-in) 5. Character/line text reveal (.text-split-chars / .text-split-lines) 6. Hero parallax + falling snow 7. STORY section snow + sparkle 8. CAST section sparkle 9. Site-wide snow + sparkle 10. 相関図 (relationship diagram) lightbox 11. CAST photo pointer-tilt ========================================================= */ (function () { "use strict"; // --------------------------------------------------------- // 1. Mobile in-page nav toggle // --------------------------------------------------------- var toggle = document.getElementById("navToggle"); var navEl = document.getElementById("pageNav"); var nav = navEl.querySelector(".page-nav-inner"); var closeFixed = document.getElementById("navCloseFixed"); function closeNav() { nav.classList.remove("is-open"); toggle.setAttribute("aria-expanded", "false"); document.body.classList.remove("nav-is-open"); document.body.style.overflow = ""; } // Full-screen takeover: the panel always covers the whole viewport // (see .page-nav-inner's mobile rule, top:0/bottom:0), regardless of // where the sticky nav bar currently sits, so it never opens into a // partial/cut-off area. The close control shown while open // (.nav-close-fixed) lives outside the sticky nav bar, toggled via // this body class — see its CSS comment for why. function openNav() { nav.classList.add("is-open"); toggle.setAttribute("aria-expanded", "true"); document.body.classList.add("nav-is-open"); document.body.style.overflow = "hidden"; } toggle.addEventListener("click", function () { if (nav.classList.contains("is-open")) { closeNav(); } else { openNav(); } }); if (closeFixed) closeFixed.addEventListener("click", closeNav); nav.querySelectorAll("a").forEach(function (link) { link.addEventListener("click", closeNav); }); window.addEventListener("resize", function () { if (window.innerWidth > 720) closeNav(); }); var reduceMotion = window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches; // --------------------------------------------------------- // 2. Opening splash — plays the logo reveal full-screen before the // hero is shown. Auto-dismisses after the animation sequence // finishes, or immediately on click/tap to skip. // --------------------------------------------------------- var splash = document.getElementById("splash"); if (splash) { var splashHidden = false; var splashTimer = null; function hideSplash() { if (splashHidden) return; splashHidden = true; splash.classList.add("is-hidden"); splash.setAttribute("aria-hidden", "true"); } var splashDelay = reduceMotion ? 300 : 3500; splashTimer = setTimeout(hideSplash, splashDelay); splash.addEventListener("click", function () { clearTimeout(splashTimer); hideSplash(); }); } // --------------------------------------------------------- // 3. Sticky nav shadow + scroll progress bar (rAF-batched) // --------------------------------------------------------- var progressBar = document.getElementById("scrollProgress"); var backToTop = document.getElementById("backToTop"); var ticking = false; function onScrollFrame() { var scrollY = window.scrollY || window.pageYOffset; var docH = document.documentElement.scrollHeight - window.innerHeight; var pct = docH > 0 ? (scrollY / docH) * 100 : 0; navEl.classList.toggle("is-scrolled", scrollY > 8); if (progressBar) progressBar.style.width = pct + "%"; if (backToTop) backToTop.classList.toggle("is-visible", scrollY > window.innerHeight * 0.6); ticking = false; } window.addEventListener("scroll", function () { if (!ticking) { window.requestAnimationFrame(onScrollFrame); ticking = true; } }, { passive: true }); onScrollFrame(); if (backToTop) { backToTop.addEventListener("click", function () { window.scrollTo({ top: 0, behavior: reduceMotion ? "auto" : "smooth" }); }); } // --------------------------------------------------------- // 4. Scroll reveal — IntersectionObserver fades/slides content in // --------------------------------------------------------- var revealEls = document.querySelectorAll(".reveal"); if ("IntersectionObserver" in window && revealEls.length) { document.body.classList.add("reveal-armed"); var io = new IntersectionObserver(function (entries) { entries.forEach(function (entry) { if (entry.isIntersecting) { entry.target.classList.add("is-visible"); io.unobserve(entry.target); } }); }, { threshold: 0.12, rootMargin: "0px 0px -40px 0px" }); revealEls.forEach(function (el) { io.observe(el); }); } // --------------------------------------------------------- // 5. Character/line text reveal — CAST's eyebrow + title split into // per-character spans (short headings read well typing on one // letter at a time), and STORY's body paragraph splits into // per-line spans (a full paragraph animating character by // character would take too long to read). Same progressive- // enhancement gating as the .reveal block above: skipped entirely // under reduced-motion or without IntersectionObserver, so the // text just renders normally. // --------------------------------------------------------- var charTargets = document.querySelectorAll(".text-split-chars"); var lineTargets = document.querySelectorAll(".text-split-lines"); if ("IntersectionObserver" in window && !reduceMotion && (charTargets.length || lineTargets.length)) { function wrapChars(el) { var text = el.textContent; el.textContent = ""; var frag = document.createDocumentFragment(); var visibleIndex = 0; for (var i = 0; i < text.length; i++) { var ch = text[i]; // A literal space is appended as a plain text node, not an // animated span -- wrapping it (even as " ") removes the // browser's normal wrap opportunity there, so a short phrase // like "CAST & CHARACTERS" stopped wrapping on narrow screens // and overflowed the viewport instead. if (ch === " ") { frag.appendChild(document.createTextNode(" ")); continue; } var span = document.createElement("span"); span.className = "char"; span.textContent = ch; span.style.transitionDelay = (visibleIndex * 28) + "ms"; visibleIndex++; frag.appendChild(span); } el.appendChild(frag); } // Grouped by CHARACTER rather than by whitespace-split "word" -- // Japanese body copy has no spaces between words at all, so // splitting on \s+ would treat the entire sentence as a single // unbreakable token and the "line" stagger would never show up. // Wrapping every character instead works for any script (CJK's // own line-breaking already allows a break between any two // characters), and which characters end up sharing a line is // measured afterwards from actual layout (regroupLines) rather // than assumed, so it stays correct at any viewport width. function wrapLineUnits(el) { var text = el.textContent; el.textContent = ""; var frag = document.createDocumentFragment(); for (var i = 0; i < text.length; i++) { var ch = text[i]; if (ch === " ") { frag.appendChild(document.createTextNode(" ")); continue; } var span = document.createElement("span"); span.className = "word"; span.textContent = ch; frag.appendChild(span); } el.appendChild(frag); } function regroupLines(el) { var lineDelayMs = 110; var wordEls = el.querySelectorAll(".word"); var lastTop = null; var lineIndex = -1; wordEls.forEach(function (w) { var top = w.offsetTop; if (lastTop === null || Math.abs(top - lastTop) > 2) { lineIndex++; lastTop = top; } w.style.transitionDelay = (lineIndex * lineDelayMs) + "ms"; }); } charTargets.forEach(wrapChars); lineTargets.forEach(wrapLineUnits); lineTargets.forEach(regroupLines); window.addEventListener("resize", function () { lineTargets.forEach(function (el) { if (!el.classList.contains("is-split-visible")) regroupLines(el); }); }); var textIo = new IntersectionObserver(function (entries) { entries.forEach(function (entry) { if (entry.isIntersecting) { entry.target.classList.add("is-split-visible"); textIo.unobserve(entry.target); } }); }, { threshold: 0.12, rootMargin: "0px 0px -40px 0px" }); charTargets.forEach(function (el) { textIo.observe(el); }); lineTargets.forEach(function (el) { textIo.observe(el); }); document.body.classList.add("text-reveal-armed"); } // --------------------------------------------------------- // 6. Hero: gentle parallax on the header image + falling snow // --------------------------------------------------------- var hero = document.querySelector(".hero"); var heroBg = document.getElementById("heroBg"); if (hero && heroBg && !reduceMotion) { var heroTicking = false; function onHeroScroll() { var rect = hero.getBoundingClientRect(); if (rect.bottom > 0 && rect.top < window.innerHeight) { var offset = Math.max(0, -rect.top) * 0.18; heroBg.style.transform = "translateY(" + offset + "px)"; } heroTicking = false; } window.addEventListener("scroll", function () { if (!heroTicking) { window.requestAnimationFrame(onHeroScroll); heroTicking = true; } }, { passive: true }); } var snowCanvas = document.getElementById("heroSnow"); if (snowCanvas && hero && !reduceMotion) { var ctx = snowCanvas.getContext("2d"); var flakes = []; var rafId = null; var dpr = Math.min(window.devicePixelRatio || 1, 2); function sizeCanvas() { var w = hero.clientWidth; var h = hero.clientHeight; snowCanvas.width = w * dpr; snowCanvas.height = h * dpr; snowCanvas.style.width = w + "px"; snowCanvas.style.height = h + "px"; ctx.setTransform(dpr, 0, 0, dpr, 0, 0); var count = Math.max(24, Math.round((w * h) / 26000)); flakes = []; for (var i = 0; i < count; i++) { flakes.push({ x: Math.random() * w, y: Math.random() * h, r: 1 + Math.random() * 2.4, speed: 0.25 + Math.random() * 0.6, drift: (Math.random() - 0.5) * 0.5, alpha: 0.35 + Math.random() * 0.5 }); } } function tick() { var w = hero.clientWidth, h = hero.clientHeight; ctx.clearRect(0, 0, w, h); ctx.fillStyle = "#fffaf3"; for (var i = 0; i < flakes.length; i++) { var f = flakes[i]; ctx.globalAlpha = f.alpha; ctx.beginPath(); ctx.arc(f.x, f.y, f.r, 0, Math.PI * 2); ctx.fill(); f.y += f.speed; f.x += f.drift; if (f.y > h + 4) { f.y = -4; f.x = Math.random() * w; } if (f.x > w + 4) f.x = -4; if (f.x < -4) f.x = w + 4; } ctx.globalAlpha = 1; rafId = window.requestAnimationFrame(tick); } var io2 = ("IntersectionObserver" in window) ? new IntersectionObserver(function (entries) { entries.forEach(function (entry) { if (entry.isIntersecting) { if (!rafId) rafId = window.requestAnimationFrame(tick); } else if (rafId) { window.cancelAnimationFrame(rafId); rafId = null; } }); }) : null; sizeCanvas(); rafId = window.requestAnimationFrame(tick); if (io2) io2.observe(hero); var resizeTimer; window.addEventListener("resize", function () { clearTimeout(resizeTimer); resizeTimer = setTimeout(sizeCanvas, 150); }); } // --------------------------------------------------------- // 7. STORY section snow + sparkle — a self-contained canvas scoped to // just the STORY dark band, so its Christmas-night mood reads as its // own little moment on the page (same technique as heroSnow above). // --------------------------------------------------------- var storySection = document.querySelector(".story-section"); var storySnowCanvas = document.getElementById("storySnow"); if (storySnowCanvas && storySection && !reduceMotion) { var stCtx = storySnowCanvas.getContext("2d"); var stParticles = []; var stRafId = null; var stDpr = Math.min(window.devicePixelRatio || 1, 2); function sizeStoryCanvas() { var w = storySection.clientWidth; var h = storySection.clientHeight; storySnowCanvas.width = w * stDpr; storySnowCanvas.height = h * stDpr; storySnowCanvas.style.width = w + "px"; storySnowCanvas.style.height = h + "px"; stCtx.setTransform(stDpr, 0, 0, stDpr, 0, 0); var flakeCount = Math.max(18, Math.round((w * h) / 34000)); var sparkleCount = Math.max(8, Math.round((w * h) / 90000)); stParticles = []; for (var i = 0; i < flakeCount; i++) { stParticles.push({ type: "flake", x: Math.random() * w, y: Math.random() * h, r: 1 + Math.random() * 2.4, speed: 0.2 + Math.random() * 0.5, drift: (Math.random() - 0.5) * 0.45, alpha: 0.25 + Math.random() * 0.4 }); } for (var j = 0; j < sparkleCount; j++) { stParticles.push({ type: "sparkle", x: Math.random() * w, y: Math.random() * h, r: 1.1 + Math.random() * 1.6, phase: Math.random() * Math.PI * 2, speed: 0.5 + Math.random() * 0.7 }); } } function stTick(ts) { var w = storySection.clientWidth, h = storySection.clientHeight; stCtx.clearRect(0, 0, w, h); for (var i = 0; i < stParticles.length; i++) { var p = stParticles[i]; if (p.type === "flake") { stCtx.globalAlpha = p.alpha; stCtx.fillStyle = "#fffaf3"; stCtx.beginPath(); stCtx.arc(p.x, p.y, p.r, 0, Math.PI * 2); stCtx.fill(); p.y += p.speed; p.x += p.drift; if (p.y > h + 4) { p.y = -4; p.x = Math.random() * w; } if (p.x > w + 4) p.x = -4; if (p.x < -4) p.x = w + 4; } else { var tw = (Math.sin((ts || 0) * 0.001 * p.speed + p.phase) + 1) / 2; stCtx.globalAlpha = 0.14 + tw * 0.55; stCtx.fillStyle = "#f3d591"; stCtx.beginPath(); stCtx.arc(p.x, p.y, p.r, 0, Math.PI * 2); stCtx.fill(); } } stCtx.globalAlpha = 1; stRafId = window.requestAnimationFrame(stTick); } var stIo = ("IntersectionObserver" in window) ? new IntersectionObserver(function (entries) { entries.forEach(function (entry) { if (entry.isIntersecting) { if (!stRafId) stRafId = window.requestAnimationFrame(stTick); } else if (stRafId) { window.cancelAnimationFrame(stRafId); stRafId = null; } }); }) : null; sizeStoryCanvas(); stRafId = window.requestAnimationFrame(stTick); if (stIo) stIo.observe(storySection); var stResizeTimer; window.addEventListener("resize", function () { clearTimeout(stResizeTimer); stResizeTimer = setTimeout(sizeStoryCanvas, 150); }); } // --------------------------------------------------------- // 8. CAST section sparkle — a denser field of twinkling gold glints // layered over the pink Christmas-ornament banner, so the section reads // as glittery rather than flat (same technique as storySnow above). // --------------------------------------------------------- var castSection = document.querySelector(".cast-section"); var castSparkleCanvas = document.getElementById("castSparkle"); if (castSparkleCanvas && castSection && !reduceMotion) { var csCtx = castSparkleCanvas.getContext("2d"); var csParticles = []; var csRafId = null; var csDpr = Math.min(window.devicePixelRatio || 1, 2); function sizeCastCanvas() { var w = castSection.clientWidth; var h = castSection.clientHeight; castSparkleCanvas.width = w * csDpr; castSparkleCanvas.height = h * csDpr; castSparkleCanvas.style.width = w + "px"; castSparkleCanvas.style.height = h + "px"; csCtx.setTransform(csDpr, 0, 0, csDpr, 0, 0); var sparkleCount = Math.max(40, Math.round((w * h) / 42000)); var driftCount = Math.max(14, Math.round((w * h) / 150000)); csParticles = []; for (var i = 0; i < sparkleCount; i++) { csParticles.push({ type: "sparkle", x: Math.random() * w, y: Math.random() * h, r: 0.8 + Math.random() * 1.7, phase: Math.random() * Math.PI * 2, speed: 0.4 + Math.random() * 0.8 }); } for (var j = 0; j < driftCount; j++) { csParticles.push({ type: "drift", x: Math.random() * w, y: Math.random() * h, r: 1.2 + Math.random() * 2, speed: 0.12 + Math.random() * 0.22, drift: (Math.random() - 0.5) * 0.3, alpha: 0.12 + Math.random() * 0.22 }); } } function csTick(ts) { var w = castSection.clientWidth, h = castSection.clientHeight; csCtx.clearRect(0, 0, w, h); for (var i = 0; i < csParticles.length; i++) { var p = csParticles[i]; if (p.type === "sparkle") { var tw = (Math.sin((ts || 0) * 0.001 * p.speed + p.phase) + 1) / 2; csCtx.globalAlpha = 0.12 + tw * 0.68; csCtx.fillStyle = "#f6dd9d"; csCtx.beginPath(); csCtx.arc(p.x, p.y, p.r, 0, Math.PI * 2); csCtx.fill(); } else { csCtx.globalAlpha = p.alpha; csCtx.fillStyle = "#fff6ec"; csCtx.beginPath(); csCtx.arc(p.x, p.y, p.r, 0, Math.PI * 2); csCtx.fill(); p.y += p.speed; p.x += p.drift; if (p.y > h + 4) { p.y = -4; p.x = Math.random() * w; } if (p.x > w + 4) p.x = -4; if (p.x < -4) p.x = w + 4; } } csCtx.globalAlpha = 1; csRafId = window.requestAnimationFrame(csTick); } var csIo = ("IntersectionObserver" in window) ? new IntersectionObserver(function (entries) { entries.forEach(function (entry) { if (entry.isIntersecting) { if (!csRafId) csRafId = window.requestAnimationFrame(csTick); } else if (csRafId) { window.cancelAnimationFrame(csRafId); csRafId = null; } }); }) : null; sizeCastCanvas(); csRafId = window.requestAnimationFrame(csTick); if (csIo) csIo.observe(castSection); var csResizeTimer; window.addEventListener("resize", function () { clearTimeout(csResizeTimer); csResizeTimer = setTimeout(sizeCastCanvas, 150); }); } // --------------------------------------------------------- // 9. Site-wide snow + sparkle — a soft, continuous winter-night // atmosphere fixed behind the whole page (not just the hero), so the // Christmas mood carries through every section as you scroll. // --------------------------------------------------------- var siteSnowCanvas = document.getElementById("siteSnow"); if (siteSnowCanvas && !reduceMotion) { var sctx = siteSnowCanvas.getContext("2d"); var sParticles = []; var sRafId = null; var sDpr = Math.min(window.devicePixelRatio || 1, 2); var sPaused = false; function sizeSiteCanvas() { var w = window.innerWidth; var h = window.innerHeight; siteSnowCanvas.width = w * sDpr; siteSnowCanvas.height = h * sDpr; siteSnowCanvas.style.width = w + "px"; siteSnowCanvas.style.height = h + "px"; sctx.setTransform(sDpr, 0, 0, sDpr, 0, 0); var flakeCount = Math.max(26, Math.round((w * h) / 42000)); var sparkleCount = Math.max(10, Math.round((w * h) / 110000)); sParticles = []; for (var i = 0; i < flakeCount; i++) { sParticles.push({ type: "flake", x: Math.random() * w, y: Math.random() * h, r: 1 + Math.random() * 2.2, speed: 0.16 + Math.random() * 0.4, drift: (Math.random() - 0.5) * 0.4, alpha: 0.2 + Math.random() * 0.35 }); } for (var j = 0; j < sparkleCount; j++) { sParticles.push({ type: "sparkle", x: Math.random() * w, y: Math.random() * h, r: 1.1 + Math.random() * 1.5, phase: Math.random() * Math.PI * 2, speed: 0.5 + Math.random() * 0.7 }); } } function sTick(ts) { var w = window.innerWidth, h = window.innerHeight; sctx.clearRect(0, 0, w, h); for (var i = 0; i < sParticles.length; i++) { var p = sParticles[i]; if (p.type === "flake") { sctx.globalAlpha = p.alpha; sctx.fillStyle = "#fffaf3"; sctx.beginPath(); sctx.arc(p.x, p.y, p.r, 0, Math.PI * 2); sctx.fill(); p.y += p.speed; p.x += p.drift; if (p.y > h + 4) { p.y = -4; p.x = Math.random() * w; } if (p.x > w + 4) p.x = -4; if (p.x < -4) p.x = w + 4; } else { var tw = (Math.sin((ts || 0) * 0.001 * p.speed + p.phase) + 1) / 2; sctx.globalAlpha = 0.12 + tw * 0.5; sctx.fillStyle = "#f3d591"; sctx.beginPath(); sctx.arc(p.x, p.y, p.r, 0, Math.PI * 2); sctx.fill(); } } sctx.globalAlpha = 1; if (!sPaused) sRafId = window.requestAnimationFrame(sTick); } sizeSiteCanvas(); sRafId = window.requestAnimationFrame(sTick); var sResizeTimer; window.addEventListener("resize", function () { clearTimeout(sResizeTimer); sResizeTimer = setTimeout(sizeSiteCanvas, 150); }); document.addEventListener("visibilitychange", function () { if (document.hidden) { sPaused = true; if (sRafId) { window.cancelAnimationFrame(sRafId); sRafId = null; } } else if (sPaused) { sPaused = false; sRafId = window.requestAnimationFrame(sTick); } }); } // --------------------------------------------------------- // 10. 相関図 lightbox — click the diagram to view it enlarged in an // overlay // --------------------------------------------------------- var lightbox = document.getElementById("lightbox"); var relationshipTrigger = document.getElementById("relationshipTrigger"); var lightboxClose = document.getElementById("lightboxClose"); function openLightbox() { if (!lightbox) return; lightbox.classList.add("is-open"); lightbox.setAttribute("aria-hidden", "false"); document.body.style.overflow = "hidden"; } function closeLightbox() { if (!lightbox) return; lightbox.classList.remove("is-open"); lightbox.setAttribute("aria-hidden", "true"); document.body.style.overflow = ""; } if (relationshipTrigger) relationshipTrigger.addEventListener("click", openLightbox); if (lightboxClose) lightboxClose.addEventListener("click", closeLightbox); if (lightbox) { lightbox.addEventListener("click", function (e) { if (e.target === lightbox) closeLightbox(); }); } window.addEventListener("keydown", function (e) { if (e.key === "Escape") closeLightbox(); }); // --------------------------------------------------------- // 11. Cast photos: subtle pointer-tilt for a livelier feel (desktop only) // --------------------------------------------------------- var supportsHover = window.matchMedia && window.matchMedia("(hover: hover) and (pointer: fine)").matches; if (supportsHover && !reduceMotion) { document.querySelectorAll(".cast-photo").forEach(function (photo) { photo.addEventListener("mousemove", function (e) { var rect = photo.getBoundingClientRect(); var px = (e.clientX - rect.left) / rect.width - 0.5; var py = (e.clientY - rect.top) / rect.height - 0.5; photo.style.transform = "scale(1.035) rotateX(" + (py * -8) + "deg) rotateY(" + (px * 8) + "deg)"; }); photo.addEventListener("mouseleave", function () { photo.style.transform = ""; }); }); } })();