3b1b-manim/animation/continual_animation.py

92 lines
2.2 KiB
Python
Raw Normal View History

2017-08-24 11:43:38 -07:00
from helpers import *
from mobject import Mobject, Group
import copy
2017-08-24 11:43:38 -07:00
class ContinualAnimation(object):
CONFIG = {
"start_up_time" : 1,
"wind_down_time" : 1,
2017-08-24 12:38:37 -07:00
"end_time" : np.inf,
2017-08-24 11:43:38 -07:00
}
def __init__(self, mobject, **kwargs):
mobject = instantiate(mobject)
assert(isinstance(mobject, Mobject))
digest_config(self, kwargs, locals())
2017-08-24 12:38:37 -07:00
self.internal_time = 0
self.external_time = 0
2017-08-24 11:43:38 -07:00
self.setup()
2017-08-24 12:38:37 -07:00
self.update(0)
2017-08-24 11:43:38 -07:00
def setup(self):
#To implement in subclass
pass
2017-08-24 12:38:37 -07:00
def begin_wind_down(self, wind_down_time = None):
if wind_down_time is not None:
self.wind_down_time = wind_down_time
self.end_time = self.external_time + self.wind_down_time
2017-08-24 11:43:38 -07:00
def update(self, dt):
2017-08-24 12:38:37 -07:00
#TODO, currenty time moves slower for a
#continual animation during its start up
#to help smooth things out. Does this have
#unwanted consequences?
self.external_time += dt
if self.external_time < self.start_up_time:
dt *= float(self.external_time)/self.start_up_time
elif self.external_time > self.end_time - self.wind_down_time:
dt *= np.clip(
float(self.end_time - self.external_time)/self.wind_down_time,
0, 1
)
self.internal_time += dt
2017-08-24 11:43:38 -07:00
self.update_mobject(dt)
def update_mobject(self, dt):
#To implement in subclass
pass
def copy(self):
return copy.deepcopy(self)
2017-08-24 11:43:38 -07:00
class ContinualAnimationGroup(ContinualAnimation):
CONFIG = {
"start_up_time" : 0,
"wind_down_time" : 0,
}
def __init__(self, *continual_animations, **kwargs):
digest_config(self, kwargs, locals())
self.group = Group(*[ca.mobject for ca in continual_animations])
ContinualAnimation.__init__(self, self.group, **kwargs)
def update_mobject(self, dt):
for continual_animation in self.continual_animations:
continual_animation.update(dt)
2017-08-24 11:43:38 -07:00
class AmbientRotation(ContinualAnimation):
CONFIG = {
"axis" : OUT,
"rate" : np.pi/12, #Radians per second
}
2017-08-24 11:43:38 -07:00
def update_mobject(self, dt):
self.mobject.rotate(dt*self.rate, axis = self.axis)
2017-08-24 11:43:38 -07:00