time

class janim.anims_core.time.TimeRange(at: float, end: float | ForeverType)

Bases: object

Identifies the time period starting at at and lasting for duration.

If end is FOREVER, it means the duration is infinite

at: float

Start time of the time-range

end: float | ForeverType

End time of the time-range

If it is FOREVER, it means the duration is infinite

property duration: float

Duration of the time range

If end=FOREVER, raises AssertionError

property num_duration: float
  • When end is not FOREVER, it is the same as duration

  • When end is FOREVER, returns 0

property num_end: float
  • When end is not FOREVER, it is the same as end

  • When end is FOREVER, returns at

to_tuple() tuple[float, float | ForeverType]
set(at: float, end: float | ForeverType) None

Set the range of this time-range

shift(delta: float) None

Shift the time-range by delta

scale(k: float) None

Scale the time-range by factor k (scaled relative to t=0)

copy() Self

Creates a copy of this object

contains(t: float) bool

Checks whether t is within this time range

class janim.anims_core.time.TimeAligner

Bases: object

Due to floating-point precision issues, it is possible that two animations designed to be connected end-to-end may have misaligned judgments

This class is used to normalize nearby floating-point numbers to the same value, ensuring that TimeRange intervals fit together precisely

align_anim_and_record(anim: Animation) None

Normalizes the time range of anim and records it

That is, performs align_and_record() on its .t_range.at and .t_range.end respectively

align_and_record(t: float) float

Aligns time t and records it if there is no nearby time point

Ensures that nearby time points are normalized to the same value and returns the normalized time value

align(t: float) float

Attempts to align time t to a nearby time point without recording it

class janim.anims_core.time.TimeChunks(iterable: Iterable[T], key: Callable[[T], TimeRange | Iterable[TimeRange]], *, step: float = 4)

Bases: Generic

Used to optimize retrieval of objects with TimeRange information based on a specific time

Motivation:

There may be a series of objects on the timeline whose “visible times” are scattered. When querying “which objects are visible at a certain time t”, we do not want to traverse the entire list

However, since these visible times are represented by intervals, it is inconvenient to optimize using binary search

The purpose of this class is to divide time into chunks, each with a duration of step, and record which objects are visible in each chunk. When retrieving visible objects at time t, the caller can use get() to obtain all objects in the chunk containing t, then only needs to check visibility among those objects

Parameters:
  • iterable – A sequence of objects

  • key – A function that maps each object to its TimeRange. It can return a single TimeRange, or a list of TimeRange objects representing multiple visible intervals. Note: TimeRange objects containing FOREVER are not allowed

  • step – The duration of each chunk

get(t: float) list[T]

Gets all objects in the chunk containing time t