Saving the result of animation - css

I wrote something like this:
<svg class="svg-cab" height="35px" width="35px" id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 202 228.7">
<defs><style>
.cls-21{
fill:white;
cursor: pointer; }
.svg-cab:hover .cls-21{
stroke-dasharray: 1000;stroke-miterlimit:10;stroke-width:20px;
stroke-dashoffset: 1000;
animation: dash 1s linear;
stroke: white;
}
#keyframes dash {
from {
stroke-dashoffset: 999;
}
to {
stroke-dashoffset: 0;
}
}
</style></defs><path class="cls-21" d="M277.3,192.9a53.3,53.3,0,1,1-51.6-51.5A53.3,53.3,0,0,1,277.3,192.9Z" transform="translate(-123 -136.3)"/><path class="cls-21" d="M269.3,264H178.7A50.7,50.7,0,0,0,128,314.7V352a8,8,0,0,0,8,8H312a8,8,0,0,0,8-8V314.7A50.7,50.7,0,0,0,269.3,264Z" transform="translate(-123 -136.3)"/></svg></a>
It s an animation when I hover on the svg the stroke looms over it
and I need to when animation has been ended the stroke was saved till mouseover
Does anyone have any idea?

You can rely on forwards and animation-play-state to simulate this. The trick is to move all the style that you need to keep inside an animation and forwards will do the job of saving them
.cls-21 {
fill: red;
cursor: pointer;
animation:
dash 1s linear paused forwards,
stroke 1s linear paused forwards;
}
.svg-cab:hover .cls-21 {
stroke-dasharray: 1000;
stroke-miterlimit: 10;
animation-play-state: running;
}
#keyframes dash {
from {
stroke-dashoffset: 999;
}
to {
stroke-dashoffset: 0;
}
}
#keyframes stroke {
1%,100% {
stroke: white;
stroke-width: 20px;
}
}
<svg class="svg-cab" height="35px" width="35px" id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 202 228.7">
<path class="cls-21" d="M277.3,192.9a53.3,53.3,0,1,1-51.6-51.5A53.3,53.3,0,0,1,277.3,192.9Z" transform="translate(-123 -136.3)"/><path class="cls-21" d="M269.3,264H178.7A50.7,50.7,0,0,0,128,314.7V352a8,8,0,0,0,8,8H312a8,8,0,0,0,8-8V314.7A50.7,50.7,0,0,0,269.3,264Z" transform="translate(-123 -136.3)"/></svg>

Related

Svg animation with d: path

