XML technologies / SVG / Filters, transformations and gradients / Using and combining filters and lighting effectively / The filter tag

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

The <filter> tag defines filters in the webpage’s <defs> section, with one or more child filter elements. The <filter> tag always closes with </filter> because filter elements go between. Its attributes are:

Note:
Higher resolution takes longer to render, especially with complex filters. Avoid excessively large values.

Note:
SVG Viewer 1.0 and 2.0 do not yet support the attribute xlink:href for the <filter> tag.

The first example illustrates these attributes. A child of the <filter> tag is required, so it uses the effect <feGaussianBlur>. This and later examples use the same graphic, shown without filters below. Its two elements illustrate effects on grouped and individual objects.

Original graphic for grouped and individual object filters

Figure: Original graphic for grouped and individual object filters.

<?xml version="1.0" standalone="no"?>
...
<svg width="300" height="150">
  <defs>
    <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(20,20), scale(0.7)">
    <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 transform="translate(120,20)">
    <desc>Der Text mit Rechteck</desc>
    <rect x="0" y="0" width="150" height="75" style="fill:red;stroke:black;stroke-width:1"/>
    <text x="20" y="55" style="font-family:'Arial Black';font-size:46;stroke:black; fill:yellow">SVG</text>
  </g>
</svg>

Note:
Later examples show only code additions for brevity. Unless stated otherwise, place the <filter> tag inside the <defs> tag.

Apply the listed filter to the button group and the text "SVG" in the rectangle. Parts of the originals appear with shadows, selected by the attributes width, height, x and y.

<filter id="AusschnittMitSchatten" filterUnits="userSpaceOnUse" x="10" y="10" width="20" height="150">
  <feGaussianBlur stdDeviation="3"/>
</filter>

Individual objects with filters

Figure: Individual objects with filters.

Set the filter’s resolution with the attribute filterRes to 600 dpi and filtered objects appear greatly enlarged.

<filter id="AusschnittMitSchatten" x="10" y="10" width="20" height="150" filterRes="600,600">
  <feGaussianBlur stdDeviation="3"/>
</filter>

Note:
Screens display at most 72 dpi on PC or 96 dpi on Mac. Only graphics at that resolution appear at original size, explaining smaller Mac text and graphics. An SVG graphic rendered at 600 dpi prints at original size on a 600 dpi printer.

Filter regions rendered at 600 dpi

Figure: Filter regions rendered at 600 dpi.

Note:
Specifying resolution with the filterRes attribute seems unreliable in SVG Viewer 2.0. Increasing the region at constant resolution makes the same graphic smaller. The larger the region, the smaller the resolution. At roughly 90 pixels wide, both objects already appear at original size.

   

 

 

 

back to top