XML technologies / SVG / Using animations / Ways to implement animations / animate provides very flexible possibilities

(Excerpt from “Das Einsteigerseminar SVG — Webgrafiken mit XML” by Helma Spona)

The <animate> tag supports most interesting animations because it can manipulate every attribute and property in the style attribute, including filter settings. It has only universal attributes. The next example uses the previous chapter’s button (see Filters, transformations and gradients), animating its shadow to make it appear pressed. Simply insert an <animate> tag into the filter <feGaussianBlur> that creates its shadow.

<?xml version="1.0" standalone="no"?>
...
<svg width="300" height="800">
  <defs>
    <g id="Bttn">
      <desc>Der Button</desc>
      <circle cx="25" cy="25" r="25" style="stroke-width:1;stroke-opacity:0.8;stroke:rgb(222,0,0);fill-opacity:1;opacity:1;fill:url(#runderVerlauf)"/>
      <ellipse cx="25" cy="15" rx="20" ry="12" style="stroke-width:1;stroke:none;fill-opacity:1;opacity:1;fill:url(#linearerVerlauf)"/>
    </g>
    <filter id="SVGObjekt" filterUnits="userSpaceOnUse" width="150" heigth="150">
      <feGaussianBlur id="Schatten" in="SourceAlpha" stdDeviation="12 6" result="Schatten">
        <animate attributeName="stdDeviation" begin="2s" dur="0.5s" values="6,3"/>
      </feGaussianBlur>
      <feImage xlink:href="#Bttn" result="Button"/>
      <feMerge>
        <feMergeNode in="Schatten"/>
        <feMergeNode in="Button"/>
      </feMerge>
    </filter>
    <desc>Es folgen die Farbverläufe</desc>
    <radialGradient id="runderVerlauf" cx="50%" cy="90%" r="60%" fx="60%" fy="80%">
      <stop offset="0%" style="stop-color:rgb(255,150,150);stop-opacity:1"/>
      <stop offset="1%" style="stop-color:rgb(255,128,128);stop-opacity:1"/>
      <stop offset="50%" style="stop-color:rgb(255,0,0);stop-opacity:1"/>
    </radialGradient>
    <linearGradient id="linearerVerlauf" gradientTransform="rotate(90)">
      <stop offset="0%" style="stop-color:rgb(255,255,255);stop-opacity:1"/>
      <stop offset="90%" style="stop-color:rgb(255,1,1);stop-opacity:1"/>
    </linearGradient>
  </defs>
  <g transform="translate(0,0)">
    <g transform="translate(20,20), scale(0.7)" style="filter:url(#SVGObjekt)" id="roterButton">
      <desc>Der Button</desc>
      <circle cx="25" cy="25" r="25" style="stroke-width:1;stroke-opacity:0.8;stroke:rgb(222,0,0);fill-opacity:1;opacity:1;fill:url(#runderVerlauf)"/>
      <ellipse cx="25" cy="15" rx="20" ry="12" style="stroke-width:1;stroke:none;fill-opacity:1;opacity:1;fill:url(#linearerVerlauf)"/>
    </g>
  </g>
</svg>

The filter’s animation reduces the button’s shadow after 2 seconds (begin="2s"). The values value list sets the attribute stdDeviation to 6 on the x axis and 3 on the y axis (values="6,3"). Specifying dur="0.5" keeps this setting for half a second, then restores the original state. The next figure shows both button states.

Button at rest on the left, “pressed” on the right

Figure: Button at rest on the left, “pressed” on the right.

The tag <animation> changes not only tag attributes but also any stylesheet settings or properties of an element’s style attribute. The next example animates text opacity from 0 to 0.7 and back to 0 over one minute, then another animation sets it to 0.5 permanently. Simply extend the <text> tag as follows:

<text id="unterschrift" x="244" y="90" style="font-family:'Arial Black',Helvetica;font-size:39;stroke-width:1;stroke:rgb(0,0,0);fill-opacity:0.5;fill:red">Scalable Vector Graphic 
  <animate attributeType="CSS" attributeName="fill-opacity" values="0;0.1;0.2;0.3;0.4;0.5;0.6;0.7;0.6;0.5;0.4;0.3;0.2;0.1;0" dur="10s" repeatDur="60" fill="freeze"/>
  <animate attributeType="CSS" attributeName="fill-opacity" begin="60s" values="0.5" dur="1s" fill="freeze"/>
</text>

   

 

 

 

back to top