Skip to content

Commit

Permalink
feat(lib): improve wipe animation (#217)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
jeertmans and pre-commit-ci[bot] committed Jul 20, 2023
1 parent 540c703 commit f4c1c34
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
15 changes: 14 additions & 1 deletion manim_slides/manim.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"MANIMGL_IMPORTED",
# Classes
"AnimationGroup",
"FadeIn",
"FadeOut",
"Mobject",
"Scene",
"ThreeDScene",
Expand Down Expand Up @@ -73,7 +75,16 @@ def suppress_stdout() -> Iterator[None]:


if MANIMGL:
from manimlib import LEFT, AnimationGroup, Mobject, Scene, ThreeDScene, config
from manimlib import (
LEFT,
AnimationGroup,
FadeIn,
FadeOut,
Mobject,
Scene,
ThreeDScene,
config,
)
from manimlib.constants import FFMPEG_BIN
from manimlib.logger import log as logger

Expand All @@ -82,6 +93,8 @@ def suppress_stdout() -> Iterator[None]:
from manim import (
LEFT,
AnimationGroup,
FadeIn,
FadeOut,
Mobject,
Scene,
ThreeDScene,
Expand Down
38 changes: 28 additions & 10 deletions manim_slides/slide.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import platform
import shutil
import subprocess
from typing import Any, List, Optional, Sequence, Tuple
from typing import Any, List, Mapping, Optional, Sequence, Tuple
from warnings import warn

import numpy as np
Expand All @@ -15,6 +15,8 @@
LEFT,
MANIMGL,
AnimationGroup,
FadeIn,
FadeOut,
Mobject,
Scene,
ThreeDScene,
Expand All @@ -39,7 +41,7 @@ def reverse_video_file(src: str, dst: str) -> None:

class Slide(Scene): # type:ignore
"""
Inherits from :class:`manim.scene.scene.Scene` or :class:`manimlib.scene.scene.Scene` and provide necessary tools for slides rendering.
Inherits from :class:`Scene<manim.scene.scene.Scene>` and provide necessary tools for slides rendering.
"""

def __init__(
Expand Down Expand Up @@ -253,14 +255,14 @@ def start_loop(self) -> None:
class LoopExample(Slide):
def construct(self):
dot = Dot(color=BLUE)
dot = Dot(color=BLUE, radius=1)
self.play(FadeIn(dot))
self.next_slide()
self.start_loop()
self.play(Indicate(dot))
self.play(Indicate(dot, scale_factor=2))
self.end_loop()
Expand Down Expand Up @@ -398,6 +400,8 @@ def wipe(
current: Sequence[Mobject] = [],
future: Sequence[Mobject] = [],
direction: np.ndarray = LEFT,
fade_in_kwargs: Mapping[str, Any] = {},
fade_out_kwargs: Mapping[str, Any] = {},
**kwargs: Any,
) -> AnimationGroup:
"""
Expand All @@ -406,7 +410,13 @@ def wipe(
:param current: A sequence of mobjects to remove from the scene.
:param future: A sequence of mobjects to add to the scene.
:direction: The wipe direction.
:param direction: The wipe direction.
:param fade_in_kwargs: Keyword arguments passed to
:class:`FadeIn<manim.animation.fading.FadeIn>`.
:param fade_out_kwargs: Keyword arguments passed to
:class:`FadeOut<manim.animation.fading.FadeOut>`.
:param kwargs: Keyword arguments passed to
:class:`AnimationGroup<manim.animation.composition.AnimationGroup>`.
Examples
--------
Expand All @@ -421,29 +431,37 @@ def construct(self):
circle = Circle(radius=3, color=BLUE)
square = Square()
text = Text("This is a wipe example").next_to(square, DOWN)
beautiful = Text("Beautiful, no?")
self.play(Create(circle))
self.next_slide()
self.play(self.wipe(circle, Group(square, text)))
self.next_slide()
self.play(self.wipe(Group(square, text), beautiful, direction=UP))
self.next_slide()
self.play(self.wipe(beautiful, circle, direction=DOWN + RIGHT))
"""
shift_amount = np.asarray(direction) * np.array(
[self.__frame_width, self.__frame_height, 0.0]
)

animations = []

for mobject in future:
mobject.shift(-shift_amount)
animations.append(FadeIn(mobject, shift=shift_amount, **fade_in_kwargs))

animations = [
mobject.animate.shift(shift_amount) for mobject in [*current, *future]
]
for mobject in current:
animations.append(FadeOut(mobject, shift=shift_amount, **fade_out_kwargs))

return AnimationGroup(*animations, **kwargs)


class ThreeDSlide(Slide, ThreeDScene): # type: ignore
"""
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.
Inherits from :class:`Slide` and :class:`ThreeDScene<manim.scene.three_d_scene.ThreeDScene>` and provide necessary tools for slides rendering.
.. note:: ManimGL does not need ThreeDScene for 3D rendering in recent versions, see `example.py`.
"""
Expand Down

0 comments on commit f4c1c34

Please sign in to comment.