// Runs one feature in try/catch so a single error never stops the others. function run(feature, arg) { try { return feature(arg); } catch (error) { console.error(`[gsap] ${feature.name} failed:`, error); } }
// Checks that every selector passed in exists before an animation runs function pageGuard(...selectors) { return selectors.every((sel) => document.querySelector(sel)); }
/* ============================================================================= BOOT The only place features start. Add or remove names to turn them on or off. Placing it above the features is fine because function declarations are hoisted. Just keep the features as "function name() {}", not as const arrows. ============================================================================= */
/* ============================================================================= FEATURES Each one returns early if its elements are not on the page, so this single file can safely load on every page of the site. ============================================================================= */
/* DOM READY features: no measuring needed, so they can start early. */
function prioritizeImages() { const imgs = document.querySelectorAll(".horiz-scroll-desktop img");
// Only the first two get high priority, since priority is relative if (i < 2) img.fetchPriority = "high"; }); }
function preloader() { if (!pageGuard("#noni-preloader-video")) { autoScroll(); return; }
const noniPreloader = document.querySelector(".noni-preloader-wrapper"); const videoParent = document.getElementById("noni-preloader-video"); const video = videoParent.firstElementChild; const logo = document.getElementById("noni-logo-preloader");
// true: assume the address bar is at the top, so pin the logo to the bottom edge // false: assume the address bar is at the bottom, so pin the logo to the top edge // Safari on iPhone and iPad defaults to a bottom bar. Chrome, Firefox and Edge // on iOS (CriOS, FxiOS, EdgiOS), and all other browsers, default to the top. const ua = navigator.userAgent; const TOP_ADDRESS_BAR = !/iPhone|iPad/i.test(ua) || /CriOS|FxiOS|EdgiOS/i.test(ua);
let finished = false;
// Optional: stop page scroll while the preloader is running document.documentElement.style.overflow = "hidden";
// Pins the logo at its current on-screen spot using pixel values function lockLogo() { const r = logo.getBoundingClientRect(); // where it is right now (includes scale) const w = logo.offsetWidth; // layout size (unscaled) const h = logo.offsetHeight;
// Reset to a known origin so we can measure the offset logo.style.position = "fixed"; logo.style.margin = "0"; logo.style.right = "auto"; logo.style.bottom = "auto"; logo.style.top = "0px"; logo.style.left = "0px"; logo.style.width = w + "px"; logo.style.height = h + "px";
// Move it back to exactly where it was (horizontal always from the left) const o = logo.getBoundingClientRect(); logo.style.left = r.left - o.left + "px";
if (TOP_ADDRESS_BAR) { // Top address bar: the bottom edge never moves, so pin to it logo.style.top = "auto"; logo.style.bottom = "0px"; const b = logo.getBoundingClientRect(); logo.style.bottom = b.bottom - r.bottom + "px"; } else { // Bottom address bar: the top edge never moves, so pin to it logo.style.top = r.top - o.top + "px"; }
// Release the scroll lock document.documentElement.style.overflow = ""; }
function finish() { if (finished) return; // guards against running twice finished = true;
// -------------------------------- // PAUSE WHEN TAB IS HIDDEN // --------------------------------
// Don't let the scroll run while the tab is in the background function syncWithVisibility() { if (controlled || !animation) return;
if (document.hidden) { if (animation.playState === "running") animation.pause(); } else if (animation.playState === "paused") { animation.play(); } }
// -------------------------------- // START ONCE // --------------------------------
startAnimation(); syncWithVisibility(); // handles a page opened in a background tab document.addEventListener("visibilitychange", syncWithVisibility);
// Continue from approximately the same point instead of jumping back to zero. animation.currentTime = Math.min(1, progress) * duration; syncWithVisibility(); });
resizeObserver.observe(track);
// -------------------------------- // TAKE MANUAL CONTROL // --------------------------------
function takeControl() { if (controlled || !animation) { return; }
// Reset to default page state when mouse leaves the menu navMenu.addEventListener("mouseleave", () => setActive(defaultIndex)); }
function preloadNavImages(elements) { elements.forEach((el) => { const bg = getComputedStyle(el).backgroundImage; for (const m of bg.matchAll(/url\(\s*"([^"]+)"\s*\)/g)) { new Image().src = m[1]; } }); }
function galleryAnim() { if (!pageGuard("#menu-gallery", "#menu-buttons-wrapper")) return;
function decodeQueue(imgs, concurrency = 2) { const queue = [...imgs]; const worker = async () => { while (queue.length) { const img = queue.shift(); try { await img.decode(); // waits for load, then decodes off the main thread } catch (e) {} } }; return Promise.all(Array.from({ length: concurrency }, worker)); }
function setupReveal({ selector, childSelector, root, childScale }) { const images = document.querySelectorAll(selector); if (!images.length) return;
// FIX 1: tell the browser the real rendered width so it picks a smaller // srcset file instead of the 2000px to 2500px one. // clientWidth is 0 for the hidden desktop/tablet copy, so it is skipped. images.forEach((el) => { const img = el.querySelector("img"); if (img && img.clientWidth) { img.sizes = Math.ceil(img.clientWidth * 1.05) + "px"; // 1.05 covers the zoom } });
// Hidden state: nearly transparent, never visibility:hidden, // so the browser still decodes and rasterizes these layers. // FIX 2: no will-change here. WAAPI promotes the layer when the animation starts. images.forEach((image) => { image.style.opacity = "0.001"; image.style.scale = "0.9";
// Decode everything ahead of time, in page order (runs during the preloader) const imgEls = [...images] .map((el) => el.querySelector("img")) .filter(Boolean); decodeQueue(imgEls, 2);
function reveal(image) { const child = image.querySelector(childSelector);
const timing = { duration: 1000, easing: "cubic-bezier(0.215, 0.61, 0.355, 1)", // close to power2.out fill: "forwards", };
function videoPlayback() { const videos = document.querySelectorAll("#track video"); if (!videos.length) return;
videos.forEach((v) => { v.muted = true; // required for play() without a user gesture v.playsInline = true; v.preload = "metadata"; // don't download all 11 MB until needed v.pause(); // make sure nothing is running from the start });
const observer = new IntersectionObserver( (entries) => { entries.forEach((entry) => { const v = entry.target; if (entry.isIntersecting) { const p = v.play(); if (p !== undefined) p.catch(() => {}); // ignore blocked or interrupted plays } else { v.pause(); } }); }, { root: null, // Start slightly before the video is visible so it is already playing // when it slides in. Left/right for desktop, top/bottom for mobile. rootMargin: "10% 25% 10% 25%", threshold: 0, } );