Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

SVG Filters

Posted in SVG Tutorial
Updated on Oct 13, 2024
By Mari Selvan
👁️ 369 - Views
⏳ 4 mins
💬 1 Comment
SVG Filters

Photo Credit to CodeToFun

🙋 Introduction

SVG Filters are powerful tools that allow developers to apply various visual effects to SVG elements, such as blurring, color shifting, and shadowing. These filters enhance the appearance and interactivity of graphics, making them more engaging for users.

This guide explores the syntax, attributes, and examples of using SVG Filters.

💡 Syntax

SVG Filters are defined within the <filter> element and are referenced by SVG elements through the filter attribute. The basic structure for defining an SVG filter is as follows:

Syntax
Copied
Copy To Clipboard
<svg>
  <defs>
    <filter id="filter-id">
      <!-- Filter effects go here -->
    </filter>
  </defs>
  <rect x="x-coordinate" y="y-coordinate" width="width" height="height" filter="url(#filter-id)" />
</svg>

🧰 Attributes

  • id: Unique identifier for the filter.
  • filterUnits: Defines the coordinate system for the attributes x, y, width, and height (e.g., userSpaceOnUse, objectBoundingBox).
  • primitiveUnits: Defines the coordinate system for the filter primitive subregion (default is userSpaceOnUse).
  • x, y, width, height: Define the position and size of the filter effect region.

🌀 Common Filter Effects

  • <feGaussianBlur>: Applies a Gaussian blur effect.
    • stdDeviation: Specifies the standard deviation for the blur.
  • <feColorMatrix>: Applies a matrix transformation to the colors.
    • type: Type of matrix transformation (e.g., matrix, saturate, hueRotate).
    • values: Specifies the values for the matrix transformation.
  • <feOffset>: Offsets the input image.
    • dx, dy: Specify the offset distances in the x and y directions.
  • <feMerge>: Merges several filter effects together.

📄 Example

Here’s an example of applying a blur filter to a rectangle:

index.html
Copied
Copy To Clipboard
<svg width="200" height="200">
  <defs>
    <filter id="blur-filter">
      <feGaussianBlur stdDeviation="5" />
    </filter>
  </defs>
  <rect x="50" y="50" width="100" height="80" fill="blue" filter="url(#blur-filter)" />
</svg>

🧠 How it works?

In this example, a Gaussian blur filter is defined with a standard deviation of 5. The filter is then applied to a blue rectangle.

🎉 Conclusion

SVG Filters are an essential feature for creating sophisticated and visually appealing graphics on the web. By mastering the use of filters, developers can enhance the visual dynamics of their SVG elements, making them more attractive and engaging for users.

Experiment with different filter effects to discover the vast possibilities SVG Filters offer.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy