Animation

From j4flmao/agent-skills · View interactive version

Animation is an AI agent skill from the GitHub repository j4flmao/agent-skills. Use when the user asks about Motion (Framer Motion), GSAP, CSS keyframes and transitions, the View Transitions API, page transitions, enter/exit or spring animations and prefers-reduced-motion. Produces performant, accessible web animation code. Not for motion principles and specs (use design-motion

How to use this skill

Skill file

Frontend Animation

Description: Implements web animations -- page transitions, gesture animations, micro-interactions, motion design. Triggered by "animation", "motion", "Framer Motion", "GSAP", "CSS animation", "page transition", "enter animation", "exit animation", "gesture animation", "spring animation", "keyframe", "motion design".

Version: 1.2.0

Author: j4flmao

License: MIT


Purpose

Deliver performant, accessible, and cohesive motion design across the frontend -- from micro-interactions to full page transitions -- while respecting user accessibility preferences and maintaining 60fps frame budget across mid-range devices.


Agent Protocol

Trigger

User request includes any of: "animation", "motion", "Framer Motion", "GSAP", "CSS animation", "page transition", "enter animation", "exit animation", "gesture animation", "spring animation", "keyframe", "motion design".

Input Context

Output Artifact

Animation strategy as text / animation code snippets.

Response Format

## Strategy
<animation-strategy>

## Implementation
<code-snippets>

## Performance Notes
<gpu-compositing, frame-budget notes>

## Accessibility
<reduced-motion handling>

--
Compression footer: frontend-animation/v1 | 4 sections | lib: <selected> | perf: <ok|warn>

No preamble. No postamble. No explanations. No filler/hedging/transitions. Compress output.

Completion Criteria

Max Response Length

4096 tokens


Component Architecture / Decision Trees

Animation Library Decision Tree

Is it a React project?
  |-- YES --> Is animation declarative (mount/unmount)?
  |     |-- YES --> Use Framer Motion (AnimatePresence, layout animations)
  |     |-- NO  --> Is it a single gesture or hover?
  |           |-- YES --> CSS transitions (no JS dep needed)
  |           |-- NO  --> Framer Motion or react-spring
  |-- NO --> Is it a complex timeline sequence?
        |-- YES --> GSAP (timelines, scrollTrigger, SVG morph)
        |-- NO  --> Is it a simple CSS animation?
              |-- YES --> CSS @keyframes + transitions
              |-- NO  --> WAAPI (Web Animations API)

Architecture Options

Option A: CSS-only animation -- Zero JS overhead. Best for: hover states, focus rings, loading spinners, skeleton pulses. Limited to simple transitions and keyframes. No timeline sequencing. Control via CSS custom properties for theming.

.element {
  transition: transform 200ms ease, opacity 200ms ease;
}
.element:hover {
  transform: scale(1.05);
}

Option B: Framer Motion (React) -- Declarative spring animations, layout animations via layout prop, exit animations via AnimatePresence, gesture handling (drag, swipe, hover, tap). Best for: component-level animations in React SPAs and Next.js. Bundle size: ~35KB gzipped.

<motion.div
  initial={{ opacity: 0, y: 20 }}
  animate={{ opacity: 1, y: 0 }}
  exit={{ opacity: 0 }}
  transition={{ type: "spring", stiffness: 300, damping: 25 }}
/>

Option C: GSAP (GreenSock) -- Professional-grade timeline control, ScrollTrigger plugin, SVG morphing, cross-browser consistency. Best for: marketing pages, complex reveal sequences, scroll-driven narratives, non-React projects. Bundle size: ~45KB gzipped with ScrollTrigger.

Option D: WAAPI (Web Animations API) -- Native browser API. Framework-agnostic. Zero dependency cost. Best for: simple imperative sequences, polyfill-free modern browsers. Limited easing options (no spring), no timeline chaining without manual orchestration.

element.animate([
  { transform: 'scale(1)', opacity: 1 },
  { transform: 'scale(1.1)', opacity: 0.8 }
], { duration: 200, easing: 'ease-out', fill: 'forwards' })

Tradeoff Matrix

| Criterion | CSS | Framer Motion | GSAP | WAAPI |

|-----------|-----|---------------|------|-------|

| Bundle cost | 0 KB | ~35 KB | ~45 KB | 0 KB |

| Spring physics | No | Yes (configurable) | Yes (GSAP 3.6+) | No |

| Timeline sequencing | Manual | AnimatePresence | Native timeline | Manual promise chain |

| Scroll-driven | Intersection Observer | useInView | ScrollTrigger | Intersection Observer |

| SVG morphing | No | layout animation | MorphSVG plugin | No |

| Cross-browser | Native | React only | All (IE11+) | Modern browsers only |

| Learning curve | Low | Medium | Medium-High | Low |


Workflow

1. Animation Library Selection

2. Page Transitions

3. Gesture Animations

4. Performance

5. Accessibility

6. Micro-interactions

7. Scroll-Triggered Animation

8. SVG Animation

9. Testing Animations


Common Pitfalls

1. Animating Layout Properties

Using width, height, top, left triggers layout recalculation on every frame. This causes jank, especially on mobile. Always use transform: scale() for size and transform: translate() for position.

/* BAD */
.element {
  animation: slide 300ms;
}
@keyframes slide {
  from { left: -100px; }
  to { left: 0; }
}

/* GOOD */
@keyframes slide {
  from { transform: translateX(-100px); }
  to { transform: translateX(0); }
}

2. Forgetting Exit Animations

When removing elements from the DOM, the element disappears instantly unless you explicitly animate exit. In React, wrap with AnimatePresence and define exit prop. For CSS, use a timeout to toggle visibility before removal.

3. Spring Over-oscillation

Default spring parameters often cause visible bounce that feels unpolished. For UI elements, use stiffness: 200-300 and damping: 15-25. Stiffness > 500 creates robotic motion. Damping < 10 causes excessive wobble.

4. Unthrottled Scroll Event Listeners

Attaching animation triggers to scroll without throttling fires hundreds of callbacks per second. Use requestAnimationFrame or IntersectionObserver instead of scroll events. If scroll events are unavoidable, throttle to 16ms intervals.

5. Animating Too Many Elements Simultaneously

More than 5-8 simultaneous animated elements will drop frames on mid-range devices. Batch animations into staggered sequences. Use will-change sparingly -- each promoted layer consumes GPU memory.

6. No Reduced Motion Check for Auto-play

Auto-playing animations (carousels, marquees, background video) that ignore prefers-reduced-motion cause vestibular disorders for users with motion sensitivity. Always gate auto-play animations behind the reduced motion media query.

7. Layout Shift from Animation Removals

When animating elements out of the document flow, surrounding content snaps to fill the gap before the animation completes. Use position: absolute on exiting elements or animate height alongside opacity with overflow: hidden on the parent.

8. Missing `fill: forwards` on Final Frame

Without animation-fill-mode: forwards or WAAPI fill: "forwards", the animated element snaps back to its pre-animation state when the animation ends. This creates a jarring visual pop.


Compared With

| Approach | Strengths | Weaknesses | Best Use Case |

|----------|-----------|------------|---------------|

| CSS transitions/keyframes | Zero JS, GPU-composited, declarative | No timeline, no spring, limited sequencing | Hover, focus, simple state changes |

| Framer Motion | Declarative springs, layout animations, exit animations, drag/gesture | React-only, ~35KB bundle | React component animations |

| GSAP | Full timeline control, ScrollTrigger, SVG morph, IE11+ | ~45KB, commercial license for some plugins | Marketing sites, complex sequences |

| react-spring | Physics-based springs, interpolation | Smaller ecosystem, fewer gesture helpers | Natural-feeling UI motion |

| WAAPI | Zero cost, native, imperative | No spring easing, no timeline chaining | Simple imperative sequences |

| Lottie/Bodymovin | After Effects export, complex vector animations | Large file sizes, no runtime interaction | Brand animations, splash screens |

| anime.js | Lightweight, versatile JS animations | Smaller community, less maintenance | General purpose JS animations |


Advanced Gesture Animations

// Framer Motion — complex gesture composition
import { motion, useMotionValue, useTransform, useSpring } from 'framer-motion';

function Card3D({ children }: { children: React.ReactNode }) {
  const x = useMotionValue(0);
  const y = useMotionValue(0);

  const rotateX = useSpring(useTransform(y, [-0.5, 0.5], [10, -10]), {
    stiffness: 300, damping: 30,
  });
  const rotateY = useSpring(useTransform(x, [-0.5, 0.5], [-10, 10]), {
    stiffness: 300, damping: 30,
  });

  function handleMouseMove(event: React.MouseEvent) {
    const rect = event.currentTarget.getBoundingClientRect();
    const xPos = (event.clientX - rect.left) / rect.width - 0.5;
    const yPos = (event.clientY - rect.top) / rect.height - 0.5;
    x.set(xPos); y.set(yPos);
  }

  function handleMouseLeave() { x.set(0); y.set(0); }

  return (
    <motion.div onMouseMove={handleMouseMove} onMouseLeave={handleMouseLeave}
      style={{ rotateX, rotateY, transformStyle: 'preserve-3d' }}>
      {children}
    </motion.div>
  );
}

Spring Physics Reference

| Feel | Stiffness | Damping | Mass | Use Case |

|------|-----------|---------|------|----------|

| Snappy UI | 300 | 25 | 0.5 | Buttons, toggles, micro-interactions |

| Gentle UI | 200 | 20 | 1.0 | Cards, modals, sheets |

| Bouncy | 150 | 10 | 1.0 | Fun reveal, celebration animations |

| Heavy | 500 | 40 | 2.0 | Drag constraints, large elements |

| Fluid | 100 | 15 | 1.0 | Page transitions, parallax |

| Stiff (no bounce) | 400 | 40 | 1.0 | Progress bars, loading indicators |

const uiSpring = { type: 'spring', stiffness: 300, damping: 20, mass: 0.5 };
const gestureSpring = { type: 'spring', stiffness: 500, damping: 30, mass: 1 };

Layout Animations (FLIP)

function ReorderList() {
  const [items, setItems] = useState(['A', 'B', 'C', 'D']);
  return (
    <div>
      <button onClick={() => setItems([...items].sort(() => Math.random() - 0.5))}>Shuffle</button>
      <ul>
        {items.map((item) => (
          <motion.li key={item} layout transition={{ type: 'spring', stiffness: 300, damping: 25 }}>
            {item}
          </motion.li>
        ))}
      </ul>
    </div>
  );
}

Shared Element Transitions

// Shared layoutId enables cross-page element transitions
// Page A: <motion.img layoutId={`product-image-${id}`} src={product.image} />
// Page B: <motion.img layoutId={`product-image-${id}`} src={product.image} style={{ width: '100%', height: 400 }} />
// Framer Motion animates the transition between the two layouts automatically

CSS-Driven Animations (Zero JS)

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}

