I. Tổng Quan
Đầu tiên, chúng ta sử dụng giao diện dòng lệnh để tạo một lớp 'Scene(Cảnh)' mà qua đó manim tạo video. Trong 'Scene' chúng ta sẽ tạo một hoạt ảnh cho một vòng tròn. Sau đó, chúng ta sẽ thêm một cảnh khác hiển thị hình vuông biến thành một vòng tròn. Đây sẽ là phần giới thiệu cho chúng ta về khả năng hoạt ảnh của Manim. Sau đó, chúng ta sẽ định vị nhiều đối tượng toán học (Mobjects). Cuối cùng, chúng ta sẽ tìm hiểu cú pháp .animate, một tính năng mạnh mẽ làm sinh động các phương thức chúng ta sử dụng để sửa đổi Mobjects.
1. Hoạt ảnh một đường tròn
a. Ví dụ
from manim import *
class CreateCircle(Scene):
def construct(self):
circle = Circle() # tao mot hinh tron
circle.set_fill(PINK, opacity=0.5) # thiet lap mau sac va do trong suot
self.play(Create(circle)) # hien thi vong tron len man hinh
2. Chuyển hình vuông thành hình tròn
a. Ví dụ
from manim import *
class SquareToCircle(Scene):
def construct(self):
circle = Circle()
circle.set_fill(PINK, opacity= 0.5)
square = Square()
square.rotate(PI/4)
self.play(Create(square))
self.play(Transform(square, circle))
self.play(FadeOut(square))
3. Định vị Mobjects S
a. Ví dụ
from manim import *
class SquareAndCircle(Scene):
def construct(self):
circle = Circle()
circle.set_fill(PINK, opacity= 0.5)
square = Square()
square.set_fill(BLUE, opacity= 0.5)
square.next_to(RIGHT, buff= 0.5)
self.play(Create(circle), Create(square))
4. Sử dụng cú pháp .animate để tạo hiệu ứng cho các methods
a. Ví dụ 1
from manim import *
class AnimatedSquareToCircle(Scene):
def construct(self):
circle = Circle()
square = Square()
self.play(Create(square))
self.play(square.animate.rotate(PI / 4))
self.play(Transform(square, circle))
self.play(circle.animate.set_fill(PINK, opacity = 0.5))
b. VÍ dụ 2
from manim import *
class DifferentRotations(Scene):
def construct(self):
left_square = Square(color = BLUE, fill_opacity = 0.7).shift(2 * LEFT)
right_square = Square(color = GREEN, fill_opacity = 0.7).shift(2 * RIGHT)
self.play(left_square.animate.rotate(PI), Rotate(right_square, angle=PI),
run_time= 2)
self.wait()
II. Một cái nhìn sâu sắc
Phần này sẽ tập trung vào việc hiểu các tệp đầu ra của Manim và một số cờ hiệu dòng lệnh chính có sẵn.
1. Thư mục đầu ra của manim
manim -pql name_file.py name_class tạo một video có chất lượng thấp
manim -pqm name_file.py name_class tạo một video có chất lượng trung bình
manim -pqh name_file.py name_class tạo một video có chất lượng cao
manim -pqk name_file.py name_class tạo một video có chất lượng 4k
manim -pql -s name_file.py name_class tạo một ảnh chất lượng thấp
manim -pqh -s name_file.py name_class tạo một ảnh chất lượng cao
manim -pqh -i name_file.py name_class tạo một ảnh gif chất lượng cao
2. Các hằng số
ORIGIN Là tâm của hệ tọa độ.
UP Là hướng trên trục tung.
DOWN Là hướng dưới trục tung
RIGHT Là hướng bên phải trục hoành
LEFT Là hướng bên trái trục hoành
UL Là góc trên cùng bên trái
UR Là góc trên cùng bên phải
DL Là góc dưới cùng bên trái
DR Là góc dưới cùng bên phải
III. Xây dựng các khối trong manim
Về cơ bản, manim đặt theo ý nghĩa của ba khái niệm khác nhau mà chúng ta có thể phối hợp với nhau để tạo ra các hoạt ảnh động toán học: Đối tượng toán học (mathematical object hay gọi tắt là mobject), hoạt ảnh (animation) và cảnh (scene). Như chúng ta sẽ thấy trong các phần sau, mỗi trong ba khái niệm này được thực hiện trong manim như một lớp riêng biệt: các lớp Mobject, Animation và Scene.
Mobjects
Mobject là khối xây dựng cơ bản cho tất cả các hoạt ảnh manim. Mỗi lớp xuất phát từ Mobject đại diện cho một đối tượng có thể được hiển thị trên màn hình. Ví dụ, các hình dạng đơn giản như Circle, Arrow và Rectangle tất cả đều là mobject. Các cấu trúc phức tạp hơn như Axes, FunctionGraph hoặc BarChart cũng là các mobject.
1. Tạo và hiển thị mobjects
a. Ví dụ
from manim import *
class CreatingMobjects(Scene):
def construct(self):
circle = Circle()
self.add(circle)
self.wait(1)
self.remove(circle)
self.wait(1)
2. Đặt mobjects
a. Ví dụ
from manim import *
class Shapes(Scene):
def construct(self):
circle = Circle()
square = Square()
triangle = Triangle()
circle.shift(LEFT)
square.shift(UP)
triangle.shift(RIGHT)
self.add(circle, square, triangle)
self.wait(1)
b. Ví dụ
from manim import *
class MobjectPlacement(Scene):
def construct(self):
circle = Circle().move_to(LEFT * 2)#di chuyen duong tron sang trai 2 don vi
square = Square().next_to(circle, LEFT)#dat hinh vuong ben trai duong tron
# can chinh vien trai cua tam giac voi vien trai cua vong tron
triangle = Triangle().align_to(circle, LEFT)
self.add(circle, square, triangle)
self.wait(1)
3. Tạo kiểu mobjects
a. Ví dụ
from manim import *
class MobjectStyling(Scene):
def construct(self):
circle = Circle().shift(LEFT)
square = Square().shift(UP)
triangle = Triangle().shift(RIGHT)
circle.set_stroke(color = GREEN, width = 20)
square.set_fill(YELLOW, opacity = 1)#do mo 100%
triangle.set_fill(PINK, opacity = 0.5)#do 50%
self.add(circle, square, triangle)
self.wait(1)
4. Thứ tự Mobject trên màn hình
a. Ví dụ
from manim import *
class MobjectStyling(Scene):
def construct(self):
circle = Circle().shift(LEFT)
square = Square().shift(UP)
triangle = Triangle().shift(RIGHT)
circle.set_stroke(color = GREEN, width = 20)
square.set_fill(YELLOW, opacity = 1)#do mo 100%
triangle.set_fill(PINK, opacity = 0)#do mo trong suot
self.add(triangle, square, circle)
self.wait(1)
Animations
Trung tâm của manim là animation. Nói chung, bạn có thể thêm animation vào cảnh của mình bằng cách gọi phương pháp play().
a. Ví dụ
from manim import *
class SomeAnimations(Scene):
def construct(self):
square = Square()
self.play(FadeIn(square))#tao hinh vuong
self.play(Rotate(square, PI / 4))#xoay hinh vuong 45 do
self.play(FadeOut(square))#xoa hinh vuong
self.wait(1)#dung 1s truoc khi ket thuc video
1. Các phương pháp tạo hoạt ảnh
a. Ví dụ
from manim import *
class AnimateExample(Scene):
def construct(self):
square = Square().set_fill(RED, opacity= 1)
self.add(square)#hien thi hinh vuong
self.play(square.animate.set_fill(WHITE))
self.wait(1)
self.play(square.animate.shift(UP).rotate(PI / 3))
self.wait(1)
2. Thời gian chạy hoạt ảnh
a. Ví dụ
from manim import *
class RunTime(Scene):
def construct(self):
square = Square()
self.add(square)#hien thi hinh vuong
self.play(square.animate.shift(UP), run_time = 3)
self.wait(1)
3. Tạo hoạt ảnh tùy chỉnh
Mặc dù Manim có nhiều hoạt ảnh tích hợp, ta sẽ thấy những lúc cần chuyển động mượt mà từ trạng thái này Mobject sang trạng thái khác.
a. Ví dụ
from manim import *
class count(Animation):
def __init__(self, number, start, end, **kwargs):
super().__init__(number, **kwargs)
self.start = start
self.end = end
def interpolate_mobject(self, alpha):
value = self.start + alpha*(self.end - self.start)
self.mobject.set_value(value)
class counting_scene(Scene):
def construct(self):
number = DecimalNumber().set_fill(WHITE).scale(5)
self.add(number)
self.wait()
self.play(count(number, 0, 100), run_time = 4, rate_func = linear)
self.wait()
4. Sử dụng tọa độ của mobject
Mobjects chứa các điểm xác định ranh giới của chúng. Những điểm này có thể được sử dụng để thêm các mobject khác tương ứng với nhau, ví dụ như các phương pháp get_center(), get_top() và get_start().
Không có nhận xét nào:
Đăng nhận xét