Manim文档及源码笔记-CE文档-示例库5进阶项目

本文详述了ManimCommunityEdition的两个高级项目——OpeningManim和SineCurveUnitCircle的实现过程,包括动画展示、代码解析和实践指导。通过这些示例,读者可以学习如何创建复杂的开场动画和用单位圆绘制正弦曲线,进一步掌握Manim的动画制作技巧。
摘要由CSDN通过智能技术生成

Manim文档及源码笔记-CE文档-示例库5进阶项目

参考原文:
Manim Community Edition
Example Gallery

前言

笔记随想:
暂未发现官方中文版,自己实践代码的同时,顺便做翻译加深理解~
除了给出原文档中的展现效果及源码,我把实践代码过程的笔记也分享出来,另外最后设计出一些技能训练的内容,强化学校效果。行文格式:

示例-标号及名称
展现效果
操作拆解
源码直击
代码实践
技能训练

更新【new】:

  • 8月23日
    • “笔记随想”前后标注了“前言”与“正文”;
    • 本系列暂告段落,把“上一节”、“下一节”升级为系列目录,放到了文尾;
  • 后续更新备忘:文中“【建设中】”的内容;欢迎朋友们催更~

正文

本示例库包含一系列最佳实践代码片段及其相应的视频/图像输出,展示了整个库的不同功能。这些都是在麻省理工学院的许可下,所以请随意复制并粘贴到您的项目中。享受这Manim的味道!

进阶项目,Advanced Projects

示例1:OpeningManim

展现效果1

OpeningManim

示例1:OpeningManim

操作拆解1

现在开始把一些基础操作小模块,组合成为较为综合的中等模块,开始可以做更为炫酷的开场动画了~

源码直击1
%%manim -v WARNING -qm OpeningManim
from manim import *

class OpeningManim(Scene):
    def construct(self):
        title = Tex(r"This is some \LaTeX")
        basel = MathTex(r"\sum_{n=1}^\infty \frac{1}{n^2} = \frac{\pi^2}{6}")
        VGroup(title, basel).arrange(DOWN)
        self.play(
            Write(title),
            FadeIn(basel, shift=DOWN),
        )
        self.wait()

        transform_title = Tex("That was a transform")
        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("This is a grid", font_size=72)
        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"That was a non-linear function \\ applied to the grid"
        )
        grid_transform_title.move_to(grid_title, UL)
        grid.prepare_for_nonlinear_transform()
        self.play(
            grid.animate.apply_function(
                lambda p: p
                          + np.array(
                    [
                        np.sin(p[1]),
                        np.sin(p[0]),
                        0,
                    ]
                )
            ),
            run_time=3,
        )
        self.wait()
        self.play(Transform(grid_title, grid_transform_title))
        self.wait()

参考:TexMathTexWriteFadeInLaggedStartNumberPlaneCreate,prepare_for_nonlinear_transform()

代码实践1

代码实践1a
代码实践1b

技能训练1

单独整理到此【建设中】

示例2:SineCurveUnitCircle

展现效果2

SineCurveUnitCircle

示例2:SineCurveUnitCircle

操作拆解2

炫酷归炫酷,还是常用数学、物理模型更为实用些;
用单位圆画出正弦类曲线,有些回忆中的味道了~

源码直击2
%%manim -v WARNING -qm SineCurveUnitCircle
from manim import *

class SineCurveUnitCircle(Scene):
    # contributed by heejin_park, https://infograph.tistory.com/230
    def construct(self):
        self.show_axis()
        self.show_circle()
        self.move_dot_and_draw_curve()
        self.wait()

    def show_axis(self):
        x_start = np.array([-6,0,0])
        x_end = np.array([6,0,0])

        y_start = np.array([-4,-2,0])
        y_end = np.array([-4,2,0])

        x_axis = Line(x_start, x_end)
        y_axis = Line(y_start, y_end)

        self.add(x_axis, y_axis)
        self.add_x_labels()

        self.origin_point = np.array([-4,0,0])
        self.curve_start = np.array([-3,0,0])

    def add_x_labels(self):
        x_labels = [
            MathTex("\pi"), MathTex("2 \pi"),
            MathTex("3 \pi"), MathTex("4 \pi"),
        ]

        for i in range(len(x_labels)):
            x_labels[i].next_to(np.array([-1 + 2*i, 0, 0]), DOWN)
            self.add(x_labels[i])

    def show_circle(self):
        circle = Circle(radius=1)
        circle.move_to(self.origin_point)
        self.add(circle)
        self.circle = circle

    def move_dot_and_draw_curve(self):
        orbit = self.circle
        origin_point = self.origin_point

        dot = Dot(radius=0.08, color=YELLOW)
        dot.move_to(orbit.point_from_proportion(0))
        self.t_offset = 0
        rate = 0.25

        def go_around_circle(mob, dt):
            self.t_offset += (dt * rate)
            # print(self.t_offset)
            mob.move_to(orbit.point_from_proportion(self.t_offset % 1))

        def get_line_to_circle():
            return Line(origin_point, dot.get_center(), color=BLUE)

        def get_line_to_curve():
            x = self.curve_start[0] + self.t_offset * 4
            y = dot.get_center()[1]
            return Line(dot.get_center(), np.array([x,y,0]), color=YELLOW_A, stroke_width=2 )


        self.curve = VGroup()
        self.curve.add(Line(self.curve_start,self.curve_start))
        def get_curve():
            last_line = self.curve[-1]
            x = self.curve_start[0] + self.t_offset * 4
            y = dot.get_center()[1]
            new_line = Line(last_line.get_end(),np.array([x,y,0]), color=YELLOW_D)
            self.curve.add(new_line)

            return self.curve

        dot.add_updater(go_around_circle)

        origin_to_circle_line = always_redraw(get_line_to_circle)
        dot_to_curve_line = always_redraw(get_line_to_curve)
        sine_curve_line = always_redraw(get_curve)

        self.add(dot)
        self.add(orbit, origin_to_circle_line, dot_to_curve_line, sine_curve_line)
        self.wait(8.5)

        dot.remove_updater(go_around_circle)

参考:MathTexCircleDotLineVGroupalways_redraw()add_updater()remove_updater()

代码实践2

代码实践2a
代码实践2b
代码实践2c

技能训练2

单独整理到此【建设中】

本章结束

可以回到第一节,看看理解和技能是否更为深入和娴熟了~

本系列目录【new】

Manim文档及源码笔记-CE文档-示例库1基本概念
Manim文档及源码笔记-CE文档-示例库2动画
Manim文档及源码笔记-CE文档-示例库3使用Manim绘图
Manim文档及源码笔记-CE文档-示例库4特殊摄像机设置
Manim文档及源码笔记-CE文档-示例库5进阶项目【本文】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值