将 mobject 转换为其他 mobject
也可以将一个 mobject 转换为另一个 mobject,如下所示:
class ExampleTransform(Scene):
def construct(self):
self.camera.background_color = WHITE
m1 = Square().set_color(RED)
m2 = Rectangle().set_color(RED).rotate(0.2)
self.play(Transform(m1,m2))
Transform 函数将前一个 mobject 的点映射到下一个 mobject 的点。这可能会导致奇怪的行为,例如,当一个 mobject 的点顺时针排列,而其他点逆时针排列。在这里,使用 flip 函数并通过 numpy 的 roll 函数重新定位点可能会有所帮助:
class ExampleRotation(Scene):
def construct(self):
self.camera.background_color = WHITE
m1a = Square().set_color(RED).shift(LEFT)
m1b = Circle().set_color(RED).shift(LEFT)
m2a = Square().set_color(BLUE).shift(RIGHT)
m2b = Circle().set_color(BLUE).shift(RIGHT)
points = m2a.points
points = np.roll(points, int(len(points)/4), axis=0)
m2a.points = points
self.play(Transform(m1a,m1b),Transform(m2a,m2b), run_time=1)
Last updated