Skip to content

Instantly share code, notes, and snippets.

@uwezi
Last active June 3, 2025 20:13
Show Gist options
  • Save uwezi/32496c61e1517f8da85cafac68a290f4 to your computer and use it in GitHub Desktop.
Save uwezi/32496c61e1517f8da85cafac68a290f4 to your computer and use it in GitHub Desktop.
[increasing a number in steps] How to increase a DecimalNumber in discrete steps in an animation. #manim #decimalnumber #updater #valuetracker
from manim import *
# https://discord.com/channels/581738731934056449/1379443317175091252/1379443317175091252
class Test(Scene):
def construct(self):
numbers = [100]*10
numbersVG = VGroup(Text(str(n)) for n in numbers)
numbersVG.arrange(RIGHT).shift(1.3 * DOWN)
self.add(numbersVG)
tally = DecimalNumber(0, num_decimal_places=0).to_edge(UP)
vt = ValueTracker(0)
def tally_updater(mobj):
tally.set_value(vt.get_value())
tally.add_updater(tally_updater, call_updater=True)
self.add(tally)
time = 1
dt = 0.2
animations = []
for i, elem in enumerate(numbersVG):
animation_group = AnimationGroup(
FadeOut(
elem,
shift=tally.get_center() - elem.get_center(),
run_time=time
),
# FadeOut(# the square #),
)
animations.append(animation_group)
def step(t):
dy = 1/np.sum(numbers)
dt = 1/(len(numbers)+2)
n = min(int(t/dt),len(numbers)+1)
part = np.sum(numbers[:n])
return part*dy
self.play(
LaggedStart(*animations, lag_ratio=dt),
vt.animate(run_time=time*(1+len(numbers)*dt), rate_func=step).set_value(np.sum(numbers))
)
self.wait(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment