Source code for svgwriter.model.elements.animation._elements

"""
The animation base and ``animate`` and ``set``.
"""

from __future__ import annotations

from collections.abc import Sequence
from typing import TYPE_CHECKING, Any, Literal, TypeAlias

from .._behavior import ConditionalAttributes, CoreAttributes, ExternalResourcesAttribute, XLinkAttributes
from ..core import Category, Child, Element
from ._easing import EaseLike
from ._keywords import AnimationFill, Restart
from ._smil import FadeShortcut, _AttributeTarget, _clock, _ValueAnimation

# region generated by `python scripts/dev.py svgwriter signatures write`: names used by the signatures
if TYPE_CHECKING:
    from collections.abc import Mapping

    from .._behavior import XmlSpace
    from ..core import AttrValue
    from ._keywords import Accumulate, Additive, CalcMode
# endregion

__all__ = ["Animate", "AnimationElement", "Clock", "RepeatCount", "Set"]


Clock: TypeAlias = str | float


RepeatCount: TypeAlias = float | Literal["indefinite"]


[docs] class AnimationElement( XLinkAttributes, ConditionalAttributes, ExternalResourcesAttribute, CoreAttributes, category=Category.ANIMATION, ): """Timing attributes shared by every animation element. Numeric clock values are written in seconds; ``float("inf")`` for ``repeat_count`` is written as ``indefinite``. Parameters ---------- *args : Element Child elements. begin, dur, end, min, max : float or str, optional Clock values: seconds as a number or any SMIL timing string. restart : Restart, optional When the animation may restart. Defaults to ``Restart.ALWAYS``, which is not written. repeat_count : float or "indefinite", optional Number of repeats. repeat_dur : float or str, optional Total duration of the repeats. fill : AnimationFill, optional State after the animation ends. **kwargs Presentation, core and other SVG attributes in Python spelling. See Also -------- Animate : Interpolate an attribute. Set : Assign a value. Examples -------- >>> str(Set("fill", "red", begin=1, dur=2)) '<set attributeName="fill" begin="1s" dur="2s" to="red"/>' """ def __init__( self, *args: Child, begin: Clock | None = None, dur: Clock | None = None, end: Clock | None = None, min: Clock | None = None, max: Clock | None = None, restart: Restart = Restart.ALWAYS, repeat_count: RepeatCount | None = None, repeat_dur: Clock | None = None, fill: AnimationFill | None = None, **kwargs: Any, ) -> None: super().__init__(*args, **kwargs) self._assign( begin=_clock(begin), dur=_clock(dur), end=_clock(end), min=_clock(min), max=_clock(max), restart=None if restart is Restart.ALWAYS else restart, repeat_count=repeat_count, repeat_dur=_clock(repeat_dur), fill=fill, )
[docs] class Animate(FadeShortcut, AnimationElement, _ValueAnimation, _AttributeTarget, tag="animate"): """``<animate>``: interpolate one attribute over time. Parameters ---------- attribute_name : str, optional The attribute to animate. attribute_type : {"CSS", "XML", "auto"}, optional Namespace of the attribute. values : sequence or str, optional The values to pass through. from_ : object, optional Start value (written ``from``). to : object, optional End value. by : object, optional Relative change. calc_mode : CalcMode, optional Interpolation mode. key_times : sequence of float or str, optional Times of each value, 0 to 1. key_splines : sequence of 4-tuples or str, optional Bezier control points for spline mode. additive : Additive, optional Combination with the underlying value. accumulate : Accumulate, optional Whether repeats accumulate. ease : str, tuple, callable or Ease, optional Timing curve over the whole timeline (see :class:`Ease`); sets ``calcMode``, ``keyTimes`` and ``keySplines`` (or samples ``values``), so leave ``calc_mode`` unset or ``"linear"``/``"discrete"``. **kwargs Timing (``begin``, ``dur``, ``repeat_count`` and so on, see :class:`AnimationElement`), core and other SVG attributes. See Also -------- Set : Assign a value instead of interpolating. AnimateTransform : Animate the ``transform`` attribute. Examples -------- >>> str(Animate.fade(1, 0, dur=2)) '<animate attributeName="opacity" from="1" to="0" dur="2s"/>' >>> str(Animate(attribute_name="x", from_=0, to=90, dur=1, ease="ease-in-out")) '<animate attributeName="x" from="0" to="90" calcMode="spline" keyTimes="0;1" keySplines="0.42 0 0.58 1" dur="1s"/>' """ # region generated by `python scripts/dev.py svgwriter signatures write` def __init__( self, *args: Child, begin: Clock | None = None, dur: Clock | None = None, end: Clock | None = None, min: Clock | None = None, max: Clock | None = None, restart: Restart | None = None, repeat_count: RepeatCount | None = None, repeat_dur: Clock | None = None, fill: AnimationFill | None = None, values: Sequence[Any] | str | None = None, from_: Any = None, to: Any = None, by: Any = None, calc_mode: CalcMode | None = None, key_times: Sequence[float] | str | None = None, key_splines: Sequence[tuple[float, float, float, float]] | str | None = None, additive: Additive | None = None, accumulate: Accumulate | None = None, ease: EaseLike | None = None, attribute_name: str | None = None, attribute_type: Literal["CSS", "XML", "auto"] | None = None, href: str | Element | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any, ) -> None: """Create a ``<animate>`` element. Parameters ---------- *args, begin, dur, end, min, max, restart, repeat_count, repeat_dur, fill, values, from_, to, by, calc_mode, key_times, key_splines, additive, accumulate, ease, attribute_name, attribute_type See the class docstring. href, xlink_title, xlink_show, xlink_actuate, xlink_role, xlink_arcrole, required_features, required_extensions, system_language, external_resources_required, xml_base, xml_lang, xml_space, id, attrs The attribute groups of :class:`~svgwriter.XLinkAttributes`, :class:`~svgwriter.ConditionalAttributes`, :class:`~svgwriter.ExternalResourcesAttribute`, :class:`~svgwriter.CoreAttributes`, :class:`~svgwriter.Element`. Defaults to None (unset). **kwargs Any other SVG attribute in Python spelling, including namespaced and open ones (``inkscape_label``, ``data_*``). """ if begin is not None: kwargs["begin"] = begin if dur is not None: kwargs["dur"] = dur if end is not None: kwargs["end"] = end if min is not None: kwargs["min"] = min if max is not None: kwargs["max"] = max if restart is not None: kwargs["restart"] = restart if repeat_count is not None: kwargs["repeat_count"] = repeat_count if repeat_dur is not None: kwargs["repeat_dur"] = repeat_dur if fill is not None: kwargs["fill"] = fill if values is not None: kwargs["values"] = values if from_ is not None: kwargs["from_"] = from_ if to is not None: kwargs["to"] = to if by is not None: kwargs["by"] = by if calc_mode is not None: kwargs["calc_mode"] = calc_mode if key_times is not None: kwargs["key_times"] = key_times if key_splines is not None: kwargs["key_splines"] = key_splines if additive is not None: kwargs["additive"] = additive if accumulate is not None: kwargs["accumulate"] = accumulate if ease is not None: kwargs["ease"] = ease if attribute_name is not None: kwargs["attribute_name"] = attribute_name if attribute_type is not None: kwargs["attribute_type"] = attribute_type self._forward_common( kwargs, id, attrs, xml_base, xml_lang, xml_space, required_features, required_extensions, system_language, external_resources_required, href, xlink_title, xlink_show, xlink_actuate, xlink_role, xlink_arcrole ) super().__init__(*args, **kwargs)
# endregion
[docs] class Set(AnimationElement, _AttributeTarget, tag="set"): """``<set>``: assign a value for a duration. Parameters ---------- attribute_name : str, optional The attribute to set. to : object, optional The value to assign. **kwargs Timing (see :class:`AnimationElement`), core and other SVG attributes. See Also -------- Animate : Interpolate instead of assigning. Examples -------- >>> str(Set("visibility", "hidden", begin=2)) '<set attributeName="visibility" begin="2s" to="hidden"/>' """ # region generated by `python scripts/dev.py svgwriter signatures write` def __init__( self, attribute_name: str | None = None, to: Any = None, *, begin: Clock | None = None, dur: Clock | None = None, end: Clock | None = None, min: Clock | None = None, max: Clock | None = None, restart: Restart | None = None, repeat_count: RepeatCount | None = None, repeat_dur: Clock | None = None, fill: AnimationFill | None = None, attribute_type: Literal["CSS", "XML", "auto"] | None = None, href: str | Element | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any, ) -> None: """Create a ``<set>`` element. Parameters ---------- attribute_name, to, begin, dur, end, min, max, restart, repeat_count, repeat_dur, fill, attribute_type See the class docstring. href, xlink_title, xlink_show, xlink_actuate, xlink_role, xlink_arcrole, required_features, required_extensions, system_language, external_resources_required, xml_base, xml_lang, xml_space, id, attrs The attribute groups of :class:`~svgwriter.XLinkAttributes`, :class:`~svgwriter.ConditionalAttributes`, :class:`~svgwriter.ExternalResourcesAttribute`, :class:`~svgwriter.CoreAttributes`, :class:`~svgwriter.Element`. Defaults to None (unset). **kwargs Any other SVG attribute in Python spelling, including namespaced and open ones (``inkscape_label``, ``data_*``). """ if begin is not None: kwargs["begin"] = begin if dur is not None: kwargs["dur"] = dur if end is not None: kwargs["end"] = end if min is not None: kwargs["min"] = min if max is not None: kwargs["max"] = max if restart is not None: kwargs["restart"] = restart if repeat_count is not None: kwargs["repeat_count"] = repeat_count if repeat_dur is not None: kwargs["repeat_dur"] = repeat_dur if fill is not None: kwargs["fill"] = fill if attribute_type is not None: kwargs["attribute_type"] = attribute_type self._forward_common( kwargs, id, attrs, xml_base, xml_lang, xml_space, required_features, required_extensions, system_language, external_resources_required, href, xlink_title, xlink_show, xlink_actuate, xlink_role, xlink_arcrole ) # endregion super().__init__(attribute_name=attribute_name, **kwargs) self._assign(to=to)