/* motion.js — reveal-on-scroll, แถบความคืบหน้า, นับเลขขึ้น หลักการ: ถ้า ไม่มี .fx-motion (JS ปิด / reduced-motion) ให้ทุกอย่างเห็นทันที ห้ามซ่อนเนื้อหาใน CSS หลัก — ซ่อนเฉพาะใต้ .fx-motion */ const REDUCED = () => window.matchMedia("(prefers-reduced-motion: reduce)").matches; export function motionEnabled() { return document.documentElement.classList.contains("fx-motion"); } /** เปิด/ปิดคลาส .fx-motion ตามการตั้งค่าผู้ใช้ (เรียกซ้ำได้เมื่อผู้ใช้เปลี่ยน setting) */ export function syncMotionPreference() { document.documentElement.classList.toggle("fx-motion", !REDUCED()); } /* ------------------------------------------------------------- แถบเลื่อน */ export function initScrollProgress() { const bar = document.querySelector(".scroll-progress__bar"); if (!bar) return; let ticking = false; const update = () => { const doc = document.documentElement; const scrollable = doc.scrollHeight - window.innerHeight; const ratio = scrollable > 0 ? Math.min(1, Math.max(0, window.scrollY / scrollable)) : 0; bar.style.transform = `scaleX(${ratio.toFixed(4)})`; ticking = false; }; window.addEventListener( "scroll", () => { if (ticking) return; ticking = true; requestAnimationFrame(update); }, { passive: true }, ); window.addEventListener("resize", update, { passive: true }); update(); } /* --------------------------------------------------------------- reveal */ export function initReveal(root = document) { const targets = [...root.querySelectorAll("[data-reveal]")]; if (!targets.length) return; // ไม่มี motion → เปิดให้เห็นทุกอันแล้วจบ if (!motionEnabled() || REDUCED()) { targets.forEach((el) => el.setAttribute("data-revealed", "")); return; } const reveal = (el) => { if (el.hasAttribute("data-revealed")) return; // หน่วงแบบต่อเนื่องเฉพาะการ์ดในกริด (คิดตามคอลัมน์ ไม่ใช่ index สะสม) el.setAttribute("data-revealed", ""); if (el.matches(".project-card")) countUpInside(el); }; if (!("IntersectionObserver" in window)) { targets.forEach(reveal); return; } const observer = new IntersectionObserver( (entries) => { entries.forEach((entry) => { if (entry.isIntersecting) { reveal(entry.target); observer.unobserve(entry.target); return; } // กระโดดข้ามบล็อกไปแล้ว (anchor jump / คืนตำแหน่ง scroll) — ต้องเปิดให้เห็น ไม่ปล่อยค้างซ่อน const rootTop = entry.rootBounds ? entry.rootBounds.top : 0; if (entry.boundingClientRect.bottom <= rootTop) { reveal(entry.target); observer.unobserve(entry.target); } }); }, { rootMargin: "2000px 0px -6% 0px", threshold: 0.01 }, ); targets.forEach((el) => observer.observe(el)); } /* --------------------------------------------------------------- count up */ export function countUpInside(scope = document) { if (!motionEnabled() || REDUCED()) return; scope.querySelectorAll("[data-countup]").forEach((el) => { if (el.dataset.countupDone === "1") return; el.dataset.countupDone = "1"; const target = Number(el.getAttribute("data-countup")) || 0; if (target <= 0) return; const duration = 720; const start = performance.now(); const tick = (now) => { const progress = Math.min(1, (now - start) / duration); const eased = 1 - Math.pow(1 - progress, 3); el.textContent = String(Math.round(target * eased)); if (progress < 1) requestAnimationFrame(tick); else el.textContent = String(target); }; el.textContent = "0"; requestAnimationFrame(tick); }); } /** เรียกครั้งเดียวหลังโหลดหน้า */ export function initMotion() { syncMotionPreference(); initScrollProgress(); if (typeof window.matchMedia === "function") { const mq = window.matchMedia("(prefers-reduced-motion: reduce)"); mq.addEventListener?.("change", () => { syncMotionPreference(); if (!REDUCED()) initReveal(document); }); } } /** แบ่งข้อความไทยเป็นช่วงคำ (ตัดด้วยช่องว่างเท่านั้น — ปลอดภัยกับสระ/วรรณยุกต์) */ export function splitWordsHtml(text, escape, startDelay = 140, step = 85) { return String(text) .trim() .split(/\s+/) .map( (word, index) => `${escape( word, )}`, ) .join(""); }