.animate-fade-in {
  animation: fadeIn 0.3s ease-out both;
}

.animate-stagger > * {
  opacity: 0;
  animation: fadeIn 0.3s ease-out both;
}

.animate-stagger > *:nth-child(1) { animation-delay: 0ms; }
.animate-stagger > *:nth-child(2) { animation-delay: 75ms; }
.animate-stagger > *:nth-child(3) { animation-delay: 150ms; }
.animate-stagger > *:nth-child(4) { animation-delay: 225ms; }
// View Transition API (Chrome 111+) — instant cross-page morphing
// app/layout.tsx
function navigateWithTransition(path: string) {
  if (document.startViewTransition) {
    document.startViewTransition(() => {
      router.push(path);
    });
  } else {
    router.push(path);
  }
}

// Set view-transition-name on shared elements
// <img style={{ viewTransitionName: 'product-image' }} />

Performance Budget by Animation Type

| Animation Type | Max Elements | Max Duration | Frame Budget |

|---------------|-------------|-------------|--------------|

| Micro-interaction (hover, tap) | 1-2 | 300ms | < 4ms JS |

| Page transition | 5-10 | 400ms | < 8ms JS |

| Scroll reveal | 10-20 | 600ms | < 6ms JS |

| Parallax | 3-5 | Continuous | < 2ms JS |

