composition¶
- class janim.anims.composition.AnimGroup(*anims: ~janim.typing.SupportsAnim, at: float = 0, duration: float | None = None, lag_ratio: float = 0, offset: float = 0, rate_func: RateFunc = <function linear>, name: str | None = None, collapse: bool = False)¶
Bases:
AnimationCollection of animations (executed in parallel)
If
durationis not provided, it takes the termination time (the maximum end time of child animations) as thedurationof this animation groupIf
durationis provided, it stretches the time of child animations to make the overall end time consistent withdurationYou can also use
atfor an overall offset (e.g., ifat=1, it delays the entire animation by 1s)
You can use
lag_ratioandoffsetto control the timing position of each sub-animation relative to the previous one:lag_ratioindicates “the percentage of completion of the previous animation at which the next animation starts”offsetindicates “the number of seconds after the previous animation starts when the next animation starts”
Time Examples:
AnimGroup( Anim1(duration=3), # 0~3s Anim2(duration=4) # 0~4s ) AnimGroup( Anim1(duration=3), # 0~4.5s Anim2(duration=4), # 0~6s duration=6 ) AnimGroup( Anim1(duration=3), # 1~5.5s Anim2(duration=4), # 1~7s at=1, duration=6 )
In addition,
collapseindicates whether to collapse this animation group in the preview interface (default is not collapsed, while for exampleTransformMatchingShapesis collapsed by default)An interactive example of
lag_ratioandoffset:00Anim1 1sAnim2 1sAnim3 2sAnim4 1sAnimGroupExample ¶
from janim.imports import * class AnimGroupExample(Timeline): def construct(self): group = Group( Circle(fill_alpha=0.5), Square(fill_alpha=0.5), Text('Text', font_size=48), color=BLUE ) group.points.arrange(buff=LARGE_BUFF) self.forward() self.play( FadeIn(group[0]), AnimGroup( FadeIn(group[1]), FadeIn(group[2]), duration=2 ) ) self.forward() self.hide(group) self.play( FadeIn(group[0], duration=2), AnimGroup( FadeIn(group[1]), FadeIn(group[2]), at=1, duration=2 ) ) self.forward()
Note
For better understanding the effect of these animation compositions, you can copy these code into your file and execute, so you can see the ranges of animations on the GUI.
- class janim.anims.composition.Succession(*anims: SupportsAnim, lag_ratio: float = 1, offset: float = 0, **kwargs)¶
Bases:
AnimGroupCollection of animations (executed sequentially)
Executes the passed animations in sequence
Equivalent to
AnimGroupwith the default valuelag_ratio=1
Time Examples:
Succession( Anim1(duration=3), # 0~3s Anim2(duration=4) # 3~7s ) Succession( Anim1(duration=2), # 0~2s Anim2(at=1, duration=2), # 3~5s Anim3(at=0.5, duration=2) # 5.5~7.5s ) Succession( Anim1(duration=2), # 0~2s Anim2(duration=2), # 2.5~4.5s Anim3(duration=2), # 5~7s offset=0.5 )
An interactive example of
lag_ratioandoffset:00Anim1 1sAnim2 1sAnim3 2sAnim4 1sSuccessionExample ¶
from janim.imports import * class SuccessionExample(Timeline): def construct(self): group = Group( Circle(fill_alpha=0.5), Square(fill_alpha=0.5), Text('Text', font_size=48), color=BLUE ) group.points.arrange(buff=LARGE_BUFF) self.forward() self.play( Succession( *map(FadeIn, group) ) ) self.forward() self.hide(group) self.play( Succession( *map(FadeIn, group), offset=1 ) ) self.forward() self.hide(group) self.play( Succession( *map(FadeIn, group), offset=-0.7 ) ) self.forward()
- class janim.anims.composition.Aligned(*anims: ~janim.typing.SupportsAnim, at: float = 0, duration: float | None = None, rate_func: RateFunc = <function linear>, name: str | None = None, collapse: bool = False)¶
Bases:
AnimGroupCollection of animations (executed in parallel and aligned)
In other words, it ignores the
atanddurationof sub-animations, causing all sub-animations to start and end together.Time Examples:
Aligned( Anim1(duration=1), Anim2(duration=2) ) # Anim1 & Anim2: 0~2s Aligned( Anim1(at=1, duration=1), Anim2(duration=2), duration=4 ) # Anim1 & Anim2: 0~4s
Warning
The code of video example is the code below, not the code of time example above.
AlignedExample ¶
from janim.imports import * class AlignedExample(Timeline): def construct(self): group = Group( Circle(fill_alpha=0.5), Square(fill_alpha=0.5), Text('Text', font_size=48), color=BLUE ) group.points.arrange(buff=LARGE_BUFF) self.forward() self.play( Aligned( FadeIn(group[0], duration=2), FadeIn(group[1], duration=3), FadeIn(group[2], at=0.5, duration=0.5) ) ) self.forward()