You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
# 导入Manim核心库
|
|
|
|
|
from manim import *
|
|
|
|
|
# 导入Manim配置模块
|
|
|
|
|
import manim
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 动画场景类必须继承自Scene
|
|
|
|
|
class BoxAnimation(Scene):
|
|
|
|
|
# construct方法是所有Manim动画的入口点
|
|
|
|
|
def construct(self):
|
|
|
|
|
"""
|
|
|
|
|
创建并动画移动一个矩形
|
|
|
|
|
步骤:
|
|
|
|
|
1. 创建带样式的矩形
|
|
|
|
|
2. 分步骤进行位移动画
|
|
|
|
|
"""
|
|
|
|
|
# 创建矩形对象
|
|
|
|
|
box = Rectangle(
|
|
|
|
|
stroke_color=GREEN, # 边框颜色设置为绿色
|
|
|
|
|
stroke_opacity=0.7, # 边框透明度70%
|
|
|
|
|
fill_color=RED_B, # 填充颜色使用红色系B版本
|
|
|
|
|
fill_opacity=0.5, # 填充透明度50%
|
|
|
|
|
height=1, # 高度1个单位(Manim默认坐标单位)
|
|
|
|
|
width=1 # 宽度1个单位
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 将矩形直接添加到场景(无动画)
|
|
|
|
|
self.add(box)
|
|
|
|
|
|
|
|
|
|
# 第一个动画:2秒内向右移动2个单位
|
|
|
|
|
self.play(box.animate.shift(RIGHT * 2), run_time=2)
|
|
|
|
|
|
|
|
|
|
# 第二个动画:2秒内向上移动3个单位
|
|
|
|
|
self.play(box.animate.shift(UP * 3), run_time=2)
|
|
|
|
|
|
|
|
|
|
# 第三个动画:2秒内向左下移动(组合位移)
|
|
|
|
|
self.play(box.animate.shift(DOWN * 5 + LEFT * 5), run_time=2)
|
|
|
|
|
|
|
|
|
|
# 第四个动画:2秒内向右上微调位置
|
|
|
|
|
self.play(box.animate.shift(UP * 1.5 + RIGHT * 1), run_time=2)
|