본문 바로가기

Programming/Manim Lectures

[04-6] Move to relative position(shift)

반응형

In the previous page, we looked at next_to, a method to move left/right/up/down based on an target object or position.

In this chapter, we will learn about shift, a method that moves left/right/up/down from the position of object itself.

 

Since we've only talked left/right/up/down, the next_to or shift method can be mistaken for only moving in these four directions, but can be moved in any direction of 360 degrees. However, this is only expressed because the movement in these four directions is used the most.

shift

manimlib.mobject.Mobject.shift(*vector)

Move the object in the direction given as an argument. As a factor, multiple directions can be given, and it moves in the final direction when moving in the directions given as factors.

At this time, it does not move stepwise for the direction given as a factor, but moves directly to the final direction.

 

For example, for self.play(dot.shift, LEFT, RIGHT, UP), the dot does not move stepwise to the 'left -> right -> up', but moves directly to the UP direction.

 

 

Parameters: *vector
	Direction vectors. 
	Multiple direction values can be given and it moves to the final direction considering all directions
   
Returns:
	The instance of the object moved to the target position
  

 

Whereas 'next_to' moves around another object, 'shift' moves in the given direction relative to the current position of the object.

When moving, the direction is determined using LEFT/RIGHT/UP/DOWN specified as a constant, and the size to move is multiplied by the value.

For example, if you move 1.5 in the right direction, the code will be 'obj.shift(RIGHT * 1.5)', and if you move up 0.5, 'obj.shift(UP * 0.5)'.

 

If you want to move 2 units to the right and 2 units up from the current position, you can say 'obj.shift(RIGHT*2, UP*2)'. At this time, it does not have a step that goes to the right and then goes up, but internally calculates the final direction and moves directly to the final destination.

 

The code below demonstrates the functionality of the shift method described above.

 

class ShiftTest(Scene):
    def construct(self):
        self.move_around_with_one_object()
        self.make_lattice_fully()
        self.make_lattice_fully()

    def move_around_with_one_object(self):
        dot = Dot()
        directions = [RIGHT, UP, LEFT, LEFT, DOWN, DOWN, RIGHT, RIGHT]

        self.add(dot)
        for d in directions:
            self.play(dot.shift, d)
        self.wait()
        self.remove(dot)

    def make_latttice(self):
        dot = Dot()
        directions = [RIGHT, UP, LEFT, LEFT, DOWN, DOWN, RIGHT, RIGHT]

        dots = VGroup()  #to remove dots
        self.add(dot)
        for d in directions:
            dot = dot.copy()
            self.play(dot.shift, d)
            dots.add(dot)
        self.wait(2)
        self.remove(dots)

    def make_lattice_fully(self):
        dot = Dot()
        dot.move_to(np.array([7, 5, 0]))
        self.add(dot)
        for y in range(4, -5, -1):
            dot = dot.copy()
            self.play(dot.shift, DOWN, LEFT * 14, run_time=0.1)
            for x in range(-7, 7):
                dot = dot.copy()
                self.play(dot.shift, RIGHT, run_time=0.05)
        self.wait()

 

 


Next: [04-7] Examples: Object moving animation

 

[04-7] Examples: Object moving animation

Let's make a simple animation showing the movement of a shape using to_edge and to_corner. The first screen is a dot on the left side, and this dot flies up/center/bottom of the wall o..

infograph.tistory.com

Go To: [99] Table of Contents

반응형