본문 바로가기

Programming/Manim Lectures

[04-3] Move to absolute position(to_edge)

반응형

In the previous page, we learned about 'move_to' method, and it was a method that moves object directly to a specified point or another object's position.

 

The 'to_edge' method moves the object from the current position toward the end of the screen edge. For example, 'object.to_edge(LEFT)' moves the object from its current position to the left end edge.

 


to_edge

manimlib.mobject.Mobject.to_edge(edge=LEFT, buff=DEFAULT_MOBJECT_TO_EDGE_BUFFER)

Moves the object to the edge of the screen in the direction specified as 'edge=LEFT/RIGHT/UP/DOWN'

At this time, how far away from the end of the screen edge is determined by the 'buff' value.

 

Parameters: **edge=LEFT**  
	Indicates the direction to move, mainly LEFT/RIGHT/UP/DOWN is used, 
    but any direction value can be used such as obj.to_edge(np.array([2,1,0])).

Parameters: **buff=DEFAULT_MOBJECT_TO_EDGE_BUFFER**  
  	A value that determines how far away from the edge of the screen. 
	The default value is DEFAULT_MOBJECT_TO_EDGE_BUFFER=0.5
    
Returns: **self**  
  object instance moved to the specified postion.

Let's look at an example using 'to_edge' method.

 

In the code, we'll make a rectangle and a text, and we'll move them to the edge of the screen.

 

The code sent to the left edge is 'group.to_edge(LEFT)'

 

The code sent to the left edge with the gap of 0.5 unit from the screen end is 'group.to_edge(LEFT, buff=0.5)'

 

class ToEdgeTest(Scene):  
    def construct(self):  
        self.to_edge_test()  
        self.to_edge_test(buff=0.5)  

    def to_edge_test(self, buff=0):  
        text = Text("Hello", font='Arial', stroke_width=1, size=0.4)  
        rect = Rectangle(width=0.3, height=text.get_height(),stroke_color=RED)  

        group = VGroup(rect,text).arrange(RIGHT)  
        group.save_state()  
        self.add(group)  

        for d in [LEFT,RIGHT,UP,DOWN]:  
            self.play(group.to_edge, d, {"buff":buff})  
            self.wait()  
            group.restore()  
        self.remove(group)

 

 


I'll explain about the sample code.  

 

This is the front part of the lecture, so I explain in detail. From this point onward, we will not explain in detail, but only the core part.

 

def construct(self):  
    self.to_edge_test()  
    self.to_edge_test(buff=0.5)

The 'construct' method is the method used like the main method in the class inherits the Scene class. The code in the 'construct' is executes automatically by the 'manim' module.

 

We can locate all codes in the 'construct' method. However, If you place all the code under the 'construct' method, readability is getting poor.

 

Here, we have created a method that allows us to use both with the buff parameter and without the buff parameter.

 

    self.to_edge_test()

    self.to_edge_test(buff=0.5)

 


    def to_edge_test(self, buff=0):  
        text = Text("Hello", font='Arial', stroke_width=1, size=0.4)  

The 'Text' class is used to make text object not LaTex object. It's possible to use all fonts installed on your PC.

 

'Text' class is possible from the Manim library o fversion 'Aug. 2019' 
Before that, only TextMobject and TexMobject are available, and since these two classes are based on using LaTex, the creation of the object is slow and it is difficult to use another fonts on the PC.

Therefore, if you are using the latest version of the Manim library, and you want to display plain text rather than a formula, I recommend using the Text class. 
Note: It's better to set stroke_width=0. Otherwise the text's shape is not pretty.

 


    group = VGroup(rect,text).arrange(RIGHT)

In order to handle Rectangle and Text objects at once, they are grouped using a class called VGroup. In Manim, it is convenient to group objects unsing VGroup like this.

 

'arrange' is a method that arranges the internal alignment of the inner objects. If you call it RIGHT, it arranges the objects one by one on the right. In the above case, it is called 'arrange(RIGHT)', so the text is located on the right side of the rect.

 


    group.save_state()

Store the current state of the object (shape, color, position, etc.) in a buffer. The saved state is restored to its original state when restore() is called.

 

In the code above, when the group is first created, the location is the center of the screen by default (=ORIGIN), so the group object remembers the shape located in the center of the screen by group.save_state(). After that, when group.restore() is called, the group object is restored to the center of the screen again. 

 


for d in [LEFT,RIGHT,UP,DOWN]:  
    self.play(group.to_edge, d, {"buff":buff})  
    self.wait()  
    group.restore()

The value in the list [LEFT,RIGHT,UP,DOWN] is pulled one by one and becomes 'd' value, and the for loop is executed.

 

The goup.to_edge method is located in self.play, and this method is animated.

In this case, d and {"buff":buff} are parameters for the to_edge method. The d is a mandated parameter to the to_edge method so it is written after the name of the method, but 'buff=' is a optional method for to_edge method so it is written as the format of dictionary.

 

That is, it is the same as goup.to_edge(d, buff=buff).  

 

You will learn more about animation techniques using Mobject's methods such as to_edge in  
'[06-2] Animation by Object's Method'.

 


Next : [04-4] Move to absolute position(to_corner)

 

[04-4] Move to absolute position(to_corner)

In the previous chapter, we looked at the to_edge that moves to the left/right/up/down direction of the screen. A similar method is to_corner. Move the object to the corner of the screen. For exampl..

infograph.tistory.com

Go To: [99] Table of Contents

반응형

'Programming > Manim Lectures' 카테고리의 다른 글

[04-5] Move to relative position(next_to)  (0) 2020.06.01
[04-4] Move to absolute position(to_corner)  (0) 2020.06.01
[04-2] Move to absolute position(move_to)  (0) 2020.06.01
[04-1]Coordinate System  (0) 2020.06.01
[04]Coordinate  (0) 2020.06.01