I'm new to SVG animation and I was hoping someone could help me achieve an effect.
I have two questions actually:
1.) As you can see in this codepen. I have an CSS keyframe animation that draws out a shape, fill a flat color then ends in a SVG gradient. However it doesn't properly transition to that gradient (it just kinda 'pops' to that state at the end)
CSS Animation
#-webkit-keyframes dash {
0% {stroke-dashoffset: 900; fill:#FFF;}
85% {stroke-dashoffset: 0; fill:#FFF;}
90% {fill:#fce669;}
100% {fill:url(#glow);}
}
SVG '#Glow' gradient
<defs>
<linearGradient id = "glow" x1 = "0%" y1 = "0%" x2 = "100%" y2 = "100%">
<stop stop-color = "#fce669" offset = "0%"/>
<stop stop-color = "#fff" offset = "100%"/>
</linearGradient>
</defs>
Is it possible to transition between these states?
Okay that aside, here's my real question:
2.) Ideally, after the drawing animation is complete, I'd like there to be a sheen effect that is also triggered on hover. Like this codepen. I tried animating the color stops on the gradient to achieve this effect and it's pretty terrible.
http://codepen.io/StuffieStephie/pen/eJdWeJ
I really have no idea how to proceed with this. Can someone point me in the right direction?
You seem to came in some hard place here...
I am not sure about the specs recommendations for this, but from tests, it seems that svg's linearGradient can't be animated through CSS. I guess that it's because it is seen as an image, just like CSS gradients are seen as background-image.
Note that Firefox won't even execute the animation if you place a <funcIRI> into it (url(#yourElement)). Once again, I couldn't find the specs about this, so can't tell for sure if that's a bug, but since Chrome is deprecating SMIL, that will probably become an issue quite soon.
But for today, the only workaround I can give you, is to use SMIL.
You can use <animate> elements to animate your linearGradient. In the example above, I do animate only one stop-color of the #glow gradient.
I also used an other animated linear-gradient to the shine effect. This linear-gradient is then applied as the fill attribute of a mask, making the black part of the gradient the only masking of the <mask> element.
Note that chrome only do render offset's animation if you do use absolute values...
svg {
max-width: 100%;
max-height: 100%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
.st0 {
stroke-dasharray: 900;
stroke-dashoffset: 0;
fill: url(#glow);
animation: dash 4s linear alternate;
-webkit-animation: dash 4s linear alternate;
}
#keyframes dash {
0% {
stroke-dashoffset: 900;
}
85% {
stroke-dashoffset: 0;
}
}
#-webkit-keyframes dash {
0% {
stroke-dashoffset: 900;
}
85% {
stroke-dashoffset: 0;
}
}
.st2 {
fill: #FFF;
}
<svg version="1.1" id="starmoon" class="icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 254.7 220.8" style="enable-background:new 0 0 254.7 220.8;" xml:space="preserve">
<style type="text/css">
.st0 {
stroke: #EFA441;
stroke-miterlimit: 10;
}
</style>
<defs>
<linearGradient id="glow" x1="0%" y1="0%" x2="100%" y2="100%">
<stop stop-color="#FFF" offset="0%">
<animate id="color" attributeName="stop-color" from="#fff" to="#fce669" dur="0.8s" begin="3.2s" fill="freeze" />
</stop>
<stop stop-color="#fff" offset="100%" />
</linearGradient>
<!-- the gradient used by the mask -->
<linearGradient id="shine" x1="0%" y1="0%" x2="100%" y2="100%">
<stop stop-color="white" offset="-10%">
<animate attributeName="offset" values="-.05;.95" dur="2s" begin="color.end" />
</stop>
<stop stop-color="black" offset="-5%">
<animate attributeName="offset" values="0;1" dur="2s" begin="color.end" />
</stop>
<stop stop-color="white" offset="0%">
<animate attributeName="offset" values=".05;1.05" dur="2s" begin="color.end" />
</stop>
</linearGradient>
<mask id="myShiningMask">
<rect x="0" y="0" width="100%" height="100%" fill="url(#shine)" />
</mask>
</defs>
<path fill="url(#glow)" class="st0 icon" d="M169,69.1l5.4-5.4c1.6-1.6,0.8-4.3-1.5-4.7l-42.6-7.6c-0.9-0.2-1.6-0.7-2-1.5l-19.7-38.4c-1-2-3.9-2-5,0
L83.5,49.4c-0.4,0.8-1.2,1.3-2,1.4l-42.7,6.5c-2.2,0.3-3.2,3.1-1.6,4.7L67.3,93c0.6,0.6,0.9,1.5,0.8,2.4l-6.7,42.3
c-0.4,2.2,1.9,3.9,4,3l38.9-18.7c0.8-0.4,1.7-0.4,2.5,0l38.6,19.7c2,1,4.4-0.6,4-2.9l-6.1-42.5c-0.1-0.9,0.2-1.8,0.8-2.4l15.2-15
c10,9.6,16.2,23.1,16.2,38.1c0,29.3-23.7,53-53,53c-10,0-19.3-2.8-27.3-7.6c10.8,21.4,33,36.1,58.6,36.1c36.2,0,65.6-29.4,65.6-65.6
C219.1,102,197.7,76.1,169,69.1z" mask="url(#myShiningMask)" />
<circle class="st2" cx="87.9" cy="67.3" r="12.4" fill="white" />
</svg>
Related
I'm trying to make an svg clip path spin in a circle using css animations, I use transform: rotate to make it rotate but this seems to produce jerky motion (or no motion):
in firefox it moves but jerks.
in chromium browsers the position only seems to update when the page is interacted with.
here's my code:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 76.0146 47.6272">
<defs>
<style>
.magnifyingGlass {
animation: spin 10s infinite linear;
transform-origin: 50% 50%;
will-change: transform;
}
#keyframes spin {
0% {transform: rotate(0deg);}
100% {transform: rotate(360deg);}
}
</style>
<linearGradient id="primary_gradient" data-name="primary gradient" x1="24.8437" y1="23.8283" x2="51.1709" y2="23.8283" gradientUnits="userSpaceOnUse">
<stop offset="0.2367" stop-color="#f78739"/>
<stop offset="0.9428" stop-color="#f73c39"/>
</linearGradient>
<clipPath class="magnifyingGlass" id="clip-path">
<circle cx="48.1997" cy="13.636" r="9.5"/>
</clipPath>
</defs>
<g stroke-linecap="round">
<g> <!-- model zoomed out -->
<path stroke="#333" fill="url(#primary_gradient)" d="M46.6709,36.9918a4.4728,4.4728,0,0,1-3.1821-1.3178l-5.128-5.128a.5.5,0,0,0-.707,0l-5.1279,5.128A4.5,4.5,0,0,1,26.1616,29.31L31.29,24.1818a.5.5,0,0,0,0-.707l-5.1279-5.128a4.5,4.5,0,0,1,6.3643-6.3642l5.1279,5.1279a.5.5,0,0,0,.707,0l5.128-5.1279a4.5,4.5,0,0,1,6.3642,6.3642l-5.1279,5.128a.5.5,0,0,0,0,.707L49.853,29.31a4.5,4.5,0,0,1-3.1821,7.6821Z"/>
</g>
<!-- zoomed in -->
<polygon stroke="#1a1a1a" clip-path="url(#clip-path)" fill="url(#primary_gradient)" stroke="#1a1a1a" points="54.031 31.864 52.639 29.736 49.722 26.819 46.758 23.855 49.508 21.105 52.639 17.973 54.18 15.418 54.441 13.234 53.768 10.768 51.435 8.283 48.705 7.426 45.695 7.967 43.889 9.223 41.314 11.798 38.007 15.104 35.12 12.217 32.126 9.223 30.521 8.063 27.57 7.413 25.166 7.974 22.301 10.664 21.647 12.576 21.662 14.703 22.218 16.372 23.661 18.259 26.615 21.213 29.257 23.855 26.423 26.689 23.375 29.736 22.016 31.783 21.563 34.111 22.113 36.665 24.16 39.152 26.242 40.113 28.774 40.214 30.932 39.42 32.582 38.03 35.687 34.926 38.007 32.605 40.672 35.269 43.889 38.487 45.387 39.591 47.436 40.244 49.446 40.186 51.67 39.279 53.913 36.64 54.452 34.132 54.031 31.864"/>
</g>
</svg>
here's live examples: https://editsvgcode.com/6g1cg0e48dckthk2d49
https://codepen.io/erinthesmallone/pen/QWggypN
this behaviour is not observed when doing translates
I was trying to find clean simple solution to create SVG kind of donut charts with gradient fill going along the edge of the circle with the possibility to animate it from 0% to x% and I found out that there's no easy way and no ready copy & paste solution. Most solution used one linear gradient, which didn't help in my case or were over complicated.
The kind of (static) result I expected to get was:
So after some research and work I created this solution which I decided to share it with you.
It is based on the prozorov's solution. His circle wasn't 360deg and lacked the needed animation part. It isn't true gradient going with the stroke around the edge of the circle (which is not easy to do with SVG) but rather two linear gradients put together, but for most cases that trick does it.
And here's the complete solution:
.animated {
animation: dash 5s infinite;
}
#keyframes dash {
0% {
stroke-dashoffset: 0;
}
50% {
stroke-dashoffset: 1570;
}
100% {
stroke-dashoffset: 0;
}
}
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="800" width="800">
<defs>
<linearGradient id="Gradient1" gradientTransform="rotate(90)">
<stop offset="0%" stop-color="#ff0000"/>
<stop offset="100%" stop-color="#00ff00"/>
</linearGradient>
<linearGradient id="Gradient2" gradientTransform="rotate(90)">
<stop offset="0%" stop-color="#0000ff"/>
<stop offset="100%" stop-color="#00ff00"/>
</linearGradient>
<pattern id="Pattern" x="0" y="0" width="600" height="600" patternUnits="userSpaceOnUse">
<g transform="rotate(0, 300, 300)">
<rect shape-rendering="crispEdges" x="0" y="0" width="300" height="600" fill="url(#Gradient1)"/>
<rect shape-rendering="crispEdges" x="300" y="0" width="300" height="600" fill="url(#Gradient2)"/>
</g>
</pattern>
</defs>
<path id='arc5' class="animated"
style="stroke: url(#Pattern);" fill='transparent'
stroke-dasharray="1570 1570"
stroke-dashoffset="0"
stroke-width='60'
d='M 300 58 A 250 250 0 1 1 299.99 58'/>
</svg
And link to JS Fiddle
I am using a gradient to fill an svg path.
like this:
<svg viewBox="0 0 40 40">
<defs>
<linearGradient id="progress-gradient" x1="0%" x2="0%" y1="0%" y2="100%">
<stop offset="var(--offset-empty)" stopColor="var(--color-empty)" />
<stop offset="var(--offset-full)" stopColor="var(--color-full)" />
</linearGradient>
</defs>
<path d="..removed" fill="url(#progress-gradient)" />
</svg>
and I am styling it with css:
#progress-gradient {
--color-empty: #ffffff;
--color-full: #ff0000;
--offset-empty: 50%;
--offset-full: 100%;
}
I am trying to set the offsets dynamically to create a kind of progress within the SVG icon.
But this isn't working. Colors are working, but not offset.
Is there any other way I can dynamically set the offset positions with CSS?
many thanks
I have a SVG in my html:
<img class="svg-loader" width="60" height="60" src="preloader.svg">
This SVG contains 3 circles which are animated inline to rotate and change their cy- and cx-position:
<svg width="57" height="57" viewBox="0 0 57 57" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="preloader-gradient" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(244,117,51);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(245,0,87);stop-opacity:1" />
</linearGradient>
</defs>
<g fill="none" fill-rule="evenodd">
<g transform="translate(1 1)" stroke-width="2">
<circle cx="5" cy="50" r="5" fill="url(#preloader-gradient)">
<animate attributeName="cy"
begin="0s" dur="2.2s"
values="50;5;50;50"
calcMode="linear"
repeatCount="indefinite" />
<animate attributeName="cx"
begin="0s" dur="2.2s"
values="5;27;49;5"
calcMode="linear"
repeatCount="indefinite" />
</circle>
<circle cx="27" cy="5" r="5" fill="url(#preloader-gradient)">
<animate attributeName="cy"
begin="0s" dur="2.2s"
from="5" to="5"
values="5;50;50;5"
calcMode="linear"
repeatCount="indefinite" />
<animate attributeName="cx"
begin="0s" dur="2.2s"
from="27" to="27"
values="27;49;5;27"
calcMode="linear"
repeatCount="indefinite" />
</circle>
<circle cx="49" cy="50" r="5" fill="url(#preloader-gradient)">
<animate attributeName="cy"
begin="0s" dur="2.2s"
values="50;50;5;50"
calcMode="linear"
repeatCount="indefinite" />
<animate attributeName="cx"
from="49" to="49"
begin="0s" dur="2.2s"
values="49;5;27;49"
calcMode="linear"
repeatCount="indefinite" />
</circle>
</g>
</g>
</svg>
The Chrome console throws a warning that "SVG's SMIL animations (, , etc.) are deprecated and will be removed. Please use CSS animations or Web animations instead". So the question is if it is possible to replace the inline animation completely with CSS animations.
Now, it is trivial to rotate the complete SVG infinitely in CSS for 360 degrees:
.svg-loader {
-webkit-animation:spin 3s linear infinite;
-moz-animation:spin 3s linear infinite;
animation:spin 3s linear infinite;
}
#-moz-keyframes spin {
100% {
-moz-transform: rotate(360deg);
}
}
#-webkit-keyframes spin {
100% {
-webkit-transform: rotate(360deg);
}
}
#keyframes spin {
100% {
-webkit-transform: rotate(360deg);
transform:rotate(360deg);
}
}
However, I am stuck with the CSS animation of the cy-positions for each circle and not sure if this possible.
Would appreciate your advice.
As mentioned by Kaiido before:
SMIL isn't really deprecated: only chrome stopped its support for it, and IE never started it
But you can use css animations to achieve the same result.
You have to put the css inside the svg file or put the svg inline, using a img tag with external markup has limitations.
For example:
<svg viewBox="0 0 57 57" width=57 height=57>
<style>
#keyframes path{
25%{ transform: translate(0,0); }
50%{ transform: translate(0,-44px); }
75%{ transform: translate(-45px,-22px); }
100%{ transform: translate(0,0); }
}
.circle{
animation: path 2.2s linear infinite;
fill: red;
}
</style>
<circle cx="49" cy="50" r=5 class="circle"></circle>
</svg>
But still above example will not work inside IE and Edge, you could resolve it with:
code it interally with HTML and CSS
split the svg in multiple svgs and animate the svg tags
use JS (inside the svg file or inline)
Small example with the second solution:
#keyframes path{
25%{ transform: translate(0,0); }
50%{ transform: translate(0,-44px); }
75%{ transform: translate(-45px,-22px); }
100%{ transform: translate(0,0); }
}
svg{
animation: path 2.2s linear infinite;
margin-left: 50px;
margin-top: 50px;
width: 10px;
height: 10px;
}
.circle{
fill: red;
}
<svg viewBox="0 0 10 10" >
<circle cx=5 cy=5 r=5 class="circle"></circle>
</svg>
I have a problem here. I need something to be implemented onclick of a button. I searched on the net and found out that SVG was an easier way to do it rather than CSS stylesheets.
I have an HTML5 canvas (which is basically a snapshot from a video stream that I have taken and drawn on to the HTML 5 canvas using drawImage()). On click of a button, I want the image with the following effect to come on the canvas instead of the original image.
My problem is that I am able to replicate the effect if I apply the effect on just an image. However, on the canvas it gives me the mask separately and the image separately. This is the code that I am using for the SVG
<svg height="0">
<defs>
<mask id="mask-radial">
<rect width="500" height="500" fill="url(#g1)"></rect>
<radialGradient id="g1" cx="80%" cy="80%" r="100%">
<stop stop-color="black" offset="50%"/>
<stop stop-color="white" offset="110%"/>
</radialGradient>
</mask>
<!--<mask id="mask-linear">
<rect width="400" height="300" fill="url(#l1)"></rect>
<linearGradient id="l1" x1="0" y1="0" x2="0" y2="1">
<stop stop-color="white" offset="0%"/>
<stop stop-color="black" offset="30%"/>
<stop stop-color="white" offset="100%"/>
</linearGradient>
</mask> -->
<filter id="filtre1">
<feGaussianBlur in="SourceGraphic" stdDeviation="5"/>
</filter>
<!-- <filter id="filtre2">
<feGaussianBlur in="SourceGraphic" stdDeviation="10"/>
</filter> -->
</defs>
</svg>
This is how I am calling it in CSS
.effet{
width: 500px; height: 500px;
margin: 0 auto 50px auto;
box-shadow: 0 1px 5px rgba(0,0,0,.5);
}
.effet img{
position: absolute;
}
.filtre--r{
-webkit-mask: -webkit-radial-gradient( center, closest-side, transparent 50%, black 80%);
-webkit-mask: radial-gradient( closest-side at center, transparent 50%, black 80%);
-webkit-filter: blur(40px);
mask: url('#mask-radial');
filter: url('#filtre1');
}
and these are my HTML elements.
<div class="effet">
<img src="/static/images/noEffect.png" alt="" />
<img class="filtre filtre--r" src="/static/images/noEffect.png" alt="" />
</div>
Can somebody please tell me how I can recreate the effect for my canvas? Thanks in advance.
I researched and found that HTML5 Canvas radial gradient was an effective solution.
var lightSize = 100;
var x = 200;
var y = 200;
var radialGradient = context.createRadialGradient(x, y, 0,x, y, lightSize);
radialGradient.addColorStop(0, "#FFFF99"); // color at start circle
radialGradient.addColorStop(0.9, "#FFFF99"); //color at offset 0.9
radialGradient.addColorStop(1, '#7D7D5C');// color of finish circle
context.globalCompositeOperation = "multiply";
context.fillStyle = radialGradient;
Was able to recreate the effect totally.