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:
filterUnitssets the filter coordinate system. Possible values areuserSpaceOnUseandobjectBoundingBox. WithuserSpaceOnUseuse the attributesx,y,widthandheightfor absolute values relative to the filtered object’s area. WithobjectBoundingBoxthose values are percentages relative to the object containing the one formatted with<filter>: a group with its own coordinates, or the drawing surface.primitiveUnitscontrols measurements within child filter elements. Possible values areuserSpaceOnUseandobjectBoundingBox. WithuserSpaceOnUselengths and widths use the current coordinate system. WithobjectBoundingBoxthey are percentages and must be written as such.x,y,widthandheighttogether define the filter region. The attributeswidthandheightmust be positive. Zero hides the filtered element; negative values cause an error. Omittingwidthandheigthdefaults both to120%. The origin-defining attributesxandydefault when omitted to-10%.filterResdefines resolution in the formx-pixel, y-pixel. The declarationfilterRes="600, 600"means 600 x 600 dpi. Vertical resolution is optional and defaults tox-pixel. Omitting the attribute uses the current screen resolution, 72 or 96 dpi, for screen output, and 400 dpi for printing.
Note:
Higher resolution takes longer to render, especially with complex filters. Avoid excessively large values.
xlink:hrefreferences another filter, incorporated into the filter with thexlink:hrefattribute as if defined there.
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.
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>
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.
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.
Copyright © verlag moderne industrie GmbH 2001
You may print the online version for your private use.
Otherwise, this chapter from “Das Einsteigerseminar SVG — Webgrafiken mit XML” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
verlag moderne industrie GmbH, Justus-von-Liebig-Straße 1, D-86899 Landsberg/Lech, fon ++49 (0) 81 91 / 125 – 0, leserservice(at)mi-verlag.de
back to top