Communitygithub.com

Lottieアニメーション

After EffectsアニメーションをWebやReactアプリケーションにレンダリングするスキルです。Lottieアニメーション、JSONベクターアニメーション、インタラクティブなアニメーションアイコン、マイクロインタラクション、ローディングアニメーションを実装する際に使用します。

Lottieアニメーション とは?

このスキルは、After Effectsで作成されたアニメーションをLottie形式(JSONまたはdotLottie)でWebアプリケーションに統合するための包括的なガイドです。LottieはAirbnbが開発したライブラリで、デザイナーが作成したアニメーションを最小限のパフォーマンスオーバーヘッドでネイティブレンダリングできます。スキルは、lottie-web、@lottiefiles/dotlottie-web、lottie-react、dotlottie-reactなど主要なライブラリをサポートし、React、Vue 3、Svelte、GSAP ScrollTrigger、Framer Motionとの統合パターンを提供します。さらに、ファイルサイズ最適化、ランタイムパフォーマンス改善、Web Workerを使った重いアニメーションのオフロード、モバイル最適化などの実践的な最適化手法をカバーしています。メモリリーク、レンダリング品質、クロスプラットフォームの一貫性といった一般的な落とし穴とその解決策も解説しています。

対応✓Claude Code~Codex CLI✓Cursor
npx skills add https://github.com/kschiavo-beep/claude-skills/tree/HEAD/.claude/skills/lottie-animations

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

Lottieアニメーション は何をしますか?

概要

Lottieは、After EffectsアニメーションをWeb、iOS、Android、React Native上でリアルタイムにレンダリングするためのライブラリです。Airbnbによって作成され、デザイナーが静的アセットと同じくらい簡単にアニメーションを提供できるようにします。アニメーションはAfter EffectsからBodymovinプラグインを使用してJSONファイルとしてエクスポートされ、最小限のパフォーマンスオーバーヘッドでネイティブにレンダリングされます。

Lottieを使用する場面:

  • デザイナーが作成したピクセルパーフェクトなアニメーション
  • 複雑なアニメーションアイコンとマイクロインタラクション
  • ローディングアニメーションと進捗インジケーター
  • オンボーディングシーケンスとチュートリアルアニメーション
  • マーケティングアニメーションとプロモーションコンテンツ
  • GIF/ビデオの代替(ファイルサイズが小さく、拡張性が高い)

主な利点:

  • ベクターベース(品質を損なわずに拡張可能)
  • GIFやビデオよりもファイルサイズが大幅に小さい
  • 実行時に編集可能(色、速度、セグメント)
  • After Effectsによるデザイナーの完全な制御
  • クロスプラットフォームでのレンダリングの一貫性
  • インタラクティブな制御(再生、一時停止、シーク、ループ)

コアコンセプト

Lottie形式の種類

1. JSON Lottie (.json)

  • 元のLottie形式
  • After EffectsからBodymovinプラグイン経由でエクスポート
  • 人間が読めるJSON構造
  • ファイルサイズが大きい(圧縮なし)
  • すべてのプラットフォームで広くサポート

2. dotLottie (.lottie)

  • 最新の圧縮形式
  • JSON + アセットを含むZIPアーカイブ
  • 1つのファイルで複数のアニメーションとテーマをサポート
  • ファイルサイズが小さい(最大90%削減)
  • プロダクション環境での使用を推奨

ライブラリのオプション

lottie-web(元のライブラリ):

import lottie from 'lottie-web';

lottie.loadAnimation({
  container: document.getElementById('lottie-container'),
  renderer: 'svg', // または 'canvas', 'html'
  loop: true,
  autoplay: true,
  path: 'animation.json' // または animationData: jsonData
});

@lottiefiles/dotlottie-web(最新版、推奨):

import { DotLottie } from '@lottiefiles/dotlottie-web';

new DotLottie({
  canvas: document.getElementById('canvas'),
  src: 'animation.lottie',
  autoplay: true,
  loop: true
});

@lottiefiles/dotlottie-react(React統合):

import { DotLottieReact } from '@lottiefiles/dotlottie-react';

<DotLottieReact
  src="animation.lottie"
  loop
  autoplay
  style={{ height: 300 }}
/>

lottie-react(代替Reactラッパー):

import Lottie from 'lottie-react';
import animationData from './animation.json';

<Lottie animationData={animationData} loop={true} />

アニメーションデータソース

1. LottieFiles (lottie.host)

  • 100,000以上の無料アニメーション
  • 直接URL埋め込み
  • CDNホスティング

2. ローカルJSON/dotLottieファイル

  • アプリケーションにバンドル
  • パフォーマンスが良い(ネットワークリクエストなし)
  • バージョン管理に適している

3. After Effectsエクスポート

  • カスタムデザイナーアニメーション
  • Bodymovinプラグインが必要
  • ファイルサイズに影響するエクスポート設定は重要

一般的なパターン

1. dotLottie-webを使った基本のHTML統合

<!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コンポーネント

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}>再生</button>
        <button onClick={handlePause}>一時停止</button>
        <button onClick={handleStop}>停止</button>
        <button onClick={() => handleSeek(30)}>フレーム30にシーク</button>
      </div>
    </div>
  );
};

