{"id":4966,"date":"2020-06-05T09:32:00","date_gmt":"2020-06-05T09:32:00","guid":{"rendered":"https:\/\/azoora.com\/blog\/?p=4966"},"modified":"2020-08-19T09:54:55","modified_gmt":"2020-08-19T09:54:55","slug":"tutorial-animating-svg-gradients","status":"publish","type":"post","link":"https:\/\/azoora.com\/blog\/code\/tutorial-animating-svg-gradients\/","title":{"rendered":"Tutorial: Animating SVG Gradients"},"content":{"rendered":"\n<p><strong>SVG gradients<\/strong> are so handy. We can fill complex shapes and create depth and character for our artwork all while having access to it in the\u00a0DOM.<\/p>\n\n\n\n<p>Within SVG linear and radial gradient elements there are several attribute options available for thorough customization. Animating these gradients takes this customization even further and presents a unique opportunity that can be used to further communicate something to our&nbsp;users.<\/p>\n\n\n\n<p>This article will cover the\u00a0<strong>basics of SVG gradients<\/strong>\u00a0and getting started with\u00a0<code>&lt;animate><\/code>, and then dive into some <strong>demos <\/strong>for further\u00a0understanding.<\/p>\n\n\n\n<h3>Browser&nbsp;Support<\/h3>\n\n\n\n<p>Basic SVG support is present across desktop and mobile browsers, going at least two versions back across the board for&nbsp;desktop.<\/p>\n\n\n\n<p>Concerning SVG SMIL animation (animating within the SVG element itself) specifically there is&nbsp;<strong>no<\/strong>&nbsp;support in IE, IE mobile, or Opera Mini, but strong support in current and at least one version back in Chrome, Firefox, Safari, Opera, and on&nbsp;mobile.<\/p>\n\n\n\n<h2>SVG&nbsp;Gradients<\/h2>\n\n\n\n<p>Before we get started with animation, it\u2019s important to have a basic understanding of how SVG gradients\u00a0work.<\/p>\n\n\n\n<p>Gradients are one of many paint server options we have available to use when we wish to add color to the fills and strokes of SVG. The idea here is that we can define gradients within our SVG but they have no visual output until we call on them with the use of the \u201cfill\u201d and\/or \u201cstroke\u201d&nbsp;attributes.<\/p>\n\n\n\n<p>The basic structure of a very simple linear SVG gradient looks something like&nbsp;this:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>&lt;svg version=<\/code><code>\"1.1\"<\/code> <code>xmlns=<\/code><code>\"http:\/\/www.w3.org\/2000\/svg\"<\/code> <code>xmlns:xlink=<\/code><code>\"http:\/\/www.w3.org\/1999\/xlink\"<\/code> <code>x=<\/code><code>\"0px\"<\/code> <code>y=<\/code><code>\"0px\"<\/code><code>width=<\/code><code>\"180px\"<\/code> <code>height=<\/code><code>\"337px\"<\/code> <code>viewBox=<\/code><code>\"0 0 180.36 336.86\"<\/code> <code>enable-background=<\/code><code>\"new 0 0 180.36 336.86\"<\/code> <code>xml:space=<\/code><code>\"preserve\"<\/code><code>&gt;<\/code><code>&lt;path fill=<\/code><code>\"#7A5034\"<\/code> <code>stroke=<\/code><code>\"7A5034\"<\/code> <code>stroke-width=<\/code><code>\"2.5448\"<\/code> <code>stroke-linecap=<\/code><code>\"round\"<\/code> <code>stroke-linejoin=<\/code><code>\"round\"<\/code> <code>stroke-miterlimit=<\/code><code>\"10\"<\/code> <code>d=\"<\/code><code>M<\/code><code>159.337<\/code><code>,<\/code><code>292.35<\/code><code>c<\/code><code>0.863<\/code><code>,<\/code><code>0.605<\/code><code>,<\/code><code>1.073<\/code><code>,<\/code><code>1.795<\/code><code>,<\/code><code>0.469<\/code><code>,<\/code><code>2.658<\/code><code>l<\/code><code>-20.974<\/code><code>,<\/code><code>29.954<\/code><code>c<\/code><code>-0.605<\/code><code>,<\/code><code>0.863<\/code><code>-1.795<\/code><code>,<\/code><code>1.073<\/code><code>-2.658<\/code><code>,<\/code><code>0.469<\/code><code>l<\/code><code>-115.15<\/code><code>-80.629<\/code><code>c<\/code><code>-0.863<\/code><code>-0.605<\/code><code>-1.073<\/code><code>-1.795<\/code><code>-0.469<\/code><code>-2.658<\/code><code>l<\/code><code>20.974<\/code><code>-29.954<\/code><code>c<\/code><code>0.605<\/code><code>-0.863<\/code><code>,<\/code><code>1.795<\/code><code>-1.073<\/code><code>,<\/code><code>2.658<\/code><code>-0.469<\/code><code>L<\/code><code>159.337<\/code><code>,<\/code><code>292.35<\/code><code>z M<\/code><code>100.669<\/code><code>,<\/code><code>289.765<\/code><code>L<\/code><code>25.16<\/code><code>,<\/code><code>236.893<\/code> <code>M<\/code><code>149.278<\/code><code>,<\/code><code>309.079<\/code><code>l<\/code><code>-77.039<\/code><code>-53.943<\/code> <code>M<\/code><code>73.154<\/code><code>,<\/code><code>244.329<\/code><code>L<\/code><code>38.237<\/code><code>,<\/code><code>219.88<\/code> <code>M<\/code><code>154.817<\/code><code>,<\/code><code>300.734<\/code><code>l<\/code><code>-25.015<\/code><code>-17.516<\/code><code>\"\/&gt;<\/code><code>&lt;path fill=<\/code><code>\"#7A5034\"<\/code> <code>stroke=<\/code><code>\"#7A5034\"<\/code> <code>stroke-width=<\/code><code>\"2.5448\"<\/code> <code>stroke-linecap=<\/code><code>\"round\"<\/code> <code>stroke-linejoin=<\/code><code>\"round\"<\/code> <code>stroke-miterlimit=<\/code><code>\"10\"<\/code> <code>d=\"<\/code><code>M<\/code><code>44.187<\/code><code>,<\/code><code>325.431<\/code><code>c<\/code><code>-0.863<\/code><code>,<\/code><code>0.605<\/code><code>-2.054<\/code><code>,<\/code><code>0.395<\/code><code>-2.658<\/code><code>-0.469<\/code><code>l<\/code><code>-20.974<\/code><code>-29.954<\/code><code>c<\/code><code>-0.605<\/code><code>-0.863<\/code><code>-0.395<\/code><code>-2.054<\/code><code>,<\/code><code>0.469<\/code><code>-2.658<\/code><code>l<\/code><code>115.15<\/code><code>-80.629<\/code><code>c<\/code><code>0.863<\/code><code>-0.605<\/code><code>,<\/code><code>2.054<\/code><code>-0.395<\/code><code>,<\/code><code>2.658<\/code><code>,<\/code><code>0.469<\/code><code>l<\/code><code>20.974<\/code><code>,<\/code><code>29.954<\/code><code>c<\/code><code>0.605<\/code><code>,<\/code><code>0.863<\/code><code>,<\/code><code>0.395<\/code><code>,<\/code><code>2.054<\/code><code>-0.469<\/code><code>,<\/code><code>2.658<\/code><code>L<\/code><code>44.187<\/code><code>,<\/code><code>325.431<\/code><code>z M<\/code><code>66.681<\/code><code>,<\/code><code>271.185<\/code><code>l<\/code><code>75.509<\/code><code>-52.872<\/code> <code>M<\/code><code>31.907<\/code><code>,<\/code><code>310.257<\/code><code>l<\/code><code>77.039<\/code><code>-53.943<\/code> <code>M<\/code><code>118.788<\/code><code>,<\/code><code>260.87<\/code><code>l<\/code><code>34.917<\/code><code>-24.449<\/code> <code>M<\/code><code>37.855<\/code><code>,<\/code><code>318.317<\/code><code>l<\/code><code>25.015<\/code><code>-17.516<\/code><code>\"\/&gt;<\/code><code>&lt;path class=<\/code><code>\"outer-flame\"<\/code> <code>fill=<\/code><code>\"url(#largeGradient)\"<\/code> <code>stroke=<\/code><code>\"url(#largeGradient)\"<\/code> <code>stroke-width=<\/code><code>\"2\"<\/code> <code>stroke-linecap=<\/code><code>\"round\"<\/code> <code>stroke-linejoin=<\/code><code>\"round\"<\/code> <code>stroke-miterlimit=<\/code><code>\"10\"<\/code> <code>d=\"<\/code><code>M<\/code><code>82.579<\/code><code>,<\/code><code>210.049<\/code><code>c<\/code><code>-4.315<\/code><code>-0.393<\/code><code>-8.678<\/code><code>-1.158<\/code><code>-12.676<\/code><code>-2.352<\/code><code>c<\/code><code>-19.553<\/code><code>-5.839<\/code><code>-36.385<\/code><code>-22.93<\/code><code>-40.572<\/code><code>-43.069<\/code><code>c<\/code><code>-1.102<\/code><code>-5.301<\/code><code>-1.471<\/code><code>-11.261<\/code><code>-0.887<\/code><code>-16.656<\/code><code>c<\/code><code>1.646<\/code><code>-15.209<\/code><code>,<\/code><code>8.889<\/code><code>-31.008<\/code><code>,<\/code><code>16.749<\/code><code>-43.975<\/code><code>C<\/code><code>55.992<\/code><code>,<\/code><code>86.183<\/code><code>,<\/code><code>67.031<\/code><code>,<\/code><code>68.265<\/code><code>,<\/code><code>74.26<\/code><code>,<\/code><code>48.634<\/code><code>c<\/code><code>2.079<\/code><code>-5.647<\/code><code>,<\/code><code>3.825<\/code><code>-11.421<\/code><code>,<\/code><code>5.117<\/code><code>-17.299<\/code><code>c<\/code><code>2.019<\/code><code>-9.181<\/code><code>,<\/code><code>3.897<\/code><code>-21.151<\/code><code>-3.384<\/code><code>-28.647<\/code><code>c<\/code><code>14.729<\/code><code>,<\/code><code>15.163<\/code><code>,<\/code><code>27.537<\/code><code>,<\/code><code>32.13<\/code><code>,<\/code><code>38.675<\/code><code>,<\/code><code>50.078<\/code><code>c<\/code><code>4.029<\/code><code>,<\/code><code>6.492<\/code><code>,<\/code><code>7.841<\/code><code>,<\/code><code>13.117<\/code><code>,<\/code><code>11.454<\/code><code>,<\/code><code>19.849<\/code><code>c<\/code><code>3.744<\/code><code>,<\/code><code>6.976<\/code><code>,<\/code><code>7.259<\/code><code>,<\/code><code>14.075<\/code><code>,<\/code><code>10.524<\/code><code>,<\/code><code>21.288<\/code><code>c<\/code><code>4.38<\/code><code>,<\/code><code>9.678<\/code><code>,<\/code><code>7.966<\/code><code>,<\/code><code>19.728<\/code><code>,<\/code><code>10.488<\/code><code>,<\/code><code>30.052<\/code><code>c<\/code><code>4.66<\/code><code>,<\/code><code>19.078<\/code><code>,<\/code><code>9.751<\/code><code>,<\/code><code>43.266<\/code><code>-2.968<\/code><code>,<\/code><code>60.391<\/code><code>c<\/code><code>-5.683<\/code><code>,<\/code><code>7.652<\/code><code>-12.951<\/code><code>,<\/code><code>13.902<\/code><code>-21.589<\/code><code>,<\/code><code>17.991<\/code><code>c<\/code><code>-10.308<\/code><code>,<\/code><code>4.879<\/code><code>-22.085<\/code><code>,<\/code><code>8.124<\/code><code>-33.541<\/code><code>,<\/code><code>8.032<\/code><code>C<\/code><code>86.931<\/code><code>,<\/code><code>210.352<\/code><code>,<\/code><code>84.761<\/code><code>,<\/code><code>210.248<\/code><code>,<\/code><code>82.579<\/code><code>,<\/code><code>210.049<\/code><code>z\"&gt;<\/code><code>&lt;\/path&gt;<\/code><code>&lt;path class=<\/code><code>\"inner-flame\"<\/code> <code>fill=<\/code><code>\"url(#smallGradient)\"<\/code> <code>stroke=<\/code><code>\"url(#smallGradient)\"<\/code> <code>stroke-width=<\/code><code>\"2\"<\/code> <code>stroke-linecap=<\/code><code>\"round\"<\/code> <code>stroke-linejoin=<\/code><code>\"round\"<\/code> <code>stroke-miterlimit=<\/code><code>\"10\"<\/code> <code>d=\"<\/code><code>M<\/code><code>90.043<\/code><code>,<\/code><code>116.799<\/code><code>c<\/code><code>-4.024<\/code><code>,<\/code><code>9.712<\/code><code>-12.607<\/code><code>,<\/code><code>17.546<\/code><code>-17.863<\/code><code>,<\/code><code>26.662<\/code><code>c<\/code><code>-5.921<\/code><code>,<\/code><code>10.268<\/code><code>-11.892<\/code><code>,<\/code><code>23.039<\/code><code>-9.362<\/code><code>,<\/code><code>35.209<\/code><code>1.865<\/code><code>,<\/code><code>8.974<\/code><code>,<\/code><code>9.366<\/code><code>,<\/code><code>16.589<\/code><code>,<\/code><code>18.078<\/code><code>,<\/code><code>19.191<\/code><code>c<\/code><code>1.782<\/code><code>,<\/code><code>0.532<\/code><code>,<\/code><code>3.726<\/code><code>,<\/code><code>0.873<\/code><code>,<\/code><code>5.648<\/code><code>,<\/code><code>1.048<\/code><code>c<\/code><code>0.972<\/code><code>,<\/code><code>0.089<\/code><code>,<\/code><code>1.939<\/code><code>,<\/code><code>0.135<\/code><code>,<\/code><code>2.877<\/code><code>,<\/code><code>0.143<\/code><code>c<\/code><code>5.104<\/code><code>,<\/code><code>0.041<\/code><code>,<\/code><code>10.352<\/code><code>-1.405<\/code><code>,<\/code><code>14.945<\/code><code>-3.579<\/code><code>c<\/code><code>3.849<\/code><code>-1.822<\/code><code>,<\/code><code>7.087<\/code><code>-4.607<\/code><code>,<\/code><code>9.62<\/code><code>-8.017<\/code><code>c<\/code><code>7.656<\/code><code>-10.307<\/code><code>,<\/code><code>3.202<\/code><code>-24.177<\/code><code>-1.21<\/code><code>-34.838<\/code><code>c<\/code><code>-6.142<\/code><code>-14.841<\/code><code>-15.085<\/code><code>-28.459<\/code><code>-22.297<\/code><code>-42.793<\/code><code>C<\/code><code>91.464<\/code><code>,<\/code><code>111.781<\/code><code>,<\/code><code>91.118<\/code><code>,<\/code><code>114.204<\/code><code>,<\/code><code>90.043<\/code><code>,<\/code><code>116.799<\/code><code>z\"&gt;<\/code><code>&lt;\/path&gt;<\/code><code>&lt;\/svg&gt;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"4967\" data-permalink=\"https:\/\/azoora.com\/blog\/code\/tutorial-animating-svg-gradients\/attachment\/greengradient\/#main\" data-orig-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/greengradient.png\" data-orig-size=\"600,146\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"greengradient\" data-image-description=\"\" data-medium-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/greengradient-300x73.png\" data-large-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/greengradient.png\" loading=\"lazy\" width=\"600\" height=\"146\" src=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/greengradient.png\" alt=\"\" class=\"wp-image-4967\" srcset=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/greengradient.png 600w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/greengradient-300x73.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<p>The gradient details are written within the&nbsp;<code>&lt;defs&gt;<\/code>&nbsp;element, but would have no output within our work until we call on it using its unique ID. At this point we would also add the shape within the same&nbsp;<code>&lt;svg&gt;<\/code>&nbsp;element, but outside of the&nbsp;<code>&lt;defs&gt;<\/code>&nbsp;element.<\/p>\n\n\n\n<p>The&nbsp;<code>&lt;stop&gt;<\/code>&nbsp;nodes represent the color selections and their locations on our mapped gradient, which we will discuss further in the following&nbsp;section.<\/p>\n\n\n\n<h3>Gradient&nbsp;Attributes<\/h3>\n\n\n\n<p>The attribute values for&nbsp;<code>&lt;linearGradient&gt;<\/code>&nbsp;and&nbsp;<code>&lt;radialGradient&gt;<\/code>&nbsp;allow us to specify the colors and coordinates of our&nbsp;gradients.<\/p>\n\n\n\n<p>Here is a quick look at the attributes that will be used in the demos to&nbsp;follow:<\/p>\n\n\n\n<h4>ID<\/h4>\n\n\n\n<p>A gradient requires a unique ID in order for it to be called on through \u201cfill\u201d and \u201cstroke\u201d\u00a0attributes.<\/p>\n\n\n\n<h4>x1, x2, y1, y2<\/h4>\n\n\n\n<p>The values within the x1, x2, y1, y2 attributes specify the start and end coordinates for linear gradients along the appropriate axis. These values default to \u201c0\u201d if left unspecified, except for x2 which defaults to&nbsp;\u201c100%\u201d.<\/p>\n\n\n\n<h4>cx, cy, r, fx, fy<\/h4>\n\n\n\n<p>Radial gradient attributes are similar to those of linear, except our coordinates will be handled much differently. Instead of being based on a straight line, these gradients will be mapped&nbsp;circularly.<\/p>\n\n\n\n<p>cx, cy, and r (radius) define the outermost circle for the gradient, with this outer perimeter being the 100% stop point. fx and fy define the focal point of the gradient, with the 0% stop mapping to these&nbsp;values.<\/p>\n\n\n\n<h2>&lt;stop&gt;<\/h2>\n\n\n\n<p>Within the gradient element itself we include the&nbsp;<code>&lt;stop&gt;<\/code>&nbsp;nodes. The attribute values within&nbsp;<code>&lt;stop&gt;<\/code>&nbsp;nodes define the colors for our gradient, where they should be placed, and any opacity that we want&nbsp;applied.<\/p>\n\n\n\n<p>The offset property indicates where the gradient stop is located. For linear gradients this location will be a distance along the gradient line, for radial gradients it will be a distance from the focal point to the edge of the mapped&nbsp;circle.<\/p>\n\n\n\n<p><strong>stop-color<\/strong>&nbsp;indicates what color will be used at the correlating stop point, and&nbsp;<strong>stop-opacity<\/strong>&nbsp;sets an opacity at this same point. Depending on how many stops we wish to use we can choose between creating subtle, gradual color changes with only a few stops, or create stripes with several closely mapped&nbsp;stops.<\/p>\n\n\n\n<h2>&lt;animate&gt;<\/h2>\n\n\n\n<p><code>&lt;animate&gt;<\/code>&nbsp;element will allow us to select gradient attributes and properties and then assign different values to them over a specified duration of time. In short, we can move them! The&nbsp;<code>&lt;animate&gt;<\/code>&nbsp;element will reside within the element we wish to animate. Here is the structure for a basic animation on a&nbsp;rectangle:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>&lt;defs&gt;<\/code><code>&lt;radialGradient id=<\/code><code>\"smallGradient\"<\/code> <code>fy=<\/code><code>\"90%\"<\/code><code>&gt;<\/code><code>&lt;stop offset=<\/code><code>\"0%\"<\/code> <code>stop-color=<\/code><code>\"#f4c708\"<\/code><code>&gt;&lt;\/stop&gt;<\/code><code>&lt;stop offset=<\/code><code>\"100%\"<\/code> <code>stop-color=<\/code><code>\"#f7e69a\"<\/code><code>&gt;&lt;\/stop&gt;<\/code><code>&lt;animate attributeName=<\/code><code>\"fy\"<\/code> <code>dur=<\/code><code>\"700ms\"<\/code> <code>from=<\/code><code>\"90%\"<\/code> <code>to=<\/code><code>\"0%\"<\/code> <code>repeatCount=<\/code><code>\"indefinite\"<\/code> <code>\/&gt;<\/code><code>&lt;\/radialGradient&gt;<\/code><code>&lt;radialGradient id=<\/code><code>\"largeGradient\"<\/code> <code>fy=<\/code><code>\"90%\"<\/code><code>&gt;<\/code><code>&lt;stop offset=<\/code><code>\"0%\"<\/code> <code>stop-color=<\/code><code>\"#ff8806\"<\/code><code>&gt;&lt;\/stop&gt;<\/code><code>&lt;stop offset=<\/code><code>\"100%\"<\/code> <code>stop-color=<\/code><code>\"#d9574a\"<\/code><code>&gt;&lt;\/stop&gt;<\/code><code>&lt;animate attributeName=<\/code><code>\"fy\"<\/code> <code>dur=<\/code><code>\"700ms\"<\/code> <code>from=<\/code><code>\"90%\"<\/code> <code>to=<\/code><code>\"0%\"<\/code> <code>repeatCount=<\/code><code>\"indefinite\"<\/code> <code>\/&gt;<\/code><code>&lt;\/radialGradient&gt;<\/code><code>&lt;\/defs&gt;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The animation will move the rectangle along the x axis 400px pixels from its starting point. The animation will complete within 5 seconds, and repeat&nbsp;indefinitely.<\/p>\n\n\n\n<h3>Attributes<\/h3>\n\n\n\n<p>Within the&nbsp;<code>&lt;animate&gt;<\/code>&nbsp;element we will use attributes to select our target and specify the details of the animation. Here is a look at the specific attributes we will focus on in the&nbsp;demos:<\/p>\n\n\n\n<h4>attributeName<\/h4>\n\n\n\n<p>attributeName defines the name of the target attribute to be animated. In taking another look at the animation code above, the&nbsp;<strong>attributeName<\/strong>&nbsp;value is \u201cx\u201d so that we can target and animate our shape on the x&nbsp;axis.<\/p>\n\n\n\n<h4>to, from<\/h4>\n\n\n\n<p>The \u201cto\u201d and \u201cfrom\u201d attributes indicate the initial and final value of the targeted&nbsp;attribute.<\/p>\n\n\n\n<h4>values<\/h4>\n\n\n\n<p>Within the&nbsp;<code>&lt;values&gt;<\/code>&nbsp;attribute we can create a list of values separated by semicolons, and the animation will apply these values in order over the duration of the&nbsp;action.<\/p>\n\n\n\n<p>By including this attribute any \u201cfrom\u201d or \u201cto\u201d values within the same&nbsp;<code>&lt;animate&gt;<\/code>&nbsp;element will be ignored, meaning that it takes the place of these values once included so there is no need for&nbsp;them.<\/p>\n\n\n\n<h4>dur<\/h4>\n\n\n\n<p>The duration defines within what time span the animation should&nbsp;complete.<\/p>\n\n\n\n<h4>fill<\/h4>\n\n\n\n<p>While \u201cfill\u201d often refers to the interior color of SVG, it has a different meaning in the context of an&nbsp;<code>&lt;animate&gt;<\/code>&nbsp;attribute. We can set a value of \u201cfreeze\u201d within this attribute so that the animation freezes once it is complete and does not&nbsp;restart.<\/p>\n\n\n\n<h4>repeatCount<\/h4>\n\n\n\n<p>The repeatCount attribute specifies the number of times an animation should take place. This value can be either a number, or&nbsp;\u201cindefinite\u201d.<\/p>\n\n\n\n<h3>Animating&nbsp;Gradients<\/h3>\n\n\n\n<p>To target gradients specifically we need to have an idea of what properties we would like to animate, like the&nbsp;<strong>stop-color<\/strong>,&nbsp;<strong>offset<\/strong>, and\/or the specific&nbsp;coordinates.<\/p>\n\n\n\n<p>When we reviewed the code for the moving rectangle animation above the&nbsp;<code>&lt;animate&gt;<\/code>&nbsp;element resided within the shape\u2019s element, the&nbsp;<code>&lt;rect&gt;<\/code>. In order to animate gradient stops we place the&nbsp;<code>&lt;animate&gt;<\/code>&nbsp;element within the specific&nbsp;<code>&lt;stop&gt;<\/code>&nbsp;we intend to animate. To animate an attribute directly within the a&nbsp;<code>&lt;linearGradient&gt;<\/code>&nbsp;or&nbsp;<code>&lt;radialGradient&gt;<\/code>&nbsp;element, like coordinates for example, we position&nbsp;<code>&lt;animate&gt;<\/code>&nbsp;within the gradient element, but outside of the&nbsp;<code>&lt;stop&gt;<\/code>&nbsp;elements.<\/p>\n\n\n\n<p>Here\u2019s a look at the structure for an animation on the&nbsp;<strong>stop-color<\/strong>&nbsp;of a&nbsp;<code>&lt;stop&gt;<\/code>&nbsp;node.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>&lt;stop offset=<\/code><code>\"0%\"<\/code> <code>stop-color=<\/code><code>\"#f4c708\"<\/code><code>&gt;&lt;\/stop&gt;<\/code><code>&lt;stop offset=<\/code><code>\"100%\"<\/code> <code>stop-color=<\/code><code>\"#f7e69a\"<\/code><code>&gt;&lt;\/stop&gt;<\/code><code>&lt;animate attributeName=<\/code><code>\"fy\"<\/code> <code>dur=<\/code><code>\"700ms\"<\/code> <code>from=<\/code><code>\"90%\"<\/code> <code>to=<\/code><code>\"0%\"<\/code> <code>repeatCount=<\/code><code>\"indefinite\"<\/code> <code>\/&gt;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2>Demos<\/h2>\n\n\n\n<p>To demonstrate the possibilities of animating SVG gradients we will look at some potential effects for a fictional camping site. The demos consist of animated gradients on fire, trees, and a&nbsp;skyline.<\/p>\n\n\n\n<h3>Demo 01:\u00a0Fire<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"4968\" data-permalink=\"https:\/\/azoora.com\/blog\/code\/tutorial-animating-svg-gradients\/attachment\/firepreview\/#main\" data-orig-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/firepreview.png\" data-orig-size=\"600,250\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"firepreview\" data-image-description=\"\" data-medium-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/firepreview-300x125.png\" data-large-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/firepreview.png\" loading=\"lazy\" width=\"600\" height=\"250\" src=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/firepreview.png\" alt=\"\" class=\"wp-image-4968\" srcset=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/firepreview.png 600w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/firepreview-300x125.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<p>Within the fire SVG we are targeting two flames to fill and animate, and as mentioned all the details are listed within a&nbsp;<code>&lt;defs&gt;<\/code>&nbsp;element in the&nbsp;<code>&lt;svg&gt;<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>&lt;defs&gt;<\/code><code>&lt;radialGradient id=<\/code><code>\"smallGradient\"<\/code> <code>fy=<\/code><code>\"90%\"<\/code><code>&gt;<\/code><code>&lt;stop offset=<\/code><code>\"0%\"<\/code> <code>stop-color=<\/code><code>\"#f4c708\"<\/code><code>&gt;&lt;\/stop&gt;<\/code><code>&lt;stop offset=<\/code><code>\"100%\"<\/code> <code>stop-color=<\/code><code>\"#f7e69a\"<\/code><code>&gt;&lt;\/stop&gt;<\/code><code>&lt;animate attributeName=<\/code><code>\"fy\"<\/code> <code>dur=<\/code><code>\"700ms\"<\/code> <code>from=<\/code><code>\"90%\"<\/code> <code>to=<\/code><code>\"0%\"<\/code> <code>repeatCount=<\/code><code>\"indefinite\"<\/code> <code>\/&gt;<\/code><code>&lt;\/radialGradient&gt;<\/code><code>&lt;radialGradient id=<\/code><code>\"largeGradient\"<\/code> <code>fy=<\/code><code>\"90%\"<\/code><code>&gt;<\/code><code>&lt;stop offset=<\/code><code>\"0%\"<\/code> <code>stop-color=<\/code><code>\"#ff8806\"<\/code><code>&gt;&lt;\/stop&gt;<\/code><code>&lt;stop offset=<\/code><code>\"100%\"<\/code> <code>stop-color=<\/code><code>\"#d9574a\"<\/code><code>&gt;&lt;\/stop&gt;<\/code><code>&lt;animate attributeName=<\/code><code>\"fy\"<\/code> <code>dur=<\/code><code>\"700ms\"<\/code> <code>from=<\/code><code>\"90%\"<\/code> <code>to=<\/code><code>\"0%\"<\/code> <code>repeatCount=<\/code><code>\"indefinite\"<\/code> <code>\/&gt;<\/code><code>&lt;\/radialGradient&gt;<\/code><code>&lt;\/defs&gt;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>There are two radial gradients, \u201csmallGradient\u201d and \u201clargeGradient\u201d. The same animation is applied to each gradient targeting the fy coordinate point and moving it from 90% to 0% within 700ms. The animation is set to take place&nbsp;indefinitely.<\/p>\n\n\n\n<p>The assignment of these gradients is done by adding the gradient ID to the \u201cfill\u201d of the shape via a URL, for&nbsp;example:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>fill=&amp;quot;<\/code><code>url<\/code><code>(#largeGradient)&amp;quot;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3>Demo 02:\u00a0Trees<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"4969\" data-permalink=\"https:\/\/azoora.com\/blog\/code\/tutorial-animating-svg-gradients\/attachment\/treepreview\/#main\" data-orig-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/treepreview.png\" data-orig-size=\"600,250\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"treepreview\" data-image-description=\"\" data-medium-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/treepreview-300x125.png\" data-large-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/treepreview.png\" loading=\"lazy\" width=\"600\" height=\"250\" src=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/treepreview.png\" alt=\"\" class=\"wp-image-4969\" srcset=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/treepreview.png 600w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/treepreview-300x125.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<p>For the trees demo we will be adding the animation to the gradient&nbsp;<strong>stop-color<\/strong>. There are two gradients to represent different colors for the trees, one with dark shades of green and one with light. Each animation is essentially the same, the only difference being the colors&nbsp;used.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>&lt;defs&gt;<\/code><code>&lt;linearGradient id=<\/code><code>\"darkGradient\"<\/code> <code>x<\/code><code>1<\/code><code>=<\/code><code>\"30%\"<\/code> <code>y<\/code><code>1<\/code><code>=<\/code><code>\"70%\"<\/code><code>&gt;<\/code><code>&lt;stop offset=<\/code><code>\"0%\"<\/code> <code>stop-color=<\/code><code>\"#0b2d13\"<\/code><code>&gt;<\/code><code>&lt;animate attributeName=<\/code><code>\"stop-color\"<\/code> <code>values=<\/code><code>\"#0b2d13; #79c98c\"<\/code> <code>dur=<\/code><code>\"3s\"<\/code> <code>fill=<\/code><code>\"freeze\"<\/code> <code>\/&gt;<\/code><code>&lt;\/stop&gt;<\/code><code>&lt;stop offset=<\/code><code>\"100%\"<\/code> <code>stop-color=<\/code><code>\"#79c98c\"<\/code><code>&gt;<\/code><code>&lt;animate attributeName=<\/code><code>\"stop-color\"<\/code> <code>values=<\/code><code>\"#79c98c; #0b2d13\"<\/code> <code>dur=<\/code><code>\"3s\"<\/code> <code>fill=<\/code><code>\"freeze\"<\/code> <code>\/&gt;<\/code><code>&lt;\/stop&gt;<\/code><code>&lt;\/linearGradient&gt;<\/code>&nbsp;<code>&lt;linearGradient id=<\/code><code>\"lightGradient\"<\/code> <code>x<\/code><code>1<\/code><code>=<\/code><code>\"30%\"<\/code> <code>y<\/code><code>1<\/code><code>=<\/code><code>\"70%\"<\/code><code>&gt;<\/code><code>&lt;stop offset=<\/code><code>\"0%\"<\/code> <code>stop-color=<\/code><code>\"#054f16\"<\/code><code>&gt;<\/code><code>&lt;animate attributeName=<\/code><code>\"stop-color\"<\/code> <code>values=<\/code><code>\"#054f16; #91bc9c\"<\/code> <code>dur=<\/code><code>\"3s\"<\/code> <code>fill=<\/code><code>\"freeze\"<\/code> <code>\/&gt;<\/code><code>&lt;\/stop&gt;<\/code><code>&lt;stop offset=<\/code><code>\"100%\"<\/code> <code>stop-color=<\/code><code>\"#91bc9c\"<\/code><code>&gt;<\/code><code>&lt;animate attributeName=<\/code><code>\"stop-color\"<\/code> <code>values=<\/code><code>\"#91bc9c; #054f16\"<\/code> <code>dur=<\/code><code>\"3s\"<\/code> <code>fill=<\/code><code>\"freeze\"<\/code> <code>\/&gt;<\/code><code>&lt;\/stop&gt;<\/code><code>&lt;\/linearGradient&gt;<\/code><code>&lt;\/defs&gt;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The&nbsp;<code>&lt;animate&gt;<\/code>&nbsp;element resides within the 100% stop point for each gradient and targets the&nbsp;<strong>stop-color<\/strong>. The color values are animated through the&nbsp;<strong>values<\/strong>&nbsp;attribute, over a period of 3 seconds, and the animation is set to stop, or \u201cfreeze\u201d once it completes one&nbsp;cycle.<\/p>\n\n\n\n<h3>Demo 03:\u00a0Sky<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"4970\" data-permalink=\"https:\/\/azoora.com\/blog\/code\/tutorial-animating-svg-gradients\/attachment\/skypreview\/#main\" data-orig-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/skypreview.png\" data-orig-size=\"600,250\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"skypreview\" data-image-description=\"\" data-medium-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/skypreview-300x125.png\" data-large-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/skypreview.png\" loading=\"lazy\" width=\"600\" height=\"250\" src=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/skypreview.png\" alt=\"\" class=\"wp-image-4970\" srcset=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/skypreview.png 600w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/skypreview-300x125.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<p>The concept of a sunset is an ideal example of when animating a stop\u2019s offset would be needed. There are two gradients here, each having a series of animated color changes. With an additional animation on the&nbsp;<strong>offset<\/strong>&nbsp;of the 100% stop point the point moves down (to 0) and then back up&nbsp;again.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>&lt;defs&gt;<\/code><code>&lt;linearGradient id=<\/code><code>\"skyGradient\"<\/code> <code>x<\/code><code>1<\/code><code>=<\/code><code>\"100%\"<\/code> <code>y<\/code><code>1<\/code><code>=<\/code><code>\"100%\"<\/code><code>&gt;<\/code><code>&lt;stop offset=<\/code><code>\"0%\"<\/code> <code>stop-color=<\/code><code>\"lightblue\"<\/code> <code>stop-opacity=<\/code><code>\".5\"<\/code><code>&gt;<\/code><code>&lt;animate attributeName=<\/code><code>\"stop-color\"<\/code> <code>values=<\/code><code>\"lightblue;blue;red;red;black;red;red;purple;lightblue\"<\/code> <code>dur=<\/code><code>\"14s\"<\/code> <code>repeatCount=<\/code><code>\"indefinite\"<\/code> <code>\/&gt;<\/code><code>&lt;\/stop&gt;<\/code><code>&lt;stop offset=<\/code><code>\"100%\"<\/code> <code>stop-color=<\/code><code>\"lightblue\"<\/code> <code>stop-opacity=<\/code><code>\".5\"<\/code><code>&gt;<\/code><code>&lt;animate attributeName=<\/code><code>\"stop-color\"<\/code> <code>values=<\/code><code>\"lightblue;orange;purple;purple;black;purple;purple;blue;lightblue\"<\/code> <code>dur=<\/code><code>\"14s\"<\/code> <code>repeatCount=<\/code><code>\"indefinite\"<\/code> <code>\/&gt;<\/code><code>&lt;animate attributeName=<\/code><code>\"offset\"<\/code> <code>values=<\/code><code>\".95;.80;.60;.40;.20;0;.20;.40;.60;.80;.95\"<\/code> <code>dur=<\/code><code>\"14s\"<\/code> <code>repeatCount=<\/code><code>\"indefinite\"<\/code> <code>\/&gt;&nbsp;&nbsp;&nbsp; <\/code><code>&lt;\/stop&gt;<\/code><code>&lt;\/linearGradient&gt;<\/code><code>&lt;\/defs&gt;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>For the&nbsp;<strong>stop-color<\/strong>&nbsp;animations there\u2019s a list of color values the gradient will cycle through within 14s. The additional&nbsp;<strong>offset<\/strong>&nbsp;animation changes the location of the 100% stop point through a series of numerical values, also within&nbsp;14s.<\/p>\n\n\n\n<p>It is important to mention here that while you should technically be able to use percentages when animating&nbsp;<strong>offset<\/strong>, and can do so without problems in Firefox, Chrome and Safari will not render the animation on the&nbsp;<strong>offset<\/strong>&nbsp;when percentage values are used within&nbsp;<code>&lt;animate&gt;<\/code>. The work around here is to simply avoid using percentages, as shown in the code&nbsp;above.<\/p>\n\n\n\n<h3>Conclusion<\/h3>\n\n\n\n<p>For this <strong>article<\/strong>, we discussed the <strong>basic structure <\/strong>of <strong>SVG gradients<\/strong>, how to utilize SVG SMIL animation once they are created, and reviewed some demos regarding how this can be handy in communicating to\u00a0users.<\/p>\n\n\n\n<p>Hopefully, all this inspires you to take a look at writing gradients within SVG code and get them moving all within the same SVG&nbsp;element.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SVG gradients are so handy. We can fill complex shapes and create depth and character for our artwork all while having access to it in the\u00a0DOM. Within SVG linear and radial gradient elements there are several attribute options available for thorough customization. Animating these gradients takes this customization even further and presents a unique opportunity [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":4971,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[4,145,62],"tags":[93,25,146,96,110,101],"jetpack_featured_media_url":"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/Animating-SVG-Gradients.jpg","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p7FQPL-1i6","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/4966"}],"collection":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/comments?post=4966"}],"version-history":[{"count":1,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/4966\/revisions"}],"predecessor-version":[{"id":4972,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/4966\/revisions\/4972"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media\/4971"}],"wp:attachment":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media?parent=4966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/categories?post=4966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/tags?post=4966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}