- Introduction To HTML
- HTML Elements And Tags
- Text Formatting And Styling
- Images And Multimedia
- Hyperlinks And Anchors
- Tables And Forms
- HTML5 Semantic Elements
- Responsive Design And Meta Tags
- Embedded Content And APIs
- Canvas
- Drawing Basic Shapes
- Working With Text And Fonts
- Working With Images
- Canvas Transformations
- Working With Animation
- Interactivity And Event Handling
- Canvas Advanced
- Introduction To SVG
- SVG Gradients And Patterns
- SVG Transformations And Transitions
- SVG Filters And Effects
- SVG Paths And Bezier Curves
- SVG Icons And Illustrations
- SVG Responsive Design And Accessibility
SVG Filters and Effects
Applying Filters Such As Blur, Brightness, Contrast, And Drop Shadow To SVG Elements
1. Blur Filter
The blur filter in SVG is used to create a blur effect, making elements appear out of focus or softened. It's commonly used for creating depth, highlighting focus areas, or adding a dreamy look to graphics.
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<defs>
<filter id="blurFilter" x="0" y="0">
<feGaussianBlur in="SourceGraphic" stdDeviation="5" />
</filter>
</defs>
<rect x="50" y="50" width="100" height="100" fill="blue" filter="url(#blurFilter)" />
</svg>
In this example, a blur filter with a stdDeviation of 5 is applied to a blue rectangle, creating a blurred effect around the edges of the rectangle.
2. Brightness Filter
The brightness filter adjusts the brightness of SVG elements, making them appear brighter or darker. It's useful for creating lighting effects or adjusting the overall tone of graphics.
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<defs>
<filter id="brightnessFilter" x="0" y="0">
<feComponentTransfer>
<feFuncR type="linear" slope="1.5" />
<feFuncG type="linear" slope="1.5" />
<feFuncB type="linear" slope="1.5" />
</feComponentTransfer>
</filter>
</defs>
<rect x="50" y="50" width="100" height="100" fill="blue" filter="url(#brightnessFilter)" />
</svg>
In this example, a brightness filter with a linear slope of 1.5 is applied to a blue rectangle, increasing its brightness.
3. Contrast Filter
The contrast filter adjusts the contrast of SVG elements, enhancing or reducing the difference between light and dark areas. It's effective for making graphics more vibrant or adjusting their tonal range.
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<defs>
<filter id="contrastFilter" x="0" y="0">
<feComponentTransfer>
<feFuncR type="linear" slope="2" intercept="-0.5" />
<feFuncG type="linear" slope="2" intercept="-0.5" />
<feFuncB type="linear" slope="2" intercept="-0.5" />
</feComponentTransfer>
</filter>
</defs>
<rect x="50" y="50" width="100" height="100" fill="blue" filter="url(#contrastFilter)" />
</svg>
In this example, a contrast filter with a linear slope of 2 and an intercept of -0.5 is applied to a blue rectangle, increasing its contrast.
4. Drop Shadow Filter
The drop shadow filter adds a shadow effect behind SVG elements, creating a sense of depth or highlighting their presence. It's commonly used for buttons, icons, and UI elements.
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<defs>
<filter id="dropShadowFilter" x="-20%" y="-20%" width="140%" height="140%">
<feGaussianBlur in="SourceAlpha" stdDeviation="3" />
<feOffset dx="2" dy="2" result="offsetBlur" />
<feFlood flood-color="black" flood-opacity="0.5" result="offsetColor" />
<feComposite in="offsetColor" in2="offsetBlur" operator="in" result="offsetResult" />
<feMerge>
<feMergeNode in="offsetResult" />
<feMergeNode in="SourceGraphic" />
</feMerge>
</filter>
</defs>
<rect x="50" y="50" width="100" height="100" fill="blue" filter="url(#dropShadowFilter)" />
</svg>
In this example, a drop shadow filter with a Gaussian blur, offset, and composite is applied to a blue rectangle, creating a subtle shadow effect around it.
Applying Multiple Filters
You can combine multiple filters to achieve complex visual effects. For example, combining blur and drop shadow filters can create a blurred shadow effect, adding depth and focus to elements.
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<defs>
<filter id="blurShadowFilter" x="-20%" y="-20%" width="140%" height="140%">
<feGaussianBlur in="SourceAlpha" stdDeviation="5" />
<feOffset dx="3" dy="3" result="offsetBlur" />
<feFlood flood-color="black" flood-opacity="0.5" result="offsetColor" />
<feComposite in="offsetColor" in2="offsetBlur" operator="in" result="offsetResult" />
<feMerge>
<feMergeNode in="offsetResult" />
<feMergeNode in="SourceGraphic" />
</feMerge>
</filter>
</defs>
<rect x="50" y="50" width="100" height="100" fill="blue" filter="url(#blurShadowFilter)" />
</svg>
In this example, a combination of blur and drop shadow filters is applied to a blue rectangle, creating a blurred shadow effect around it.
Practice Excercise Practice now
Combining Multiple Filters To Create Complex Visual Effects
Understanding SVG Filters
SVG filters are powerful tools that modify the appearance of SVG elements. They work by processing graphical elements through a series of operations, such as blurring, color manipulation, and shadow generation. Filters are defined using <filter> elements within an SVG file or embedded directly into HTML using CSS.
Basics of Combining Filters
Filter Elements:
<feGaussianBlur>
: Applies a blur effect.<feColorMatrix>
: Manipulates colors.<feOffset>:
Shifts elements by a specified amount.<feBlend>
: Blends two input images.
Combining Filters:
Filters can be combined by chaining them together within a <filter> element.
Each filter operation modifies the appearance of the element based on the previous operation.
Example 1: Blur and Brightness
Let's start with a basic example of combining a blur effect with brightness adjustment:
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<defs>
<filter id="blurBrightness">
<feGaussianBlur in="SourceGraphic" stdDeviation="5" />
<feComponentTransfer>
<feFuncR type="linear" slope="1.2" />
<feFuncG type="linear" slope="1.2" />
<feFuncB type="linear" slope="1.2" />
</feComponentTransfer>
</filter>
</defs>
<rect x="50" y="50" width="100" height="100" fill="blue" filter="url(#blurBrightness)" />
</svg>
In this example:
<feGaussianBlur>
creates a blur effect with a stdDeviation of 5.<feComponentTransfer>
adjusts the brightness with a linear slope of 1.2 for each color channel.
Example 2: Blur and Drop Shadow
Combining a blur effect with a drop shadow can create a visually appealing depth effect:
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<defs>
<filter id="blurShadow">
<feGaussianBlur in="SourceAlpha" stdDeviation="3" result="blur" />
<feOffset in="blur" dx="2" dy="2" result="offsetBlur" />
<feBlend in="SourceGraphic" in2="offsetBlur" mode="normal" />
</filter>
</defs>
<rect x="50" y="50" width="100" height="100" fill="blue" filter="url(#blurShadow)" />
</svg>
Here:
- <feGaussianBlur> creates a blur effect using the alpha channel.
- <feOffset> offsets the blurred image to create a shadow.
- <feBlend> blends the shadow with the source graphic.
Example 3: Blur, Brightness, and Contrast
Combining blur, brightness, and contrast can produce dynamic visual effects:
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<defs>
<filter id="blurBrightnessContrast">
<feGaussianBlur in="SourceGraphic" stdDeviation="3" />
<feComponentTransfer>
<feFuncR type="linear" slope="1.5" />
<feFuncG type="linear" slope="1.5" />
<feFuncB type="linear" slope="1.5" />
</feComponentTransfer>
<feComponentTransfer>
<feFuncR type="gamma" amplitude="1" exponent="0.8" offset="0" />
<feFuncG type="gamma" amplitude="1" exponent="0.8" offset="0" />
<feFuncB type="gamma" amplitude="1" exponent="0.8" offset="0" />
</feComponentTransfer>
</filter>
</defs>
<rect x="50" y="50" width="100" height="100" fill="blue" filter="url(#blurBrightnessContrast)" />
</svg>
- We apply a blur effect with
<feGaussianBlur>
and adjust brightness and contrast using<feComponentTransfer>
. <feFuncR>
,<feFuncG>
, and<feFuncB>
are used to control the red, green, and blue channels, respectively.
Example 4: Advanced Blend Modes
SVG supports blend modes like multiply, screen, overlay, etc., which can be combined with other filters for creative effects:
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<defs>
<filter id="blendMode">
<feGaussianBlur in="SourceGraphic" stdDeviation="3" result="blur" />
<feBlend in="SourceGraphic" in2="blur" mode="screen" />
</filter>
</defs>
<rect x="50" y="50" width="100" height="100" fill="blue" filter="url(#blendMode)" />
</svg>
Here, we use <feBlend>
with mode="screen" to blend the source graphic with the blurred version using a screen blend mode.
Practice Excercise Practice now
Using SVG Filters For Image Manipulation And Enhancement
Understanding SVG Filters
SVG filters operate by taking input from an SVG graphic, applying various operations to it, and producing modified output. Filters can be applied to entire SVG elements or specific parts of an SVG image.
Filters are defined using the <filter>
element within an SVG file or in an external file that can be referenced using CSS. Each filter consists of one or more filter primitives, such as <feGaussianBlur>, <feColorMatrix>, <feOffset>,
etc. These primitives define the operations that the filter performs.
Common SVG Filter Primitives
- feGaussianBlur: Applies a Gaussian blur to the input.
- feColorMatrix: Allows color transformations like grayscale, sepia, hue rotation, etc.
- feOffset: Shifts the input image by a specified amount.
- feBlend: Blends two input images using various blend modes.
- feDropShadow: Adds a drop shadow to the input image.
- feComposite: Combines two images using a specified operator.
- feConvolveMatrix: Applies a matrix convolution to the input.
- These primitives can be combined and configured to achieve a wide range of visual effects.
Example 1: Applying a Gaussian Blur
Let's start with a simple example of applying a Gaussian blur to an image using SVG filters.
<svg width="400" height="300" xmlns="http://www.w3.org/2000/svg">
<defs>
<filter id="blurFilter" x="0" y="0" width="100%" height="100%">
<feGaussianBlur in="SourceGraphic" stdDeviation="5" />
</filter>
</defs>
<image x="0" y="0" width="400" height="300" xlink:href="image.jpg" filter="url(#blurFilter)" />
</svg>
In this example:
- We define a filter named blurFilter that applies a Gaussian blur with a standard deviation of 5 to the input image (in="SourceGraphic").
- The
<image>
element displays the image image.jpg and applies the blurFilter to it using the filter attribute.
Example 2: Creating a Sepia Effect
Now, let's create a sepia effect using the <feColorMatrix>
primitive.
<svg width="400" height="300" xmlns="http://www.w3.org/2000/svg">
<defs>
<filter id="sepiaFilter" x="0" y="0" width="100%" height="100%">
<feColorMatrix type="matrix"
values="0.393 0.769 0.189 0 0
0.349 0.686 0.168 0 0
0.272 0.534 0.131 0 0
0 0 0 1 0" />
</filter>
</defs>
<image x="0" y="0" width="400" height="300" xlink:href="image.jpg" filter="url(#sepiaFilter)" />
</svg>
Here, we define a filter named sepiaFilter that uses a <feColorMatrix>
to apply a sepia color transformation to the input image.
Example 3: Adding a Drop Shadow
Let's add a drop shadow effect to an image using the
<feDropShadow>
primitive.<defs>
<filter id="dropShadowFilter" x="-20%" y="-20%" width="140%" height="140%">
<feOffset in="SourceAlpha" dx="5" dy="5" result="offsetBlur" />
<feGaussianBlur in="offsetBlur" stdDeviation="5" result="shadowBlur" />
<feBlend in="SourceGraphic" in2="shadowBlur" mode="normal" />
</filter>
</defs>
<image x="0" y="0" width="400" height="300" xlink:href="image.jpg" filter="url(#dropShadowFilter)" />
</svg>
In this example, we define a filter named dropShadowFilter that first applies an offset to create a shadow, then blurs it using Gaussian blur, and finally blends it with the source graphic to create a drop shadow effect.
Example 4: Combining Filters
Filters can also be combined to achieve complex effects. Let's combine blur and color matrix filters to create a dreamy effect.
<svg width="400" height="300" xmlns="http://www.w3.org/2000/svg">
<defs>
<filter id="dreamyEffect" x="0" y="0" width="100%" height="100%">
<feGaussianBlur in="SourceGraphic" stdDeviation="10" result="blur" />
<feColorMatrix in="blur" type="matrix"
values="1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 0.5 0" />
</filter>
</defs>
<image x="0" y="0" width="400" height="300" xlink:href="image.jpg" filter="url(#dreamyEffect)" />
</svg>
In this example, we first apply a Gaussian blur with a standard deviation of 10 to the source graphic and then apply a color matrix to reduce the opacity (values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0.5 0"), creating a dreamy effect.
External SVG Filters with CSS
You can also define SVG filters in an external file and reference them using CSS.
<!-- filter.svg -->
<svg xmlns="http://www.w3.org/2000/svg">
<filter id="externalBlur" x="0" y="0" width="100%" height="100%">
<feGaussianBlur in="SourceGraphic" stdDeviation="5" />
</filter>
</svg>
css
Copy code
/* styles.css */
.image-container {
filter: url(filter.svg#externalBlur);
}
In this example, we define a filter in filter.svg and apply it to an image container using CSS.
Practice Excercise Practice now
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP