animation

Warning

Tips: This section covers the implementations of JAnim, which may be quite complex. If you do not have the need to delve into the source code, you may choose to read it at your discretion.

class janim.anims.animation.Animation(*, at: float = 0, duration: float | ForeverType = 1, rate_func: RateFunc = <function smooth>, name: str | None = None)

Bases: object

Base class of animations

  • Creates an animation from at to at + duration

  • duration 可以是 FOREVER (一般用于 Display, 以及特殊情况下的 DataUpdater 等, 但是 AnimGroup 及其衍生类不能传入 FOREVER

  • 指定 rate_func 可以设定插值函数,默认为 janim.utils.rate_funcs.smooth() 即平滑插值

  • 设置 name 可以将文字显示在预览界面的时间轴标签上,不影响渲染(如果不设置则默认为类名)

Warning

动画对象不能复用,例如这样会导致意外行为:

class Test(Timeline):
    def construct(self):
        a = Square()
        b = Circle()
        anim1 = Transform(a, b)
        anim2 = Transform(b, a)
        self.play(anim1)
        self.play(anim2)
        self.play(anim1)

正确写法:

class Test(Timeline):
    def construct(self):
        a = Square()
        b = Circle()
        self.play(Transform(a, b))
        self.play(Transform(b, a))
        self.play(Transform(a, b))
label_color: tuple[float, float, float] = (192, 198, 205)
shift_range(delta: float) Self

delta 的变化量移动时间区段

scale_range(k: float) Self

k 的倍率缩放时间区段(相对于 t=0 进行缩放)

finalize() None
get_alpha_on_global_t(global_t: float) float
transfer_params(other: Animation) None
global_t_ctx: ContextVar[float] = <ContextVar name='Animation.global_t_ctx'>
schedule_show_and_hide(item: Item, show_at_begin: bool, hide_at_end: bool) None
class janim.anims.animation.ItemAnimation(item: Item, *, show_at_begin: bool = True, hide_at_end: bool = False, **kwargs)

Bases: Animation

auto_detect = True
class ApplyParams(global_t: 'float', anims: 'list[ItemAnimation]', index: 'int')

Bases: object

global_t: float
anims: list[ItemAnimation]
index: int
apply(data: Item, p: ApplyParams) None
apply(data: None, p: ApplyParams) Item

global_t 时的动画效果作用到 data

其中

  • 对于 Display 而言,dataNone,返回值是 Item 对象

  • 而对于其它大多数的而言,data 是前一个动画作用的结果,返回值是 None

class janim.anims.animation.ApplyAligner(item: Item, stacks: list[AnimStack], **kwargs)

Bases: ItemAnimation

pre_apply(data: Item, p: ApplyParams) None
class janim.anims.animation.TimeRange(at: float, end: float | ForeverType)

Bases: object

标识了从 at 开始,到 end 结束的时间区段

end 也可以是 FOREVER

at: float

时间区段的开始时刻

end: float | ForeverType

时间区段的结束时刻

property duration: float

时间区段的时长,即 end - at,如果 end=FOREVER 则抛出 AssertionError

另见 num_duration()

property num_duration: float
  • end 不是 FOREVER 时,与 duration() 一致

  • endFOREVER 时,此时返回 0

(这用于 AnimGroupend=FOREVER 的子动画的处理,也就是把这种子动画当成 end=at 来计算时间)

property num_end: float
  • end 不是 FOREVER 时,此时返回 end

  • endFOREVER 时,此时返回 at

(这用于 AnimGroupend=FOREVER 的子动画的处理,也就是把这种子动画当成 end=at 来计算时间)

set(at: float, end: float | ForeverType) None

设置该时间区段的范围

shift(delta: float) None

delta 的变化量移动时间区段

scale(k: float) None

k 的倍率缩放时间区段(相对于 t=0 进行缩放)

copy() TimeRange
class janim.anims.animation.TimeAligner

Bases: object

由于浮点数精度的问题,有可能出现比如原本设计上首尾相连的两个动画,却出现判定的错位

该类用于将相近的浮点数归化到同一个值,使得 TimeRange 区间严丝合缝

align(anim: Animation) None

归化 anim 的时间区段, 即分别对 .t_range.at.t_range.end 进行 align_t() 的操作

align_t(t: float) float

对齐时间 t,确保相近的时间点归化到相同的值,返回归化后的时间值

align_t_for_render(t: float) float

align_t() 类似,但区别在于

  • 该方法使用二分查找而不是倒序查找

  • 该方法在查找后不记录 t 的值

class janim.anims.animation.TimeSegments(iterable: Iterable[T], key: Callable[[T], TimeRange | Iterable[TimeRange]], *, step: float = 4)

Bases: Generic

get(t: float) list[T]