Skip to content

Commit

Permalink
feat: ApplyWave & WiggleOutThenIn
Browse files Browse the repository at this point in the history
  • Loading branch information
jkjkil4 committed May 27, 2024
1 parent 8630522 commit cc25699
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 9 deletions.
Binary file added doc/source/_static/videos/ApplyWaveExample.mp4
Binary file not shown.
Binary file not shown.
1 change: 0 additions & 1 deletion doc/source/autodoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def _generate_autodoc_file(module_path: str, filename: str, rst_path: str) -> st
name = filename[:-3]
module_name = f'{module_path}.{name}'
if module_name in generate_autodoc_exclude:
print(module_name)
return None

rst_file_path = os.path.join(rst_path, f'{name}.rst')
Expand Down
45 changes: 42 additions & 3 deletions doc/source/janim/anims/indication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,46 @@ indication

self.forward(0.3)

.. # TODO: ApplyWave
.. # TODO: WiggleOutThenIn
.. # TODO: TurnInsideOut
.. autoclass:: janim.anims.indication.ApplyWave
:show-inheritance:

.. janim-example:: ApplyWaveExample
:media: ../../_static/videos/ApplyWaveExample.mp4

from janim.imports import *

class ApplyWaveExample(Timeline):
def construct(self):
group = Group(
Square(fill_alpha=0.5),
Circle(fill_alpha=0.5),
Text('Text', font_size=48),
color=BLUE
).show()
group.points.scale(1.5).arrange(RIGHT, buff=2)

self.play(*map(ApplyWave, group))
self.forward()
.. autoclass:: janim.anims.indication.WiggleOutThenIn
:show-inheritance:

.. janim-example:: WiggleOutThenInExample
:media: ../../_static/videos/WiggleOutThenInExample.mp4

from janim.imports import *

class WiggleOutThenInExample(Timeline):
def construct(self) -> None:
group = Group(
Square(fill_alpha=0.5),
Circle(fill_alpha=0.5),
Text('Text', font_size=48),
color=BLUE
).show()
group.points.scale(1.5).arrange(RIGHT, buff=2)

self.play(*map(WiggleOutThenIn, group))
self.forward()
.. # TODO: FlashyFadeIn
1 change: 1 addition & 0 deletions doc/source/janim/anims/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ anims
fading
growing
indication
movement
rotation
timeline
transform
Expand Down
8 changes: 8 additions & 0 deletions doc/source/janim/anims/movement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
movement
========

.. automodule:: janim.anims.movement
:members:
:undoc-members:
:show-inheritance:

92 changes: 87 additions & 5 deletions janim/anims/indication.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from __future__ import annotations

import math

import numpy as np

from janim.anims.animation import Animation
from janim.anims.composition import AnimGroup, Succession
from janim.anims.creation import Create, ShowPartial
from janim.anims.fading import FadeOut
from janim.anims.movement import Homotopy
from janim.anims.updater import DataUpdater, UpdaterParams
from janim.components.rgbas import Cmpt_Rgbas
from janim.constants import (C_LABEL_ANIM_ABSTRACT, C_LABEL_ANIM_INDICATION,
GREY, ORIGIN, RIGHT, TAU, YELLOW)
GREY, ORIGIN, RIGHT, TAU, UP, YELLOW)
from janim.items.geometry.arc import Circle, Dot
from janim.items.geometry.line import Line
from janim.items.item import Item
Expand All @@ -19,7 +22,7 @@
from janim.typing import JAnimColor, Vect
from janim.utils.bezier import interpolate
from janim.utils.config import Config
from janim.utils.rate_functions import RateFunc, smooth, there_and_back
from janim.utils.rate_functions import RateFunc, smooth, there_and_back, wiggle


class FocusOn(DataUpdater[Dot]):
Expand Down Expand Up @@ -324,7 +327,86 @@ def create_lines(self) -> Group:
return lines


# TODO: ApplyWave
# TODO: WiggleOutThenIn
# TODO: TurnInsideOut
class ApplyWave(Homotopy):
label_color = C_LABEL_ANIM_INDICATION

def __init__(
self,
item: Item,
direction: Vect = UP,
amplitude: float = 0.2,
*,
duration: float = 1.0,
root_only: bool = False,
**kwargs
):
if root_only:
box = item(Points).points.self_box
else:
box = item(Points).points.box

left_x = box.left[0]
right_x = box.right[0]
vect = amplitude * direction

def homotopy(x, y, z, t):
alpha = (x - left_x) / (right_x - left_x)
power = math.exp(2.0 * (alpha - 0.5))
nudge = there_and_back(t**power)
return np.array([x, y, z]) + nudge * vect

super().__init__(
item,
homotopy,
duration=duration,
**kwargs
)


class WiggleOutThenIn(DataUpdater):
label_color = C_LABEL_ANIM_INDICATION

def __init__(
self,
item: Item,
*,
scale: float = 1.1,
angle: float = 0.01 * TAU,
n_wiggles: int = 6,
scale_about_point: Vect | None = None,
rotate_about_point: Vect | None = None,
duration: float = 2,
root_only: bool = False,
**kwargs
):
if root_only:
box = item(Points).points.self_box
else:
box = item(Points).points.box
self.scale = scale
self.angle = angle
self.n_wiggles = n_wiggles
self.scale_about_point = scale_about_point or box.center
self.rotate_about_point = rotate_about_point or box.center
super().__init__(
item,
self.updater,
duration=duration,
root_only=root_only,
**kwargs
)

def updater(self, data: Item, p: UpdaterParams) -> None:
if not isinstance(data, Points):
return
data.points.scale(
interpolate(1, self.scale, there_and_back(p.alpha)),
about_point=self.scale_about_point
)
data.points.rotate(
wiggle(p.alpha, self.n_wiggles) * self.angle,
about_point=self.rotate_about_point
)


# TODO: FlashyFadeIn
3 changes: 3 additions & 0 deletions janim/anims/movement.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np

from janim.anims.updater import DataUpdater, UpdaterParams
from janim.constants import C_LABEL_ANIM_STAY
from janim.items.item import Item
from janim.items.points import Points
from janim.items.vitem import VItem
Expand Down Expand Up @@ -66,6 +67,8 @@ def homotopy(x, y, z, t):


class MoveAlongPath(DataUpdater):
label_color = C_LABEL_ANIM_STAY

def __init__(
self,
item: Item,
Expand Down

0 comments on commit cc25699

Please sign in to comment.