Skip to content

Commit

Permalink
fix: incorrect time-range of render calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jkjkil4 committed Jun 27, 2024
1 parent 3cd2430 commit 0156cc8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions janim/anims/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def get_alpha_on_global_t(self, global_t: float) -> float:
anim_t = self.parent.get_anim_t(self.parent.get_alpha_on_global_t(global_t), self)
return self.rate_func(anim_t / self.local_range.duration)

def is_visible(self, global_t: float) -> bool:
# + 1e-5 是为了避免在两端的浮点误差
return self.global_range.at <= global_t + 1e-5 < self.global_range.end

global_t_ctx: ContextVar[float] = ContextVar('Animation.global_t_ctx')
'''
对该值进行设置,使得进行 :meth:`anim_on` 和 :meth:`render` 时不需要将 ``global_t`` 作为参数传递也能获取到
Expand Down
2 changes: 1 addition & 1 deletion janim/anims/composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def anim_on_alpha(self, alpha: float) -> None:

for anim in self.anims:
anim_t = self.get_anim_t(alpha, anim)
if anim.global_range.at <= global_t < anim.global_range.end:
if anim.is_visible(global_t):
anim.anim_on(anim_t)


Expand Down
4 changes: 2 additions & 2 deletions janim/anims/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def schedule(self, at: float, func: Callable, *args, **kwargs) -> None:
会在进度达到 ``at`` 时,对 ``func`` 进行调用,
可传入 ``*args`` 和 ``**kwargs``
'''
rough_at = round(at, 4) # 防止因为精度误差使得本来计划更迟的任务被更早地执行了
rough_at = round(at, 3) # 防止因为精度误差使得本来计划更迟的任务被更早地执行了
task = Timeline.ScheduledTask(rough_at, func, args, kwargs)
insort(self.scheduled_tasks, task, key=lambda x: x.at)

Expand Down Expand Up @@ -824,7 +824,7 @@ def render_all(self, ctx: mgl.Context) -> None:
*[
anim.render_call_list
for anim in self.flattened
if anim.render_call_list and anim.global_range.at <= self._time < anim.global_range.end
if anim.render_call_list and anim.is_visible(self._time)
],
key=lambda x: x.depth,
reverse=True
Expand Down

0 comments on commit 0156cc8

Please sign in to comment.