I have an svg with a few elements that looks something like this:
I would like to create an animation where each element "pulses" (grows about 20% in size and then returns to its normal size without moving to a different position) one after the other, going around in a circle on repeat.
I began creating a css animation for this but my attempts to add a scale animation clearly were not using the correct center point because every scaling movement would shift that element to a different position as well.
I tried exploring different values for the property transform-origin, but none seemed to achieve the desired behaviour.
I have included a demo reprex that shows the behaviour:
#Ellipse_1 {
/* No transform-origin */
animation: pulse 2s linear infinite alternate;
animation-delay: 2.6s fill-opacity: 50%;
}
#Ellipse_2 {
/* transform-origin same as circle's center location */
transform-origin: 4 8;
fill-opacity: 50%;
animation: pulse 2s linear infinite alternate;
animation-delay: 3.4s
}
#Ellipse_3 {
/* Trying transform-origin center center keyword */
transform-origin: center center;
fill-opacity: 50%;
animation: pulse 2s linear infinite alternate;
animation-delay: 3s
}
#Path_1 {
/* Trying transform-origin center keyword */
transform-origin: center;
animation: pulse 2s linear infinite alternate;
animation-delay: 4s
}
#Path_2 {
/* This goes off screen as soon as animation starts */
animation: pulse 2s linear infinite alternate;
animation-delay: 4s
}
#keyframes pulse {
0% {
transform: scale(1);
-ms-transform: scale(1);
-webkit-transform: scale(1);
}
100% {
transform: scale(2);
-ms-transform: scale(2);
-webkit-transform: scale(2);
}
}
<svg id="shapes" data-name="shapes data" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-10 -10 100 100">
<circle id="Ellipse_1" data-name="Ellipse 1" cx="2.083" cy="2.083" r="2.083" transform="translate(14 3)" fill="red"/>
<path id="Path_1" data-name="Path 259" d="M60.749,74.282a103.267,103.267,0,0,0-5.686,23.5.459.459,0,0,1-.455.408H48.887a.46.46,0,0,1-.453-.38l-3.1-17.357a.458.458,0,0,1,.321-.519A75.754,75.754,0,0,0,60.24,73.846C60.708,73.591,60.936,73.783,60.749,74.282Z" transform="translate(-45.326 -63.944)" fill-rule="evenodd" fill="green"/>
<circle id="Ellipse_2" data-name="Ellipse 2" cx="2.083" cy="2.083" r="2.083" transform="translate(4 8)" fill="blue"/>
<circle id="Ellipse_3" data-name="Ellipse 3" cx="1.62" cy="1.62" r="1.62" transform="translate(21.942)" fill="green"/>
<path id="Path_2" data-name="Path 2" d="M97.486,54.462C94.879,58.549,90.751,66.907,88.6,81.11a.6.6,0,0,1-.572.505,16.478,16.478,0,0,0-8.995,3.049.355.355,0,0,1-.562-.322,90.68,90.68,0,0,1,6.77-25.419.966.966,0,0,1,.352-.4q1.521-.866,3.1-1.629a.016.016,0,0,1,.009,0,50.611,50.611,0,0,1,8.261-3.124h0A.465.465,0,0,1,97.486,54.462Z" transform="translate(-66.545 -51.149)" fill-rule="evenodd" fill="red"/>
</svg>
When you "overwrite" the transform using CSS the translate is removed. Here I moved the transform/translate to a parent <g>. So each element is moved using the transform in <g> and then each element is scaled using CSS.
The circles are easy to scale because their origin is in the center already, but the other shapes need either to be moved so that 0,0 is in the center (change all values in the d attribute) or use transform-origin to move the origin. I guess that the result here is a combination - that could be optimized (up to you :-)).
#Ellipse_1 {
/* No transform-origin */
animation: pulse 2s linear infinite alternate;
animation-delay: 2.6s fill-opacity: 50%;
}
#Ellipse_2 {
/* transform-origin same as circle's center location */
fill-opacity: 50%;
animation: pulse 2s linear infinite alternate;
animation-delay: 3.4s
}
#Ellipse_3 {
/* Trying transform-origin center center keyword */
fill-opacity: 50%;
animation: pulse 2s linear infinite alternate;
animation-delay: 3s
}
#Path_1 {
/* Trying transform-origin center keyword */
transform-origin: 50px 80px;
animation: pulse 2s linear infinite alternate;
animation-delay: 4s
}
#Path_2 {
/* This goes off screen as soon as animation starts */
transform-origin: 80px 70px;
animation: pulse 2s linear infinite alternate;
animation-delay: 4s
}
#keyframes pulse {
0% {
transform: scale(1);
-ms-transform: scale(1);
-webkit-transform: scale(1);
}
100% {
transform: scale(1.2);
-ms-transform: scale(1.2);
-webkit-transform: scale(1.2);
}
}
<svg id="shapes" data-name="shapes data" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<g transform="translate(14 3)">
<circle id="Ellipse_1" data-name="Ellipse 1" cx="2.083" cy="2.083" r="2.083" fill="red"/>
</g>
<g transform="translate(-45.326 -63.944)">
<path id="Path_1" data-name="Path 259" d="M60.749,74.282a103.267,103.267,0,0,0-5.686,23.5.459.459,0,0,1-.455.408H48.887a.46.46,0,0,1-.453-.38l-3.1-17.357a.458.458,0,0,1,.321-.519A75.754,75.754,0,0,0,60.24,73.846C60.708,73.591,60.936,73.783,60.749,74.282Z" fill-rule="evenodd" fill="green"/>
</g>
<g transform="translate(4 10)">
<circle id="Ellipse_2" data-name="Ellipse 2" r="2.083" fill="blue"/>
</g>
<g transform="translate(22 3)">
<circle id="Ellipse_3" data-name="Ellipse 3" r="1.62" fill="green"/>
</g>
<g transform="translate(-66.545 -51.149)">
<path id="Path_2" data-name="Path 2" d="M97.486,54.462C94.879,58.549,90.751,66.907,88.6,81.11a.6.6,0,0,1-.572.505,16.478,16.478,0,0,0-8.995,3.049.355.355,0,0,1-.562-.322,90.68,90.68,0,0,1,6.77-25.419.966.966,0,0,1,.352-.4q1.521-.866,3.1-1.629a.016.016,0,0,1,.009,0,50.611,50.611,0,0,1,8.261-3.124h0A.465.465,0,0,1,97.486,54.462Z" fill-rule="evenodd" fill="red"/>
</g>
</svg>
I'm having some trouble with a SVG logo animation. I made the outlines animation using the vivus tool (https://maxwellito.github.io/vivus-instant/). That works great.
Now I want the paths to fill in at about 80%.
So that at the end the whole logo is visible, not just the outlines. I tried adding a piece of code I found here: https://codepen.io/kay8/pen/GgEQra
But that just makes the whole logo fill in at 80%, but the outlines animation isn't working anymore.
Here's my code:
<svg version="1.1" id="wino" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 662.1 176.1" style="background-color: #e97965;" xml:space="preserve">
<style type="text/css">
.st0{fill:none;stroke:#000000;stroke-miterlimit:10;}
.st1{fill:none;stroke:#FFFFFF;stroke-miterlimit:10;}
.HqJTsNPU_0 {
stroke-dasharray: 1397 1399;
stroke-dashoffset: 1398;
animation: HqJTsNPU_draw 2000ms ease-out 0ms forwards;
}
.HqJTsNPU_1 {
stroke-dasharray: 821 823;
stroke-dashoffset: 822;
animation: HqJTsNPU_draw 2000ms ease-out 111ms forwards;
}
.HqJTsNPU_2 {
stroke-dasharray: 227 229;
stroke-dashoffset: 228;
animation: HqJTsNPU_draw 2000ms ease-out 222ms forwards;
}
.HqJTsNPU_3 {
stroke-dasharray: 92 94;
stroke-dashoffset: 93;
animation: HqJTsNPU_draw 2000ms ease-out 333ms forwards;
}
.HqJTsNPU_4 {
stroke-dasharray: 457 459;
stroke-dashoffset: 458;
animation: HqJTsNPU_draw 2000ms ease-out 444ms forwards;
}
.HqJTsNPU_5 {
stroke-dasharray: 426 428;
stroke-dashoffset: 427;
animation: HqJTsNPU_draw 2000ms ease-out 555ms forwards;
}
.HqJTsNPU_6 {
stroke-dasharray: 96 98;
stroke-dashoffset: 97;
animation: HqJTsNPU_draw 2000ms ease-out 666ms forwards;
}
.HqJTsNPU_7 {
stroke-dasharray: 462 464;
stroke-dashoffset: 463;
animation: HqJTsNPU_draw 2000ms ease-out 777ms forwards;
}
.HqJTsNPU_8 {
stroke-dasharray: 227 229;
stroke-dashoffset: 228;
animation: HqJTsNPU_draw 2000ms ease-out 888ms forwards;
}
.HqJTsNPU_9 {
stroke-dasharray: 92 94;
stroke-dashoffset: 93;
animation: HqJTsNPU_draw 2000ms ease-out 1000ms forwards;
}
#keyframes HqJTsNPU_draw {
100% {
stroke-dashoffset: 0;
}
}
/*fill in animation*/
.st0{
fill: #000000;
fill-opacity: 1;
animation: fill-element 3s ease-in-out 0s forwards;
}
.st1{
fill: #ffffff;
fill-opacity: 1;
animation: fill-element 3s ease-in-out 0s forwards;
}
#keyframes fill-element{
0% {fill-opacity: 0;}
80% {fill-opacity: 0;}
100% {fill-opacity: 1;}
}
</style>
<path id="black-outline_2_" class="st0 HqJTsNPU_0" d="M17.4,126.9L17.4,91.6L645.2,91.6L645.2,162.1L17.4,162.1L17.4,126.9"></path>
<path class="st1 HqJTsNPU_1" d="M97.8,137.7L68,137.7L42.4,23.1L71.4,23.1L85.3,93.8L105.3,23.1L131.6,23.1L151.6,93.8L165.5,23.1L
193.5,23.1L168,137.7L138.2,137.7L118,64.7Z"></path>
<path class="st1 HqJTsNPU_2" width="26.5" height="86.6" d="M205.5 51.1 L232 51.1 L232 137.7 L205.5 137.7 Z"></path>
<path class="st1 HqJTsNPU_3" d="M218.7,43.1c3.9,0.1,7.6-1.5,10.3-4.3c2.8-2.7,4.3-6.4,4.2-10.3c0.1-3.9-1.5-7.6-4.2-10.3
c-5.7-5.7-14.9-5.7-20.6,0c-2.8,2.7-4.3,6.4-4.2,10.3c-0.1,3.9,1.5,7.6,4.2,10.3C211.1,41.6,214.8,43.1,218.7,43.1"></path>
<path class="st1 HqJTsNPU_4" d="M274.9,137.7V79c1.8-2.2,4-3.9,6.5-5c2.8-1.2,5.9-1.8,9-1.8c0.6,0,1.1,0,1.7,0c8.2,0.5,14.5,7.5,14,15.7v49.8
h26.5V84.3c0-10.1-3.2-18.5-9.6-25s-14.7-9.8-24.9-9.8c-4.3,0-8.5,0.6-12.5,2c-3.9,1.2-7.5,3.1-10.7,5.6v-5.9h-26.5v86.6h26.5V137.7
z"></path>
<path class="st1 HqJTsNPU_5" d="M391.4,116.4c-5.7,0.1-11.1-2.2-15-6.4c-4.1-4.1-6.3-9.8-6.1-15.5c-0.1-5.8,2.1-11.5,6.1-15.7
c0.2-0.2,0.4-0.4,0.6-0.6c8.3-8,21.4-7.7,29.4,0.6c4.1,4.2,6.3,9.9,6.1,15.7c0.1,5.8-2.1,11.4-6.1,15.5
C402.5,114.2,397.1,116.5,391.4,116.4 M344.4,94.4c0,6.1,1.2,12.1,3.7,17.6c2.4,5.4,5.8,10.2,10,14.3c4.3,4.1,9.4,7.3,14.9,9.5
c11.9,4.6,25,4.6,36.8,0c5.5-2.2,10.6-5.4,14.9-9.5c4.2-4.1,7.6-8.9,10-14.3c4.9-11.2,4.9-24,0-35.2c-2.4-5.4-5.8-10.2-10-14.3
c-4.3-4.1-9.4-7.4-14.9-9.6c-11.8-4.7-25-4.7-36.8,0c-5.5,2.2-10.6,5.4-14.9,9.5c-4.2,4.1-7.6,8.9-10,14.3
C345.7,82.4,344.4,88.4,344.4,94.4"></path>
<path class="st1 HqJTsNPU_6" d="M460.7,109c-4,0-7.9,1.6-10.7,4.5c-2.9,2.9-4.6,6.8-4.5,10.9c0.1,8.4,6.9,15.1,15.3,15
c8.4,0,15.2-6.8,15.2-15.2S469.1,109,460.7,109"></path>
<path class="st1 HqJTsNPU_7" d="M483.3,128c5.4,3.9,11.6,6.8,18.1,8.4c7.5,2,15.1,2.9,22.8,2.9c5.3,0,10.5-0.7,15.6-2.2
c4.5-1.3,8.6-3.4,12.4-6.1c3.3-2.5,6.1-5.7,8.1-9.3c2-3.6,3-7.6,3-11.6c0.2-6.3-2.4-12.4-7-16.8c-4.6-4.4-11.2-7.2-19.7-8.4
l-17.2-2.6c-3.7-0.6-6.3-1.3-7.9-2.4c-1.5-0.9-2.3-2.5-2.3-4.2c0.1-2,1.3-3.8,3.2-4.5c2.5-1.3,5.4-1.8,8.2-1.7
c4.3,0,8.6,0.6,12.7,1.8c4.7,1.5,9.2,3.5,13.4,6l12.6-17c-5.4-3.4-11.3-6-17.4-7.8c-6.1-1.8-12.4-2.7-18.8-2.7
c-11.6,0-20.7,2.5-27.3,7.6c-6.7,5-10,11.9-10,20.6c0,7.1,2.3,12.9,6.9,17.3c4.6,4.4,11.1,7.3,19.6,8.6l17.2,2.6
c2.4,0.2,4.7,1,6.7,2.2c1.3,0.9,2.1,2.4,2.1,4c0,2.1-1.3,3.8-3.8,5c-3.3,1.4-6.8,2-10.4,1.9c-4.5,0-9-0.8-13.3-2.2
c-5.2-1.8-10.2-4.2-14.9-7.1L483.3,128"></path>
<path class="st1 HqJTsNPU_8" width="26.5" height="86.6" d="M573.3 51.1 L599.8 51.1 L599.8 137.7 L573.3 137.7 Z"></path>
<path class="st1 HqJTsNPU_9" d="M586.5,43.1c3.9,0.1,7.6-1.5,10.4-4.3c2.8-2.7,4.3-6.4,4.3-10.3c0.1-3.9-1.5-7.6-4.3-10.3
c-5.7-5.7-14.9-5.7-20.6,0c-2.8,2.7-4.3,6.4-4.2,10.3c-0.1,3.9,1.5,7.6,4.2,10.3C578.9,41.6,582.7,43.1,586.5,43.1"></path>
</svg>
I'm trying to animate this SVG path/arc, from the beginning point to the end point, but I can not achieve that. Actually, the animation starts at the midpoint of the arc.
My arc's code is:
#arc {
display: block;
stroke-dashoffset: 3925px;
stroke-dasharray: 3925px;
}
#arc {
-webkit-animation: dashAnim 1s 1s linear alternate forwards;
animation: dashAnim 1s 1s linear alternate forwards;
}
#keyframes dashAnim {
0% {
stroke-dashoffset: 3925px;
}
100% {
stroke-dashoffset: 0px;
}
}
<svg id="arc" viewBox="0 0 1922 100.2">
<path id="arc-stroke" fill="none" stroke="#FF0000" strokeWidth="5"
strokeMiterlimit="10" d="M969.3,21.9c-344.4,0-669.8,82.4-956.8,229.7v6
h1922.3C1646,106.7,1317.2,21.9,969.3,21.9z"/>
</svg>
Can anyone help?
When opened in application like Illustrator, your path had additional points off canvas. I cleaned them up and the animation seems to work as you wanted.
There were no other obvious problems with the path that I could find.
My guess is - when you animate a closed path that is effectively a circle, something has to be the beginning and the end. In this case it was the middle of the arc.
#arc {
display: block;
stroke-dashoffset: 3925px;
stroke-dasharray: 3925px;
}
#arc {
-webkit-animation: dashAnim 1s 1s linear alternate forwards;
animation: dashAnim 1s 1s linear alternate forwards;
}
#keyframes dashAnim {
0% {
stroke-dashoffset: 3925px;
}
100% {
stroke-dashoffset: 0px;
}
}
<svg id="arc" viewBox="0 0 1922 100.2">
<path id="arc-stroke" fill="none" stroke="#FF0000" strokeWidth="5"
strokeMiterlimit="10" d="M1591.4,115.8c-196.6-61.1-405.6-93.9-622.1-93.9c-218.8,0-430,33.3-628.5,95.3"/>
</svg>
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>