I am trying to animate my svg with path property by following this codepen, but my animation path is not smooth and going with some weird steps. Is there something wrong with path values or what am I doing wrong here ?
.p-4 path {
d: path("");
fill: #ff9fba;
animation-name: dash;
animation-duration: 3s;
animation-timing-function: ease-in;
animation-fill-mode: forwards;
}
#keyframes dash {
0% {
d: path("");
}
25% {
d: path("M-375.047,1034.989a3.005,3.005,0,0,1-.562-.053A3,3,0,0,1-378,1031.43c3.037-16.029,8.67-25.866,16.742-29.239,9.747-4.073,19.479,2.47,23.143,4.933l.872.572a3.033,3.033,0,0,1,1.216,4.057,2.967,2.967,0,0,1-3.985,1.266c-.29-.15-.788-.47-1.451-.915-2.931-1.97-10.715-7.2-17.482-4.376-6.009,2.511-10.559,11.094-13.161,24.82A3,3,0,0,1-375.047,1034.989Z");
}
50% {
d: path("M-375.047,1034.989a3.005,3.005,0,0,1-.562-.053A3,3,0,0,1-378,1031.43c3.037-16.029,8.67-25.866,16.742-29.239,9.747-4.073,19.479,2.47,23.143,4.933l.872.572c2.935,1.514,5.3,1.92,7.007,1.2,3.235-1.366,4.915-6.892,6.265-11.331l3.13.02,2.61,1.726c-1.714,5.637-3.848,12.653-9.671,15.113-3.426,1.447-7.5.975-12.11-1.4-.29-.15-.788-.469-1.451-.915-2.931-1.97-10.715-7.2-17.482-4.376-6.009,2.511-10.559,11.094-13.161,24.82A3,3,0,0,1-375.047,1034.989Z");
}
75% {
d: path("M-375.047,1034.989a3.005,3.005,0,0,1-.562-.053A3,3,0,0,1-378,1031.43c3.037-16.029,8.67-25.866,16.742-29.239,9.747-4.073,19.479,2.47,23.143,4.933l.872.572c2.935,1.514,5.3,1.92,7.007,1.2,3.235-1.366,4.915-6.892,6.265-11.331l3.273-.478-2.89-.8c2.144-7.713,7.106-11.175,14.747-10.291a3,3,0,0,1,2.635,3.325,3,3,0,0,1-3.325,2.635c-5.923-.685-7.377,2.705-8.276,5.937l-.423,1.42c-1.714,5.637-3.848,12.653-9.671,15.113-3.426,1.447-7.5.975-12.11-1.4-.29-.15-.788-.469-1.451-.915-2.931-1.97-10.715-7.2-17.482-4.376-6.009,2.511-10.559,11.094-13.161,24.82A3,3,0,0,1-375.047,1034.989Z");
}
100% {
d: path("M-375.047,1034.989a3.005,3.005,0,0,1-.562-.053A3,3,0,0,1-378,1031.43c3.037-16.029,8.67-25.866,16.742-29.239,9.747-4.073,19.479,2.47,23.143,4.933l.872.572c2.935,1.514,5.3,1.92,7.007,1.2,3.235-1.366,4.915-6.892,6.265-11.331l3.273-.478-2.89-.8c2.144-7.713,7.106-11.175,14.747-10.291,3.293.381,11.241-.41,13.927-15.954a3,3,0,0,1,3.467-2.445A3,3,0,0,1-289,971.059c-3.658,21.167-16.638,21.342-20.529,20.892-5.923-.685-7.377,2.705-8.276,5.937l-.423,1.42c-1.714,5.637-3.848,12.653-9.671,15.113-3.426,1.447-7.5.975-12.11-1.4-.29-.15-.788-.469-1.451-.915-2.931-1.97-10.715-7.2-17.482-4.376-6.009,2.511-10.559,11.094-13.161,24.82A3,3,0,0,1-375.047,1034.989Z");
}
}
<svg xmlns="http://www.w3.org/2000/svg" width="89.095" height="67.442" viewBox="0 0 89.095 67.442" class="usp_overlap_photo p-4">
<path id="home-usp4-p4" transform="translate(378.051 -967.547)"/>
</svg>
Would it be ok if the shape was turned into a path with a stroke instead of an outline of the shape with a fill?
Here I refactured the shape and animate the stroke-dasharray instead of the d attribute.
.p-4 path {
fill: none;
stroke: #ff9fba;
stroke-width: 5px;
stroke-linecap:round;
animation-name: dash;
animation-duration: 3s;
animation-timing-function: ease-in;
animation-fill-mode: forwards;
}
#keyframes dash {
0% {
stroke-dasharray: 0 100;
}
25% {
stroke-dasharray: 25 100;
}
50% {
stroke-dasharray: 50 100;
}
75% {
stroke-dasharray: 75 100;
}
100% {
stroke-dasharray: 100 100;
}
}
<svg xmlns="http://www.w3.org/2000/svg" width="200" viewBox="0 0 100 100" class="usp_overlap_photo p-4">
<path transform="translate(5 5)" id="home-usp4-p4" d="M 0,63 C 0,63 5,23 32,38 43,45 49,42 53,33 57,24 56,19 69,19 82,20 85,0 85,0" pathLength="100" stroke-dashoffset="0"/>
</svg>
Well, then we don't need all the keys in the keyframe:
.p-4 path {
fill: none;
stroke: #ff9fba;
stroke-width: 5px;
stroke-linecap:round;
animation-name: dash;
animation-duration: 3s;
animation-timing-function: ease-in;
animation-fill-mode: forwards;
}
#keyframes dash {
0% {
stroke-dasharray: 0 100;
}
100% {
stroke-dasharray: 100 100;
}
}
<svg xmlns="http://www.w3.org/2000/svg" width="200" viewBox="0 0 100 100" class="usp_overlap_photo p-4">
<path transform="translate(5 5)" id="home-usp4-p4" d="M 0,63 C 0,63 5,23 32,38 43,45 49,42 53,33 57,24 56,19 69,19 82,20 85,0 85,0" pathLength="100" stroke-dashoffset="0"/>
</svg>

stroke-dashoffset not working with SVG

i am new to SVG and animation.
just created a path and wanted to draw it with animation, i am using "stroke-dashoffset" but its not working.
Here is my HTML :
<div class="Q">
<svg height="100%" width="100%" viewBox="200 0 400 400">
<path id="latter_q" d="M1003.3425022861358,2828.211816939241a655.718421,655.718421,0,1,1,-397.5557043956452,543.2070169791295" style="fill: none; stroke-width: 300px; stroke-linecap: round;" transform="matrix(-0.220662 -0.00474159 0.00474159 -0.220662 452.115 953.136)" stroke="purple"/>
<circle id="e4_circle" cx="322" cy="293" stroke="purple" style="stroke-width: 1px; vector-effect: non-scaling-stroke;" r="38.1936" fill="purple" />
<polygon stroke="purple" id="e5_polygon" style="stroke-width: 1px;" points="625.5 543.206 885.5 7.20558 1149.5 535.206 1021.5 481.206 889.5 225.206 767.5 481.206" fill="purple" transform="matrix(0.618136 0 0 0.618136 -4.20822 17.3249)"/>
</svg>
</div>
and CSS
#latter_q{
animation: DrwaQ 2s linear alternate infinite;
animation-delay: 1s;
/*stroke-dashArray: 1100;*/
stroke-dashoffset: 1100;
}
.Q{
width: 100%;
height: 100%;
position: absolute;
/*opacity: 0;*/
}
#keyframes DrawQ {
to { stroke-dashOffset: 0; }
}
finally got it worked.
CSS :
#latter_q{
animation: DrwaQ 2s ease-in ;
animation-delay: 0s;
stroke-dasharray: 3435;
}
#keyframes DrwaQ {
to {
stroke-dashoffset: 6904;
}
from {
stroke-dashoffset: 3447;
}
}
stroke-dashoffset is not a CSS property (yet) - you have to do this with JavaScript
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference

Reducing size of SVG tick

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

Set keyframe property for svg

I've animated a SVG and I want to use keyframe to set the speed but I'm not able to achieve what I want. I want that the line go fast and then the text to be less fast. But I don't really understand how it's work.
Here is a Jsfiddle for better explanation.
<svg class="svg-path" version="1.1" id="Calque_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="-195.9 282.3 995.9 35.5" enable-background="new -195.9 282.3 995.9 35.5" xml:space="preserve">
<path class="path" stroke-width="1" fill="none" stroke="#000000" d="M-195.9,316.9c0.1,0,853.9,0,854.2,0c0.6,0,2.5,0,2.9,0c0.8,0,1.4,0,1.9,0c1,0,1.9,0,2.5,0
c0.9,0,1.2,0,2,0c0.9,0,0.8,0,1.9,0c1.5,0,1.9-0.2,3-0.6c1-0.4,1.9-1,2.7-1.7c0.7-0.7,1.3-1.5,1.7-2.5c0.4-1,0.6-3,0.6-4.1
c0-1.2-0.3-2.1-0.7-2.9c-0.4-0.8-1-1.4-1.9-1.9c-0.8-0.5-1.7-1-2.7-1.3s-2.1-0.7-3.1-1c-1.1-0.3-2.1-0.7-3.1-1.1
c-1-0.4-1.9-0.9-2.7-1.6c-0.8-0.7-1.4-1.4-1.9-2.4c-0.5-1-0.7-2.2-0.7-3.6c0-1.1,0-1.5,0.5-2.5c0.4-1,1.4-2.1,2-2.7
c0.8-0.8,1.9-1.2,3.1-1.7c1.2-0.5,2.6-0.7,4.2-0.7c1.7,0,4.5,0,5.9,0c1.7,0,5.2,0,8.4,0v32.8h10.4l0-32.9l15.5,25.7l15-25.6l0,32.9
h20.3c0,0,15.1,0,15.1-17.9c0-16.8-14.8-16.2-14.8-16.2s-14.2-0.6-14.2,16.2c0,18,13.2,17.9,14.2,18c0,0,20.9,0,21.9,0v-32l24.4,32
v-33.5h7.6"/>
</svg>
CSS
svg.svg-path {
position: absolute;
top:25px;
left: 0px;
width: 100%;
height: auto;
}
svg.svg-path path {
stroke-dasharray: 3800;
animation: dash 3.5s linear reverse;
}
#keyframes dash {
0% {
stroke-dashoffset: 0;
transition: 'stroke-dashoffset';
}
50% {
stroke-dashoffset: 2000;
transition: 'stroke-dashoffset';
}
100% {
stroke-dashoffset: 3800;
transition: 'stroke-dashoffset';
}
}
Can you try to elaborate on, what you're trying to achieve?
If you want the line to slow down in the end to write out the letters in a slower fashion, you would just allocate more time to complete the animation from about stroke-dashoffset by making it go from 0-2000 in the beginning 5% of the animation, and then reserving the rest of it for the remainder of the time, like this:
#keyframes dash {
0% {
stroke-dashoffset: 0;
transition: 'stroke-dashoffset';
}
5% {
stroke-dashoffset: 2000;
transition: 'stroke-dashoffset';
}
100% {
stroke-dashoffset: 3800;
transition: 'stroke-dashoffset';
}
}
But I'm not really sure I understood correctly, so I'm not sure this is what you're trying to achieve.

CSS how to revert SVG animation on mouseout from current frame

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>

Resources