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:
<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:
<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:
Author
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
If you have any doubts regarding this article (SVG Filters), please comment here. I will help you immediately.