I want to create an transition from the current state to a new state for an arbitrary attribute. I have this code:
const animate: SVGAnimateElement;
function transition(attribute: keyof SVGGElement, to: string) {
const initialState = animate.parentElement[attribute].animVal;
animate.setAttribute('from', initialState);
animate.setAttribute('to', to);
animate.beginElement();
}
This works fine for string or numeric attributes, but sometimes the attribute will be an SVGRect
or an SVGTransformList
or any other non-string type. setAttribute
only accepts strings.
Is there a good way to either:
- Convert an arbitrary attribute value from
animState
to a string suitable for use insetAttribute
. - Directly set the value in animate to a more complex object like
SVGRect
orSVGTransformList
- Start the animation from the current state in some other way.
I can’t use CSS transititions since it doesn’t support some attributes (like viewBox)
2
Answers
You need the
value
property on theanimVal
object to get the current value. An then you also need to change theattributeName
attribute.You will have to write case distinctions yourself, and even a serialization method at least in some cases like
SVGRect
.SVGTransformList
can be serialized viaSVGTransformList.consolidate().matrix.toString()
.No. The spec states:
Manipulating the animation timeline would be the domain of WAAPI, as it was initially intended. But in fact it is only implemented for CSS animations and transitions. Nobody works on implementing it for SMIL, or even on writing an appropriate spec.