[ PROMPT_NODE_27752 ]
Manim 动画
[ SKILL_DOCUMENTATION ]
# Manim 中的动画
动画是随时间应用于数学对象 (mobjects) 的变换。Manim 提供了一套丰富的内置动画,用于创建流畅、专业的过渡效果。
## 基本动画模式
python
from manim import *
class BasicAnimation(Scene):
def construct(self):
circle = Circle()
# 播放动画
self.play(Create(circle))
self.wait(1)
## 创建动画
用于引入数学对象的动画:
python
class CreationAnimations(Scene):
def construct(self):
square = Square()
circle = Circle()
text = Text("Hello")
# Create (绘制描边)
self.play(Create(square))
self.wait(0.5)
# Fade in (淡入)
self.play(FadeIn(circle))
self.wait(0.5)
# Write (用于文本)
self.play(Write(text))
self.wait(0.5)
# Grow from center (从中心生长)
triangle = Triangle()
self.play(GrowFromCenter(triangle))
## 移除动画
用于移除数学对象的动画:
python
class RemovalAnimations(Scene):
def construct(self):
shapes = VGroup(*[Circle() for _ in range(4)])
shapes.arrange(RIGHT, buff=0.5)
self.add(shapes)
# Fade out (淡出)
self.play(FadeOut(shapes[0]))
# Shrink to center (收缩到中心)
self.play(ShrinkToCenter(shapes[1]))
# Uncreate (Create 的反向)
self.play(Uncreate(shapes[2]))
# Unwrite (Write 的反向)
text = Text("Bye")
self.add(text)
self.play(Unwrite(text))
## 变换动画
将一个数学对象变形为另一个:
python
class TransformAnimations(Scene):
def construct(self):
square = Square()
circle = Circle()
self.play(Create(square))
self.wait(0.5)
# Transform (原始对象变为目标对象)
self.play(Transform(square, circle))
self.wait(0.5)
# ReplacementTransform (替换为新对象)
triangle = Triangle()
self.play(ReplacementTransform(square, triangle))
## 移动动画
python
class MovementAnimations(Scene):
def construct(self):
circle = Circle()
self.add(circle)
# Shift (相对移动)
self.play(circle.animate.shift(RIGHT * 2))
# Move to (绝对位置)
self.play(circle.animate.move_to(UP * 2))
# Rotate (旋转)
self.play(circle.animate.rotate(PI / 2))
# Scale (缩放)
self.play(ci