AI Resource Pro / website · airesourcepro.com / src / components / home / start-motion.ts
| 1 | import type { gsap as Gsap } from "gsap"; |
| 2 | import type { ScrollTrigger as ScrollTriggerType } from "gsap/ScrollTrigger"; |
| 3 | import type LenisType from "lenis"; |
| 4 | import type { ParticleField, Pointer } from "./particle-field"; |
| 5 | |
| 6 | type Libs = { gsap: typeof Gsap; ScrollTrigger: typeof ScrollTriggerType; Lenis: typeof LenisType }; |
| 7 | |
| 8 | export function startMotion(root: HTMLElement, { gsap, ScrollTrigger, Lenis }: Libs) { |
| 9 | const reduce = matchMedia("(prefers-reduced-motion: reduce)").matches; |
| 10 | const fine = matchMedia("(pointer: fine)").matches; |
| 11 | gsap.registerPlugin(ScrollTrigger); |
| 12 | if (fine) document.body.classList.add("xp-fine"); |
| 13 | |
| 14 | const cleanups: (() => void)[] = []; |
| 15 | const listen = (el: EventTarget, type: string, fn: (e: Event) => void) => { |
| 16 | el.addEventListener(type, fn); |
| 17 | cleanups.push(() => el.removeEventListener(type, fn)); |
| 18 | }; |
| 19 | |
| 20 | /* smooth scroll */ |
| 21 | let lenis: LenisType | null = null; |
| 22 | if (!reduce) { |
| 23 | try { |
| 24 | lenis = new Lenis({ lerp: 0.085, smoothWheel: true }); |
| 25 | } catch (e) { |
| 26 | console.warn("Smooth scroll disabled:", e); |
| 27 | } |
| 28 | } |
| 29 | const tick = (t: number) => lenis?.raf(t * 1000); |
| 30 | if (lenis) { |
| 31 | lenis.on("scroll", ScrollTrigger.update); |
| 32 | gsap.ticker.add(tick); |
| 33 | gsap.ticker.lagSmoothing(0); |
| 34 | } |
| 35 | const goTo = (target: HTMLElement) => |
| 36 | lenis ? lenis.scrollTo(target, { duration: 1.8 }) : target.scrollIntoView({ behavior: reduce ? "auto" : "smooth" }); |
| 37 | |
| 38 | /* reveals: the hero is hidden by .xp-wait until this point, so it never flashes before animating in */ |
| 39 | const h1 = root.querySelector<HTMLElement>("#xp-h1"); |
| 40 | const chars = h1 ? [...h1.querySelectorAll<HTMLElement>(".ch")] : []; |
| 41 | const cw = chars.map(() => 400); |
| 42 | root.classList.remove("xp-wait"); |
| 43 | const ctx = gsap.context(() => { |
| 44 | if (reduce) return; |
| 45 | gsap.from(".hero .wi", { yPercent: 115, duration: 1.4, ease: "expo.out", stagger: 0.07, delay: 0.15 }); |
| 46 | gsap.from(".hero [data-r]", { y: 28, opacity: 0, duration: 1.1, ease: "power3.out", stagger: 0.09, delay: 0.55 }); |
| 47 | root.querySelectorAll<HTMLElement>(".sec:not(.hero)").forEach((sec) => { |
| 48 | const copy = sec.querySelector(".copy"); |
| 49 | if (!copy) return; |
| 50 | const tl = gsap.timeline({ scrollTrigger: { trigger: sec, start: "top 62%" } }); |
| 51 | tl.from(copy.querySelectorAll("h2 .wi"), { yPercent: 115, duration: 1.2, ease: "expo.out", stagger: 0.055 }).from( |
| 52 | copy.querySelectorAll("[data-r]"), |
| 53 | { y: 26, opacity: 0, duration: 1, ease: "power3.out", stagger: 0.08 }, |
| 54 | 0.25 |
| 55 | ); |
| 56 | }); |
| 57 | // each line travels its full length while the band is on screen, so every word passes through view |
| 58 | const m1 = root.querySelector<HTMLElement>("#xp-m1"), m2 = root.querySelector<HTMLElement>("#xp-m2"); |
| 59 | if (m1 && m2) { |
| 60 | const mq = { trigger: ".marquee", start: "top bottom", end: "bottom top", scrub: 0.6, invalidateOnRefresh: true }; |
| 61 | gsap.fromTo(m1, { x: () => innerWidth * 0.55 }, { x: () => -(m1.scrollWidth - innerWidth * 0.45), ease: "none", scrollTrigger: mq }); |
| 62 | gsap.fromTo(m2, { x: () => -(m2.scrollWidth - innerWidth * 0.45) }, { x: () => innerWidth * 0.55, ease: "none", scrollTrigger: { ...mq } }); |
| 63 | } |
| 64 | }, root); |
| 65 | |
| 66 | /* custom cursor and magnetic buttons */ |
| 67 | const cur = document.querySelector<HTMLElement>(".xp-cur"); |
| 68 | const cDot = cur?.querySelector<HTMLElement>(".c-dot"), cRing = cur?.querySelector<HTMLElement>(".c-ring"); |
| 69 | const mouse = { x: innerWidth / 2, y: innerHeight / 2, rx: innerWidth / 2, ry: innerHeight / 2 }; |
| 70 | const pointer: Pointer = { nx: 0, ny: 0, active: false }; |
| 71 | listen(window, "pointermove", (e) => { |
| 72 | const p = e as PointerEvent; |
| 73 | if (p.pointerType !== "mouse") return; |
| 74 | mouse.x = p.clientX; mouse.y = p.clientY; pointer.active = fine; |
| 75 | pointer.nx = (p.clientX / innerWidth) * 2 - 1; pointer.ny = -((p.clientY / innerHeight) * 2 - 1); |
| 76 | }); |
| 77 | listen(document, "pointerleave", () => (pointer.active = false)); |
| 78 | document.querySelectorAll("a, button").forEach((el) => { |
| 79 | listen(el, "pointerenter", () => cur?.classList.add("hover")); |
| 80 | listen(el, "pointerleave", () => cur?.classList.remove("hover")); |
| 81 | }); |
| 82 | if (fine && !reduce) { |
| 83 | root.querySelectorAll<HTMLElement>(".mag").forEach((el) => { |
| 84 | listen(el, "pointermove", (e) => { |
| 85 | const p = e as PointerEvent, r = el.getBoundingClientRect(); |
| 86 | 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" }); |
| 87 | }); |
| 88 | listen(el, "pointerleave", () => gsap.to(el, { x: 0, y: 0, duration: 1, ease: "elastic.out(1, 0.35)" })); |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | /* section dots, and the scroll position as a stage from 0 (hero) to the last section */ |
| 93 | const secs = [...root.querySelectorAll<HTMLElement>("[data-shape]")]; |
| 94 | const dots = [...document.querySelectorAll<HTMLButtonElement>(".xp-dots button")]; |
| 95 | dots.forEach((d) => listen(d, "click", () => goTo(secs[Number(d.dataset.go)]))); |
| 96 | let anchors: number[] = []; |
| 97 | const measure = () => { |
| 98 | anchors = secs.map((s) => s.getBoundingClientRect().top + scrollY + s.offsetHeight / 2 - innerHeight / 2); |
| 99 | anchors[0] = 0; |
| 100 | }; |
| 101 | const stageAt = (y: number) => { |
| 102 | if (y <= anchors[0]) return 0; |
| 103 | for (let i = 0; i < anchors.length - 1; i++) if (y < anchors[i + 1]) return i + (y - anchors[i]) / (anchors[i + 1] - anchors[i]); |
| 104 | return anchors.length - 1; |
| 105 | }; |
| 106 | measure(); |
| 107 | |
| 108 | let field: ParticleField | null = null; |
| 109 | let resizeT = 0; |
| 110 | listen(window, "resize", () => { |
| 111 | clearTimeout(resizeT); |
| 112 | resizeT = window.setTimeout(() => { |
| 113 | measure(); |
| 114 | field?.resize(); |
| 115 | ScrollTrigger.refresh(); |
| 116 | }, 180); |
| 117 | }); |
| 118 | document.fonts?.ready.then(() => { |
| 119 | measure(); |
| 120 | ScrollTrigger.refresh(); |
| 121 | }); |
| 122 | |
| 123 | /* frame loop */ |
| 124 | let sS = 0, lastY = scrollY, vel = 0, raf = 0; |
| 125 | const t0 = performance.now(); |
| 126 | const frame = (now: number) => { |
| 127 | const t = (now - t0) * 0.001 * (reduce ? 0.25 : 1); |
| 128 | const y = scrollY; |
| 129 | sS += (stageAt(y) - sS) * (reduce ? 1 : 0.07); |
| 130 | vel = vel * 0.9 + (Math.min(Math.abs(y - lastY), 140) / 140) * 0.1; |
| 131 | lastY = y; |
| 132 | |
| 133 | const on = Math.min(secs.length - 1, Math.max(0, Math.round(sS))); |
| 134 | dots.forEach((d, i) => d.classList.toggle("on", i === on)); |
| 135 | |
| 136 | mouse.rx += (mouse.x - mouse.rx) * 0.16; mouse.ry += (mouse.y - mouse.ry) * 0.16; |
| 137 | if (fine && cDot && cRing) { |
| 138 | cDot.style.transform = `translate(${mouse.x}px, ${mouse.y}px)`; |
| 139 | cRing.style.transform = `translate(${mouse.rx}px, ${mouse.ry}px)`; |
| 140 | } |
| 141 | |
| 142 | // hero letters thicken near the cursor |
| 143 | if (fine && !reduce && y < innerHeight) { |
| 144 | for (let i = 0; i < chars.length; i++) { |
| 145 | const r = chars[i].getBoundingClientRect(), dx = r.left + r.width / 2 - mouse.x, dy = r.top + r.height / 2 - mouse.y; |
| 146 | const goal = pointer.active ? 400 + 420 * Math.exp(-(dx * dx + dy * dy) / (190 * 190)) : 400; |
| 147 | cw[i] += (goal - cw[i]) * 0.12; |
| 148 | chars[i].style.fontWeight = String(Math.round(cw[i])); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | field?.render(sS, t, reduce ? 0 : Math.min(vel * 3, 1.5), pointer); |
| 153 | raf = requestAnimationFrame(frame); |
| 154 | }; |
| 155 | raf = requestAnimationFrame(frame); |
| 156 | |
| 157 | return { |
| 158 | setField(f: ParticleField | null) { |
| 159 | field = f; |
| 160 | }, |
| 161 | stop() { |
| 162 | cancelAnimationFrame(raf); |
| 163 | clearTimeout(resizeT); |
| 164 | cleanups.forEach((fn) => fn()); |
| 165 | ctx.revert(); |
| 166 | if (lenis) { |
| 167 | gsap.ticker.remove(tick); |
| 168 | gsap.ticker.lagSmoothing(500, 33); |
| 169 | lenis.destroy(); |
| 170 | } |
| 171 | document.body.classList.remove("xp-fine"); |
| 172 | field?.dispose(); |
| 173 | }, |
| 174 | }; |
| 175 | } |