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:

  1. <filter> Element:

    • Define a filter using the <filter> element within the <defs> section of your SVG.
    • Specify filter primitives within the <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>
  2. Filter Primitives:

    • Use filter primitives like <feGaussianBlur>, <feColorMatrix>, <feBlend>, etc., to create specific visual effects.
    • Adjust the attributes of these primitives to control the intensity and behavior of the filter.
    xml
    <filter id="blurFilter"> <feGaussianBlur in="SourceGraphic" stdDeviation="5" /> </filter>
  3. Apply Multiple Filters:

    • You can apply multiple filters to an element by chaining them using the url() function in the filter attribute.
    xml
    <rect width="100" height="100" style="fill: blue;" filter="url(#filter1) url(#filter2)" />
  4. Use with CSS:

    • Apply filters using CSS by referencing the filter ID.
    css
    .myElement { filter: url(#myFilter); }
  5. Animate Filters:

    • You can animate filter parameters to create dynamic effects over time.
    xml
    <animate attributeName="stdDeviation" values="0;5;0" dur="2s" repeatCount="indefinite" />
  6. Combine Filters with Blend Modes:

    • Combine filters with blend modes to achieve complex visual effects.
    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.