from manim import * #导入manim命名空间 #这是一个最基本的manim结构,类名叫做BaseFrame,传入一个场景Scene,并且包含一个construct方法,传入self class BaseFrame(Scene): def construct(self): self.wait() class CreateCircle(Scene): def construct(self): circle = Circle() # 创建了一个Circle对象:circle circle.set_fill(PINK, opacity=0.5) # set the color and transparency self.play(Create(circle)) # show the circle on screen class SquareToCircle(Scene): def construct(self): circle = Circle() # create a circle circle.set_fill(PINK, opacity=0.5) # set color and transparency square = Square() # create a square square.rotate(PI / 4) # rotate a certain amount self.play(Create(square)) # animate the creation of the square self.play(Transform(square, circle)) # interpolate the square into the circle self.play(FadeOut(square)) # fade out animation class SquareAndCircle(Scene): def construct(self): circle = Circle() # create a circle circle.set_fill(PINK, opacity=0.5) # set the color and transparency square = Square() # create a square square.set_fill(BLUE, opacity=0.5) # set the color and transparency square.next_to(circle, RIGHT, buff=0.5) # set the position self.play(Create(circle), Create(square)) # show the shapes on screen class ThreePar(Scene): def construct(self): circle = Circle() circle.set_fill(RED, opacity=0.6) star = Star() star.set_fill(BLUE, opacity=0.4) square = Square() square.set_fill(BLUE, opacity=0.5) circle.next_to(star, LEFT, buff=0.5) square.next_to(star, RIGHT, buff=0.5) #占用1秒的时长, #如果你的Create方法是在play里面的,他就会占用一秒钟的时间去展示创建对象的过程 self.add(star) self.add(circle)#无意义的,后面的Create(circle)会创建,系统会删除次代码 self.play(Create(circle),Create(square)) self.wait(1) self.remove(star) self.wait(1) class AnimatedSquareToCircle(Scene): def construct(self): circle = Circle() # create a circle square = Square() # create a square #Play_Scripts_Start self.play(Create(square)) # show the square on screen self.play(square.animate.rotate(PI / 4)) # 旋转图形,参数是弧度制 self.play( ReplacementTransform(square, circle) ) # transform the square into a circle self.play( circle.animate.set_fill(PINK, opacity=0.5) ) # color the circle on screen #Play_Scripts_End 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() #默认情况下,对象会生成在屏幕中心点 class Learn01_move(Scene): def construct(self): circle = Circle() triangle = Triangle() self.add(circle,triangle) print ("移动") self.wait(1) circle.shift(LEFT*3 + UP*2) #瞬间移动,LEFT,UP是单位方向向量 #以自身为参考 self.wait(1) triangle.next_to(circle,RIGHT) #瞬间移动 #以传入对象为参考 self.wait(1) circle.move_to(DOWN) #瞬间移动 #以屏幕中心为参考 self.wait(1) triangle.align_to(circle,DOWN) #瞬间移动 #以传入对象的假想边界作为调整 #传入DOWN(0,-1),那么物体的纵坐标就会"对其"对象的下边界 #这个假想的边界一般以物体以中心为原点的第二象限区域 self.wait(1) class Learn02_beauty(Scene): def construct(self): circle = Circle().shift(LEFT*2) star = Star().shift(RIGHT*2) self.play(Create(circle), Create(star)) self.wait(1) #set_fill() 改变图形的内部 #set_stroke()改变图形的边框 star.set_stroke(color=YELLOW, width=20) circle.set_stroke(color=BLUE, width=20) star.set_fill(YELLOW, opacity=0.7) circle.set_fill(YELLOW, opacity=0.7) self.wait(1) class Learn03_animations(Scene): def construct(self): star = Star() self.play(FadeIn(star))#渐入特效 #self.play(FadeIn(star),run_time = 5)可以设置时间 self.wait(1) self.play(Rotate(star, 2*PI))#旋转特效 self.wait(1) self.play(FadeOut(star))#渐出特效 #animate方法 #这样就可以 class Learn04_animateMethod(Scene): def construct(self): star = Star() self.play(star.animate.set_fill(YELLOW, opacity=0.4)) self.wait(1) self.play(star.animate.set_fill(WHITE)) self.wait(1) #也可以同时进行animate方法 self.play( star.animate .shift(UP+LEFT) .rotate(2/3*PI) .set_fill(RED) ) self.wait(1) #也可以在play中添加run_time参数,修改动画持续时间 self.play( star.animate .shift(-UP-LEFT) .rotate(2/3*PI) .set_fill(BLUE) ,run_time = 5 ) self.wait(1) #创建自定义动画 #重写interpolate_mobject()方法 class Countqq(Animation): #kwargs是可变参数,此处暂时用不上 def __init__(self, number: DecimalNumber, start: float, end: float, **kwargs): # Pass number as the mobject of the animation #这个是构造方法,使用到我们这个Countqq这个类的时候就会自动把参数传入此方法中 #self在构造的时候不需要传递 super().__init__(number, **kwargs) # Set start and end self.start = start self.end = end def interpolate_mobject(self, alpha: float): # Set value of DecimalNumber according to alpha #这一行是增长的 这个是manim系统内部调用的,我们就不管他了。 value = self.start + (alpha * (self.end - self.start)) self.mobject.set_value(value) class Learn05_CountingScene(Scene): def construct(self): #创建一个Decimal数字,设置颜色,缩放比 number = DecimalNumber().set_color(BLUE).scale(5).move_to(LEFT) #添加一个定时更新器,ORIGIN相当于[0,0,0],移动到屏幕中心点 #匿名函数lambda构建方法 lambda XXX:___ number.add_updater(lambda number: number) self.add(number) self.wait(1) # Play the Count Animation to count from 0 to 314 in 4 seconds #Count传入数字范围从0到100,设置持续时间4秒,线性增长 #play可以传递一个Animation类型的参数,用于动画的播放,参数可设置:run_time,rate_func there_and_back self.play(Countqq(number, 0, 100), run_time=4, rate_func=linear) self.wait(1) class Learn06_MobjectExample(Scene): def construct(self): p1= [-1,-1,0] p2= [1,-1,0] p3= [1,1,0] p4= [-1,1,0] #append_points()方法是增加节点,连成折线。 #方法中传入起点与终点的Line的points a = Line(p1,p2).append_points(Line(p2,p3).points).append_points(Line(p3,p4).points) #寻找线的开头&结尾&重心 get_start get_end get_center point_start= a.get_start() point_end = a.get_end() point_center = a.get_center() #这个self.mobjects[-1]是指上一个add的对象 #np是numpy的意思,manim中,numpy as np,即数学库 #UR指的是UP+RIGHT,在边界的右上角 self.add(Text(f"a.get_start() = {np.round(point_start,2).tolist()}", font_size=24).to_edge(UR).set_color(YELLOW)) self.add(Text(f"a.get_end() = {np.round(point_end,2).tolist()}", font_size=24).next_to(self.mobjects[-1],DOWN).set_color(RED)) self.add(Text(f"a.get_center() = {np.round(point_center,2).tolist()}", font_size=24).next_to(self.mobjects[-1],DOWN).set_color(BLUE)) #还可以get各种位置 get_bottom get_center point_from_proportion是线的中点 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) #测试,不用管这里 class Learn07_MobjectTest(Scene): def construct(self): p1=[-1,-1,0] p2=[1,1,0] p3=[-1,1,0] a =Line(p1,p2).append_points(Line(p2,p3).points) 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) class Learn08_Transform(Scene): def construct(self): #设置摄像机的背景颜色,即屏幕背景 self.camera.background_color = GREEN_A #创建一个正方形 m1 = Circle().set_color(RED) #创建一个矩形,旋转一定角度 m2 = Rectangle().set_color(RED).rotate(PI/6) #转变图形 self.play(Transform(m1,m2)) #会直接把m1变成m2的外观,m2不会显示 class Test_Transform(Scene): def construct(self): #渐变色 mobj1 = Text(f"早上好,我是?", font_size=93,t2g={'[1:-1]': (RED,GREEN),}).shift(UP*2) self.play(FadeIn(mobj1),run_time=1) mobj2 = Text(f"傻逼!", font_size=256).next_to(self.mobjects[-1],DOWN).set_color(BLUE) self.play(Transform(mobj1,mobj2),run_time=2) class Learn09_roll(Scene): def construct(self): self.camera.background_color = WHITE m1a = Square().set_color(RED).shift(LEFT) m1b = Circle().set_color(RED).shift(LEFT) m2a= Square().set_color(BLUE).shift(RIGHT) m2b= Circle().set_color(BLUE).shift(RIGHT) points = m2a.points points = np.roll(points, int(len(points)/4), axis=0) m2a.points = points self.play(Transform(m1a,m1b),Transform(m2a,m2b), run_time=1) class Learn10_HelloLaTeX(Scene): def construct(self): tex = MathTex( r"\sum_{i=1}^{n}{X_i}" ) tex2 = MathTex( r"e^x = x^0 + x^1 + \frac{1}{2} x^2 + \frac{1}{6} x^3 + \cdots + \frac{1}{n!} x^n + \cdots" ,substrings_to_isolate="x"#隔离x作为字串,后面就可以单独渲染它的颜色了 ).next_to(tex,DOWN) tex2.set_color_by_tex("x", YELLOW) self.add(tex,tex2)