Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SVG Shadow

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



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



  • dx The attribute defines the offset of the shadow in the horizontal direction.

  • dy The attribute defines the offset of the shadow in the vertical direction.

  • stdDeviation The attribute defines the blur radius of the shadow, usually used to control the degree of blur of the shadow.

  • flood-color The attribute defines the color of the shadow, which defaults to black.

  • flood-opacity The 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 element is used to create shadow effects. The idea is to take an SVG graphic (image or element) and move it a little on the xy plane.

The first example offsets a rectangle (with ), then blends the top of the offset image (with ):

Below is the SVG code:

Example

 

   

     

     

   

 

 

  fill="yellow" filter="url(#f1)" />

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

 

   

     

     

     

   

 

 

  fill="yellow" filter="url(#f1)" />

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

 

   

     

     

     

   

 

 

  fill="yellow" filter="url(#f1)" />

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

 

   

     

     

      values="0.2 0 0 0 0 0 0.2 0 0 0 0 0 0.2 0 0 0 0 0 1 0" />

     

     

   

 

 

  fill="yellow" filter="url(#f1)" />

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 dx and dy can 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.