3b1b-manim/manim_docs/modules/animation.md

173 lines
4.8 KiB
Markdown
Raw Normal View History

2025-04-06 00:05:12 +05:30
# Animation Module README
## Overview
The animation module is the core of Manim's animation system. It provides classes and utilities for creating and managing animations of mobjects (mathematical objects).
## Key Components
### 1. Base Animation Class (`animation.py`)
- **Purpose**: Base class for all animations in Manim
- **Key Features**:
- Manages animation timing and interpolation
- Handles mobject updates during animation
- Provides core animation functionality
- **Usage**:
```python
class CustomAnimation(Animation):
def interpolate(self, alpha):
# Custom interpolation logic
pass
```
### 2. Transform Animations (`transform.py`)
- **Purpose**: Handles transformations between mobjects
- **Key Classes**:
- `Transform`: Base transformation class
- `ReplacementTransform`: Replaces one mobject with another
- `TransformFromCopy`: Creates a copy for transformation
- `MoveToTarget`: Moves mobject to its target position
- **Usage**:
```python
scene.play(Transform(circle, square))
scene.play(ReplacementTransform(text1, text2))
```
### 3. Creation Animations (`creation.py`)
- **Purpose**: Animates the creation of mobjects
- **Key Classes**:
- `ShowCreation`: Draws mobject from start to end
- `Write`: Writes text or equations
- `DrawBorderThenFill`: Draws border then fills
- **Usage**:
```python
scene.play(ShowCreation(circle))
scene.play(Write(text))
```
### 4. Fading Animations (`fading.py`)
- **Purpose**: Handles fade in/out animations
- **Key Classes**:
- `FadeIn`: Fades in a mobject
- `FadeOut`: Fades out a mobject
- `FadeInFrom`: Fades in from a specific direction
- **Usage**:
```python
scene.play(FadeIn(circle))
scene.play(FadeOut(text))
```
### 5. Movement Animations (`movement.py`)
- **Purpose**: Handles movement and rotation animations
- **Key Classes**:
- `MoveAlongPath`: Moves along a path
- `Rotate`: Rotates mobject
- `ApplyMethod`: Applies a method with animation
- **Usage**:
```python
scene.play(MoveAlongPath(dot, path))
scene.play(Rotate(circle, PI/2))
```
### 6. Composition Animations (`composition.py`)
- **Purpose**: Combines multiple animations
- **Key Classes**:
- `AnimationGroup`: Plays animations simultaneously
- `Succession`: Plays animations in sequence
- `LaggedStart`: Starts animations with delay
- **Usage**:
```python
scene.play(AnimationGroup(
FadeIn(circle),
Write(text),
lag_ratio=0.5
))
```
### 7. Indication Animations (`indication.py`)
- **Purpose**: Highlights and draws attention to mobjects
- **Key Classes**:
- `Flash`: Creates a flash effect
- `Indicate`: Highlights a mobject
- `ShowPassingFlash`: Shows a passing flash
- **Usage**:
```python
scene.play(Flash(circle))
scene.play(Indicate(text))
```
### 8. Update Animations (`update.py`)
- **Purpose**: Updates mobjects based on functions
- **Key Classes**:
- `UpdateFromFunc`: Updates based on function
- `UpdateFromAlphaFunc`: Updates based on alpha
- **Usage**:
```python
def update_func(mob):
mob.rotate(0.1)
scene.play(UpdateFromFunc(circle, update_func))
```
## Interaction with Other Modules
1. **Mobject Module**:
- Animations operate on mobjects
- Uses mobject properties and methods for transformations
- Interacts with mobject families and groups
2. **Scene Module**:
- Scenes play and manage animations
- Handles timing and synchronization
- Manages animation queues and playback
3. **Camera Module**:
- Animations can affect camera position
- Camera movements are handled as animations
- 3D animations interact with camera orientation
## Best Practices
1. **Animation Timing**:
- Use appropriate `run_time` for smooth animations
- Consider `lag_ratio` for sequential animations
- Use `rate_func` for custom timing curves
2. **Performance**:
- Group similar animations together
- Use `AnimationGroup` for parallel animations
- Consider using `LaggedStart` for complex sequences
3. **Code Organization**:
- Create custom animations for reusable effects
- Use composition for complex animations
- Keep animations focused and modular
## Example Usage
```python
class ComplexAnimation(Scene):
def construct(self):
# Create mobjects
circle = Circle()
square = Square()
text = Text("Example")
# Basic animations
self.play(ShowCreation(circle))
self.wait()
# Transform animation
self.play(Transform(circle, square))
self.wait()
# Composition of animations
self.play(AnimationGroup(
FadeIn(text),
Indicate(square),
lag_ratio=0.5
))
# Custom animation
def update_func(mob, alpha):
mob.rotate(alpha * TAU)
self.play(UpdateFromAlphaFunc(square, update_func))
```