2018-03-31 15:11:35 -07:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
from constants import *
|
|
|
|
|
|
|
|
from animation.animation import Animation
|
2018-03-31 18:05:02 -07:00
|
|
|
from mobject.svg.tex_mobject import TextMobject
|
|
|
|
from mobject.types.vectorized_mobject import VMobject
|
|
|
|
from mobject.types.vectorized_mobject import VectorizedPoint
|
2018-03-31 15:11:35 -07:00
|
|
|
from animation.transform import Transform
|
|
|
|
from utils.bezier import interpolate
|
|
|
|
from utils.config_ops import digest_config
|
|
|
|
from utils.paths import counterclockwise_path
|
|
|
|
from utils.rate_functions import double_smooth
|
|
|
|
from utils.rate_functions import smooth
|
|
|
|
|
2018-04-06 13:08:57 -07:00
|
|
|
# Drawing
|
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
|
|
|
|
class ShowPartial(Animation):
|
|
|
|
def update_submobject(self, submobject, starting_submobject, alpha):
|
|
|
|
submobject.pointwise_become_partial(
|
|
|
|
starting_submobject, *self.get_bounds(alpha)
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_bounds(self, alpha):
|
|
|
|
raise Exception("Not Implemented")
|
|
|
|
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class ShowCreation(ShowPartial):
|
|
|
|
CONFIG = {
|
2018-04-06 13:08:57 -07:00
|
|
|
"submobject_mode": "one_at_a_time",
|
2018-03-31 15:11:35 -07:00
|
|
|
}
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
def get_bounds(self, alpha):
|
|
|
|
return (0, alpha)
|
|
|
|
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class Uncreate(ShowCreation):
|
|
|
|
CONFIG = {
|
2018-04-06 13:08:57 -07:00
|
|
|
"rate_func": lambda t: smooth(1 - t),
|
|
|
|
"remover": True
|
2018-03-31 15:11:35 -07:00
|
|
|
}
|
|
|
|
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class Write(ShowCreation):
|
|
|
|
CONFIG = {
|
2018-04-06 13:08:57 -07:00
|
|
|
"rate_func": None,
|
|
|
|
"submobject_mode": "lagged_start",
|
2018-03-31 15:11:35 -07:00
|
|
|
}
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
def __init__(self, mob_or_text, **kwargs):
|
2018-04-06 13:08:57 -07:00
|
|
|
digest_config(self, kwargs)
|
2018-03-31 15:11:35 -07:00
|
|
|
if isinstance(mob_or_text, str):
|
|
|
|
mobject = TextMobject(mob_or_text)
|
|
|
|
else:
|
|
|
|
mobject = mob_or_text
|
|
|
|
if "run_time" not in kwargs:
|
|
|
|
self.establish_run_time(mobject)
|
|
|
|
if "lag_factor" not in kwargs:
|
|
|
|
if len(mobject.family_members_with_points()) < 4:
|
|
|
|
min_lag_factor = 1
|
|
|
|
else:
|
|
|
|
min_lag_factor = 2
|
|
|
|
self.lag_factor = max(self.run_time - 1, min_lag_factor)
|
|
|
|
ShowCreation.__init__(self, mobject, **kwargs)
|
|
|
|
|
|
|
|
def establish_run_time(self, mobject):
|
|
|
|
num_subs = len(mobject.family_members_with_points())
|
|
|
|
if num_subs < 15:
|
|
|
|
self.run_time = 1
|
|
|
|
else:
|
|
|
|
self.run_time = 2
|
|
|
|
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class DrawBorderThenFill(Animation):
|
|
|
|
CONFIG = {
|
2018-04-06 13:08:57 -07:00
|
|
|
"run_time": 2,
|
|
|
|
"stroke_width": 2,
|
|
|
|
"stroke_color": None,
|
|
|
|
"rate_func": double_smooth,
|
2018-03-31 15:11:35 -07:00
|
|
|
}
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
def __init__(self, vmobject, **kwargs):
|
|
|
|
if not isinstance(vmobject, VMobject):
|
|
|
|
raise Exception("DrawBorderThenFill only works for VMobjects")
|
|
|
|
self.reached_halfway_point_before = False
|
|
|
|
Animation.__init__(self, vmobject, **kwargs)
|
|
|
|
|
|
|
|
def update_submobject(self, submobject, starting_submobject, alpha):
|
|
|
|
submobject.pointwise_become_partial(
|
2018-04-06 13:08:57 -07:00
|
|
|
starting_submobject, 0, min(2 * alpha, 1)
|
2018-03-31 15:11:35 -07:00
|
|
|
)
|
|
|
|
if alpha < 0.5:
|
|
|
|
if self.stroke_color:
|
2018-04-06 13:08:57 -07:00
|
|
|
color = self.stroke_color
|
2018-03-31 15:11:35 -07:00
|
|
|
elif starting_submobject.stroke_width > 0:
|
|
|
|
color = starting_submobject.get_stroke_color()
|
|
|
|
else:
|
|
|
|
color = starting_submobject.get_color()
|
2018-04-06 13:08:57 -07:00
|
|
|
submobject.set_stroke(color, width=self.stroke_width)
|
|
|
|
submobject.set_fill(opacity=0)
|
2018-03-31 15:11:35 -07:00
|
|
|
else:
|
|
|
|
if not self.reached_halfway_point_before:
|
|
|
|
self.reached_halfway_point_before = True
|
|
|
|
submobject.points = np.array(starting_submobject.points)
|
|
|
|
width, opacity = [
|
2018-04-06 13:08:57 -07:00
|
|
|
interpolate(start, end, 2 * alpha - 1)
|
2018-03-31 15:11:35 -07:00
|
|
|
for start, end in [
|
|
|
|
(self.stroke_width, starting_submobject.get_stroke_width()),
|
|
|
|
(0, starting_submobject.get_fill_opacity())
|
|
|
|
]
|
|
|
|
]
|
2018-04-06 13:08:57 -07:00
|
|
|
submobject.set_stroke(width=width)
|
|
|
|
submobject.set_fill(opacity=opacity)
|
|
|
|
|
|
|
|
# Fading
|
2018-03-31 15:11:35 -07:00
|
|
|
|
|
|
|
|
|
|
|
class FadeOut(Transform):
|
|
|
|
CONFIG = {
|
2018-04-06 13:08:57 -07:00
|
|
|
"remover": True,
|
2018-03-31 15:11:35 -07:00
|
|
|
}
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
def __init__(self, mobject, **kwargs):
|
|
|
|
target = mobject.copy()
|
|
|
|
target.fade(1)
|
|
|
|
Transform.__init__(self, mobject, target, **kwargs)
|
|
|
|
|
2018-04-06 13:08:57 -07:00
|
|
|
def clean_up(self, surrounding_scene=None):
|
2018-03-31 15:11:35 -07:00
|
|
|
Transform.clean_up(self, surrounding_scene)
|
|
|
|
self.update(0)
|
|
|
|
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class FadeIn(Transform):
|
|
|
|
def __init__(self, mobject, **kwargs):
|
|
|
|
target = mobject.copy()
|
|
|
|
Transform.__init__(self, mobject, target, **kwargs)
|
|
|
|
self.starting_mobject.fade(1)
|
|
|
|
if isinstance(self.starting_mobject, VMobject):
|
2018-04-06 13:08:57 -07:00
|
|
|
self.starting_mobject.set_stroke(width=0)
|
|
|
|
self.starting_mobject.set_fill(opacity=0)
|
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
|
|
|
|
class FadeInAndShiftFromDirection(Transform):
|
2018-07-14 10:45:29 -07:00
|
|
|
CONFIG = {
|
|
|
|
"direction": DOWN,
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, mobject, direction=None, **kwargs):
|
2018-03-31 15:11:35 -07:00
|
|
|
digest_config(self, kwargs)
|
|
|
|
target = mobject.copy()
|
2018-07-14 10:45:29 -07:00
|
|
|
direction = direction or self.direction
|
2018-04-19 15:27:38 -07:00
|
|
|
mobject.shift(direction)
|
2018-03-31 15:11:35 -07:00
|
|
|
mobject.fade(1)
|
|
|
|
Transform.__init__(self, mobject, target, **kwargs)
|
|
|
|
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class FadeInFromDown(FadeInAndShiftFromDirection):
|
|
|
|
"""
|
|
|
|
Essential a more convenient form of FadeInAndShiftFromDirection
|
|
|
|
"""
|
|
|
|
CONFIG = {
|
2018-04-06 13:08:57 -07:00
|
|
|
"direction": DOWN,
|
2018-03-31 15:11:35 -07:00
|
|
|
}
|
|
|
|
|
2018-05-16 00:04:59 -07:00
|
|
|
|
2018-07-14 10:45:29 -07:00
|
|
|
class FadeOutAndShift(FadeOut):
|
|
|
|
CONFIG = {
|
|
|
|
"direction": DOWN,
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, mobject, direction=None, **kwargs):
|
|
|
|
FadeOut.__init__(self, mobject, **kwargs)
|
|
|
|
direction = direction or self.direction
|
|
|
|
self.target_mobject.shift(direction)
|
|
|
|
|
|
|
|
|
|
|
|
class FadeOutAndShiftDown(FadeOutAndShift):
|
|
|
|
CONFIG = {
|
|
|
|
"direction": DOWN,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-16 00:04:59 -07:00
|
|
|
class VFadeIn(Animation):
|
|
|
|
"""
|
|
|
|
VFadeIn and VFadeOut only work for VMobjects, but they can be applied
|
|
|
|
to mobjects while they are being animated in some other way (e.g. shifting
|
|
|
|
then) in a way that does not work with FadeIn and FadeOut
|
|
|
|
"""
|
|
|
|
def update_submobject(self, submobject, starting_submobject, alpha):
|
|
|
|
submobject.set_stroke(
|
|
|
|
width=interpolate(0, starting_submobject.get_stroke_width(), alpha)
|
|
|
|
)
|
|
|
|
submobject.set_fill(
|
|
|
|
opacity=interpolate(0, starting_submobject.get_fill_opacity(), alpha)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class VFadeOut(VFadeIn):
|
|
|
|
CONFIG = {
|
|
|
|
"remover": True
|
|
|
|
}
|
|
|
|
|
|
|
|
def update_submobject(self, submobject, starting_submobject, alpha):
|
|
|
|
VFadeIn.update_submobject(
|
|
|
|
self, submobject, starting_submobject, 1 - alpha
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-04-06 13:08:57 -07:00
|
|
|
# Growing
|
|
|
|
|
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class GrowFromPoint(Transform):
|
|
|
|
CONFIG = {
|
2018-04-06 13:08:57 -07:00
|
|
|
"point_color": None,
|
2018-03-31 15:11:35 -07:00
|
|
|
}
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
def __init__(self, mobject, point, **kwargs):
|
|
|
|
digest_config(self, kwargs)
|
|
|
|
target = mobject.copy()
|
|
|
|
point_mob = VectorizedPoint(point)
|
|
|
|
if self.point_color:
|
|
|
|
point_mob.set_color(self.point_color)
|
|
|
|
mobject.replace(point_mob)
|
|
|
|
mobject.set_color(point_mob.get_color())
|
|
|
|
Transform.__init__(self, mobject, target, **kwargs)
|
|
|
|
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class GrowFromCenter(GrowFromPoint):
|
|
|
|
def __init__(self, mobject, **kwargs):
|
|
|
|
GrowFromPoint.__init__(self, mobject, mobject.get_center(), **kwargs)
|
|
|
|
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-04-12 15:11:45 -07:00
|
|
|
class GrowFromEdge(GrowFromPoint):
|
|
|
|
def __init__(self, mobject, edge, **kwargs):
|
|
|
|
GrowFromPoint.__init__(
|
|
|
|
self, mobject, mobject.get_critical_point(edge), **kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class GrowArrow(GrowFromPoint):
|
|
|
|
def __init__(self, arrow, **kwargs):
|
|
|
|
GrowFromPoint.__init__(self, arrow, arrow.get_start(), **kwargs)
|
|
|
|
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class SpinInFromNothing(GrowFromCenter):
|
|
|
|
CONFIG = {
|
2018-04-06 13:08:57 -07:00
|
|
|
"path_func": counterclockwise_path()
|
2018-03-31 15:11:35 -07:00
|
|
|
}
|
|
|
|
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class ShrinkToCenter(Transform):
|
|
|
|
def __init__(self, mobject, **kwargs):
|
|
|
|
Transform.__init__(
|
|
|
|
self, mobject, mobject.get_point_mobject(), **kwargs
|
|
|
|
)
|