Skip to content

Instantly share code, notes, and snippets.

@Dioarya
Last active May 27, 2022 20:33
Show Gist options
  • Save Dioarya/4fcaa86ead6fb1d60315b5776bd85fb9 to your computer and use it in GitHub Desktop.
Save Dioarya/4fcaa86ead6fb1d60315b5776bd85fb9 to your computer and use it in GitHub Desktop.
Animation broilerplate code
import time
fps = 60
animation_start = time.perf_counter()
skip_frames = 0
for framecount, frame in enumerate(animation):
if skip_frames > 0:
skip_frames -= 1
continue
frame_start = time.perf_counter()
# animation
frame_end = time.perf_counter()
corrector_start = time.perf_counter()
correct_framecount = int((frame_end - animation_start) * fps)
# too little frames rendered.
if framecount < correct_framecount:
skip_frames = correct_framecount - framecount
# too many frames rendered.
elif framecount > correct_framecount:
corrector_end = time.perf_counter()
time.sleep(max(0, (1 / fps) * (framecount - correct_framecount)) + (corrector_end - corrector_start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment