svg animation on an animated path - animation

I have a piece of svg code that links an animation to a simple path and that works well :
<path id="p0" d="M 110,150 C 300,80 400,300 450,100 500,-100 -90,220 110,150"
stroke="black" fill="none" stroke-width="1" />
<ellipse rx="20" ry="12" fill="#aaa" stroke="#666" stroke-width="2" opacity=".8">
<animateMotion id="One" dur="10s" fill="freeze"
rotate="auto" repeatCount="indefinite" >
<mpath xlink:href="#p0"/>
</animateMotion>
</ellipse>
Now I need to have the same animation on an animated path. I tried the following :
<path id = "p0" stroke = "black" stroke-width = "1" fill = "none" >
<animate attributeName="d" dur="2s" repeatCount="indefinite" values=
"M 100 200
C 100 150 250 150 100 100
C 0 50 100 400 100 200;
M 100 200
C 100 150 250 150 100 100
C 0 50 100 100 100 200;
M 100 200
C 100 150 250 150 100 100
C 0 50 100 400 100 200" />
</path>
<ellipse rx="20" ry="12" fill="#aaa" stroke="#666" stroke-width="2" opacity=".8">
<animateMotion id="One" dur="10s" fill="freeze"
rotate="auto" repeatCount="indefinite" >
<mpath xlink:href="#p0"/>
</ellipse>
But to no avail : the ellipse doesn't follow the path and remains invisible...
Any idea ? Is it at least possible ?
Thanks in advance.
EDIT : I just noticed that it works on Firefox, but the problem is that it links the ellipse to the first part of the path values only; in other words it doesn't follow the path animation... :(

If you give the path a valid d attribute, it seems to work in Chrome.
<svg width="500" height="500">
<path id = "p0" stroke = "black" stroke-width = "1" fill = "none"
d="M 100 200 C 100 150 250 150 100 100 C 0 50 100 400 100 200">
<animate attributeName="d" dur="2s" repeatCount="indefinite" values=
"M 100 200
C 100 150 250 150 100 100
C 0 50 100 400 100 200;
M 100 200
C 100 150 250 150 100 100
C 0 50 100 100 100 200;
M 100 200
C 100 150 250 150 100 100
C 0 50 100 400 100 200" />
</path>
<ellipse rx="20" ry="12" fill="#aaa" stroke="#666" stroke-width="2" opacity=".8">
<animateMotion id="One" dur="10s" fill="freeze"
rotate="auto" repeatCount="indefinite" >
<mpath xlink:href="#p0"/>
</animateMotion>
</ellipse>
</svg>

Related

What am I doing wrong trying to rotate some SVG through an animateTransform tag with "spline" calcmode?

I'm trying to animate a triangle so that its rotation accelerates (and later comes to a stop after a full rotation) and I am trying to use the animateTransform SVG tag, with the calcmode attribute set to "spline".. I can easily get the triangle to rotate with a continuous motion, but it's not the smooth acceleration I'm looking for.
Here is the code I'm using - when viewing this in Firefox it shows no animation at all :
<svg xml:lang="fr" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<style type="text/css">
<![CDATA[
*{
fill:none;
stroke:black;
}
]]>
</style>
<g class="triangle">
<path d="M 349.9999999999999 163.3974596215561 L 200 250.00000000000006 " />
<path d="M 200 250.00000000000006 L 350 336.60254037844385 " />
<path d="M 350 336.60254037844385 L 349.9999999999999 163.3974596215561 " />
<animateTransform
attributeName="transform"
type="rotate"
from="0 300 250" to="360 300 250"
begin="0s" dur="10s"
repeatCount="indefinite"
calcmode="spline"
keyTimes="0; 0.25; 0.5; 0.75; 1"
keySplines="0.5 0 0.5 1; 0.5 0 0.5 1; 0.5 0 0.5 1; 0.5 0 0.5 1"
/>
</g>
</svg>
What am I doing wrong? Should I use CSS animations instead? Any kind of non-continuous rotation would be a win.
I tried various value combinations for the keyTimes / keySplines attributes, but the only result I got was a continuous rotation - for example, with
keyTimes="0; 1"
keySplines="0.5 0 0.5 1"
The SVG specification says:
For animations specified with a ‘values’ list, the ‘keyTimes’ attribute if specified must have exactly as many values as there are in the ‘values’ attribute. For from/to/by animations, the ‘keyTimes’ attribute if specified must have two values.
So we either need to reduce the number of keyTimes values or move to using values and then have a matching number of values.
I've set some example values below and the shape now rotates at various speeds.
html, body {
width: 100%;
height: 100%;
}
svg {
width: 100%;
height: 200%;
transform: translate(-100px, -130px);
}
<svg xml:lang="fr" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<style type="text/css">
<![CDATA[
path {
fill:none;
stroke:black;
stroke-width:10px;
}
]]>
</style>
<g class="triangle">
<path d="M 349.9999999999999 163.3974596215561 L 200 250.00000000000006 " />
<path d="M 200 250.00000000000006 L 350 336.60254037844385 " />
<path d="M 350 336.60254037844385 L 349.9999999999999 163.3974596215561 " />
<animateTransform
attributeName="transform"
type="rotate"
values="0 300 250;100 300 250;130 300 250;200 300 250;360 300 250"
begin="0s" dur="10s"
repeatCount="indefinite"
calcmode="spline"
keyTimes="0; 0.25; 0.5; 0.75; 1"
keySplines="0.5 0 0.5 1; 0.5 0 0.5 1; 0.5 0 0.5 1; 0.5 0 0.5 1"
/>
</g>
</svg>

SVG dashed line animation (draw and flow together)

I have to make svg animation with dashed line.
I want to draw a line along with the flow.
Or draw line first and if drawing is finish, start flow.
But I can only implement separately.
How can I animate it together?
Help please..
This is my code.
https://codepen.io/hyhong/pen/KKBpvKQ
.item1 .dashed {
stroke-dasharray: 10 7;
}
.item1 .path {
animation: draw 1.5s alternate linear forwards;
}
#keyframes draw {
from {
stroke-dashoffset: 364;
}
to {
stroke-dashoffset: 0;
}
}
.item2 .path {
stroke-dasharray: 10;
stroke-dashoffset: 100;
animation: flow 3s linear infinite;
}
#keyframes flow {
100% {
stroke-dashoffset: 0;
}
}
<!-- draw svg -->
<svg class="item1" width="700" height="700">
<defs>
<path
id="dashed" class="dashed"
d="M 280 270 L 470 410 L 410 460 L 450 490"
stroke-miterlimit="10"
fill="none"
stroke="white"
stroke-width="5"
></path>
<mask id="mask">
<use xlink:href="#dashed"/>
</mask>
</defs>
<path
class="path"
d="M 280 270 L 470 410 L 410 460 L 450 490"
mask="url(#mask)"
stroke-miterlimit="10"
fill="none"
stroke="#3835B9"
stroke-width="5"
stroke-dasharray="364"
stroke-dashoffet="364"
></path>
</svg>
<!-- flow svg -->
<svg class="item2" width="700" height="700">
<path
class="path"
d="M 280 270 L 470 410 L 410 460 L 450 490"
stroke-miterlimit="10"
fill="none"
stroke="#3835B9"
stroke-width="5"
></path>
</svg>
I don't get your question 100%. but SVG SMIL Animation can be used in a situation where you need to time the starting (and ending) of different animations using <animate>. So, here are three examples where I animate stroke-dashoffset.
In the first the animations are independent, the green just starts 2 seconds after the red. In the second example the green starts when the red ends. You can see that the begin attribute refer to the the id of the red. The same goes for the third example.
<p>Green begin 2s after red:</p>
<svg viewBox="0 0 250 10">
<path d="M 20 5 h 100" stroke="red" stroke-width="10"
stroke-dasharray="100" stroke-dashoffset="100" pathLenth="100">
<animate attributeName="stroke-dashoffset"
values="100;-100" dur="4s" fill="freeze"
repeatCount="1" />
</path>
<path d="M 120 5 h 100" stroke="green" stroke-width="10"
stroke-dasharray="100" stroke-dashoffset="100" pathLenth="100">
<animate attributeName="stroke-dashoffset"
values="100;0" dur="2s" fill="freeze"
repeatCount="1" begin="2s" />
</path>
</svg>
<p>Green begin when red ends:</p>
<svg viewBox="0 0 250 10">
<path d="M 20 5 h 100" stroke="red" stroke-width="10"
stroke-dasharray="100" stroke-dashoffset="100" pathLenth="100">
<animate id="a1" attributeName="stroke-dashoffset"
values="100;-100" dur="4s" fill="freeze"
repeatCount="1" />
</path>
<path d="M 120 5 h 100" stroke="green" stroke-width="10"
stroke-dasharray="100" stroke-dashoffset="100" pathLenth="100">
<animate attributeName="stroke-dashoffset"
values="100;0" dur="2s" fill="freeze"
repeatCount="1" begin="a1.end" />
</path>
</svg>
<p>Green begin 1s after red begin:</p>
<svg viewBox="0 0 250 10">
<path d="M 20 5 h 100" stroke="red" stroke-width="10"
stroke-dasharray="100" stroke-dashoffset="100" pathLenth="100">
<animate id="a2" attributeName="stroke-dashoffset"
values="100;-100" dur="4s" fill="freeze"
repeatCount="1" />
</path>
<path d="M 120 5 h 100" stroke="green" stroke-width="10"
stroke-dasharray="100" stroke-dashoffset="100" pathLenth="100">
<animate attributeName="stroke-dashoffset"
values="100;0" dur="2s" fill="freeze"
repeatCount="1" begin="a2.begin+1s" />
</path>
</svg>
You can use the values from the css in the SVG "animate" property. The CSS animation is looping the "stroke-dashoffset" values from 100 to 0. On the other hand it overrides the stroke-dasharray="10 7" to stroke-dasharray="10" without reason. So you can use the original "10 7" (10 pixel dash, 7 pixel gap) design, if you like to.
<svg width="250" height="250">
<path
d="M 30 10 L 220 150 L 160 200 L 200 230"
stroke-miterlimit="10"
fill="none"
stroke="#3835B9"
stroke-width="5"
stroke-dasharray="10"
stroke-dashoffset="1">
<animate
attributeName="stroke-dashoffset"
values="100;0"
dur="3s"
calcMode="linear"
repeatCount="indefinite" />
</path>
</svg>

