Examples¶
Contents of example.py
.
Do not forget to import Manim Slides and Manim or ManimGL:
from manim import *
from manim_slides import Slide, ThreeDSlide
or
from manimlib import *
from manim_slides import Slide, ThreeDSlide
Then, each presentation, named SCENE
, was generated with those two commands:
manim example.py SCENE # or manimgl example SCENE
manim-slides convert SCENE scene.html -ccontrols=true
where -ccontrols=true
indicates that we want to display the blue navigation arrows.
Basic Example¶
Basic example from quickstart.
1class BasicExample(Slide):
2 def construct(self):
3 circle = Circle(radius=3, color=BLUE)
4 dot = Dot()
5
6 self.play(GrowFromCenter(circle))
7
8 self.next_slide(loop=True)
9 self.play(MoveAlongPath(dot, circle), run_time=2, rate_func=linear)
10 self.next_slide()
11
12 self.play(dot.animate.move_to(ORIGIN))
3D Example¶
Example using 3D camera. As Manim and ManimGL handle 3D differently, definitions are slightly different.
With Manim¶
1class ThreeDExample(ThreeDSlide):
2 def construct(self):
3 axes = ThreeDAxes()
4 circle = Circle(radius=3, color=BLUE)
5 dot = Dot(color=RED)
6
7 self.add(axes)
8
9 self.set_camera_orientation(phi=75 * DEGREES, theta=30 * DEGREES)
10
11 self.play(GrowFromCenter(circle))
12 self.begin_ambient_camera_rotation(rate=75 * DEGREES / 4)
13
14 self.next_slide()
15
16 self.next_slide(loop=True)
17 self.play(MoveAlongPath(dot, circle), run_time=4, rate_func=linear)
18 self.next_slide()
19
20 self.stop_ambient_camera_rotation()
21 self.move_camera(phi=75 * DEGREES, theta=30 * DEGREES)
22
23 self.play(dot.animate.move_to(ORIGIN))
24 self.next_slide()
25
26 self.play(dot.animate.move_to(RIGHT * 3))
27 self.next_slide()
28
29 self.next_slide(loop=True)
30 self.play(MoveAlongPath(dot, circle), run_time=2, rate_func=linear)
31 self.next_slide()
32
33 self.play(dot.animate.move_to(ORIGIN))
34
With ManimGL¶
1# WARNING: 3b1b's manim change how ThreeDScene work,
2# this is why things have to be managed differently.
3class ThreeDExample(ThreeDSlide):
4 def construct(self):
5 axes = ThreeDAxes()
6 circle = Circle(radius=3, color=BLUE)
7 dot = Dot(color=RED)
8
9 self.add(axes)
10
11 frame = self.camera.frame
12 frame.set_euler_angles(
13 theta=30 * DEGREES,
14 phi=75 * DEGREES,
15 )
16
17 self.play(GrowFromCenter(circle))
18
19 def updater(m, dt):
20 return m.increment_theta((75 * DEGREES / 4) * dt)
21
22 frame.add_updater(updater)
23
24 self.next_slide()
25
26 self.next_slide(loop=True)
27 self.play(MoveAlongPath(dot, circle), run_time=4, rate_func=linear)
28 self.next_slide()
29
30 frame.remove_updater(updater)
31 self.play(frame.animate.set_theta(30 * DEGREES))
32 self.play(dot.animate.move_to(ORIGIN))
33 self.next_slide()
34
35 self.play(dot.animate.move_to(RIGHT * 3))
36 self.next_slide()
37
38 self.next_slide(loop=True)
39 self.play(MoveAlongPath(dot, circle), run_time=2, rate_func=linear)
40 self.next_slide()
41
42 self.play(dot.animate.move_to(ORIGIN))
43
Subclass Custom Scenes¶
For compatibility reasons, Manim Slides only provides subclasses for
Scene
and ThreeDScene
.
However, subclassing other scene classes is totally possible,
and very simple to do actually!
For example,
you can subclass the MovingCameraScene
class from manim
with the following code:
1from manim import *
2from manim_slides import Slide
3
4
5class MovingCameraSlide(Slide, MovingCameraScene):
6 pass
And later use this class anywhere in your code:
1class SubclassExample(MovingCameraSlide):
2 """Example taken from ManimCE's docs."""
3
4 def construct(self):
5 self.camera.frame.save_state()
6
7 ax = Axes(x_range=[-1, 10], y_range=[-1, 10])
8 graph = ax.plot(lambda x: np.sin(x), color=WHITE, x_range=[0, 3 * PI])
9
10 dot_1 = Dot(ax.i2gp(graph.t_min, graph))
11 dot_2 = Dot(ax.i2gp(graph.t_max, graph))
12 self.add(ax, graph, dot_1, dot_2)
13
14 self.play(self.camera.frame.animate.scale(0.5).move_to(dot_1))
15 self.next_slide()
16 self.play(self.camera.frame.animate.move_to(dot_2))
17 self.next_slide()
18 self.play(Restore(self.camera.frame))
19 self.wait()
Note
If you do not plan to reuse MovingCameraSlide
more than once, then you can
directly write the construct
method in the body of MovingCameraSlide
.
Advanced Example¶
A more advanced example is ConvertExample
, which is used as demo slide and tutorial.
1class ConvertExample(Slide):
2 """WARNING: this example does not seem to work with ManimGL."""
3
4 def construct(self):
5 self.wait_time_between_slides = 0.1
6
7 title = VGroup(
8 Text("From Manim animations", t2c={"From": BLUE}),
9 Text("to slides presentation", t2c={"to": BLUE}),
10 Text("with Manim Slides", t2w={"[-12:]": BOLD}, t2c={"[-13:]": YELLOW}),
11 ).arrange(DOWN)
12
13 step_1 = Text("1. In your scenes file, import Manim Slides")
14 step_2 = Text("2. Replace Scene with Slide")
15 step_3 = Text("3. In construct, add pauses where you need")
16 step_4 = Text("4. You can also create loops")
17 step_5 = Text("5. Render your scene with Manim")
18 step_6 = Text("6. Open your presentation with Manim Slides")
19
20 for step in [step_1, step_2, step_3, step_4, step_5, step_6]:
21 step.scale(0.5).to_corner(UL)
22
23 step = step_1
24
25 self.play(FadeIn(title))
26
27 self.next_slide()
28
29 code = Code(
30 code="""from manim import *
31
32
33class Example(Scene):
34 def construct(self):
35 dot = Dot()
36 self.add(dot)
37
38 self.play(Indicate(dot, scale_factor=2))
39
40 square = Square()
41 self.play(Transform(dot, square))
42
43 self.play(Rotate(square, angle=PI/2))
44""",
45 language="python",
46 )
47
48 code_step_1 = Code(
49 code="""from manim import *
50from manim_slides import Slide
51
52class Example(Scene):
53 def construct(self):
54 dot = Dot()
55 self.add(dot)
56
57 self.play(Indicate(dot, scale_factor=2))
58
59 square = Square()
60 self.play(Transform(dot, square))
61
62 self.play(Rotate(square, angle=PI/2))
63""",
64 language="python",
65 )
66
67 code_step_2 = Code(
68 code="""from manim import *
69from manim_slides import Slide
70
71class Example(Slide):
72 def construct(self):
73 dot = Dot()
74 self.add(dot)
75
76 self.play(Indicate(dot, scale_factor=2))
77
78 square = Square()
79 self.play(Transform(dot, square))
80
81 self.play(Rotate(square, angle=PI/2))
82""",
83 language="python",
84 )
85
86 code_step_3 = Code(
87 code="""from manim import *
88from manim_slides import Slide
89
90class Example(Slide):
91 def construct(self):
92 dot = Dot()
93 self.add(dot)
94
95 self.play(Indicate(dot, scale_factor=2))
96 self.next_slide()
97 square = Square()
98 self.play(Transform(dot, square))
99 self.next_slide()
100 self.play(Rotate(square, angle=PI/2))
101""",
102 language="python",
103 )
104
105 code_step_4 = Code(
106 code="""from manim import *
107from manim_slides import Slide
108
109class Example(Slide):
110 def construct(self):
111 dot = Dot()
112 self.add(dot)
113 self.next_slide(loop=True)
114 self.play(Indicate(dot, scale_factor=2))
115 self.next_slide()
116 square = Square()
117 self.play(Transform(dot, square))
118 self.next_slide()
119 self.play(Rotate(square, angle=PI/2))
120""",
121 language="python",
122 )
123
124 code_step_5 = Code(
125 code="manim-slide render example.py Example",
126 language="console",
127 )
128
129 code_step_6 = Code(
130 code="manim-slides Example",
131 language="console",
132 )
133
134 or_text = Text("or generate HTML presentation").scale(0.5)
135
136 code_step_7 = Code(
137 code="manim-slides convert Example slides.html --open",
138 language="console",
139 ).shift(DOWN)
140
141 self.wipe(title, code)
142 self.next_slide()
143
144 self.play(FadeIn(step, shift=RIGHT))
145 self.play(Transform(code, code_step_1))
146 self.next_slide()
147
148 self.play(Transform(step, step_2))
149 self.play(Transform(code, code_step_2))
150 self.next_slide()
151
152 self.play(Transform(step, step_3))
153 self.play(Transform(code, code_step_3))
154 self.next_slide()
155
156 self.play(Transform(step, step_4))
157 self.play(Transform(code, code_step_4))
158 self.next_slide()
159
160 self.play(Transform(step, step_5))
161 self.play(Transform(code, code_step_5))
162 self.next_slide(auto_next=True)
163
164 self.play(Transform(step, step_6))
165 self.play(Transform(code, code_step_6))
166 self.play(code.animate.shift(UP), FadeIn(code_step_7), FadeIn(or_text))
167
168 watch_text = Text("Watch results on next slides!").shift(2 * DOWN).scale(0.5)
169
170 self.next_slide(loop=True)
171 self.play(FadeIn(watch_text))
172 self.play(FadeOut(watch_text))
173 self.next_slide()
174 self.clear()
175
176 dot = Dot()
177 self.add(dot)
178 self.next_slide(loop=True)
179 self.play(Indicate(dot, scale_factor=2))
180 self.next_slide()
181 square = Square()
182 self.play(Transform(dot, square))
183 self.remove(dot)
184 self.add(square)
185 self.next_slide()
186 self.play(Rotate(square, angle=PI / 4))
187 self.next_slide()
188
189 learn_more_text = (
190 VGroup(
191 Text("Learn more about Manim Slides:"),
192 Text("https://github.com/jeertmans/manim-slides", color=YELLOW),
193 )
194 .arrange(DOWN)
195 .scale(0.75)
196 )
197
198 self.play(Transform(square, learn_more_text))