| Particle (DOM) | 5-8 | 2s | < 8ms JS |

| Particle (Canvas) | 100-1000 | 5s | < 10ms JS |

Performance Considerations

Frame Budget Breakdown

Total budget per frame at 60fps: 16.67ms. Recommended allocation: JS animation work < 8ms, style recalculation < 3ms, paint + composite < 5ms. Exceeding 16ms causes dropped frames.

Layer Promotion Pitfalls

The browser promotes elements to GPU compositor layers when will-change, 3D transforms (translateZ(0)), or <video>/<canvas> are present. Each layer consumes ~1-2MB of GPU memory. On mobile devices with 128-256MB GPU memory, 50+ layers exhaust memory and crash the GPU process.

Measurement Tools

Security Considerations

Accessibility Considerations (Expanded)


Ecosystem & Tooling

Libraries

Design Tools

Performance Profilers


Rules

  1. Animate only transform and opacity. Never animate width, height, top, left, margin, padding.
  2. Respect prefers-reduced-motion -- instant transitions on reduce, full motion on no-preference.
  3. Spring animations for UI elements: stiffness between 100-300, damping between 10-20.
  4. Micro-interactions must complete within 300ms.
  5. Page transitions must complete within 500ms.
  6. Use will-change sparingly -- only on elements that animate continuously, and remove when idle.
  7. Profile frame budget -- JS animation work must stay under 10ms per frame.
  8. Provide reduced-motion fallback that still conveys state change (e.g., opacity fade instead of scale + rotate).
  9. Never auto-play motion without checking prefers-reduced-motion.
  10. Stagger animated elements with 50-100ms delay between each, never all at once.
  11. Use transform-origin correctly for scale/rotate animations -- default is center.
  12. Avoid animating box-shadow -- prefer pseudo-element opacity trick for performance.
  13. Test on a mid-range Android device (Moto G4 equivalent) before shipping.
  14. GSAP ScrollTrigger animations must use scrub: 1 or toggleActions for smooth scroll-linked motion.
  15. Never animate elements that are currently being measured by ResizeObserver or IntersectionObserver.
  16. Validate Lottie JSON from untrusted sources before rendering with lotti-web.

References

Handoff

When complete, output the animation strategy with implementation snippets. If the request scope exceeds page transitions + micro-interactions (e.g., full motion design system), flag for a dedicated motion designer handoff. For GSAP ScrollTrigger-heavy work, flag for scroll experience specialist.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.