← circus

Crossfade

segment animations

Python Code


from dataclasses import replace

from svan2d import (
    CircleState,
    Color,
    configure_logging,
    ConverterType,
    easing,
    Point2D,
    SquareState,
    VElement,
    VScene,
    VSceneExporter,
)
from svan2d.transition import segment

configure_logging(level="INFO")

CIRCLE_COLOR = Color("#FDBE02")
RECTANGLE_COLOR = Color("#AA0000")


def main():
    scene = VScene(width=256, height=256, background=Color("#000017"))

    # Create two different shapes
    c1 = CircleState(
        radius=20,
        pos=Point2D(-90, 0),
        fill_color=CIRCLE_COLOR,
    )
    c2 = replace(c1, radius=60, pos=Point2D(0, 0))

    s1 = SquareState(
        size=100,
        pos=Point2D(0, 0),
        fill_color=RECTANGLE_COLOR,
    )
    s2 = replace(s1, size=40, pos=Point2D(90, 0))

    # Crossfade from circle to rectangle
    circle_out, rectangle_in = segment.crossfade(
        c2,
        s1,
        t_start=0.1,
        t_end=0.9,
        delay=0.2,
        easing_dict={"opacity": easing.in_out_sine},
    )

    elem_circle = VElement().keystate(c1, at=0).segment(circle_out)
    elem_rectangle = VElement().segment(rectangle_in).keystate(s2, at=1)

    scene = scene.add_elements([elem_circle, elem_rectangle])

    # Export
    exporter = VSceneExporter(
        scene=scene,
        converter=ConverterType.PLAYWRIGHT_HTTP,
        output_dir="output/",
    )

    exporter.to_mp4(
        filename="crossfade",
        total_frames=90,
        framerate=30,
        png_width_px=1024,
    )


if __name__ == "__main__":
    main()