from manim import *
from scipy.io import wavfile
import random
# .next_to(aa, DOWN) 将一个对象放置到另一个对象的旁边或相对位置
# .shift(UP * 2 + RIGHT * 3) 沿指定方向平移对象,可以通过指定x和y坐标的位移量来移动对象
# .align_to(aa, DOWN) 将一个对象对齐到另一个对象的指定位置
# .align(RIGHT) 对齐对象的中心或边缘
# .move_to(2 * UP + 3 * RIGHT) 直接将对象移动到指定的坐标位置
# .to_edge(LEFT) 将对象移动到画布的边缘,可以指定对齐方式(上、下、左、右)
class Try(Scene):
def construct(self):
random.seed()
config.tex_template = TexTemplateLibrary.ctex # 设置中文显示
# --------------------------------------------------------------------------------------------
s0 = Text("奥数时间到咯", font_size=40)
self.play(Write(s0))
self.wait()
s0.move_to([0, 0, 0])
# self.play(s0.move_to([0, 0, 0]).animate.move_to(4 * RIGHT + 3 * DOWN))
self.play(s0.move_to([0, 0, 0]).animate.to_corner(RIGHT + DOWN).scale(0.8))
s1 = Tex(r"小橘子在干嘛呢?")
s2 = Tex(r"等差数列和 = (首项 + 末项) * 项数 / 2", font_size=35)
VGroup(s1, s2).arrange(
DOWN, buff=0.5
) # arrange方法用于在指定方向上排列VGroup对象中的子对象。第一个参数指定排列的方向,这里是DOWN,表示垂直向下排列;第二个参数buff是指定子对象之间的间距。
self.play(
Write(s1), # s1写入
FadeIn(s2, shift=DOWN), # s2淡入
)
self.wait()
s3 = Tex("小橘子快来上课", font_size=40)
s3.to_corner(UP + LEFT)
self.play(
Transform(s1, s3), # s1 -> s3
LaggedStart(
[
FadeOut(obj, shift=DOWN) for obj in [s2, s0]
] # s2淡出,[]里可以放多个,LaggedStart可以创建一系列动画对象之间有延迟的效果。
),
)
s4 = Text("粑粑想你啦!", font_size=35).move_to([0, 0, 0])
self.play(Write(s4, shift=DOWN, run_time=1))
self.wait(1.5)
self.play(FadeOut(s4, shift=DOWN))
# self.clear()
# --------------------------------------------------------------------------------------------
a = 2 # horizontal radius
b = 3 # vertical radius
m = 16
fontsize = 35
h_max = Tex("15", font_size=fontsize).get_height()
decrease = 8 # 5%
t_values = np.linspace(0, 2 * np.pi, m, endpoint=False)
points = [np.array([a * np.cos(t), b * np.sin(t), 0]) for t in t_values]
# 15*2在最右边, 25*2在最左边
aa = [Tex(f"15*{i}={15*i}", font_size=fontsize) for i in range(2, 10)]
for i, point in enumerate(points[0:8]):
if i <= m // 2:
scale = 1 - i * decrease / 100
else:
scale = 1 - (m - i) * decrease / 100
aa[i].set_height(scale * h_max).move_to(point)
aa.extend([Tex(f"25*{i}={25*i}", font_size=fontsize) for i in range(2, 10)])
for i, point in enumerate(points[8:16]):
j = abs(i + 8)
if j <= m // 2:
scale = 1 - j * decrease / 100
else:
scale = 1 - (m - j) * decrease / 100
aa[i + 8].set_height(scale * h_max).move_to(point)
animations = []
k = 0
for j in range(10):
for i in range(0, len(aa)):
j = abs(i - k)
if j <= m // 2:
scale = 1 - j * decrease / 100
else:
scale = 1 - (m - j) * decrease / 100
animations.append(
aa[i].animate.move_to(aa[i - 1]).set_height(scale * h_max)
)
self.play(animations, run_time=1)
# k是最右边的0号点,此点最大
k = k + 1
if k == m:
k = 0
self.wait(2)
########################################################################################################################旋转椭圆
# a = 1 # horizontal radius
# b = 2 # vertical radius
# t_values = np.linspace(0, 2 * np.pi, 30)
# points = [np.array([a * np.cos(t), b * np.sin(t), 0]) for t in t_values]
# # dot[0]和dot[-1]其实是同一个点
# dots = VGroup(*[Dot(point, color=RED, fill_opacity=0.9) for point in points])
# animations = []
# for i in range(1, len(dots)):
# # dot[0]和dot[-1]其实是同一个点,所以无需处理dot[0]
# animations.append(dots[i].animate.move_to(dots[i - 1].get_center()))
# self.play(animations, run_time=1)
# self.wait()
########################################################################################################################
# texts = [Text(f"Text {i}", font="Arial", slant=ITALIC) for i in range(10)]
# # 设置初始位置和缩放大小
# for i, text in enumerate(texts):
# x = 2 * np.cos(i * 2 * np.pi / 10)
# y = 2 * np.sin(i * 2 * np.pi / 10)
# text.move_to([x, y, 0])
# text.scale(0.8)
# # 添加文本对象到场景并依次进行旋转
# for text in texts:
# self.play(
# Write(text),
# text.animate.apply_complex_function(lambda z: 0.5 * z).rotate(
# 2 * np.pi / 10
# ),
# )
# self.wait(2)
########################################################################################################################
# # gp = VGroup()
# animations = []
# for x in range(-2, 3):
# for y in range(-2, 3):
# dot1 = Dot([x * 2, y, 0], color=RED, radius=0.45)
# line1 = Arrow(
# [random.randint(-6, 6), random.randint(-6, 6), 0],
# [random.randint(-6, 6), random.randint(-6, 6), 0],
# color=RED_A,
# stroke_width=5,
# )
# exp1 = RegularPolygon(n=6, fill_color=BLUE, fill_opacity=1.0, color=RED)
# exp1.move_to([random.randint(-6, 6), random.randint(-6, 6), 0])
# animations.append(Create(dot1))
# animations.append(Create(line1))
# animations.append(Create(exp1))
# self.play(AnimationGroup(animations, lag_ratio=0.0))
# self.wait(1)
########################################################################################################################
# config.tex_template = TexTemplateLibrary.ctex # 设置中文显示
# title = Tex(r"小橘子在干嘛呢?")
# basel = Tex(r"等差数列和 = (首项 + 末项) * 项数 / 2", font_size=35)
# VGroup(title, basel).arrange(DOWN)
# self.play(
# Write(title),
# FadeIn(basel, shift=DOWN),
# )
# self.wait()
# transform_title = Tex("小橘子快来上课", font_size=50)
# transform_title.to_corner(UP + LEFT)
# self.play(
# Transform(title, transform_title),
# LaggedStart(*[FadeOut(obj, shift=DOWN) for obj in basel]),
# )
# self.wait()
# grid = NumberPlane()
# grid_title = Tex("爸爸想你啦", font_size=50)
# grid_title.move_to(transform_title)
# self.add(grid, grid_title) # Make sure title is on top of grid
# self.play(
# FadeOut(title),
# FadeIn(grid_title, shift=UP),
# Create(grid, run_time=3, lag_ratio=0.1),
# )
# self.wait()
# grid_transform_title = Tex(r"在阿婆家 \\ 干嘛呢")
# grid_transform_title.move_to(grid_title, UL)
# grid.prepare_for_nonlinear_transform()
# self.play(
# grid.animate.apply_function(
# lambda p: p
# + np.array(
# [
# 2 * np.sin(p[1]),
# 2 * np.sin(p[0]),
# 0,
# ]
# )
# ),
# run_time=6,
# )
# self.wait()
# self.play(Transform(grid_title, grid_transform_title))
# self.wait()
########################################################################################################################
# # 创建一个坐标系
# axes = ThreeDAxes()
# # 创建螺旋曲线
# t_values = np.linspace(0, 4 * PI, 100)
# spiral_curve = ParametricFunction(
# lambda t: np.array([t * np.cos(t), t * np.sin(t), t / (4 * PI)]),
# t_range=[0, 4 * PI],
# color=BLUE,
# stroke_width=4,
# )
# # 创建小球
# ball = Sphere(radius=0.1, color=RED).move_to(spiral_curve.get_start())
# # 创建小球沿螺旋曲线运动的路径
# ball_path = ParametricFunction(
# lambda t: np.array([t * np.cos(t), t * np.sin(t), t / (4 * PI)]),
# t_range=[0, 4 * PI],
# color=WHITE,
# stroke_width=0,
# )
# # 小球沿螺旋曲线运动的动画
# move_ball = MoveAlongPath(ball, ball_path, rate_func=linear, run_time=8)
# # 创建路径轨迹
# path_trace = TracedPath(ball.get_center, stroke_color=YELLOW, stroke_width=2)
# # 设置摄像机的初始角度
# # self.set_camera_orientation(phi=75 * DEGREES, theta=30 * DEGREES)
# # 添加元素到场景
# self.add(axes, spiral_curve, ball, path_trace)
# # 播放动画
# self.play(move_ball)
# self.wait(2)
########################################################################################################################
# # 创建一个坐标系
# axes = Axes(
# x_range=[0, 2 * PI, PI / 2],
# y_range=[-1.5, 1.5, 0.5],
# axis_config={"color": BLUE},
# tips=False,
# )
# # 创建标签
# x_label = Text("x").next_to(axes.x_axis.get_end(), RIGHT)
# y_label = Text("sin(x)").next_to(axes.y_axis.get_end(), UP)
# labels = VGroup(x_label, y_label)
# # 创建正弦曲线
# sin_curve = axes.plot(lambda x: np.sin(x), color=YELLOW, stroke_width=8)
# # 创建小球
# ball = Dot().move_to(axes.c2p(0, 0))
# ball.set_color(RED)
# # 创建小球沿正弦曲线运动的路径
# ball_path = axes.plot(lambda x: np.sin(x), color=WHITE, stroke_width=0)
# # 创建渐变路径
# gradient_path = VGroup()
# for i in np.linspace(0, 2 * PI, 100):
# dot = Dot(radius=0.05).move_to(axes.c2p(i, np.sin(i)))
# dot.set_color(color=[RED, BLUE, GREEN, YELLOW])
# gradient_path.add(dot)
# # 小球沿正弦曲线运动的动画
# move_ball = MoveAlongPath(ball, ball_path, rate_func=linear, run_time=5)
# # 创建路径轨迹
# path_trace = TracedPath(ball.get_center, stroke_color=WHITE, stroke_width=5)
# # 添加元素到场景
# self.add(axes, labels, sin_curve, gradient_path, ball, path_trace)
# # 播放动画
# self.play(move_ball)
# self.wait(2)
########################################################################################################################
# # 创建一个坐标系
# axes = Axes(
# x_range=[0, 2 * PI, PI / 2],
# y_range=[-1.5, 1.5, 0.5],
# axis_config={"color": BLUE},
# )
# # 创建正弦曲线
# sin_curve = axes.plot(lambda x: np.sin(x), color=YELLOW)
# # 创建小球
# ball = Dot().move_to(axes.c2p(0, 0))
# # 创建小球沿正弦曲线运动的动画
# ball_path = axes.plot(lambda x: np.sin(x), color=WHITE)
# move_ball = MoveAlongPath(ball, ball_path, rate_func=linear, run_time=4)
# # 添加坐标系、正弦曲线和小球到场景
# self.add(axes, sin_curve, ball)
# # 播放小球沿正弦曲线运动的动画
# self.play(move_ball)
# self.wait()
# c = Circle(fill_opacity=1)
# s = Square(color=YELLOW, fill_opacity=1)
# self.play(FadeIn(c))
# self.wait()
# self.play(ReplacementTransform(c, s))
# self.wait()
# self.play(FadeOut(s))
# self.wait()
manim test1.py
于 2024-07-15 11:48:57 首次发布