SVG path morph easing

i'd like to control the interpolation of a path animation in svg, but i can only get it working with linear interpolation. I've create this code pen as a simple example:
http://codepen.io/adamdaly/pen/yzhCm/
As you can see the black path is animating correctly using linear interpolation, but the red line that is set up for non-linear isn't animating. How should this work?
Adam
You have a couple of issues which cause the animation to be in error and so be ignored.
There must be exactly as many keyTimes entries as values.
There must be one fewer keySplines entry than keyTimes entries
You aren't following these rules and so the animation is in error. Here's one way to correct things...
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 400 400" width="100%" height="100%">
<path d="M100 200 c0,-100 200,-100 200,0" stroke="#000000" stroke-width="4" fill="none">
<animate
dur="2"
repeatCount="indefinite"
attributeName="d"
from="M100 200 c0,-100 200,-100 200,0"
to="M100 200 c0,-100 200,-100 200,0"
values="M100 200 c50,-100 250,-100 200,0; M100 200 c-50,-100 150,-100 200,0; M100 200 c50,-100 250,-100 200,0">
</animate>
</path>
<path d="M100 200 c0,-100 200,-100 200,0" stroke="#FF0000" stroke-width="4" fill="none">
<animate
dur="2"
repeatCount="indefinite"
attributeName="d"
from="M100 200 c0,-100 200,-100 200,0"
to="M100 200 c0,-100 200,-100 200,0"
values="M100 200 c50,-100 250,-100 200,0; M100 200 c-50,-100 150,-100 200,0; M100 200 c50,-100 250,-100 200,0"
keyTimes="0; 0.5; 1"
keySplines="0.25 0 0.75 1; 0.25 0 0.75 1"
calcMode="spline">
</animate>
</path>
</svg>

SVG: animateMotion with animated path

