mirror of
https://github.com/ManimCommunity/manim.git
synced 2026-06-22 10:01:47 +00:00
Add SpiralIn Animation (#2404)
* Added a cool animation. * Added Spiral Effect * Added SpiralIn to logo.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Made a better example to show how to use SpiralIn. * Added docstring * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Removed SpiralInExample.py, and moved my example into basic.py with its friends. * Update manim/animation/creation.py Gotcha, yeah. I like this. Co-authored-by: GameDungeon <60719255+GameDungeon@users.noreply.github.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fixing docstring * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update manim/animation/creation.py Co-authored-by: Darylgolden <darylgolden@gmail.com> * imported TAU from constants.py. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Got rid of redundant, weird factor of 8 multiplication which was confusing. Now default value is 8. * Added the ability to modify the opacity effect at the beginning. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Something is still wrong with opacity in this commit, but it's a step forward... * Fixed the opacity issues. Seems I have to re-save the mobject state immediately after restoring it every time. * Added fade_in_fraction to basic.py so people learning how to use it can understand the usage a bit better. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: danielkwalsh <dan@crossnokaye.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: GameDungeon <60719255+GameDungeon@users.noreply.github.com> Co-authored-by: Darylgolden <darylgolden@gmail.com>
This commit is contained in:
parent
8e6a4c48ac
commit
0bc593a8a0
3 changed files with 97 additions and 27 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue