例1
from manim import *
class NameOfAnimation(Scene):
def construct(self):
#创建矩形,设置边颜色和透明度,填充颜色和透明度
box = Rectangle(stroke_color=GREEN,
stroke_opacity=0.7, fill_color = RED_B,
fiIl _opacity = 0.5, height=1, width=1)
#无动画添加到场景中
self.add(box)
#2s内向右移动两个单位
self.play(box.animate.shift(RIGHT*2), run time=2)
#向上移动三个单位
self.play(box.animate.shift(UP*3), run time=2)
#向下自动五个单位向左移动5个单位
self.play(box.animate.shift(DOWN*5+LEFT*5), run_time=2)
#向上移动1.5单位向右移动一个单位
self.play(box.animate.shift(UP*1.5+RIGHT*1), run time=2)
例2
- 画一个坐标系放在左边
- 画一个圆放在右下角
- 画一个三角形放在圆的左边
- 将圆变小,然后向三角转变
class FittingObjects(Scene) :
def construct(self):
# 坐标系放在左侧边缘
axes = Axes(x_range=[-3,3,1], y_range=[-3,3,1], x_length = 6, y_length=6)
#坐标系距离左侧有0.5的缓冲距离
axes.to _edge (LEFT, buff=0.5)
#圆
circle = Circle(stroke _width = 6, stroke _color = YELLOW, fill color = RED C, fiIl opacity = 0.8)
#圆的宽度是2,位置在右下角,无缓冲距离
circIe.set_width(2).to_edge (DR, buff=0)
#创建三角形,高度2位置右下3.3处
triangle = Triangle(stroke_color = ORANGE, stroke_width = 10,fill_color = GREY) .set_height(2) .shift(DOWN*3+RIGHT*3)
#创建动画 画坐标系
self.play(Write(axes))
#创建动画 画线然后填充圆
self.play(DrawBorderThenFill(circle))
#形态动画 圆变化宽度
self.play(circle.animate.set _width(1))
#转变动画 将圆向三角转变
self.play(Transform(circle, triangle), run _time=3)
例3
- 画一个方框
- 写一个公式,颜色渐变,位置放在方框内部
- 动画1移动方框,公式保持在方框内部
- 动画2移动方框,公式位置不变
- 使用updater
class Updaters(Scene):
def construct(self):
rectangle = RoundedRectangle(stroke width = 8, stroke color =
WHITE,
fill color = BLUE B, width = 4.5, height = 2).shift(UE*3+LEFT*4)
mathtext = MathTex("\\frac{3}{4} = 0.75"
).set_color by _gradient(GREEN, PINK).set _height(1.5)
mathtext.move to(rectangle.get center())
mathtext.add updater(lambda x : x.move_to(rectangle.get _center()))
self.play(FadeIn(rectangle))
self.play(Write(mathtext))
self.play(rectangle.animate.shift(RIGHT*1.5+DOWN*5), run_time=6)
self.wait()
mathtext.clear updaters()
self.play(rectangle.animate.shift (LEFT*2 + UP*1), run_time-6)