How To Zoom In/Out In Manim

The easiest way to zoom into a scene in Manim is to group every object on the scene using a VGroup() and then change the scale of the group.

The second (slightly harder) way to zoom in or out is to use a moving camera scene.

Let’s look at some examples for both of these options:

How to zoom in on Manim by using a group

To zoom in Manim put all of your objects that you want to resize into a VGroup() and then scale that group.

Example:

from manim import *

class zooming(Scene):
    def construct(self):
        c = Circle(radius = 0.5)
        s = Square(side_length = 1)
        t = Triangle().scale(0.5)

        g = VGroup(c, s ,t).arrange()

        self.play(Write(g))
        self.play(g.animate.scale(3).move_to(UL))
        self.play(g.animate.scale(0.2).move_to([5, -3, 0]))
        self.play(g.animate.scale(1/0.6).move_to(ORIGIN))
        self.wait()

Output:

Code explanation:

Line 1: I imported the Manim library into VS Code so that Python understands Manim’s code syntax.

Lines 3 and 4: I created a new Manim scene and named it “zooming”.

Line 5: I created a circle with a radius of 0.5 and assigned it to a new variable I named c.

Line 6: I created a square with a side length of 1 and assigned it to a new variable I named s.

Line 7: I created a triangle, assigned it to a new variable I named t, and scaled it to 0.5 times of its original size.

Line 9: I put the circle, square, and rectangle into a VGroup() and assigned it to a new variable I named g.

Line 11: I animated the group on the screen using the Write() animation type.

Line 12: I scaled the group to 3 times its original size and moved it to the upper left edge of the frame using the move_to() command. When you want to show an object moving on the screen in Manim you have to attach the .animate parameter to the thing you want to animate and put everything inside of the self.play() command.

Line 13: I scaled the group to 0.2 times its size and moved it to coordinates (x = 5; y = -3; z = 0) using the move_to() command. The z coordinate is always 0 when animating in a 2D environment.

Line 14: I scaled the group by 1/0.6 times (~1,666) to scale it back to its original size, since before, I had scaled it up to 3x and then scaled it down by 0.2x, so now the group became 0.6x (1*3*0.2) its original size). I also positioned it in the center of the screen by using the move_to() command and saying that it should move to the ORIGIN (which means the center).

Line 15: I paused the animation on the screen for 1 second

How to zoom in Manim by using a moving camera scene

To zoom into a specific portion of the screen in Manim you can create a MovingCameraScene instead of the regular Scene and then specify where you want the camera to move with the self.camera.frame.animaet.set() command.

Example:

from manim import *

class zoom_in(MovingCameraScene):
    def construct(self):
        t1 = Text("Aa", color = RED, font = "Arial").scale(5).to_edge(UL)
        t2 = Text("Bb", color = RED, font = "Arial").shift(RIGHT*4, DOWN*2)
        t3 = Text("Cc", color = RED, font = "Arial").next_to(t2, RIGHT)

        g = VGroup(t2, t3)

        self.play(Write(t1), Write(t2))
 
        self.camera.frame.save_state() #saving camera state so that we can restore it later
        self.play(self.camera.frame.animate.set(width = t1.width*2).move_to(t1))
        self.wait()
        self.play(Restore(self.camera.frame))
        self.wait()
        self.play(self.camera.frame.animate.set(width = g.width*1.5).move_to(g))
        self.play(Write(t3))
        self.wait()
        self.play(Restore(self.camera.frame))
        self.wait()

Output:

Code explanation:

Line 1: I imported Manim into the Python file.

Lines 3 and 4: I created a new MovingCameraScene named “zoom_in”. For normal animations, you would create a regular Scene but if you want to zoom in you have to create a moving camera scene.

Line 5: I created a red piece of text “Aa” and assigned it to a new variable I named t1. I then scaled this text to 5x its original size with the .scale() command and moved it to the upper left edge of the screen with the .to_edge() command.

Line 6: I created a red piece of text “Bb” and assigned it to a new variable I named t2. I used the .shift() command to move this text on the screen 4 squares to the right and 2 squares down in the Manim coordinate system.

Line 7: I created a red piece of text “Cc” and assigned it to a new variable I named t3. Then, I positioned this text next to the “Bb” text and on the right side of it using the .next_to() command.

Line 9: I grouped the second and third pieces of text so that I could zoom my camera into both of them later.

Line 11: I animated the first two texts on the screen by using the Write() animation type.

Line 13: I saved the current state of the camera so that I can restore it to this position later.

Line 14: I zoomed into the first piece of text and set the zoom amount to be 2 times the width of the text. That’s why the sides of the first text didn’t touch the sides of the screen. I used the .move_to() command to tell the camera which object to zoom to, otherwise, it would have zoomed into the center and the zoom amount would have been 2 times the size of the width of the first text. Of course, I put everything inside of the self.play() since I wanted to see the zoom movement happening on the screen.

Line 15: I paused the animation for 1 second.

Line 16: I restored the saved camera state which made it zoom out to the original size that I saved with the 13th line of code.

Line 17: I paused the animation for 1 second.

Line 18: I zoomed into the group which has the second (“Bb”) and third (“Cc”) pieces of text inside of it, and only the second text is currently visible. I set the zoom amount to be 1.5 times the width of all the objects in the group as well as told the camera to zoom into the group with the .move_to() command.

Line 19: I animated the third text on the screen with the Write() animation type.

Line 20: I paused the animation for 1 second.

Line 21: I restored the original zoom amount from the camera state that I saved in the 13th line of code.

Line 22: I paused the animation for 1 more second.

Let me help you learn Manim

If you want to skip the headache of trying to learn Manim from a bunch of scattered information, I put together a comprehensive 3-hour Manim course for complete beginners.

It will give you all the foundational skills you need to start creating stunning animations with code.

Enroll In Manim Course For Beginners