Lottie-Animationen
Übersicht
Lottie ist eine Bibliothek zur Darstellung von After Effects-Animationen in Echtzeit auf Web, iOS, Android und React Native. Entwickelt von Airbnb, ermöglicht es Designern, Animationen so einfach auszuliefern wie statische Assets. Animationen werden aus After Effects als JSON-Dateien mit dem Bodymovin-Plugin exportiert und dann nativ mit minimalem Performance-Overhead gerendert.
Wann Lottie verwenden:
- Designerstellte Animationen, die pixelgenaue Wiedergabetreue benötigen
- Komplexe animierte Icons und Mikrointeraktionen
- Lade-Animationen und Fortschrittsanzeigen
- Onboarding-Sequenzen und Tutorial-Animationen
- Marketing-Animationen und Werbeinhalte
- Alternative zu GIF/Video mit kleineren Dateigrößen und Skalierbarkeit
Wichtige Vorteile:
- Vektorbasiert (skalierbar ohne Qualitätsverlust)
- Deutlich kleinere Dateigrößen als GIF oder Video
- Zur Laufzeit bearbeitbar (Farben, Geschwindigkeit, Segmente)
- Volle Designerkontrolle über After Effects
- Plattformübergreifende Darstellungskonsistenz
- Interaktive Steuerelemente (Abspielen, Pause, Suchen, Loopen)
Kernkonzepte
Lottie-Formattypen
1. JSON Lottie (.json)
- Ursprüngliches Lottie-Format
- Exportiert aus After Effects über das Bodymovin-Plugin
- Für Menschen lesbare JSON-Struktur
- Größere Dateigrößen (unkomprimiert)
- Breit unterstützt auf allen Plattformen
2. dotLottie (.lottie)
- Modernes komprimiertes Format
- ZIP-Archiv mit JSON + Assets
- Unterstützt mehrere Animationen und Themes in einer Datei
- Kleinere Dateigrößen (bis zu 90% Reduktion)
- Für die Produktion empfohlen
Bibliotheksoptionen
lottie-web (ursprüngliche Bibliothek):
import lottie from 'lottie-web';
lottie.loadAnimation({
container: document.getElementById('lottie-container'),
renderer: 'svg', // oder 'canvas', 'html'
loop: true,
autoplay: true,
path: 'animation.json' // oder animationData: jsonData
});
@lottiefiles/dotlottie-web (modern, empfohlen):
import { DotLottie } from '@lottiefiles/dotlottie-web';
new DotLottie({
canvas: document.getElementById('canvas'),
src: 'animation.lottie',
autoplay: true,
loop: true
});
@lottiefiles/dotlottie-react (React-Integration):
import { DotLottieReact } from '@lottiefiles/dotlottie-react';
<DotLottieReact
src="animation.lottie"
loop
autoplay
style={{ height: 300 }}
/>
lottie-react (alternative React-Wrapper):
import Lottie from 'lottie-react';
import animationData from './animation.json';
<Lottie animationData={animationData} loop={true} />
Animations-Datenquellen
1. LottieFiles (lottie.host)
- Über 100.000 kostenlose Animationen
- Direkte URL-Einbindung
- CDN-Hosting
2. Lokale JSON/dotLottie-Dateien
- Mit der Anwendung gebündelt
- Bessere Performance (keine Netzwerkanfrage)
- Versionsverwaltungsfreundlich
3. After Effects-Export
- Benutzerdefinierte Designer-Animationen
- Bodymovin-Plugin erforderlich
- Exporteinstellungen entscheidend für die Dateigröße
Häufige Muster
1. Einfache HTML-Integration mit dotLottie-web
<!DOCTYPE html>
<html>
<head>
<style>
#canvas {
width: 400px;
height: 400px;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script type="module">
import { DotLottie } from 'https://cdn.jsdelivr.net/npm/@lottiefiles/dotlottie-web/+esm';
new DotLottie({
canvas: document.getElementById('canvas'),
src: 'https://lottie.host/4db68bbd-31f6-4cd8-84eb-189de081159a/IGmMCqhzpt.lottie',
autoplay: true,
loop: true
});
</script>
</body>
</html>
2. React-Komponente mit Steuerelementen
import React from 'react';
import { DotLottieReact } from '@lottiefiles/dotlottie-react';
const AnimatedButton = () => {
const [dotLottie, setDotLottie] = React.useState(null);
const handlePlay = () => dotLottie?.play();
const handlePause = () => dotLottie?.pause();
const handleStop = () => dotLottie?.stop();
const handleSeek = (frame) => dotLottie?.setFrame(frame);
return (
<div>
<DotLottieReact
src="button-animation.lottie"
loop
autoplay={false}
dotLottieRefCallback={setDotLottie}
style={{ height: 200 }}
/>
<div>
<button onClick={handlePlay}>Play</button>
<button onClick={handlePause}>Pause</button>
<button onClick={handleStop}>Stop</button>
<button onClick={() => handleSeek(30)}>Seek to frame 30</button>
</div>
</div>
);
};
3. Ereignis-Listener und Lifecycle-Hooks
import React, { useEffect } from 'react';
import { DotLottieReact } from '@lottiefiles/dotlottie-react';
const EventDrivenAnimation = () => {
const [dotLottie, setDotLottie] = React.useState(null);
useEffect(() => {
if (!dotLottie) return;
const onLoad = () => console.log('Animation loaded');
const onPlay = () => console.log('Animation started');
const onPause = () => console.log('Animation paused');
const onComplete = () => console.log('Animation completed');
const onFrame = ({ currentFrame }) => console.log('Frame:', currentFrame);
dotLottie.addEventListener('load', onLoad);
dotLottie.addEventListener('play', onPlay);
dotLottie.addEventListener('pause', onPause);
dotLottie.addEventListener('complete', onComplete);
dotLottie.addEventListener('frame', onFrame);
return () => {
dotLottie.removeEventListener('load', onLoad);
dotLottie.removeEventListener('play', onPlay);
dotLottie.removeEventListener('pause', onPause);
dotLottie.removeEventListener('complete', onComplete);
dotLottie.removeEventListener('frame', onFrame);
};
}, [dotLottie]);
return (
<DotLottieReact
src="animation.lottie"
loop
autoplay
dotLottieRefCallback={setDotLottie}
/>
);
};
4. Scrollgesteuerte Animation mit lottie-react
import Lottie from 'lottie-react';
import robotAnimation from './robot.json';
const ScrollAnimation = () => {
const interactivity = {
mode: 'scroll',
actions: [
{
visibility: [0, 0.2],
type: 'stop',
frames: [0]
},
{
visibility: [0.2, 0.45],
type: 'seek',
frames: [0, 45]
},
{
visibility: [0.45, 1.0],
type: 'loop',
frames: [45, 60]
}
]
};
return (
<Lottie
animationData={robotAnimation}
style={{ height: 300 }}
interactivity={interactivity}
/>
);
};
5. Hover-gesteuerte Segment-Wiedergabe
import { useLottie, useLottieInteractivity } from 'lottie-react';
import likeButton from './like-button.json';
const HoverAnimation = () => {
const lottieObj = useLottie({
animationData: likeButton
});
const Animation = useLottieInteractivity({
lottieObj,
mode: 'cursor',
actions: [
{
position: { x: [0, 1], y: [0, 1] },
type: 'loop',
frames: [45, 60]
},
{
position: { x: -1, y: -1 },
type: 'stop',
frames: [45]
}
]
});
return <div style={{ height: 300, border: '2px solid black' }}>{Animation}</div>;
};
6. Multi-Animation und Theme-Unterstützung
import { DotLottieReact } from '@lottiefiles/dotlottie-react';
import React, { useState, useEffect } from 'react';
const ThemedAnimation = () => {
const [dotLottie, setDotLottie] = useState(null);
const [animations, setAnimations] = useState([]);
const [themes, setThemes] = useState([]);
const [currentAnimationId, setCurrentAnimationId] = useState('');
const [currentThemeId, setCurrentThemeId] = useState('');
useEffect(() => {
if (!dotLottie) return;
const onLoad = () => {
setAnimations(dotLottie.manifest.animations || []);
setThemes(dotLottie.manifest.themes || []);
setCurrentAnimationId(dotLottie.activeAnimationId);
setCurrentThemeId(dotLottie.activeThemeId);
};
dotLottie.addEventListener('load', onLoad);
return () => dotLottie.removeEventListener('load', onLoad);
}, [dotLottie]);
return (
<div>
<DotLottieReact
src="multi-animation.lottie"
dotLottieRefCallback={setDotLottie}
animationId={currentAnimationId}
themeId={currentThemeId}
/>
{themes.length > 0 && (
<select value={currentThemeId} onChange={(e) => setCurrentThemeId(e.target.value)}>
{themes.map((theme) => (
<option key={theme.id} value={theme.id}>{theme.id}</option>
))}
</select>
)}
{animations.length > 0 && (
<select value={currentAnimationId} onChange={(e) => setCurrentAnimationId(e.target.value)}>
{animations.map((anim) => (
<option key={anim.id} value={anim.id}>{anim.id}</option>
))}
</select>
)}
</div>
);
};
7. Web Worker für Performance (DotLottieWorker)
import { DotLottieWorker } from '@lottiefiles/dotlottie-web';
// Auslagerung der Animationsdarstellung an einen Web Worker
new DotLottieWorker({
canvas: document.getElementById('canvas'),
src: 'heavy-animation.lottie',
autoplay: true,
loop: true,
workerId: 'worker-1' // Mehrere Animationen nach Worker gruppieren
});
// Mehrere Animationen in separaten Workern
new DotLottieWorker({
canvas: document.getElementById('canvas-2'),
src: 'animation-2.lottie',
autoplay: true,
loop: true,
workerId: 'worker-2'
});
Integrationsmuster
Mit GSAP ScrollTrigger
import Lottie from 'lottie-react';
import gsap from 'gsap';
import ScrollTrigger from 'gsap/ScrollTrigger';
import animationData from './animation.json';
gsap.registerPlugin(ScrollTrigger);
const GSAPLottieIntegration = () => {
const lottieRef = React.useRef();
React.useEffect(() => {
const anim = lottieRef.current;
if (!anim) return;
// Lottie mit Scroll synchronisieren
gsap.to(anim, {
scrollTrigger: {
trigger: '#animation-section',
start: 'top center',
end: 'bottom center',
scrub: 1,
onUpdate: (self) => {
const frame = Math.floor(self.progress * (anim.totalFrames - 1));
anim.goToAndStop(frame, true);
}
}
});
}, []);
return (
<div id="animation-section" style={{ height: '200vh' }}>
<Lottie
lottieRef={lottieRef}
animationData={animationData}
autoplay={false}
loop={false}
/>
</div>
);
};
Mit Framer Motion
import { motion } from 'framer-motion';
import { DotLottieReact } from '@lottiefiles/dotlottie-react';
const MotionLottie = () => {
return (
<motion.div
initial={{ opacity: 0, scale: 0.8 }}
animate={{ opacity: 1, scale: 1 }}
transition={{ duration: 0.6 }}
>
<DotLottieReact
src="animation.lottie"
loop
autoplay
style={{ height: 400 }}
/>
</motion.div>
);
};
Vue 3-Integration
<script setup>
import { DotLottieVue } from '@lottiefiles/dotlottie-vue';
</script>
<template>
<DotLottieVue
style="height: 500px; width: 500px"
autoplay
loop
src="https://path-to-animation.lottie"
/>
</template>
Svelte-Integration
<script lang="ts">
import { DotLottieSvelte } from '@lottiefiles/dotlottie-svelte';
import type { DotLottie } from '@lottiefiles/dotlottie-svelte';
let dotLottie: DotLottie | null = null;
function play() {
dotLottie?.play();
}
</script>
<DotLottieSvelte
src="animation.lottie"
loop={true}
autoplay={true}
dotLottieRefCallback={(ref) => dotLottie = ref}
/>
<button on:click={play}>Play</button>
Leistungsoptimierung
Dateigrößenoptimierung
1. Exporteinstellungen in After Effects:
- "Nicht verwendete Bilder überspringen" aktivieren
- Nach Möglichkeit "Glyphen" anstelle von Schriftarten verwenden
- Pfade vereinfachen (Punkte in Illustrator reduzieren)
- Effekte vermeiden, die große Datenmengen erzeugen (Partikel, Rauschen)
- Formebenen anstelle von Vektorebenen verwenden
2. Komprimierung:
- dotLottie-Format (.lottie) für automatische Komprimierung verwenden
- JSON mit Lottie-Optimierungswerkzeugen durchlaufen
- Nicht benötigte Metadaten entfernen
3. Lazy Loading:
const LazyLottie = () => {
const [shouldLoad, setShouldLoad] = React.useState(false);
React.useEffect(() => {
const observer = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting) {
setShouldLoad(true);
}
});
observer.observe(document.getElementById('lottie-trigger'));
return () => observer.disconnect();
}, []);
return (
<div id="lottie-trigger">
{shouldLoad && <DotLottieReact src="animation.lottie" loop autoplay />}
</div>
);
};
Laufzeit-Performance
1. Renderer-Auswahl:
// SVG: Beste Qualität, langsamer bei komplexen Animationen
// Canvas: Bessere Performance, gerastert
// HTML: Eingeschränkte Unterstützung, nur für einfache Animationen
// Für komplexe Animationen Canvas bevorzugen
new DotLottie({
canvas: document.getElementById('canvas'),
src: 'animation.lottie',
autoplay: true,
loop: true,
renderConfig: {
devicePixelRatio: window.devicePixelRatio || 1
}
});
2. Web Worker:
// Für schwere Animationen an Worker auslagern
import { DotLottieWorker } from '@lottiefiles/dotlottie-web';
new DotLottieWorker({
canvas: document.getElementById('canvas'),
src: 'heavy-animation.lottie',
autoplay: true,
loop: true
});
3. Mobile Optimierung:
// Qualität auf Mobilgeräten reduzieren
const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
new DotLottie({
canvas: document.getElementById('canvas'),
src: isMobile ? 'animation-low.lottie' : 'animation-high.lottie',
autoplay: true,
loop: true,
renderConfig: {
devicePixelRatio: isMobile ? 1 : window.devicePixelRatio
}
});
Häufige Fallstricke
1. Speicherlecks durch unsachgemäße Bereinigung
Problem: Lottie-Instanzen werden beim Unmounten von Komponenten nicht zerstört.
Lösung:
const SafeAnimation = () => {
const [dotLottie, setDotLottie] = React.useState(null);
React.useEffect(() => {
return () => {
if (dotLottie) {
dotLottie.destroy();
}
};
}, [dotLottie]);
return (
<DotLottieReact
src="animation.lottie"
dotLottieRefCallback={setDotLottie}
/>
);
};
2. Zu viele gleichzeitige Animationen
Problem: Mehrere Lottie-Instanzen laufen gleichzeitig und verursachen Ruckeln.
Lösung:
// Gemeinsame Animationsschleife verwenden oder Animationen pausieren, wenn sie nicht sichtbar sind
const VisibilityManager = () => {
const [isVisible, setIsVisible] = React.useState(true);
React.useEffect(() => {
const handleVisibility = () => setIsVisible(!document.hidden);
document.addEventListener('visibilitychange', handleVisibility);
return () => document.removeEventListener('visibilitychange', handleVisibility);
}, []);
return isVisible ? <ExpensiveAnimation /> : null;
};
3. Falsche Pfade und relative URLs
Problem: Animationen werden nicht geladen, weil der Pfad zur JSON/Lottie-Datei falsch ist.
Lösung:
- Verwende absolute URLs für öffentlich gehostete Assets
- Verwende Importe für gebündelte Assets
- Überprüfe die Netzwerk-Registerkarte auf 404-Fehler
4. Ignorieren von dotLottieRefCallback
Problem: Zugriff auf die Lottie-Instanz ohne den richtigen Callback.
Lösung:
// Falsch: kein Zugriff auf die Instanz
<DotLottieReact src="animation.lottie" />
// Richtig: Instanz über Callback erfassen
<DotLottieReact
src="animation.lottie"
dotLottieRefCallback={(instance) => {
// Instanz speichern für spätere Steuerung
window.myLottie = instance;
}}
/>
5. Vernachlässigung der Barrierefreiheit
Problem: Animationen können für einige Benutzer ablenkend oder problematisch sein.
Lösung:
- Respektiere die Benutzereinstellung
prefers-reduced-motion - Biete eine Möglichkeit, Animationen zu pausieren oder zu stoppen
- Verwende
aria-labelfür interaktive Animationen
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (prefersReducedMotion) {
// Nur die erste oder letzte Frame anzeigen, nicht abspielen
new DotLottie({
canvas: document.getElementById('canvas'),
src: 'animation.lottie',
autoplay: false,
loop: false
});
}