3b1b-manim/camera/moving_camera.py

41 lines
1.2 KiB
Python
Raw Normal View History

2018-03-31 15:20:30 -07:00
from __future__ import absolute_import
2018-04-09 23:32:57 -07:00
from constants import FRAME_HEIGHT
2018-03-31 15:20:30 -07:00
from camera.camera import Camera
2018-04-09 23:32:57 -07:00
from mobject.frame import ScreenRectangle
2018-03-31 15:20:30 -07:00
2018-03-31 15:20:30 -07:00
class MovingCamera(Camera):
"""
2018-05-16 21:17:14 +02:00
Stays in line with the height, width and position
of a given mobject
2018-03-31 15:20:30 -07:00
"""
CONFIG = {
2018-05-16 21:17:14 +02:00
"aligned_dimension": "width" # or height
2018-03-31 15:20:30 -07:00
}
2018-04-09 23:32:57 -07:00
def __init__(self, frame=None, **kwargs):
2018-03-31 15:20:30 -07:00
"""
2018-05-16 21:17:14 +02:00
frame is a Mobject, (should be a rectangle) determining
which region of space the camera displys
2018-03-31 15:20:30 -07:00
"""
2018-04-09 23:32:57 -07:00
if frame is None:
frame = ScreenRectangle(height=FRAME_HEIGHT)
2018-05-16 21:17:14 +02:00
frame.fade(1)
2018-03-31 15:20:30 -07:00
self.frame = frame
Camera.__init__(self, **kwargs)
2018-05-16 21:17:14 +02:00
def capture_mobjects(self, *args, **kwargs):
self.space_center = self.frame.get_center()
self.realign_frame_shape()
Camera.capture_mobjects(self, *args, **kwargs)
def realign_frame_shape(self):
height, width = self.frame_shape
if self.aligned_dimension == "height":
self.frame_shape = (self.frame.get_height(), width)
else:
self.frame_shape = (height, self.frame.get_width())
self.resize_frame_shape(0 if self.aligned_dimension == "height" else 1)