import type { gsap as Gsap } from "gsap"; import type { ScrollTrigger as ScrollTriggerType } from "gsap/ScrollTrigger"; import type LenisType from "lenis"; import type { ParticleField, Pointer } from "./particle-field"; type Libs = { gsap: typeof Gsap; ScrollTrigger: typeof ScrollTriggerType; Lenis: typeof LenisType }; export function startMotion(root: HTMLElement, { gsap, ScrollTrigger, Lenis }: Libs) { const reduce = matchMedia("(prefers-reduced-motion: reduce)").matches; const fine = matchMedia("(pointer: fine)").matches; gsap.registerPlugin(ScrollTrigger); if (fine) document.body.classList.add("xp-fine"); const cleanups: (() => void)[] = []; const listen = (el: EventTarget, type: string, fn: (e: Event) => void) => { el.addEventListener(type, fn); cleanups.push(() => el.removeEventListener(type, fn)); }; /* smooth scroll */ let lenis: LenisType | null = null; if (!reduce) { try { lenis = new Lenis({ lerp: 0.085, smoothWheel: true }); } catch (e) { console.warn("Smooth scroll disabled:", e); } } const tick = (t: number) => lenis?.raf(t * 1000); if (lenis) { lenis.on("scroll", ScrollTrigger.update); gsap.ticker.add(tick); gsap.ticker.lagSmoothing(0); } const goTo = (target: HTMLElement) => lenis ? lenis.scrollTo(target, { duration: 1.8 }) : target.scrollIntoView({ behavior: reduce ? "auto" : "smooth" }); /* reveals: the hero is hidden by .xp-wait until this point, so it never flashes before animating in */ const h1 = root.querySelector("#xp-h1"); const chars = h1 ? [...h1.querySelectorAll(".ch")] : []; const cw = chars.map(() => 400); root.classList.remove("xp-wait"); const ctx = gsap.context(() => { if (reduce) return; gsap.from(".hero .wi", { yPercent: 115, duration: 1.4, ease: "expo.out", stagger: 0.07, delay: 0.15 }); gsap.from(".hero [data-r]", { y: 28, opacity: 0, duration: 1.1, ease: "power3.out", stagger: 0.09, delay: 0.55 }); root.querySelectorAll(".sec:not(.hero)").forEach((sec) => { const copy = sec.querySelector(".copy"); if (!copy) return; const tl = gsap.timeline({ scrollTrigger: { trigger: sec, start: "top 62%" } }); tl.from(copy.querySelectorAll("h2 .wi"), { yPercent: 115, duration: 1.2, ease: "expo.out", stagger: 0.055 }).from( copy.querySelectorAll("[data-r]"), { y: 26, opacity: 0, duration: 1, ease: "power3.out", stagger: 0.08 }, 0.25 ); }); // each line travels its full length while the band is on screen, so every word passes through view const m1 = root.querySelector("#xp-m1"), m2 = root.querySelector("#xp-m2"); if (m1 && m2) { const mq = { trigger: ".marquee", start: "top bottom", end: "bottom top", scrub: 0.6, invalidateOnRefresh: true }; gsap.fromTo(m1, { x: () => innerWidth * 0.55 }, { x: () => -(m1.scrollWidth - innerWidth * 0.45), ease: "none", scrollTrigger: mq }); gsap.fromTo(m2, { x: () => -(m2.scrollWidth - innerWidth * 0.45) }, { x: () => innerWidth * 0.55, ease: "none", scrollTrigger: { ...mq } }); } }, root); /* custom cursor and magnetic buttons */ const cur = document.querySelector(".xp-cur"); const cDot = cur?.querySelector(".c-dot"), cRing = cur?.querySelector(".c-ring"); const mouse = { x: innerWidth / 2, y: innerHeight / 2, rx: innerWidth / 2, ry: innerHeight / 2 }; const pointer: Pointer = { nx: 0, ny: 0, active: false }; listen(window, "pointermove", (e) => { const p = e as PointerEvent; if (p.pointerType !== "mouse") return; mouse.x = p.clientX; mouse.y = p.clientY; pointer.active = fine; pointer.nx = (p.clientX / innerWidth) * 2 - 1; pointer.ny = -((p.clientY / innerHeight) * 2 - 1); }); listen(document, "pointerleave", () => (pointer.active = false)); document.querySelectorAll("a, button").forEach((el) => { listen(el, "pointerenter", () => cur?.classList.add("hover")); listen(el, "pointerleave", () => cur?.classList.remove("hover")); }); if (fine && !reduce) { root.querySelectorAll(".mag").forEach((el) => { listen(el, "pointermove", (e) => { const p = e as PointerEvent, r = el.getBoundingClientRect(); gsap.to(el, { x: (p.clientX - r.left - r.width / 2) * 0.32, y: (p.clientY - r.top - r.height / 2) * 0.42, duration: 0.5, ease: "power3.out" }); }); listen(el, "pointerleave", () => gsap.to(el, { x: 0, y: 0, duration: 1, ease: "elastic.out(1, 0.35)" })); }); } /* section dots, and the scroll position as a stage from 0 (hero) to the last section */ const secs = [...root.querySelectorAll("[data-shape]")]; const dots = [...document.querySelectorAll(".xp-dots button")]; dots.forEach((d) => listen(d, "click", () => goTo(secs[Number(d.dataset.go)]))); let anchors: number[] = []; const measure = () => { anchors = secs.map((s) => s.getBoundingClientRect().top + scrollY + s.offsetHeight / 2 - innerHeight / 2); anchors[0] = 0; }; const stageAt = (y: number) => { if (y <= anchors[0]) return 0; for (let i = 0; i < anchors.length - 1; i++) if (y < anchors[i + 1]) return i + (y - anchors[i]) / (anchors[i + 1] - anchors[i]); return anchors.length - 1; }; measure(); let field: ParticleField | null = null; let resizeT = 0; listen(window, "resize", () => { clearTimeout(resizeT); resizeT = window.setTimeout(() => { measure(); field?.resize(); ScrollTrigger.refresh(); }, 180); }); document.fonts?.ready.then(() => { measure(); ScrollTrigger.refresh(); }); /* frame loop */ let sS = 0, lastY = scrollY, vel = 0, raf = 0; const t0 = performance.now(); const frame = (now: number) => { const t = (now - t0) * 0.001 * (reduce ? 0.25 : 1); const y = scrollY; sS += (stageAt(y) - sS) * (reduce ? 1 : 0.07); vel = vel * 0.9 + (Math.min(Math.abs(y - lastY), 140) / 140) * 0.1; lastY = y; const on = Math.min(secs.length - 1, Math.max(0, Math.round(sS))); dots.forEach((d, i) => d.classList.toggle("on", i === on)); mouse.rx += (mouse.x - mouse.rx) * 0.16; mouse.ry += (mouse.y - mouse.ry) * 0.16; if (fine && cDot && cRing) { cDot.style.transform = `translate(${mouse.x}px, ${mouse.y}px)`; cRing.style.transform = `translate(${mouse.rx}px, ${mouse.ry}px)`; } // hero letters thicken near the cursor if (fine && !reduce && y < innerHeight) { for (let i = 0; i < chars.length; i++) { const r = chars[i].getBoundingClientRect(), dx = r.left + r.width / 2 - mouse.x, dy = r.top + r.height / 2 - mouse.y; const goal = pointer.active ? 400 + 420 * Math.exp(-(dx * dx + dy * dy) / (190 * 190)) : 400; cw[i] += (goal - cw[i]) * 0.12; chars[i].style.fontWeight = String(Math.round(cw[i])); } } field?.render(sS, t, reduce ? 0 : Math.min(vel * 3, 1.5), pointer); raf = requestAnimationFrame(frame); }; raf = requestAnimationFrame(frame); return { setField(f: ParticleField | null) { field = f; }, stop() { cancelAnimationFrame(raf); clearTimeout(resizeT); cleanups.forEach((fn) => fn()); ctx.revert(); if (lenis) { gsap.ticker.remove(tick); gsap.ticker.lagSmoothing(500, 33); lenis.destroy(); } document.body.classList.remove("xp-fine"); field?.dispose(); }, }; }