SVG animation with timing function not working in Firefox - animation

Works on Chrome/Safari.
<svg height="50" viewBox="0 0 16 10">
<rect class="background" width="16" height="10"></rect>
<rect class="vertical" width="2" x="5">
<animate calcMode="spline" keyTimes="0;0.22;0.33;0.55;0.66;0.88;1" keySplines="0.1 0.8 0.2 1; 0.1 0.8 0.2 1; 0.1 0.8 0.2 1; 0.1 0.8 0.2 1; 0.1 0.8 0.2 1; 0.1 0.8 0.2 1" attributeName="height" from="0" to="10" dur="0.5s" begin="1.3s" fill="freeze"></animate>
</rect>
<rect class="horizontal" height="2" y="4">
<animate calcMode="spline" keyTimes="0;0.22;0.33;0.55;0.66;0.88;1" keySplines="0.1 0.8 0.2 1; 0.1 0.8 0.2 1; 0.1 0.8 0.2 1; 0.1 0.8 0.2 1; 0.1 0.8 0.2 1; 0.1 0.8 0.2 1" attributeName="width" from="0" to="16" dur="0.5s" begin="0.8s" fill="freeze"></animate>
</rect>
</svg>
If I remove the calcMode, keyTimes and keySplines attributes it starts working in Firefox too.

