I have an animated svg line using css.
I want animation takes 200s as duration, but I want that line restart automatically again after it finish.
This is an example of my code.
line {
stroke-linejoin: round;
stroke-linecap: round;
stroke-dasharray: 500%;
stroke-dasharray: 0 \0/;
stroke-dashoffset: 0 \0/;
-webkit-animation: draw 200s infinite;
animation: draw 200s infinite;
}
/* Safari 4.0 - 8.0 */
#-webkit-keyframes draw {
0% {
stroke-dashoffset: 500%;
},
100% {
stroke-dashoffset: 0%;
}
}
#keyframes draw {
0% {
stroke-dashoffset: 500%;
},
100% {
stroke-dashoffset: 0%;
}
}
<body>
<svg height="100" width="250">
<line x1="25" y1="30" x2="45" y2="30" style="stroke:rgb(255,0,0);stroke-width:4" />
</svg>
</body>
In order to restart the animation, I changed animation tag:
-webkit-animation: draw 200s 2s infinite;
animation: draw 200s 2s infinite;
However, the effect I got was:
first: the line ends after two seconds.
It starts over with the desired duration (200s). However, after it finish, it doesn't start again immediately.
How can I restart animation automatically after it finish. Do I need to use javascript or jquery?
The issue is that the duration of your animation is set to 200s, therefore according to your CSS it will loop over again after 200 seconds and a further 2 seconds delay. From what I understand, you want the line to be drawn slowly and therefore you're using a 200s animation, which isn't the best way to achieve that -- at least not since you want the animation to restart after a short delay.
You can make the line slower by using ease-in in conjunction with changing the animation as per below. This should achieve the desired affect you're looking for.
line {
stroke-linejoin: round;
stroke-linecap: round;
stroke-dasharray: 500%;
stroke-dasharray: 0 \0/;
stroke-dashoffset: 0 \0/;
-webkit-animation: draw 2s ease-in infinite;
animation: draw 2s ease-in infinite;
}
/* Safari 4.0 - 8.0 */
#-webkit-keyframes draw {
0% {
stroke-dashoffset: 500%;
},
99% {
stroke-dashoffset: 500%;
},
100% {
stroke-dashoffset: 0%;
}
}
#keyframes draw {
0% {
stroke-dashoffset: 500%;
},
99% {
stroke-dashoffset: 500%;
},
100% {
stroke-dashoffset: 0%;
}
}
<body>
<svg height="100" width="250">
<line x1="25" y1="30" x2="45" y2="30" style="stroke:rgb(255,0,0);stroke-width:4" />
</svg>
</body>
I have a SVG path that I'm trying to animate to "draw" itself, using the stroke-dasharray/stroke-dashoffset combination trick (see this article for more info). However, that trick does not work on this path, despite (as far as I can tell) everything being correctly implemented. So, my question is, what have I done wrong here?
This is the path in question:
<path class="cls-1" d="M13.36,28.18c-8.06,5.19-9.74,17-4,24.91a31.38,31.38,0,0,0,3.19-4.71L34.92,9.74C38.67,3.19,44.1,0,48.65,0,65.17,0,63.9,21,47.13,26.66c16,10.62,4.47,40.4-20.36,40.4C-2.29,67.06-7.39,35.05,10,24ZM35,27.94l-2.24-.24-14,24.19a42.77,42.77,0,0,1-4.15,5.91,23.84,23.84,0,0,0,12,2.87C46.73,60.67,54.48,32,35,27.94Zm.56-5.11c8.46-.16,13.17-2,16.36-8,4.15-7.82-3.59-14-9.66-3.51Z"></path>
And the CSS (simplified for example) I'm using:
path {
stroke-dasharray: 415.9850769042969;
stroke-dashoffset: 415.9850769042969;
animation: letterB 5s linear infinite;
}
#keyframes letterB {
to {
stroke-dashoffset: 0;
}
}
I have tried:
Adjusting the length of the dashoffset/dasharray
Testing in other browsers (Safari 11.0.3, Firefox 57.0.4)
Not really sure what to do, or what's up, so any guidance on why this animation isn't working would be greatly appreciated.
Also, I created a reduced case on JSFiddle.
Primary environment:
Chrome v64.0.3282.140
You will need to set the fill:none to the svg to aniamtion take place...also a stroke and stroke-width...
...actually the idea is here to animate your stroke
Stack Snippet
svg {
padding: 20px;
}
path {
stroke-dasharray: 415.9850769042969;
stroke-dashoffset: 415.9850769042969;
animation: letterB 5s linear forwards infinite;
-webkit-animation: letterB 5s linear forwards infinite;
}
#keyframes letterB {
0% {
stroke-dashoffset: 415.9850769042969;
}
100% {
stroke-dashoffset: 0;
}
}
#-webkit-keyframes letterB {
0% {
stroke-dashoffset: 415.9850769042969;
}
100% {
stroke-dashoffset: 0;
}
}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 302.67 67.06">
<path class="cls-1" d="M13.36,28.18c-8.06,5.19-9.74,17-4,24.91a31.38,31.38,0,0,0,3.19-4.71L34.92,9.74C38.67,3.19,44.1,0,48.65,0,65.17,0,63.9,21,47.13,26.66c16,10.62,4.47,40.4-20.36,40.4C-2.29,67.06-7.39,35.05,10,24ZM35,27.94l-2.24-.24-14,24.19a42.77,42.77,0,0,1-4.15,5.91,23.84,23.84,0,0,0,12,2.87C46.73,60.67,54.48,32,35,27.94Zm.56-5.11c8.46-.16,13.17-2,16.36-8,4.15-7.82-3.59-14-9.66-3.51Z"
fill="none" stroke-width="2" stroke="#000000"></path>
</svg>
I have created an SVG animation tick here: https://plnkr.co/edit/5FlA5j8iXO4EPCzxAugs?p=preview
How can i reduce the size of the tick? For example, to half of the size shown?
#check {
fill: none;
stroke: green;
stroke-width: 20;
stroke-dasharray: 180;
stroke-dashoffset: 180;
-webkit-animation: draw 1.2s infinite ease;
animation: draw 1.2s infinite ease;
animation-fill-mode: forwards;
animation-iteration-count: 1;
}
-webkit-#keyframes draw {
to {
stroke-dashoffset: 0;
}
}
#keyframes draw {
to {
stroke-dashoffset: 0;
}
}
<svg width="150" height="150">
<path id="check" d="M10,30 l30,50 l95,-70" />
</svg>
You can use css transform: scale(0.5); to #check like this:
The CSS transform property lets you modify the coordinate space of the
CSS visual formatting model. Using it, elements can be translated,
rotated, scaled, and skewed. - by Mozilla MDN
#check {
transform: scale(0.5);
fill: none;
stroke: green;
stroke-width: 20;
stroke-dasharray: 180;
stroke-dashoffset: 180;
-webkit-animation: draw 1.2s infinite ease;
animation: draw 1.2s infinite ease;
animation-fill-mode: forwards;
animation-iteration-count: 1;
}
#-webkit-keyframes draw {
to {
stroke-dashoffset: 0;
}
}
#keyframes draw {
to {
stroke-dashoffset: 0;
}
}
<svg width="150" height="150">
<path id="check" d="M10,30 l30,50 l95,-70" />
</svg>
I'd suggest to add the viewbox attribute to your svg element so you could properly control the size of your element by simply changing the width and/or the height while keeping its aspect and its internal coordinate system.
Your element has approx.ly a 140 x 95 viewbox so you could write
<svg width="50" viewbox="0 0 140 95">
<path id="check" d="M10,30 l30,50 l95,-70" />
</svg>
Example: https://plnkr.co/edit/ERuQr4NsKfYHT7kebjkR?p=preview
I'm doing some button animation with SVG and can't make it to work exactly I want. I tried find same case but no luck. So I end up here, because I spend too much time on this already. Any help would be much appreciated.
Here is the code: http://jsfiddle.net/wq4djg9z/2/
It works fine, but with one flaw. It's always starts animation from fixed value.
#button-border {
stroke-dasharray: 150;
stroke-dashoffset: 150;
stroke-width: 4px;
-webkit-animation: dash-back 1.0s linear;
fill: none;
pointer-events: all;
}
#button-border:hover {
-webkit-animation: dash 1.0s linear forwards;
pointer-events: all;
}
#-webkit-keyframes dash {
to {
stroke-dashoffset: 0;
}
}
#-webkit-keyframes dash-back {
from {
stroke-dashoffset: 0;
}
to {
stroke-dashoffset: 150;
}
}
Is there a way to start animation from current animation frame when mouse out the button to smooth animation?
What about using transitions instead of animations to do the reverse part ?
#button-border {
stroke-dasharray: 150;
stroke-dashoffset: 150;
stroke-width: 4px;
-webkit-animation: dash-back 1.0s linear;
animation: dash-back 1.0s linear;
fill: none;
pointer-events: all;
transition: stroke-dashoffset 1s linear;
-webkit-transition: stroke-dashoffset 1s linear;
}
#button-border:hover {
stroke-dashoffset: 0;
pointer-events: all;
}
#-webkit-keyframes dash-back {
from {
stroke-dashoffset: 0;
}
to {
stroke-dashoffset: 150;
}
}
#keyframes dash-back {
from {
stroke-dashoffset: 0;
}
to {
stroke-dashoffset: 150;
}
}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100.00000" height="50.00000" id="svg1" version="1.1" viewBox="0 0 100 50" enable-background="new 0 0 100 50" xml:space="preserve">
<style type="text/css">
<![CDATA[]]>
</style>
<g id="button-border">
<path class="path" style="fill:none;stroke:#000000;stroke-opacity:1" d="m 100,50.0 0,-50.00000 -100,00.00000" id="path2983" />
<path class="path" style="fill:none;stroke:#000000;stroke-opacity:1" d="m 0,0 0,50 100,0" id="path2984" />
<text x="30" y="30" font-family="Verdana" font-size="15" fill="blue">Hello</text>
</g>
</svg>
I have this keyframe animation, that is supposed to change the color of a div at the 50% mark, then after a 2s delay, it is supposed to animate another div. And another after that.
And loop like that.
But it doesn't work like it should. Instead of the div running one after another, they run at the same time.
How can I fix this?
div#wifi-waves svg path#w01 {
-webkit-animation: colorchnage 1s infinite;
animation: colorchnage 1s infinite;
}
div#wifi-waves svg path#w02 {
-webkit-animation: colorchnage 1s infinite;
animation: colorchnage 1s infinite;
-webkit-animation-delay: 2s;
animation-delay: 2s;
}
div#wifi-waves svg path#w03 {
-webkit-animation: colorchnage 1s infinite;
animation: colorchnage 1s infinite;
-webkit-animation-delay: 2s;
animation-delay: 2s;
}
div#wifi-waves svg path#w04 {
-webkit-animation: colorchnage 1s infinite;
animation: colorchnage 1s infinite;
-webkit-animation-delay: 2s;
animation-delay: 2s;
}
#-webkit-keyframes colorchnage {
0% { fill: #ecf0f1; }
50% { fill: rgba(26, 60, 88, 0.9); }
100% { fill: #ecf0f1; }
}
#keyframes colorchnage {
0% { fill: #ecf0f1; }
50% { fill: rgba(26, 60, 88, 0.9); }
100% { fill: #ecf0f1; }
}
SVG:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 60 45" xml:space="preserve" preserveAspectRatio="xMixYMid">
<path id="w04" d=""></path>
<path id="w03" d=""></path>
<path id="w02" d=""></path>
<path id="w01" d=""></path>
</svg>
Because you wish to make each item animate one after the other, you should make the sure that the animation on the first item completes before the time the second one starts. Effectively, the delay you add on the each element is the amount of time for which the animation can run on the previous element.
Also, the element which was animated previously should stay idle for the rest of the time (while the animation on other elements are happening) to make it look like they are happening in an iterative manner. So effectively your total animation duration should be equal to the [Delay on each element * No. of elements].
Here you are expecting a 2s delay between each element getting animated and hence the total duration of the animation should be 8s. Plus, the duration on each element should get completed in 2s (which is 25% of 8s).So, your animation code should look like in the below snippet. (The SVG in question wasn't working, so I copied something from the net).
div#wifi-waves svg path#w01 {
-webkit-animation: colorchnage 8s infinite;
animation: colorchnage 8s infinite;
}
div#wifi-waves svg path#w02 {
-webkit-animation: colorchnage 8s infinite;
animation: colorchnage 8s infinite;
-webkit-animation-delay: 2s;
animation-delay: 2s;
}
div#wifi-waves svg path#w03 {
-webkit-animation: colorchnage 8s infinite;
animation: colorchnage 8s infinite;
-webkit-animation-delay: 4s;
animation-delay: 4s;
}
div#wifi-waves svg path#w04 {
-webkit-animation: colorchnage 8s infinite;
animation: colorchnage 8s infinite;
-webkit-animation-delay: 6s;
animation-delay: 6s;
}
#-webkit-keyframes colorchnage {
0% {
fill: #ecf0f1;
}
12.5% {
fill: rgba(26, 60, 88, 0.9);
}
25% {
fill: #ecf0f1;
}
}
#keyframes colorchnage {
0% {
fill: #ecf0f1;
}
12.5% {
fill: rgba(26, 60, 88, 0.9);
}
25% {
fill: #ecf0f1;
}
<div id="wifi-waves">
<svg width="200px" height="260px" version="1.1" xmlns="http://www.w3.org/2000/svg">
<path id="w01" d="M10 80 Q 95 10 180 80" stroke="black" fill="#ecf0f1" />
<path id="w02" d="M10 120 Q 95 40 180 120" stroke="black" fill="#ecf0f1" />
<path id="w03" d="M10 160 Q 95 80 180 160" stroke="black" fill="#ecf0f1" />
<path id="w04" d="M10 200 Q 95 120 180 200" stroke="black" fill="#ecf0f1" />
</svg>
</div>
CSS doesn't read the properties from top to bottom, delaying each animation 2s after the other. You have to give each wave a delay, like this:
div#wifi-waves svg path{
animation: colorchnage 1s infinite;
}
div#wifi-waves svg path#w02{
animation-delay: 2s;
}
div#wifi-waves svg path#w03{
animation-delay: 4s;
}
div#wifi-waves svg path#w04{
animation-delay: 6s;
}