Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Add SpiralIn Animation (#2404)

Dan Walsh0bc593a8a07e
diff --git a/example_scenes/basic.py b/example_scenes/basic.py
index 57ea69fc2..f7257ab8f 100644
--- a/example_scenes/basic.py
+++ b/example_scenes/basic.py
@@ -130,4 +130,35 @@ class UpdatersExample(Scene):
self.wait()
+class SpiralInExample(Scene):
+ def construct(self):
+ logo_green = "#81b29a"
+ logo_blue = "#454866"
+ logo_red = "#e07a5f"
+
+ font_color = "#ece6e2"
+
+ pi = MathTex(r"\pi").scale(7).set_color(font_color)
+ pi.shift(2.25 * LEFT + 1.5 * UP)
+
+ circle = Circle(color=logo_green, fill_opacity=0.7, stroke_width=0).shift(LEFT)
+ square = Square(color=logo_blue, fill_opacity=0.8, stroke_width=0).shift(UP)
+ triangle = Triangle(color=logo_red, fill_opacity=0.9, stroke_width=0).shift(
+ RIGHT
+ )
+ pentagon = Polygon(
+ *[
+ [np.cos(2 * np.pi / 5 * i), np.sin(2 * np.pi / 5 * i), 0]
+ for i in range(5)
+ ],
+ color=PURPLE_B,
+ fill_opacity=1,
+ stroke_width=0
+ ).shift(UP + 2 * RIGHT)
+ shapes = VGroup(triangle, square, circle, pentagon, pi)
+ self.play(SpiralIn(shapes, fade_in_fraction=0.9))
+ self.wait()
+ self.play(FadeOut(shapes))
+
+
# See many more examples at https://docs.manim.community/en/stable/examples.html
diff --git a/manim/animation/creation.py b/manim/animation/creation.py
index 7b413c97a..1b2eddda7 100644
--- a/manim/animation/creation.py
+++ b/manim/animation/creation.py
@@ -65,6 +65,7 @@ __all__ = [
"Unwrite",
"ShowPartial",
"ShowIncreasingSubsets",
+ "SpiralIn",
"AddTextLetterByLetter",
"ShowSubmobjectsOneByOne",
"AddTextWordByWord",
@@ -85,6 +86,7 @@ from manim.mobject.opengl.opengl_vectorized_mobject import OpenGLVMobject
from ..animation.animation import Animation
from ..animation.composition import Succession
+from ..constants import TAU
from ..mobject.mobject import Group, Mobject
from ..mobject.types.vectorized_mobject import VMobject
from ..utils.bezier import integer_interpolate
@@ -409,6 +411,68 @@ class Unwrite(Write):
)
+class SpiralIn(Animation):
+ r"""Create the Mobject with sub-Mobjects flying in on spiral trajectories.
+
+ Parameters
+ ----------
+ shapes
+ The Mobject on which to be operated.
+
+ scale_factor
+ The factor used for scaling the effect.
+
+ fade_in_fraction
+ Fractional duration of initial fade-in of sub-Mobjects as they fly inward.
+
+ Examples
+ --------
+ .. manim :: SpiralInExample
+
+ class SpiralInExample(Scene):
+ def construct(self):
+ pi = MathTex(r"\pi").scale(7)
+ pi.shift(2.25 * LEFT + 1.5 * UP)
+ circle = Circle(color=GREEN_C, fill_opacity=1).shift(LEFT)
+ square = Square(color=BLUE_D, fill_opacity=1).shift(UP)
+ shapes = VGroup(pi, circle, square)
+ self.play(SpiralIn(shapes))
+ """
+
+ def __init__(
+ self,
+ shapes: Mobject,
+ scale_factor: float = 8,
+ fade_in_fraction=0.3,
+ **kwargs,
+ ) -> None:
+ self.shapes = shapes
+ self.scale_factor = scale_factor
+ self.shape_center = shapes.get_center()
+ self.fade_in_fraction = fade_in_fraction
+ for shape in shapes:
+ shape.final_position = shape.get_center()
+ shape.initial_position = (
+ shape.final_position
+ + (shape.final_position - self.shape_center) * self.scale_factor
+ )
+ shape.move_to(shape.initial_position)
+ shape.save_state()
+
+ super().__init__(shapes, **kwargs)
+
+ def interpolate_mobject(self, alpha: float) -> None:
+ for shape in self.shapes:
+ shape.restore()
+ shape.save_state()
+ opacity = shape.get_fill_opacity()
+ new_opacity = min(opacity, alpha * opacity / self.fade_in_fraction)
+ shape.shift((shape.final_position - shape.initial_position) * alpha)
+ shape.rotate(TAU * alpha, about_point=self.shape_center)
+ shape.rotate(-TAU * alpha, about_point=shape.get_center_of_mass())
+ shape.set_opacity(new_opacity)
+
+
class ShowIncreasingSubsets(Animation):
"""Show one submobject at a time, leaving all previous ones displayed on screen.
diff --git a/manim/mobject/logo.py b/manim/mobject/logo.py
index 3441e89c1..67339d47b 100644
--- a/manim/mobject/logo.py
+++ b/manim/mobject/logo.py
@@ -11,7 +11,7 @@ from manim.mobject.text.tex_mobject import MathTex, Tex
from ..animation.animation import override_animation
from ..animation.composition import AnimationGroup, Succession
-from ..animation.creation import Create
+from ..animation.creation import Create, SpiralIn
from ..animation.fading import FadeIn
from ..constants import DOWN, LEFT, ORIGIN, RIGHT, TAU, UP
from ..mobject.types.vectorized_mobject import VGroup
@@ -127,33 +127,8 @@ class ManimBanner(VGroup):
:class:`~.AnimationGroup`
An animation to be used in a :meth:`.Scene.play` call.
"""
- shape_center = self.shapes.get_center()
- expansion_factor = 8 * self.scale_factor
-
- for shape in self.shapes:
- shape.final_position = shape.get_center()
- shape.initial_position = (
- shape.final_position
- + (shape.final_position - shape_center) * expansion_factor
- )
- shape.move_to(shape.initial_position)
- shape.save_state()
-
- def spiral_updater(shapes, alpha):
- for shape in shapes:
- shape.restore()
- shape.shift((shape.final_position - shape.initial_position) * alpha)
- shape.rotate(TAU * alpha, about_point=shape_center)
- shape.rotate(-TAU * alpha, about_point=shape.get_center_of_mass())
- shape.set_opacity(min(1, alpha * 3))
-
return AnimationGroup(
- UpdateFromAlphaFunc(
- self.shapes,
- spiral_updater,
- run_time=run_time,
- rate_func=ease_out_sine,
- ),
+ SpiralIn(self.shapes),
FadeIn(self.M, run_time=run_time / 2),
lag_ratio=0.1,
)