Skip to content

Commit f4c1c34

Browse files
feat(lib): improve wipe animation (#217)
* feat(lib): improve wipe animation Improve wipe transition by using FadeIn and FadeOut. The also allows to support introducers and removers. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 540c703 commit f4c1c34

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

manim_slides/manim.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"MANIMGL_IMPORTED",
1919
# Classes
2020
"AnimationGroup",
21+
"FadeIn",
22+
"FadeOut",
2123
"Mobject",
2224
"Scene",
2325
"ThreeDScene",
@@ -73,7 +75,16 @@ def suppress_stdout() -> Iterator[None]:
7375

7476

7577
if MANIMGL:
76-
from manimlib import LEFT, AnimationGroup, Mobject, Scene, ThreeDScene, config
78+
from manimlib import (
79+
LEFT,
80+
AnimationGroup,
81+
FadeIn,
82+
FadeOut,
83+
Mobject,
84+
Scene,
85+
ThreeDScene,
86+
config,
87+
)
7788
from manimlib.constants import FFMPEG_BIN
7889
from manimlib.logger import log as logger
7990

@@ -82,6 +93,8 @@ def suppress_stdout() -> Iterator[None]:
8293
from manim import (
8394
LEFT,
8495
AnimationGroup,
96+
FadeIn,
97+
FadeOut,
8598
Mobject,
8699
Scene,
87100
ThreeDScene,

manim_slides/slide.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import platform
33
import shutil
44
import subprocess
5-
from typing import Any, List, Optional, Sequence, Tuple
5+
from typing import Any, List, Mapping, Optional, Sequence, Tuple
66
from warnings import warn
77

88
import numpy as np
@@ -15,6 +15,8 @@
1515
LEFT,
1616
MANIMGL,
1717
AnimationGroup,
18+
FadeIn,
19+
FadeOut,
1820
Mobject,
1921
Scene,
2022
ThreeDScene,
@@ -39,7 +41,7 @@ def reverse_video_file(src: str, dst: str) -> None:
3941

4042
class Slide(Scene): # type:ignore
4143
"""
42-
Inherits from :class:`manim.scene.scene.Scene` or :class:`manimlib.scene.scene.Scene` and provide necessary tools for slides rendering.
44+
Inherits from :class:`Scene<manim.scene.scene.Scene>` and provide necessary tools for slides rendering.
4345
"""
4446

4547
def __init__(
@@ -253,14 +255,14 @@ def start_loop(self) -> None:
253255
254256
class LoopExample(Slide):
255257
def construct(self):
256-
dot = Dot(color=BLUE)
258+
dot = Dot(color=BLUE, radius=1)
257259
258260
self.play(FadeIn(dot))
259261
self.next_slide()
260262
261263
self.start_loop()
262264
263-
self.play(Indicate(dot))
265+
self.play(Indicate(dot, scale_factor=2))
264266
265267
self.end_loop()
266268
@@ -398,6 +400,8 @@ def wipe(
398400
current: Sequence[Mobject] = [],
399401
future: Sequence[Mobject] = [],
400402
direction: np.ndarray = LEFT,
403+
fade_in_kwargs: Mapping[str, Any] = {},
404+
fade_out_kwargs: Mapping[str, Any] = {},
401405
**kwargs: Any,
402406
) -> AnimationGroup:
403407
"""
@@ -406,7 +410,13 @@ def wipe(
406410
407411
:param current: A sequence of mobjects to remove from the scene.
408412
:param future: A sequence of mobjects to add to the scene.
409-
:direction: The wipe direction.
413+
:param direction: The wipe direction.
414+
:param fade_in_kwargs: Keyword arguments passed to
415+
:class:`FadeIn<manim.animation.fading.FadeIn>`.
416+
:param fade_out_kwargs: Keyword arguments passed to
417+
:class:`FadeOut<manim.animation.fading.FadeOut>`.
418+
:param kwargs: Keyword arguments passed to
419+
:class:`AnimationGroup<manim.animation.composition.AnimationGroup>`.
410420
411421
Examples
412422
--------
@@ -421,29 +431,37 @@ def construct(self):
421431
circle = Circle(radius=3, color=BLUE)
422432
square = Square()
423433
text = Text("This is a wipe example").next_to(square, DOWN)
434+
beautiful = Text("Beautiful, no?")
424435
425436
self.play(Create(circle))
426437
self.next_slide()
427438
428439
self.play(self.wipe(circle, Group(square, text)))
440+
self.next_slide()
441+
442+
self.play(self.wipe(Group(square, text), beautiful, direction=UP))
443+
self.next_slide()
444+
445+
self.play(self.wipe(beautiful, circle, direction=DOWN + RIGHT))
429446
"""
430447
shift_amount = np.asarray(direction) * np.array(
431448
[self.__frame_width, self.__frame_height, 0.0]
432449
)
433450

451+
animations = []
452+
434453
for mobject in future:
435-
mobject.shift(-shift_amount)
454+
animations.append(FadeIn(mobject, shift=shift_amount, **fade_in_kwargs))
436455

437-
animations = [
438-
mobject.animate.shift(shift_amount) for mobject in [*current, *future]
439-
]
456+
for mobject in current:
457+
animations.append(FadeOut(mobject, shift=shift_amount, **fade_out_kwargs))
440458

441459
return AnimationGroup(*animations, **kwargs)
442460

443461

444462
class ThreeDSlide(Slide, ThreeDScene): # type: ignore
445463
"""
446-
Inherits from :class:`Slide` and :class:`manim.scene.three_d_scene.ThreeDScene` or :class:`manimlib.scene.three_d_scene.ThreeDScene` and provide necessary tools for slides rendering.
464+
Inherits from :class:`Slide` and :class:`ThreeDScene<manim.scene.three_d_scene.ThreeDScene>` and provide necessary tools for slides rendering.
447465
448466
.. note:: ManimGL does not need ThreeDScene for 3D rendering in recent versions, see `example.py`.
449467
"""

0 commit comments

Comments
 (0)