跳转至

值跟踪器

影片代码
import manimgx as m


def curve(x: float) -> float:
    return x**2 / 4


class ValueTrackersHero(m.Scene):
    def construct(self) -> None:
        axes = m.Axes(x_range=[0, 5, 1], y_range=[0, 7, 1], x_length=8, y_length=5)
        graph = axes.plot(curve, color=m.BLUE)
        x = m.ValueTracker(1)
        dot = m.always_redraw(
            lambda: m.Dot(axes.c2p(x.get_value(), curve(x.get_value())), color=m.YELLOW)
        )
        number = m.always_redraw(
            lambda: m.DecimalNumber(x.get_value()).next_to(dot, m.UL)
        )
        self.add(axes, graph, dot, number)
        self.play(x.animate.set_value(4.5), run_time=3)
        self.wait()

值跟踪器持有一个数字且不显示任何内容。用 tracker.animate.set_value(10) 动画它的数字;在更新器中,或在 always_redraw 调用的函数中用 tracker.get_value() 读取它。跟踪器的一个动画随后移动每个读取它的 mobject。

ValueTracker

代码
import manimgx as m


class ValueTrackerExample(m.Scene):
    def construct(self) -> None:
        x = m.ValueTracker(-4)
        line = m.NumberLine(x_range=(-6, 6, 1), length=12).add_numbers()
        pointer = m.Vector(1.5 * m.DOWN, color=m.YELLOW)
        pointer.add_updater(lambda p: p.next_to(line.n2p(x.get_value()), m.UP))
        label = m.MathTex("x", font_size=72, color=m.YELLOW)
        label.add_updater(lambda t: t.next_to(pointer, m.UP))
        self.add(line, pointer, label)
        self.play(x.animate.set_value(5), run_time=2)
        x -= 3
        self.wait(0.5)
        self.play(x.animate.increment_value(-5))

一个可被动画改变、可被更新器读取的数。

该数保存在追踪器唯一点的 x 坐标中,因此任何移动 mobject 的操作都会改变它:tracker.animate.set_value(5) 以动画 速率函数的节奏将其变为 5,其他 mobject 的更新器通过 get_value 读取它。追踪器从不被绘制;仅当 其自身有更新器时才将其添加到场景。tracker += 1 和 tracker -= 1 立即改变该数。

m.ValueTracker(value=0, **kwargs)
value

起始实数;复数请使用 ComplexValueTracker。

color

填充与描边共用的颜色(默认白色);None 表示该类的默认值。

fill_color

填充的颜色;未给出时为 color。多个颜色沿 sheen_direction 构成渐变。

fill_opacity

填充的不透明度,取值从 0 到 1(默认 0:无填充)。

stroke_color

描边的颜色;未给出时为 color。多个颜色构成渐变。

stroke_opacity

描边的不透明度,取值从 0 到 1(默认 1)。

stroke_width

描边的宽度,以场景单位的百分之一计(默认 4;0:无描边)。

background_stroke_color

绘制于填充之后的轮廓颜色(默认黑色)。

background_stroke_opacity

轮廓的不透明度,取值从 0 到 1(默认 1)。

background_stroke_width

轮廓的宽度,以场景单位的百分之一计(默认 0:无)。

sheen_factor

颜色朝 sheen_direction 变亮的程度,取值从 -1 到 1(默认 0); 负值变暗。

sheen_direction

颜色变亮的方向(默认 UL)。

joint_type

描边在路径转折处的连接方式:圆角、斜切或斜接,按二维场景的绘制方式 (参见 LineJointType;默认 AUTO:斜接)。

cap_style

描边在其显示的每一端的端点样式(开放路径的端点、虚线的端点):圆头、平头 或方头,按二维场景的绘制方式(参见 CapStyleType;默认 AUTO:平头)。

shade_in_3d

三维场景的光是否对该 mobject 着色。

material

其表面在三维场景中如何反射场景的光(参见 Material);None:Manim 的着色(默认)。

name

mobject 的名称;未给出时为其类名。

z_index

其在绘制顺序中的位置:索引较高者绘制于较低者之上 (默认 0)。

target

MoveToTarget 将 mobject 移动至的状态。

还接受 样式关键字。

源码

manimgx\mobject.py

def __init__(self, value: V = 0, **kwargs: Unpack[Style]) -> None:
    super().__init__(**kwargs)
    self.points = np.zeros((1, 3))
    self.set_value(value)

get_value

追踪器持有的数。

tracker.get_value()
源码

manimgx\mobject.py

def get_value(self) -> V:
    """追踪器持有的数。"""
    return float(self.points[0, 0])  # pyright: ignore[reportReturnType]  # V is float here

set_value

