SVG (Scalable Vector Graphics) provides various ways to apply filters to elements, allowing you to create visual effects and enhance the appearance of your graphics. Here are some common ways to use filters in SVG:
<filter>
Element:
<filter>
element within the <defs>
section of your SVG.<filter>
element to define the visual effect.xml<svg>
<defs>
<filter id="myFilter" x="0" y="0" width="100%" height="100%">
<!-- Define filter primitives here -->
</filter>
</defs>
<!-- Apply the filter to an element using the filter attribute -->
<rect width="100" height="100" style="fill: blue;" filter="url(#myFilter)" />
</svg>
Filter Primitives:
<feGaussianBlur>
, <feColorMatrix>
, <feBlend>
, etc., to create specific visual effects.xml<filter id="blurFilter">
<feGaussianBlur in="SourceGraphic" stdDeviation="5" />
</filter>
Apply Multiple Filters:
url()
function in the filter
attribute.xml<rect width="100" height="100" style="fill: blue;" filter="url(#filter1) url(#filter2)" />
Use with CSS:
css.myElement {
filter: url(#myFilter);
}
Animate Filters:
xml<animate attributeName="stdDeviation" values="0;5;0" dur="2s" repeatCount="indefinite" />
Combine Filters with Blend Modes:
xml<filter id="filter1">
<feGaussianBlur in="SourceGraphic" stdDeviation="5" />
</filter>
<filter id="filter2">
<feColorMatrix in="SourceGraphic" type="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 5 -2" />
</filter>
<rect width="100" height="100" style="fill: blue; mix-blend-mode: multiply;" filter="url(#filter1) url(#filter2)" />
Remember that browser support for certain filters and effects may vary, so it's a good idea to check compatibility for the specific filters and features you plan to use.