transform

备注

使用 Transform 进行不同文字间的变换可能不会有足够好的效果,在使用时请多加斟酌

class janim.anims.transform.Transform(src_item: Item, target_item: Item, *, path_arc: float = 0, path_arc_axis: Vect = array([0., 0., 1.]), path_func: PathFunc | None = None, hide_src: bool = True, show_target: bool = True, root_only: bool = False, **kwargs)

基类:Animation

创建从 src_itemtarget_item 的插值动画

  • path_arcpath_arc_axis 可以指定插值的圆弧路径的角度,若不传入则是直线

  • 也可以直接传入 path_func 来指定路径方法

TransformExample
from janim.imports import *

class TransformExample(Timeline):
    def construct(self):
        A = Text('Text-A', font_size=72)
        B = Text('Text-B', font_size=72)
        C = Text('C-Text', font_size=72)

        A.show()
        self.forward()
        self.play(Transform(A, B))
        self.forward()
        self.play(Transform(B, C))
        self.forward()
class janim.anims.transform.TransformInSegments(src: Item, src_segments: Iterable[Iterable[int]] | Iterable[int], target: Item, target_segments: Iterable[Iterable[int]] | Iterable[int] | EllipsisType, *, trs_kwargs: dict = {}, **kwargs)

基类:AnimGroup

依照切片列表进行 srctarget 之间的变换

基本用法

TransformInSegments(a, [[0,3], [5,7]],
                    b, [[1,3], [5,7]])

相当于

AnimGroup(Transform(a[0:3], b[1:3]),
          Transform(a[5:7], b[5:7]))

省略变换目标的切片

使用 ... 表示与变换来源的切片相同

TransformInSegments(a, [[0,3], [5,7]],
                    b, ...)

相当于

TransformInSegments(a, [[0,3], [5,7]],
                    b, [[0,3], [5,7]])

连续切片

TransformInSegments(a, [[0,3], [5,7,9]],
                    b, [[1,3], [4,7], [10,14]])

相当于

TransformInSegments(a, [[0,3], [5,7], [7,9]],
                    b, [[1,3], [4,7], [10,14]])

切片简写

如果总共只有一个切片,可以省略一层嵌套

TransformInSegments(a, [0, 4, 6, 8],
                    b, ...)

相当于

TransformInSegments(a, [[0, 4, 6, 8]],
                    b, ...)

连续切片倒序

倒过来写即可使切片倒序

TransformInSegments(a, [8, 6, 4, 0],
                    b, ...)

相当于

TransformInSegments(a, [[6,8], [4,6], [0,4]],
                    b, ...)

请留意 Python 切片中左闭右开的原则,对于倒序序列 [8, 6, 4, 0] 来说则是左开右闭

TransformInSegmentsExample
from janim.imports import *

class TransformInSegmentsExample(Timeline):
    def construct(self):
        typ1 = Typst('sin x + cos x')
        typ2 = Typst('cos y + sin y')
        typ2.match_pattern(typ1, '+')
        Group(typ1, typ2).points.scale(3)

        self.show(typ1)
        self.forward(0.5)
        self.play(TransformInSegments(typ1, [[0,3,4], [5,8,9]],
                                      typ2, ...,
                                      lag_ratio=0.5))
        self.forward(0.5)
class janim.anims.transform.MethodTransform(item: Item, **kwargs)

基类:Transform

依据物件的变换而创建的补间过程

具体参考 anim()

MethodTransformExample
from janim.imports import *

class MethodTransformExample(Timeline):
    def construct(self):
        A = Text("Text-A")
        A.points.to_border(LEFT)

        A.show()
        self.forward()
        self.play(
            A.anim.points.scale(3).shift(RIGHT * 7 + UP * 2)
        )
        self.play(
            A.anim.color.set(BLUE)
        )
        self.forward()
class janim.anims.transform.TransformMatchingShapes(src: ~janim.items.item.Item, target: ~janim.items.item.Item, *, mismatch: tuple[~typing.Callable, ~typing.Callable] = (<class 'janim.anims.fading.FadeOutToPoint'>, <class 'janim.anims.fading.FadeInFromPoint'>), duration: float = 2, lag_ratio: float = 0, **kwargs)

基类:AnimGroup

匹配形状进行变换

  • mismatch 表示对于不匹配的形状的处理

  • 注:所有传入该动画类的额外参数都会被传入 mismatch 的方法中

TransformMatchingShapesExample
class TransformMatchingShapesExample(Timeline):
    def construct(self):
        a = Text("the morse code", font_size=48).show()
        b = Text("here come dots", font_size=48)

        self.forward()
        self.play(TransformMatchingShapes(a, b, path_arc=PI/2))
        self.forward()
        self.play(TransformMatchingShapes(b, a, path_arc=PI/2))
        self.forward()