| name | camera-techniques |
| description | Camera manipulation including zoom, pan, save/restore state, line width compensation, and focus transitions. |
Camera Techniques
Cinematic camera techniques for Manim animations.
MovingCameraScene (2D)
Basic Setup
from manim import *
class MyScene(MovingCameraScene):
def construct(self):
self.camera.frame.save_state()
Camera Properties
self.camera.frame.width
self.camera.frame.height
self.camera.frame.get_center()
self.camera.frame.set(width=10)
self.camera.frame.set(height=6)
Zoom Operations
Zoom In (Make Larger)
self.play(
self.camera.frame.animate.scale(0.5),
run_time=2
)
self.play(
self.camera.frame.animate.set(height=4),
run_time=2
)
Zoom Out (Show More)
self.play(
self.camera.frame.animate.scale(2),
run_time=2
)
Zoom to Object
target = some_mobject
self.play(
self.camera.frame.animate
.set(height=target.height * 1.5)
.move_to(target),
run_time=2
)
Zoom Levels
| Scale Factor | Effective Zoom | Use Case |
|---|
| 0.25 | 4x zoom in | Fine detail |
| 0.5 | 2x zoom in | Detail view |
| 1.0 | Normal | Overview |
| 2.0 | 0.5x zoom out | Wide shot |
Pan Operations
Pan to Object
self.play(
self.camera.frame.animate.move_to(target),
run_time=1.5
)
Pan to Coordinates
self.play(
self.camera.frame.animate.move_to([3, 2, 0]),
run_time=1.5
)
Pan with Offset
self.play(
self.camera.frame.animate.move_to(target).shift(UP * 0.5),
run_time=1.5
)
Combined Operations
Zoom + Pan
self.play(
self.camera.frame.animate
.scale(0.3)
.move_to(detail_element),
run_time=2
)
Smooth Multi-Step
self.play(
self.camera.frame.animate.move_to(area),
run_time=1
)
self.play(
self.camera.frame.animate.scale(0.5),
run_time=1.5
)
Save/Restore Pattern
Basic Usage
class MyScene(MovingCameraScene):
def construct(self):
main_content = self.create_content()
self.camera.frame.save_state()
self.wait(2)
self.play(
self.camera.frame.animate
.set(height=detail.height * 2)
.move_to(detail),
run_time=2
)
self.wait(3)
self.play(Restore(self.camera.frame), run_time=2)
Multiple Saves
self.camera.frame.save_state()
self.play(self.camera.frame.animate.scale(0.5).move_to(area1))
state_area1 = self.camera.frame.copy()
self.play(Restore(self.camera.frame))
self.play(self.camera.frame.animate.become(state_area1))
Line Width Management
The Problem
When zooming, line widths don't scale, causing:
- Zoomed in: Lines appear thicker
- Zoomed out: Lines appear thinner
The Solution
class ConsistentLinesScene(MovingCameraScene):
def construct(self):
INITIAL_LINE_WIDTH = self.camera.cairo_line_width_multiple
INITIAL_FRAME_WIDTH = config.frame_width
def maintain_line_width(m):
proportion = self.camera.frame.width / INITIAL_FRAME_WIDTH
self.camera.cairo_line_width_multiple = INITIAL_LINE_WIDTH * proportion
line_manager = Mobject()
line_manager.add_updater(maintain_line_width)
self.add(line_manager)
When to Use
- Complex diagrams with many lines
- Significant zoom changes (>2x)
- When visual consistency matters
Focus Transitions
Sequential Focus Tour
elements = [element_a, element_b, element_c]
self.camera.frame.save_state()
for element in elements:
self.play(
self.camera.frame.animate
.set(height=element.height * 2)
.move_to(element),
run_time=1.5
)
self.wait(2)
self.play(Restore(self.camera.frame), run_time=1.5)
Tour with Varying Zoom
tour_stops = [
(element_a, 1.5, 2.0),
(element_b, 2.0, 1.5),
(element_c, 1.2, 3.0),
]
self.camera.frame.save_state()
for element, height_factor, wait in tour_stops:
self.play(
self.camera.frame.animate
.set(height=element.height * height_factor)
.move_to(element),
run_time=1.5
)
self.wait(wait)
self.play(Restore(self.camera.frame), run_time=2)
ThreeDScene Camera
Setup
class My3DScene(ThreeDScene):
def construct(self):
self.set_camera_orientation(
phi=75 * DEGREES,
theta=-45 * DEGREES
)
Camera Rotation
self.move_camera(
phi=60 * DEGREES,
theta=-60 * DEGREES,
run_time=2
)
self.begin_ambient_camera_rotation(rate=0.1)
self.wait(5)
self.stop_ambient_camera_rotation()
3D Zoom
self.set_camera_orientation(zoom=0.5)
self.set_camera_orientation(zoom=2)
Timing Guidelines
| Movement | Duration | Notes |
|---|
| Quick pan | 0.5-1.0s | Minor adjustment |
| Standard pan | 1.5s | Normal movement |
| Dramatic zoom in | 2.0-3.0s | Building focus |
| Slow reveal zoom out | 3.0-4.0s | Grand reveal |
| Restore overview | 2.0s | Return home |
Rate Functions for Camera
self.play(
self.camera.frame.animate.move_to(target),
rate_func=smooth
)
self.play(
self.camera.frame.animate.scale(0.3),
rate_func=rush_into
)
self.play(
Restore(self.camera.frame),
rate_func=rush_from
)
self.play(
self.camera.frame.animate.shift(RIGHT * 3),
rate_func=linear
)
Complete Example
from manim import *
class CameraDemo(MovingCameraScene):
def construct(self):
INIT_WIDTH = self.camera.cairo_line_width_multiple
INIT_FRAME = config.frame_width
manager = Mobject()
manager.add_updater(lambda m:
setattr(self.camera, 'cairo_line_width_multiple',
INIT_WIDTH * self.camera.frame.width / INIT_FRAME))
self.add(manager)
circles = VGroup(*[
Circle(radius=0.5).move_to([x, y, 0])
for x in range(-4, 5, 2)
for y in range(-2, 3, 2)
])
self.add(circles)
self.camera.frame.save_state()
self.wait(1)
center = circles[len(circles)//2]
self.play(
self.camera.frame.animate
.set(height=center.height * 3)
.move_to(center),
run_time=2
)
label = Text("Center", font_size=12).next_to(center, UP, buff=0.1)
self.play(Write(label))
self.wait(2)
self.play(
FadeOut(label),
Restore(self.camera.frame),
run_time=2
)
self.wait(1)