In SVG1.1 (I don't know about SVG2),
You must have a values attribute along the keyTimes attribute.
specs on keySplines say that :
there must be one fewer sets of control points than there are
‘keyTimes’.
FF is then correct here, and others should raise an error and not animate.
<svg height="50" viewBox="0 0 16 10">
<rect class="vertical" width="2" x="5">
<animate calcMode="spline" values="0;2.2;3.3;5.5;6.6;8.8;10" keyTimes="0;0.22;0.33;0.55;0.66;0.88;1" keySplines="0.1 0.8 0.2 1; 0.1 0.8 0.2 1; 0.1 0.8 0.2 1; 0.1 0.8 0.2 1; 0.1 0.8 0.2 1; 0.1 0.8 0.2 1;" attributeName="height" from="0" to="10" dur="0.5s" begin="1.3s" fill="freeze"></animate>
</rect>
<rect class="horizontal" height="2" y="4">
<animate calcMode="spline" keyTimes="0;0.22;0.33;0.55;0.66;0.88;1" values="0;2.2;3.3;5.5;6.6;8.8;10" keySplines="0.1 0.8 0.2 1; 0.1 0.8 0.2 1; 0.1 0.8 0.2 1; 0.1 0.8 0.2 1; 0.1 0.8 0.2 1; 0.1 0.8 0.2 1" attributeName="width" from="0" to="16" dur="0.5s" begin="0.8s" fill="freeze"></animate>
</rect>
</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>

Animate SVG path d=... coordinates without stroke

I'm trying to animate the following GIF in SVG.
Code:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36" width="100" height="100">
<path d="M18.53.359h.225A16.684 16.684 0 0130.56 5.12a16.902 16.902 0 014.813 12.007A18.372 18.372 0 0130.18 29.66l-.671.672a18.215 18.215 0 01-11.99 5.163l-.436.012h-.13A16.757 16.757 0 015.12 30.662a16.845 16.845 0 01-4.74-12.08A18.35 18.35 0 015.513 6.114l.307-.311A18.203 18.203 0 0118.095.371l.435-.012zM16.413 2.6c-3.508 0-6.778 1.443-9.558 4.253-3.306 3.299-4.719 7.273-4.102 11.522.495 3.425 2.323 6.938 5.012 9.63l.31.303c4.452 4.235 13.288 7.796 20.265 1.086l.248-.244.236-.248.354-.344a12.592 12.592 0 003.686-11.167 17.578 17.578 0 00-4.95-9.618c-2.689-2.699-6.17-4.532-9.597-5.032-.63-.093-1.267-.14-1.904-.141z">
<animate
attributeName="d"
from="start_path"
to="end_path"
dur="0.66s"
fill="freeze"
repeatCount="indefinite"
/>
</path>
</svg>
The solution I have is using clip-path but just wanna try using path coordinates if possible.
Perhaps the simplest solution is to use a circle as a mask. Then animate the dash array of the circle.
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36" width="100" height="100">
<defs>
<mask id="wipe">
<circle cx="18" cy="18" r="16"
fill="none" stroke="white" stroke-width="5" stroke-dasharray="100.6" stroke-dashoffset="100.6"
transform="rotate(-35,18,18)">
<animate attributeName="stroke-dashoffset"
from="100.6"
to="0"
dur="0.66s"
repeatCount="indefinite"/>
</circle>
</mask>
</defs>
<path d="M18.53.359h.225A16.684 16.684 0 0130.56 5.12a16.902 16.902 0 014.813 12.007A18.372 18.372 0 0130.18 29.66l-.671.672a18.215 18.215 0 01-11.99 5.163l-.436.012h-.13A16.757 16.757 0 015.12 30.662a16.845 16.845 0 01-4.74-12.08A18.35 18.35 0 015.513 6.114l.307-.311A18.203 18.203 0 0118.095.371l.435-.012zM16.413 2.6c-3.508 0-6.778 1.443-9.558 4.253-3.306 3.299-4.719 7.273-4.102 11.522.495 3.425 2.323 6.938 5.012 9.63l.31.303c4.452 4.235 13.288 7.796 20.265 1.086l.248-.244.236-.248.354-.344a12.592 12.592 0 003.686-11.167 17.578 17.578 0 00-4.95-9.618c-2.689-2.699-6.17-4.532-9.597-5.032-.63-.093-1.267-.14-1.904-.141z" mask="url(#wipe)"/>
</svg>
You'll probably want to tweak the animation to add a short pause at the end. And to fade the colours as per your GIF version.
Here is path d solution with 2 arcs, whithout stroke, only fill:
let r = 90,
rx = 70/90,
ry = 85/90,
circ = Math.PI*2;
requestAnimationFrame(draw)
function draw(t) {
t = (t/1000)%1;
let a = t*circ,
la = a % circ > Math.PI?1:0,
x = r * Math.cos(a),
y = r * Math.sin(a);
path.setAttribute("d", [
"M", r, 0,
"A", r, r, 0, la, 1, x, y,
"L", rx*x, ry*y,
"A", r*rx, r*ry, 0, la, 0, r*rx, 0,
"Z"
].join(" "));
path.setAttribute("opacity", 1-t);
requestAnimationFrame(draw);
}
<svg width=90vw height=90vh viewBox="-100,-100,200,200" >
<path id="path" transform=rotate(-45) ></path>
</svg>

SVG animateMotion (calcMode spline) not working in FF and Safari

I want to move a circle along a path inside an svg with different easing. I wanted to use animateMotion but have never used it before. Using JS is not an option in this case.
It works fine in Chrome and Opera, but not in Safari and Firefox.
<animateMotion
dur="4s"
repeatCount="indefinite"
calcMode="spline"
keySplines="0 0 0.5 1 ; 0 0 0.5 1 ; 0.3 0 1 1"
keyTimes="0 ; 0.3 ; 0.6 ; 1"
path="M54,183.8c0-86.5,128.5-87.2,128.5,0c0-86.5,116.9-87.2,116.9,0c0-86.5,115.1-87.2,115.1,0
c0-86.5,55.5-123.5,55.5,111" />
If I remove calcMode, keySplines and keyTimes it works in all browsers.
I would also appreciate any alternative solution for moving an element on rounded curves with different easings.
Solution
I figured out, that two things were independently from each other creating the issue:
Safari does not accept spaces between KeyTimes Values (all other browsers do, and Safari also does within KeySplines).
Firefox seems to need one more value for KeyTimes and KeySplines. So I added one value to each line.
Fixed Code:
keySplines="0 0 0.5 1 ; 0 0 0.5 1 ; 0 0 0.5 1 ; 0 0 0.5 1"
keyTimes="0;0.2;0.4;0.6;1"
Full Example:
<svg version="1.1" id="Layer_5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 500 500" style="enable-background:new 0 0 500 500;" xml:space="preserve">
<g>
<circle r="13.5" fill="black">
<animateMotion
dur="4s"
repeatCount="indefinite"
calcMode="spline"
keySplines="0 0 0.5 1 ; 0 0 0.5 1 ; 0 0 0.5 1 ; 0 0 0.5 1"
keyTimes="0;0.2;0.4;0.6;1"
path="M54,183.8c0-86.5,128.5-87.2,128.5,0c0-86.5,116.9-87.2,116.9,0c0-86.5,115.1-87.2,115.1,0c0-86.5,55.5-123.5,55.5,111" />
</circle>
<path stroke="red" fill="none" d="M54,183.8c0-86.5,128.5-87.2,128.5,0c0-86.5,116.9-87.2,116.9,0c0-86.5,115.1-87.2,115.1,0c0-86.5,55.5-123.5,55.5,111"/>
</g>
</svg>

svg animation on an animated path

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>

Resources