lottie
Play After Effects animations on web and mobile with Lottie — load JSON animation files, control playback, listen to events, and integrate animations into React, Vue, or vanilla JS apps. Use when tasks involve adding motion graphics, animated icons, loading indicators, or micro-interactions exported from After Effects or other animation tools.
What this skill does
# Lottie
Render After Effects animations exported as JSON. Lightweight, scalable, and interactive.
## Setup
```bash
# Install lottie-web for vanilla JS/TS projects.
npm install lottie-web
```
## Basic Playback
```typescript
// src/lottie/player.ts — Load and play a Lottie animation in a DOM container.
// The animation JSON is typically exported from After Effects via Bodymovin.
import lottie, { AnimationItem } from "lottie-web";
export function playAnimation(
container: HTMLElement,
animationData: object
): AnimationItem {
return lottie.loadAnimation({
container,
renderer: "svg", // "canvas" or "html" also available
loop: true,
autoplay: true,
animationData,
});
}
// Load from URL instead of inline data
export function playFromUrl(container: HTMLElement, path: string): AnimationItem {
return lottie.loadAnimation({
container,
renderer: "svg",
loop: true,
autoplay: true,
path, // URL to the JSON file
});
}
```
## Playback Controls
```typescript
// src/lottie/controls.ts — Control animation playback: play, pause, seek, speed.
import type { AnimationItem } from "lottie-web";
export function setupControls(anim: AnimationItem) {
// Play / Pause
anim.play();
anim.pause();
anim.stop();
// Go to specific frame (frame 30, and play)
anim.goToAndPlay(30, true);
// Go to specific frame and stop
anim.goToAndStop(0, true);
// Playback speed (2x)
anim.setSpeed(2);
// Play direction (-1 = reverse)
anim.setDirection(-1);
// Play only a segment (frames 10-50)
anim.playSegments([10, 50], true);
}
```
## Event Handling
```typescript
// src/lottie/events.ts — Listen to animation lifecycle events for triggering
// UI updates, chaining animations, or tracking analytics.
import type { AnimationItem } from "lottie-web";
export function attachEvents(anim: AnimationItem) {
anim.addEventListener("complete", () => {
console.log("Animation completed");
});
anim.addEventListener("loopComplete", () => {
console.log("Loop finished");
});
anim.addEventListener("enterFrame", (e) => {
// Fires every frame — use sparingly
const progress = (e as any).currentTime / anim.totalFrames;
document.getElementById("progress")!.style.width = `${progress * 100}%`;
});
anim.addEventListener("DOMLoaded", () => {
console.log("Animation DOM elements ready");
});
}
```
## React Integration
```tsx
// src/components/LottiePlayer.tsx — React component wrapping lottie-web.
// Handles cleanup on unmount and exposes ref for external control.
import { useEffect, useRef } from "react";
import lottie, { AnimationItem } from "lottie-web";
interface Props {
animationData: object;
loop?: boolean;
autoplay?: boolean;
className?: string;
}
export function LottiePlayer({ animationData, loop = true, autoplay = true, className }: Props) {
const containerRef = useRef<HTMLDivElement>(null);
const animRef = useRef<AnimationItem | null>(null);
useEffect(() => {
if (!containerRef.current) return;
animRef.current = lottie.loadAnimation({
container: containerRef.current,
renderer: "svg",
loop,
autoplay,
animationData,
});
return () => {
animRef.current?.destroy();
};
}, [animationData, loop, autoplay]);
return <div ref={containerRef} className={className} />;
}
```
## Dynamic Color Updates
```typescript
// src/lottie/theme.ts — Modify colors inside a Lottie JSON before rendering.
// Useful for theming animations to match brand colors at runtime.
export function recolorAnimation(
animationData: any,
colorMap: Record<string, [number, number, number]>
): any {
const data = JSON.parse(JSON.stringify(animationData));
function walkShapes(shapes: any[]) {
for (const shape of shapes) {
if (shape.ty === "fl" && shape.c?.k) {
const hex = rgbToHex(shape.c.k[0], shape.c.k[1], shape.c.k[2]);
if (colorMap[hex]) {
const [r, g, b] = colorMap[hex];
shape.c.k = [r, g, b, 1];
}
}
if (shape.it) walkShapes(shape.it);
}
}
for (const layer of data.layers || []) {
if (layer.shapes) walkShapes(layer.shapes);
}
return data;
}
function rgbToHex(r: number, g: number, b: number): string {
return "#" + [r, g, b].map((v) => Math.round(v * 255).toString(16).padStart(2, "0")).join("");
}
```
Related in Web Dev
generating-lwc-components
IncludedLightning Web Components with PICKLES methodology and 165-point scoring. Use this skill when the user creates or edits LWC components, builds wire service patterns, or writes Jest tests for LWC. TRIGGER when: user creates/edits LWC components, touches lwc/**/*.js, .html, .css, .js-meta.xml files, or asks about wire service, SLDS, or Jest LWC tests. DO NOT TRIGGER when: Apex classes (use generating-apex), Aura components, or Visualforce.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Set up queries with useQuery, mutations with useMutation, configure QueryClient caching strategies, implement optimistic updates, and handle infinite scroll with useInfiniteQuery. Use when: setting up data fetching in React projects, migrating from v4 to v5, or fixing object syntax required errors, query callbacks removed issues, cacheTime renamed to gcTime, isPending vs isLoading confusion, keepPreviousData removed problems.
document-processor-api
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
nutrient-document-processing
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Covers useMutationState, simplified optimistic updates, throwOnError, network mode (offline/PWA), and infiniteQueryOptions. Use when setting up data fetching, fixing v4→v5 migration errors (object syntax, gcTime, isPending, keepPreviousData), or debugging SSR/hydration issues with streaming server components.
accelint-nextjs-best-practices
IncludedNext.js performance optimization and best practices. Use when writing Next.js code (App Router or Pages Router); implementing Server Components, Server Actions, or API routes; optimizing RSC serialization, data fetching, or server-side rendering; reviewing Next.js code for performance issues; fixing authentication in Server Actions; or implementing Suspense boundaries, parallel data fetching, or request deduplication.