UNPKG

@omicrxn/mercury

Version:

Mercury is a Svelte animation library using AnimeJS under the hood. It simplifies complex animations with Svelte actions, provides layout and exit animations, and integrates seamlessly with Svelte features.

86 lines (85 loc) 2.66 kB
import { mapTransitionToGSAP } from '../utils.js'; let gsapModule = null; let loadPromise = null; async function loadGSAP() { if (gsapModule) return gsapModule; if (typeof window !== 'undefined' && window.gsap) { gsapModule = window.gsap; return gsapModule; } if (!loadPromise) { loadPromise = import('gsap').then((mod) => { gsapModule = mod.default ?? mod; return gsapModule; }, () => { throw new Error('Please install GSAP: npm install gsap'); }); } return loadPromise; } export const GSAPEngine = { animate(element, params) { const { animate: props, transition, callbacks } = params; let tween = null; let gsapReady = false; let pendingOperations = []; // Load GSAP in background loadGSAP().then(gsap => { gsapReady = true; tween = gsap.to(element, { ...props, ...mapTransitionToGSAP(transition, callbacks) }); // Execute queued operations pendingOperations.forEach(op => op()); pendingOperations = []; }).catch(error => { console.error('Failed to load GSAP:', error); }); const instance = { completed: false, play: () => { if (gsapReady && tween) { tween.restart(); } else { pendingOperations.push(() => tween?.restart()); } }, pause: () => { if (gsapReady && tween) { tween.pause(); } else { pendingOperations.push(() => tween?.pause()); } }, cancel: () => { if (gsapReady && tween) { tween.kill(); } else { pendingOperations.push(() => tween?.kill()); } }, onComplete: (onResolve) => { if (gsapReady && tween) { return tween.then(() => { onResolve(); instance.completed = true; }); } else { pendingOperations.push(() => { tween?.then(() => { onResolve(); instance.completed = true; }); }); } } }; return instance; } };