In this fiddle there are 2 animated paths, curve0:
<path id="curve0" stroke="black" fill="inherit" stroke-width="3"
fill-opacity=".5"
d="M 0,100
C 50,100 50,50, 100,50 C 150,50 150,100 200,100
C 300,100 200,50, 300,50 C 400,50 300,100 400,100">
<animate attributeName="d" dur="5s"
values="M 0,100
C 50,100 50,50, 100,50 C 150,50 150,150 200,150
C 250,150 250,50, 300,50 C 350,50 350,100 400,100;
M 0,100
C 50,100 50,150, 100,150 C 150,150 150, 50 200, 50
C 250, 50 250,150, 300,150 C 350,150 350,100 400,100;
M 0,100
C 50,100 50,50, 100,50 C 150,50 150,150 200,150
C 250,150 250,50, 300,50 C 350,50 350,100 400,100"
repeatCount="indefinite"/>
</path>
and curve1,
<path id="curve1" stroke="black" fill="yellowgreen" stroke-width="3" fill-opacity=".5" >
<animate attributeName="d" dur="3s"
values="M 100,0 0,100 70,50 130,150 200,100 z;
M 100,0 0,100 70,150 130, 50 200,100 z;
M 100,0 0,100 70,50 130,150 200,100 z"
repeatCount="indefinite"/>
</path>
as you can see the ellipse moves along well with curve0, but if you change curve0 on line #36 and #46 to curve1, the ellipse stops moving.
Why curve1 doesn't work and where in the specification states animateMotion works with one kind of animated path and not the other?
It works, but you need to change the <mpath> element to point to curve1 too, not just the <use> element (which draws the curve).
<animateMotion dur="5s" rotate="auto" repeatCount="indefinite">
<mpath xlink:href="#curve1"/>
</animateMotion>
See fiddle.

SVG animate point in cycle

I have a path with various arcs. I want to animate just a single arc indefinitely. Currently, what I can do is this :
http://jsfiddle.net/gLxkt/1/
<animate id="c1" xlink:href="#p1" attributeName="d" attributeType="XML"
from="M 300 300 C 300 300 600 300 300 400 "
to="M 300 300 C 300 300 400 300 300 400 " dur="1s" fill="freeze" />
<animate id="c2" begin="c1.end" xlink:href="#p1" attributeName="d" attributeType="XML"
from="M 300 300 C 300 300 400 300 300 400 "
to="M 300 300 C 300 300 600 300 300 400 " dur="1s" fill="freeze" />
Which can do this once. How can I make the animation indefinite?
The end="indefinite" makes it repeat and the begin makes it start both at 0s and when the other animation finishes. Continuously repeats in Firefox.
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%"
>
<path id="p1" d="M 300 300 C 300 300 600 300 300 400 " stroke="blue" fill="none" stroke-width="4" />
<g>
<path id="p1" d="M 300 300 C 300 300 600 300 300 400 " stroke="blue" fill="none" stroke-width="4" />
<animate id="c1" begin="c2.end; 0s" end="indefinite" xlink:href="#p1" attributeName="d" attributeType="XML"
from="M 300 300 C 300 300 600 300 300 400 "
to="M 300 300 C 300 300 400 300 300 400 " dur="1s" fill="freeze" />
<animate id="c2" begin="c1.end" end="indefinite" xlink:href="#p1" attributeName="d" attributeType="XML"
from="M 300 300 C 300 300 400 300 300 400 "
to="M 300 300 C 300 300 600 300 300 400 " dur="1s" fill="freeze" />
</g>
</svg>
An easy way is to use the "values" array - which works in Chrome, Firefox & Safari (and I presume Opera) but not IE which has no support for SMIL anyway (although there's a polyfill library somewhere). But Robert's answer is clearly more elegant.
<animate id="c1" xlink:href="#p1" attributeName="d" attributeType="XML"
values="M 300 300 C 300 300 600 300 300 400;M 300 300 C 300 300 400 300 300 400;M 300 300 C 300 300 600 300 300 400" dur="2s" repeatCount="indefinite" fill="freeze" />
http://jsfiddle.net/gLxkt/2/

Resources