/** * Animation preset registry. Built-in presets (fadeUp, scaleIn, etc.) and registerPreset/getPreset. * * @module animation/registry * @see ANIMATION_IDEAS.md ยง1 Presets */ import type { PresetDef } from './types'; /** * Built-in animation presets. When `preset` is used in animate() or revealInSequence, these provide from/to/ease. * Duration is taken from the call-site or config. */ export const BUILTIN_PRESETS: Record = { fadeUp: { from: { opacity: 0, y: 20 }, to: { opacity: 1, y: 0 }, ease: 'power2.out', }, fadeIn: { from: { opacity: 0 }, to: { opacity: 1 }, ease: 'power2.out', }, scaleIn: { from: { opacity: 0, scale: 0.9 }, to: { opacity: 1, scale: 1 }, ease: 'back.out(1.2)', }, slideLeft: { from: { opacity: 0, x: 40 }, to: { opacity: 1, x: 0 }, ease: 'power2.out', }, slideRight: { from: { opacity: 0, x: -40 }, to: { opacity: 1, x: 0 }, ease: 'power2.out', }, zoomIn: { from: { opacity: 0, scale: 0.7 }, to: { opacity: 1, scale: 1 }, ease: 'power2.out', }, blurIn: { from: { opacity: 0, filter: 'blur(12px)' }, to: { opacity: 1, filter: 'blur(0)' }, ease: 'power2.out', }, spring: { from: { opacity: 0, y: 30 }, to: { opacity: 1, y: 0 }, ease: 'elastic.out(1, 0.4)', }, drop: { from: { y: -80, opacity: 0 }, to: { y: 0, opacity: 1 }, ease: 'back.out(1.5)', }, fadeOut: { from: { opacity: 1 }, to: { opacity: 0 }, ease: 'power2.in', }, }; const custom: Record = {}; /** * Returns a preset by name. Built-ins first, then custom. Null if not found. */ export function getPreset(name: string): PresetDef | null { return BUILTIN_PRESETS[name] ?? custom[name] ?? null; } /** * Registers a custom preset. Overrides a built-in if the same name is used. */ export function registerPreset(name: string, def: PresetDef): void { custom[name] = { ...def }; } /** * Resolves from/to/ease/duration from preset, motion (meta.motions), or explicit overrides. * - If `preset` or `motion` is set: resolve from `motions?.[name]` (deck.meta.motions) first, else getPreset(name). * - Merges from/to/ease/duration from input over the resolved preset/motion. * * @param input - preset?, motion?, from?, to?, ease?, duration? * @param defaults - fallbacks when a value is missing (e.g. from config or revealInSequence defaults). * @param motions - optional Record from deck.meta.motions; keys are motion ids, values are { from, to, ease?, duration? }. * @returns { from, to, ease, duration } with `to` always set; `from` can be {} for gsap.to. */ export function resolveAnimationFromInput( input: { preset?: string; motion?: string; from?: Record; to?: Record; ease?: string; duration?: number; }, defaults?: { from?: Record; to?: Record; ease?: string; duration?: number }, motions?: Record; to?: Record; ease?: string; duration?: number }> ): { from: Record; to: Record; ease: string; duration: number } { let from: Record = { ...(defaults?.from ?? {}) }; let to: Record = { ...(defaults?.to ?? { opacity: 1 }) }; let ease = defaults?.ease ?? 'power2.out'; let duration = defaults?.duration ?? 0.5; const name = input.preset || input.motion; if (name) { const p = (motions && motions[name]) || getPreset(name); if (p) { if (p.from != null) from = { ...p.from }; if (p.to != null) to = { ...p.to }; if (p.ease != null) ease = p.ease; if (p.duration != null) duration = p.duration; } } if (input.from && Object.keys(input.from).length) from = { ...from, ...input.from }; if (input.to && Object.keys(input.to).length) to = { ...to, ...input.to }; if (input.ease != null) ease = input.ease; if (input.duration != null) duration = input.duration; if (Object.keys(to).length === 0) to = { opacity: 1 }; return { from, to, ease, duration }; }