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)
if self.stroke_color: return ShowCreation(
color = self.stroke_color vmobject,
elif starting_submobject.stroke_width > 0: **self.draw_border_animation_config
color = starting_submobject.get_stroke_color() )
else:
color = starting_submobject.get_color() def get_stroke_color(self, vmobject):
submobject.set_stroke(color, width=self.stroke_width) if self.stroke_color:
submobject.set_fill(opacity=0) return self.stroke_color
else: elif vmobject.get_stroke_width() > 0:
if not self.reached_halfway_point_before: return vmobject.get_stroke_color()
self.reached_halfway_point_before = True return vmobject.get_color()
submobject.points = np.array(starting_submobject.points)
width, opacity = [ def get_fill_animation(self, vmobject):
interpolate(start, end, 2 * alpha - 1) return Transform(
for start, end in [ vmobject,
(self.stroke_width, starting_submobject.get_stroke_width()), self.original_vmobject,
(0, starting_submobject.get_fill_opacity()) **self.fill_animation_config,
] )
]
submobject.set_stroke(width=width) def update_mobjects(self, dt):
submobject.set_fill(opacity=opacity) super().update_mobjects(dt)
self.original_vmobject.update(dt)
class Write(DrawBorderThenFill): class Write(DrawBorderThenFill):