设置追踪器持有的数;通过 animate 可为其变化制作动画。

tracker.set_value(value)
value

新的数。

源码

manimgx\mobject.py

def set_value(self, value: V) -> Self:
    """设置追踪器持有的数;通过
    [`animate`][manimgx.Mobject.animate] 可为其变化制作动画。

    Args:
        value: 新的数。
    """
    points = np.array(self.points)
    points[0, 0] = value
    self.points = points
    return self

increment_value

增加追踪器持有的数;通过 animate 可为该变化制作动画。

tracker.increment_value(d_value)
d_value

要增加的量;负数则减少。

源码

manimgx\mobject.py

def increment_value(self, d_value: V) -> Self:
    """增加追踪器持有的数;通过
    [`animate`][manimgx.Mobject.animate] 可为该变化制作动画。

    Args:
        d_value: 要增加的量;负数则减少。
    """
    return self.set_value(self.get_value() + d_value)

ComplexValueTracker

代码
import manimgx as m


class ComplexValueTrackerExample(m.Scene):
    def construct(self) -> None:
        plane = m.ComplexPlane().add_coordinates()
        z = m.ComplexValueTracker(2 + 1j)
        dot = m.Dot(radius=0.15, color=m.YELLOW)
        dot.add_updater(lambda d: d.move_to(plane.n2p(z.get_value())))
        self.add(plane, dot)
        self.play(z.animate.set_value(-3 + 2j))
        self.play(z.animate.set_value(z.get_value() * 1j))
        self.play(z.animate.set_value(z.get_value() / (1 + 1j)))

一个可被动画改变、可被更新器读取的复数。

该数保存在追踪器的唯一点中,实部为 x,虚部为 y,因此 动画沿复平面的一条直线移动它: tracker.animate.set_value(3 + 2j)。+= 和 -= 立即以 复数运算改变它。

m.ComplexValueTracker(value=0, **kwargs)
value

起始数。

color

填充与描边共用的颜色(默认白色);None 表示该类的默认值。

fill_color

填充的颜色;未给出时为 color。多个颜色沿 sheen_direction 构成渐变。

fill_opacity

填充的不透明度,取值从 0 到 1(默认 0:无填充)。

stroke_color

描边的颜色;未给出时为 color。多个颜色构成渐变。

stroke_opacity

描边的不透明度,取值从 0 到 1(默认 1)。

stroke_width

描边的宽度,以场景单位的百分之一计(默认 4;0:无描边)。

background_stroke_color

绘制于填充之后的轮廓颜色(默认黑色)。

background_stroke_opacity

轮廓的不透明度,取值从 0 到 1(默认 1)。

background_stroke_width

轮廓的宽度,以场景单位的百分之一计(默认 0:无)。

sheen_factor

颜色朝 sheen_direction 变亮的程度,取值从 -1 到 1(默认 0); 负值变暗。

sheen_direction

颜色变亮的方向(默认 UL)。

joint_type

描边在路径转折处的连接方式:圆角、斜切或斜接,按二维场景的绘制方式 (参见 LineJointType;默认 AUTO:斜接)。

cap_style

描边在其显示的每一端的端点样式(开放路径的端点、虚线的端点):圆头、平头 或方头,按二维场景的绘制方式(参见 CapStyleType;默认 AUTO:平头)。

shade_in_3d

三维场景的光是否对该 mobject 着色。

material

其表面在三维场景中如何反射场景的光(参见 Material);None:Manim 的着色(默认)。

name

mobject 的名称;未给出时为其类名。

z_index

其在绘制顺序中的位置:索引较高者绘制于较低者之上 (默认 0)。

target

MoveToTarget 将 mobject 移动至的状态。

还接受 样式关键字。

源码

manimgx\mobject.py

def __init__(self, value: V = 0, **kwargs: Unpack[Style]) -> None:
    super().__init__(**kwargs)
    self.points = np.zeros((1, 3))
    self.set_value(value)

get_value

追踪器持有的复数。

tracker.get_value()
源码

manimgx\mobject.py

def get_value(self) -> complex:
    """追踪器持有的复数。"""
    return complex(*self.points[0, :2])

set_value

设置追踪器持有的复数;通过 animate 可沿直线为其变化制作动画。

tracker.set_value(value)
value

新的数;实数被视为复数。

源码

manimgx\mobject.py

def set_value(self, value: complex) -> Self:
    """设置追踪器持有的复数;通过
    [`animate`][manimgx.Mobject.animate] 可沿直线为其变化制作动画。

    Args:
        value: 新的数;实数被视为复数。
    """
    z = complex(value)
    points = np.array(self.points)
    points[0, :2] = (z.real, z.imag)
    self.points = points
    return self