mirror of
https://github.com/3b1b/manim.git
synced 2025-08-01 17:29:06 +08:00
Preliminarily done with Chapter 10 animations
This commit is contained in:
@ -3,10 +3,9 @@
|
||||
from mobject.vectorized_mobject import VMobject
|
||||
from mobject.tex_mobject import TexMobject
|
||||
from animation import Animation
|
||||
from scene import Scene
|
||||
from helpers import *
|
||||
|
||||
|
||||
|
||||
class DecimalNumber(VMobject):
|
||||
CONFIG = {
|
||||
"num_decimal_points" : 2,
|
||||
@ -30,57 +29,66 @@ class DecimalNumber(VMobject):
|
||||
buff = self.digit_to_digit_buff
|
||||
)
|
||||
|
||||
#Todo, this class is now broken
|
||||
|
||||
|
||||
class RangingValues(Animation):
|
||||
class RangingValue(Animation):
|
||||
CONFIG = {
|
||||
"num_decimal_points" : 2,
|
||||
"rate_func" : None,
|
||||
"tracking_function" : None,
|
||||
"value_function" : None,
|
||||
"tracked_mobject" : None,
|
||||
"tracked_mobject_next_to_kwargs" : {},
|
||||
"scale_factor" : None
|
||||
"scale_factor" : None,
|
||||
"color" : WHITE,
|
||||
}
|
||||
def __init__(self, start_val = 0, end_val = 1, **kwargs):
|
||||
def __init__(self, value_function, **kwargs):
|
||||
"""
|
||||
Value function should return a real value
|
||||
depending on the state of the surrounding scene
|
||||
"""
|
||||
digest_config(self, kwargs, locals())
|
||||
Animation.__init__(self, self.get_mobject_at_alpha(0), **kwargs)
|
||||
self.update_mobject()
|
||||
Animation.__init__(self, self.mobject, **kwargs)
|
||||
|
||||
def update_mobject(self, alpha):
|
||||
target = self.get_mobject_at_alpha(alpha)
|
||||
self.mobject.submobjects = target.submobjects
|
||||
|
||||
def get_number(self, alpha):
|
||||
if self.value_function:
|
||||
return self.value_function(alpha)
|
||||
return interpolate(self.start_val, self.end_val, alpha)
|
||||
|
||||
def get_mobject_at_alpha(self, alpha):
|
||||
mob = DecimalNumber(
|
||||
self.get_number(alpha),
|
||||
num_decimal_points=self.num_decimal_points
|
||||
def update_mobject(self, alpha = 0):
|
||||
mobject = DecimalNumber(
|
||||
self.value_function(),
|
||||
num_decimal_points = self.num_decimal_points,
|
||||
color = self.color,
|
||||
)
|
||||
if not hasattr(self, "mobject"):
|
||||
self.mobject = mobject
|
||||
else:
|
||||
self.mobject.points = mobject.points
|
||||
self.mobject.submobjects = mobject.submobjects
|
||||
if self.scale_factor:
|
||||
mob.scale(self.scale_factor)
|
||||
if self.tracking_function:
|
||||
self.tracking_function(alpha, mob)
|
||||
self.mobject.scale(self.scale_factor)
|
||||
elif self.tracked_mobject:
|
||||
mob.next_to(
|
||||
self.mobject.next_to(
|
||||
self.tracked_mobject,
|
||||
**self.tracked_mobject_next_to_kwargs
|
||||
)
|
||||
return mob
|
||||
return self
|
||||
|
||||
|
||||
class RangingValueScene(Scene):
|
||||
CONFIG = {
|
||||
"ranging_values" : []
|
||||
}
|
||||
|
||||
def add_ranging_value(self, value_function, **kwargs):
|
||||
self.ranging_values.append(
|
||||
RangingValue(value_function, **kwargs)
|
||||
)
|
||||
|
||||
def update_frame(self, *args, **kwargs):
|
||||
for val in self.ranging_values:
|
||||
self.remove(val.mobject)
|
||||
val.update_mobject()
|
||||
self.add(val.mobject)
|
||||
return Scene.update_frame(self, *args, **kwargs)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def set_tracking_function(self, func):
|
||||
"""
|
||||
func shoudl be of the form func(alpha, mobject), and
|
||||
should dictate where to place running number during an
|
||||
animation
|
||||
"""
|
||||
self.tracking_function = func
|
||||
|
||||
def set_value_function(self, func):
|
||||
"""
|
||||
func must be of the form alpha->number
|
||||
"""
|
||||
self.value_function = func
|
||||
|
Reference in New Issue
Block a user