← circus
Slide - Hold - Slide
segment animations
Python Code
"""Example: M->N shape group morphing using ShapeCollectionState
Demonstrates morphing between different numbers of independent shapes:
- 3 triangles + 2 squares -> 1 ellipse + 3 rectangles
Each shape has independent colors and properties. The mapping uses
the same VertexLoopMapper strategies as hole morphing (ClusteringMapper by default).
"""
from svan2d.component.state import (
ShapeCollectionState,
TriangleState,
SquareState,
EllipseState,
RectangleState,
)
from svan2d.converter.converter_type import ConverterType
from svan2d.core.logger import configure_logging
from svan2d.core.point2d import Point2D
from svan2d.velement import VElement
from svan2d.velement.segments import hold
from svan2d.vscene import VScene
from svan2d.vscene.vscene_exporter import VSceneExporter
from svan2d.core.color import Color
configure_logging(level="INFO")
def main():
# Create the scene
scene = VScene(width=256, height=256, background=Color("#000017"))
state1 = ShapeCollectionState(
shapes=[
TriangleState(
pos=Point2D(-70, -50),
size=30,
fill_color=Color("#F3B700"),
stroke_color=Color("#ffffff"),
stroke_width=2,
),
TriangleState(
pos=Point2D(0, -50),
size=30,
fill_color=Color("#FAA300"),
stroke_color=Color("#ffffff"),
stroke_width=2,
),
TriangleState(
pos=Point2D(70, -50),
size=30,
fill_color=Color("#E57C04"),
stroke_color=Color("#ffffff"),
stroke_width=2,
),
SquareState(
pos=Point2D(-50, 50),
size=40,
fill_color=Color("#F63E02"),
stroke_color=Color("#ffffff"),
stroke_width=2,
),
SquareState(
pos=Point2D(50, 50),
size=40,
fill_color=Color("#FF0000"),
stroke_color=Color("#ffffff"),
stroke_width=2,
),
]
)
state2 = ShapeCollectionState(
shapes=[
EllipseState(
pos=Point2D(0, -60),
rx=70,
ry=30,
fill_color=Color("#FDBE02"),
stroke_color=Color("#ffffff"),
stroke_width=4,
),
RectangleState(
pos=Point2D(90, 50),
width=50,
height=30,
fill_color=Color("#FDBE02"),
stroke_color=Color("#ffffff"),
stroke_width=4,
),
]
)
# Create element with morphing animation
states = [state1, state2, state1]
element = VElement().segment(hold(states=states, duration=1.0 / (2 * len(states))))
scene = scene.add_element(element)
# Create the exporter
exporter = VSceneExporter(
scene=scene,
converter=ConverterType.PLAYWRIGHT,
output_dir="output/",
)
# Export to MP4
exporter.to_mp4(
filename="shape_collection_morph",
total_frames=180,
framerate=30,
png_width_px=1024,
)
if __name__ == "__main__":
main()