from manim import * import numpy as np from numpy import array run_time = 1 data = [[1, 2], [3, 4]] defdot(pos, **kwargs): return Dot(pos, **kwargs) classAnimateExample(Scene): defconstruct(self): square = Square().set_fill(RED, opacity=1.0) self.play(*[FadeIn(_o) for _o in [square]], run_time=1.0) # animate the change of color self.play(square.animate.set_fill(WHITE)) self.wait(1) # animate the change of position and the rotation at the same time self.play(square.animate.shift(UP).rotate(PI / 3)) self.wait(1) self.wait(0.60)
from manim import * import numpy as np from numpy import array run_time = 1 data = [[1, 2], [3, 4]] defdot(pos, **kwargs): return Dot(pos, **kwargs) classMobjectExample(Scene): defconstruct(self): p1= [-1,-1,0] p2= [1,-1,0] p3= [1,1,0] p4= [-1,1,0] a = Line(p1,p2).append_points(Line(p2,p3).points).append_points(Line(p3,p4).points) point_start= a.get_start() point_end = a.get_end() point_center = a.get_center() self.play(*[FadeIn(_o) for _o in [Text(f"a.get_start() = {np.round(point_start,2).tolist()}", font_size=24).to_edge(UR).set_color(YELLOW)]], run_time=1.0) self.play(*[FadeIn(_o) for _o in [Text(f"a.get_end() = {np.round(point_end,2).tolist()}", font_size=24).next_to(self.mobjects[-1],DOWN).set_color(RED)]], run_time=1.0) self.play(*[FadeIn(_o) for _o in [Text(f"a.get_center() = {np.round(point_center,2).tolist()}", font_size=24).next_to(self.mobjects[-1],DOWN).set_color(BLUE)]], run_time=1.0) self.play(*[FadeIn(_o) for _o in [Dot(a.get_start()).set_color(YELLOW).scale(2)]], run_time=1.0) self.play(*[FadeIn(_o) for _o in [Dot(a.get_end()).set_color(RED).scale(2)]], run_time=1.0) self.play(*[FadeIn(_o) for _o in [Dot(a.get_top()).set_color(GREEN_A).scale(2)]], run_time=1.0) self.play(*[FadeIn(_o) for _o in [Dot(a.get_bottom()).set_color(GREEN_D).scale(2)]], run_time=1.0) self.play(*[FadeIn(_o) for _o in [Dot(a.get_center()).set_color(BLUE).scale(2)]], run_time=1.0) self.play(*[FadeIn(_o) for _o in [Dot(a.point_from_proportion(0.5)).set_color(ORANGE).scale(2)]], run_time=1.0) self.play(*[FadeIn(_o) for _o in [*[Dot(x) for x in a.points]]], run_time=1.0) self.play(*[FadeIn(_o) for _o in [a]], run_time=1.0) # 兼容 Manim CE 0.20+:补一个动画以便输出视频 ifself.mobjects: self.play(FadeIn(Group(*self.mobjects))) else: self.wait(1)
self.add(Dot(a.get_start()).set_color(YELLOW).scale(2)) self.add(Dot(a.get_end()).set_color(RED).scale(2)) self.add(Dot(a.get_top()).set_color(GREEN_A).scale(2)) self.add(Dot(a.get_bottom()).set_color(GREEN_D).scale(2)) self.add(Dot(a.get_center()).set_color(BLUE).scale(2)) self.add(Dot(a.point_from_proportion(0.5)).set_color(ORANGE).scale(2)) self.add(*[Dot(x) for x in a.points]) self.add(a)
使用移动对象的坐标将移动对象转换为其他移动对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14
from manim import * import numpy as np from numpy import array run_time = 1 data = [[1, 2], [3, 4]] defdot(pos, **kwargs): return Dot(pos, **kwargs) classExampleTransform(Scene): defconstruct(self): self.camera.background_color = WHITE m1 = Square().set_color(RED) m2 = Rectangle().set_color(RED).rotate(0.2) self.play(Transform(m1,m2)) self.wait(4.60)