Rewrote DrawBorderThenFill to work with Succession

This commit is contained in:
Grant Sanderson 2019-02-08 16:53:37 -08:00
parent 85616f0019
commit 72c5c53c1e

View file

@ -2,6 +2,7 @@ import numpy as np
from manimlib.animation.animation import Animation from manimlib.animation.animation import Animation
from manimlib.animation.transform import Transform from manimlib.animation.transform import Transform
from manimlib.animation.composition import Succession
from manimlib.constants import * from manimlib.constants import *
from manimlib.mobject.svg.tex_mobject import TextMobject from manimlib.mobject.svg.tex_mobject import TextMobject
from manimlib.mobject.types.vectorized_mobject import VMobject from manimlib.mobject.types.vectorized_mobject import VMobject
@ -45,46 +46,59 @@ class Uncreate(ShowCreation):
} }
class DrawBorderThenFill(Animation): class DrawBorderThenFill(Succession):
CONFIG = { CONFIG = {
"run_time": 2, "run_time": 2,
"stroke_width": 2, "stroke_width": 2,
"stroke_color": None, "stroke_color": None,
"rate_func": double_smooth, "draw_border_animation_config": {},
"fill_animation_config": {},
} }
def __init__(self, vmobject, **kwargs): def __init__(self, vmobject, **kwargs):
self.check_validity_of_input(vmobject)
self.vmobject = vmobject
self.original_vmobject = vmobject.copy()
digest_config(self, kwargs)
Succession.__init__(
self,
self.get_draw_border_animation(vmobject),
self.get_fill_animation(vmobject),
**kwargs,
)
def check_validity_of_input(self, vmobject):
if not isinstance(vmobject, VMobject): if not isinstance(vmobject, VMobject):
raise Exception("DrawBorderThenFill only works for VMobjects") raise Exception("DrawBorderThenFill only works for VMobjects")
self.reached_halfway_point_before = False
Animation.__init__(self, vmobject, **kwargs)
def interpolate_submobject(self, submobject, starting_submobject, alpha): def get_draw_border_animation(self, vmobject):
submobject.pointwise_become_partial( vmobject.set_stroke(
starting_submobject, 0, min(2 * alpha, 1) color=self.get_stroke_color(vmobject),
width=self.stroke_width
) )
if alpha < 0.5: vmobject.set_fill(opacity=0)
return ShowCreation(
vmobject,
**self.draw_border_animation_config
)
def get_stroke_color(self, vmobject):
if self.stroke_color: if self.stroke_color:
color = self.stroke_color return self.stroke_color
elif starting_submobject.stroke_width > 0: elif vmobject.get_stroke_width() > 0:
color = starting_submobject.get_stroke_color() return vmobject.get_stroke_color()
else: return vmobject.get_color()
color = starting_submobject.get_color()
submobject.set_stroke(color, width=self.stroke_width) def get_fill_animation(self, vmobject):
submobject.set_fill(opacity=0) return Transform(
else: vmobject,
if not self.reached_halfway_point_before: self.original_vmobject,
self.reached_halfway_point_before = True **self.fill_animation_config,
submobject.points = np.array(starting_submobject.points) )
width, opacity = [
interpolate(start, end, 2 * alpha - 1) def update_mobjects(self, dt):
for start, end in [ super().update_mobjects(dt)
(self.stroke_width, starting_submobject.get_stroke_width()), self.original_vmobject.update(dt)
(0, starting_submobject.get_fill_opacity())
]
]
submobject.set_stroke(width=width)
submobject.set_fill(opacity=opacity)
class Write(DrawBorderThenFill): class Write(DrawBorderThenFill):