3. イベントリスナーとライフサイクルフック

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('アニメーション読み込み完了');
    const onPlay = () => console.log('アニメーション開始');
    const onPause = () => console.log('アニメーション一時停止');
    const onComplete = () => console.log('アニメーション完了');
    const onFrame = ({ currentFrame }) => console.log('フレーム:', 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. 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. ホバーでトリガーされるセグメント再生

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. マルチアニメーションとテーマサポート

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(DotLottieWorker)

import { DotLottieWorker } from '@lottiefiles/dotlottie-web';

// アニメーションレンダリングをWeb Workerにオフロード
new DotLottieWorker({
  canvas: document.getElementById('canvas'),
  src: 'heavy-animation.lottie',
  autoplay: true,
  loop: true,
  workerId: 'worker-1' // 複数のアニメーションをワーカーでグループ化
});

// 別々のワーカーで複数のアニメーション
new DotLottieWorker({
  canvas: document.getElementById('canvas-2'),
  src: 'animation-2.lottie',
  autoplay: true,
  loop: true,
  workerId: 'worker-2'
});

統合パターン

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をスクロールに同期
    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>
  );
};

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統合

<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統合

<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}>再生</button>

パフォーマンス最適化

ファイルサイズ最適化

1. After Effectsのエクスポート設定:

  • 「使用されていない画像をスキップ」を有効にする
  • 可能な場合はフォントの代わりに「グリフ」を使用
  • パスを簡略化(Illustratorのポイントを減らす)
  • 大きなデータを作成するエフェクト(パーティクル、ノイズ)を避ける
  • ベクターレイヤーの代わりにシェイプレイヤーを使用

2. 圧縮:

  • 自動圧縮のためにdotLottie形式(.lottie)を使用
  • Lottie最適化ツールでJSONを実行
  • 不要なメタデータを削除

3. レイジーローディング:

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>
  );
};

ランタイムパフォーマンス

1. レンダラーの選択:

// SVG: 最高品質、複雑なアニメーションでは遅い
// Canvas: パフォーマンスが良い、ラスタライズされる
// HTML: サポートは限定的、シンプルなアニメーションにのみ使用

// 複雑なアニメーションではCanvasを優先
new DotLottie({
  canvas: document.getElementById('canvas'),
  src: 'animation.lottie',
  autoplay: true,
  loop: true,
  renderConfig: {
    devicePixelRatio: window.devicePixelRatio || 1
  }
});

2. Web Workers:

// 重いアニメーションをワーカーにオフロード
import { DotLottieWorker } from '@lottiefiles/dotlottie-web';

new DotLottieWorker({
  canvas: document.getElementById('canvas'),
  src: 'heavy-animation.lottie',
  autoplay: true,
  loop: true
});

3. モバイル最適化:

// モバイルで品質を下げる
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
  }
});

よくある落とし穴

1. 不適切なクリーンアップによるメモリリーク

問題: コンポーネントがアンマウントされたときにLottieインスタンスが破棄されない。

解決策:

const SafeAnimation = () => {
  const [dotLottie, setDotLottie] = React.useState(null);

  React.useEffect(() => {
    return () => {
      if (dotLottie) {
        dotLottie.destroy();
      }
    };
  }, [dotLottie]);

  return (
    <DotLottieReact
      src="animation.lottie"
      loop
      autoplay
      dotLottieRefCallback={setDotLottie}
    />
  );
};

2. パフォーマンスのボトルネック

問題: 複雑なアニメーションでメインスレッドがブロックされ、UIがカクつく。

解決策:

  • 可能な場合はCanvasレンダラーを使用
  • 複雑なアニメーションにはDotLottieWorkerでWeb Workersを活用
  • devicePixelRatioを下げてレンダリング品質を調整
  • アニメーションを複数の小さなセグメントに分割

3. JSONパースエラー

問題: 破損したJSONファイルや互換性のないエクスポート設定。

解決策:

  • LottieFilesの検証ツールでJSONファイルを検証
  • Bodymovinプラグインが最新バージョンであることを確認
  • 可能な場合はJSONの代わりに.lottie形式を使用
  • 開発時にエラーバウンダリでJSONエラーをキャッチ

4. レンダリングの不一致

問題: 異なるプラットフォームやブラウザでアニメーションの見た目が異なる。

解決策:

  • .lottie形式を使用して一貫性を確保
  • After Effectsでアニメーションのベースラインをテスト
  • フォントの代わりにグリフを使用
  • テキストレイヤーを回避(シェイプに変換)
  • レンダリング結果を複数のブラウザでテスト

リファレンス

ドキュメント

ツール

  • LottieFiles - アニメーションの発見とテスト
  • Lottie Editor - アニメーションの編集と最適化
  • Bodymovin - After Effectsエクスポートプラグイン

ベストプラクティス

  1. 常に .lottie 形式を優先: 自動圧縮、マルチアニメーション、テーマサポート
  2. アニメーションを遅延読み込み: IntersectionObserverを使用
  3. 複雑なアニメーションにはCanvasレンダラー: パフォーマンス向上のため
  4. コンポーネントのアンマウント時に破棄: メモリリーク防止
  5. アニメーションをテスト: ターゲットとするすべてのプラットフォームとブラウザで
  6. アニメーションファイルを最適化: パスを簡略化、未使用のアセットを削除
  7. アニメーションの区間化: 長いアニメーションを小さな再生セグメントに分割
  8. モバイルでのパフォーマンス監視: モバイルではアニメーション品質を調整

Individual skills in this repo

This repo contains 2 individual skills — each has its own dedicated page.

関連スキル