SVGPath

Structured representation of an SVG path that supports morphing

Stores path as a list of command objects (MoveTo, LineTo, etc.) rather than a string, enabling smooth interpolation between paths.

Constructor

SVGPath(
    commands: list[PathCommand],
    path_string: str | None = None
) -> None

Methods

from_string

from_string(path_string: str) -> SVGPath

Parses an SVG path data string (e.g., "M 0,0 L 100,100") into an SVGPath object.

Parameters

path_string
The raw SVG path data string.
Raises
ValueError
If parsing encounters an unexpected command or missing coordinates.

interpolate

interpolate(
    path1: 'SVGPath',
    path2: 'SVGPath',
    t: float,
    auto_normalize: bool = True
) -> 'SVGPath'

Interpolate between two paths with automatic normalization

Parameters

path1
Starting path.
path2
Ending path.
t
Interpolation factor (0.0 to 1.0).
auto_normalize
If True, automatically normalize paths for morphing.
Raises
ValueError
If paths are incompatible and auto_normalize fails

is_compatible_for_morphing

is_compatible_for_morphing(other: SVGPath) -> bool

Check if two paths can be morphed.

Parameters

other
Path to check compatibility with.

length

length(num_samples: int = 100) -> float

Calculate the total length of the path using adaptive sampling.

Parameters

num_samples
Number of samples per curve segment for accurate length calculation

Returns

Total path length in user units

normalize_for_morphing

normalize_for_morphing(other: 'SVGPath') -> tuple['SVGPath', 'SVGPath']

Normalize two paths to be compatible for morphing

Parameters

other
Path to normalize with

Returns

Tuple of (normalized_self, normalized_other) Raises: ValueError: If paths can't be made compatible

to_absolute

to_absolute()

Convert all commands to absolute coordinates.

to_cubic_beziers

to_cubic_beziers()

Convert all curve commands to cubic Bezier curves

to_string

to_string()

Convert to SVG path data string (e.g., "M 0,0 L 100,100")