SVG Shadow
is an element in SVG filters used to create an offset effect on an image, which can be used to create shadows, outlines, and other visual effects. Its function is to move each pixel of the input image a certain distance along the specified horizontal and vertical directions, and then output the resulting image as the filter effect.
Basic Syntax
-
dxThe attribute defines the offset of the shadow in the horizontal direction. -
dyThe attribute defines the offset of the shadow in the vertical direction.
The following code defines an offset filter and then applies it to a red-filled rectangle, giving the rectangle a shadow with an offset effect; the shadow is offset by 5 pixels in both the horizontal and vertical directions.
Example
SVG shadow effects can be achieved through the element, which can add shadow effects to SVG graphic elements, making them look more three-dimensional and realistic.
Basic Syntax
-
dxThe attribute defines the offset of the shadow in the horizontal direction. -
dyThe attribute defines the offset of the shadow in the vertical direction. -
stdDeviationThe attribute defines the blur radius of the shadow, usually used to control the degree of blur of the shadow. -
flood-colorThe attribute defines the color of the shadow, which defaults to black. -
flood-opacityThe attribute defines the opacity of the shadow, which defaults to 1 (fully opaque).
The following code defines a shadow filter and then applies it to a red-filled rectangle, giving the rectangle a gray shadow effect; the shadow is offset by 5 pixels in both the horizontal and vertical directions, the blur radius is 5 pixels, the shadow color is gray, and the opacity is 0.5.
Example
Example 1
The first example offsets a rectangle (with
Below is the SVG code:
Example
Try it »
Click to view: View SVG File.
Code analysis:
The element's id attribute defines a unique name for a filter The element's filter attribute is used to link the element to the "f1" filter
Example 2
Now, the offset image can be blurred (with
Below is the SVG code:
Example
Try it »
Click to view: View SVG File.
Code analysis:
The element's stdDeviation attribute defines the amount of blur
Example 3
Now, create a black shadow:
Below is the SVG code:
Example
Try it »
Click to view: View SVG File.
Code analysis:
The element's attribute is changed to "SourceAlpha" to use the residual image in the Alpha channel, rather than the entire RGBA pixel.
Example 4
Now, paint a color onto the shadow:
Below is the SVG code:
Example
Try it »
Click to view: View SVG File.
Code analysis:
The filter is used to convert the offset image so that its color is closer to black. The three values of the '0.2' matrix are multiplied by the red, green, and blue channels. Lowering its value brings the color to black (black is 0)
Notes
-
The element can be combined with other filter effects, such as blur, color matrices, etc. -
The offset amount of the offset effect
dxanddycan be a positive value (indicating an offset to the right or downward) or a negative value (indicating an offset to the left or upward). -
The element is typically used to create shadow effects; combined with other filter effects, it can achieve more complex visual effects.
By using element, you can create offset effects for SVG graphic elements, giving them a more vivid and three-dimensional appearance.