Skip to content

Commit 584d802

Browse files
byter11behacklpre-commit-ci[bot]
authored
Allow using :meth:.MovingCamera.auto_zoom without animation (#2693)
* Allow using `MovingCamera.auto_zoom` without animation * added test for auto_zoom width * type hints for MovingCamera.auto_zoom * black * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: Benjamin Hackl <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a34d54d commit 584d802

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

manim/camera/moving_camera.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from ..camera.camera import Camera
1515
from ..constants import DOWN, LEFT, ORIGIN, RIGHT, UP
1616
from ..mobject.frame import ScreenRectangle
17+
from ..mobject.mobject import Mobject
1718
from ..mobject.types.vectorized_mobject import VGroup
1819
from ..utils.color import WHITE
1920

@@ -175,7 +176,13 @@ def get_mobjects_indicating_movement(self):
175176
"""
176177
return [self.frame]
177178

178-
def auto_zoom(self, mobjects, margin=0, only_mobjects_in_frame=False):
179+
def auto_zoom(
180+
self,
181+
mobjects: list[Mobject],
182+
margin: float = 0,
183+
only_mobjects_in_frame: bool = False,
184+
animate: bool = True,
185+
):
179186
"""Zooms on to a given array of mobjects (or a singular mobject)
180187
and automatically resizes to frame all the mobjects.
181188
@@ -195,11 +202,14 @@ def auto_zoom(self, mobjects, margin=0, only_mobjects_in_frame=False):
195202
only_mobjects_in_frame
196203
If set to ``True``, only allows focusing on mobjects that are already in frame.
197204
205+
animate
206+
If set to ``False``, applies the changes instead of returning the corresponding animation
207+
198208
Returns
199209
-------
200-
_AnimationBuilder
201-
Returns an animation that zooms the camera view to a given
202-
list of mobjects.
210+
Union[_AnimationBuilder, ScreenRectangle]
211+
_AnimationBuilder that zooms the camera view to a given list of mobjects
212+
or ScreenRectangle with position and size updated to zoomed position.
203213
204214
"""
205215
scene_critical_x_left = None
@@ -242,8 +252,9 @@ def auto_zoom(self, mobjects, margin=0, only_mobjects_in_frame=False):
242252
new_width = abs(scene_critical_x_left - scene_critical_x_right)
243253
new_height = abs(scene_critical_y_up - scene_critical_y_down)
244254

255+
m_target = self.frame.animate if animate else self.frame
245256
# zoom to fit all mobjects along the side that has the largest size
246257
if new_width / self.frame.width > new_height / self.frame.height:
247-
return self.frame.animate.set_x(x).set_y(y).set(width=new_width + margin)
258+
return m_target.set_x(x).set_y(y).set(width=new_width + margin)
248259
else:
249-
return self.frame.animate.set_x(x).set_y(y).set(height=new_height + margin)
260+
return m_target.set_x(x).set_y(y).set(height=new_height + margin)

tests/test_camera.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from __future__ import annotations
2+
3+
from manim import MovingCamera, Square
4+
5+
6+
def test_movingcamera_auto_zoom():
7+
camera = MovingCamera()
8+
square = Square()
9+
margin = 0.5
10+
camera.auto_zoom([square], margin=margin, animate=False)
11+
assert camera.frame.height == square.height + margin

0 commit comments

Comments
 (0)