SVG Drop Shadows

SVG Drop Shadows (SVG阴影)

Description of SVG filters

Description of SVG filters (SVG过滤器的描述)

All SVG filters are defined inside a <defs> element. The <defs> element is a short form of definitions. It contains a definition of specific elements like filters. The <filter> element defines an SVG filter. This element has an id attribute (required) identifying the filter.

To create drop shadows, use the <feOffset> element. You will need to take an SVG graphic and move it in the xy plane.

Example of the SVG <feOffset> element:

<!DOCTYPE html>
<html>
 <head>
   <title>Title of the document</title>
 </head>
 <body>
   <svg width="150" height="150">
     <defs>
       <filter id="filter" x="0" y="0" width="150%" height="150%">
         <feOffset result="offOut" in="SourceGraphic" dx="30" dy="30" />
         <feBlend in="SourceGraphic" in2="offOut" mode="normal" />
       </filter>
     </defs>
     <rect width="110" height="110" stroke="purple" stroke-width="5" fill="pink" 
           filter="url(#filter)" /> 
     Sorry, your browser doesn't support inline SVG.
(抱歉,您的浏览器不支持内联SVG。)
   </svg>
 </body>
</html>

In the above-mentioned example, the <filter> element’s id attribute specifies a unique name for the filter, and the <rect> element’s filter attribute links the element to the “filter” filter.

Example of the SVG <feGaussianBlur> element:

<!DOCTYPE html>
<html>
 <head>
   <title>Title of the document</title>
 </head>
 <body>
   <svg width="200" height="200">
     <defs>
       <filter id="filter" x="0" y="0" width="250%" height="250%">
         <feOffset result="offOut" in="SourceGraphic" dx="30" dy="30" />
         <feGaussianBlur result="blurOut" in="offOut" stdDeviation="10" />
         <feBlend in="SourceGraphic" in2="blurOut" mode="normal" />
       </filter>
     </defs>
     <rect width="150" height="150" stroke="coral" stroke-width="5" fill="pink" 
           filter="url(#filter)" /> 
     Sorry, your browser doesn't support inline SVG.
(抱歉,您的浏览器不支持内联SVG。)
   </svg>
 </body>
</html>

Here, it is possible to blur the offset image with the <feGaussianBlur> element. This element’s stdDeviation attribute specifies the amount of blur.

In the following example, the <feOffset> element’s in attribute is changed to “SourceAlpha”. It uses the Alpha channel to apply blur instead of the whole RGBA pixel.

Example of coloring the shadow:

<!DOCTYPE html>
<html>
 <head>
   <title>Title of the document</title>
 </head>
 <body>
   <svg height="200" width="200">
     <defs>
       <filter id="filter" x="0" y="0" width="150%" height="150%">
         <feOffset result="offOut" in="SourceAlpha" dx="15" dy="15" />
         <feGaussianBlur result="blurOut" in="offOut" stdDeviation="8" />
         <feBlend in="SourceGraphic" in2="blurOut" mode="normal" />
       </filter>
     </defs>
     <rect width="120" height="120" stroke="purple" stroke-width="5" fill="pink" 
           filter="url(#filter)" /> 
      Sorry, your browser doesn't support inline SVG.
(抱歉,您的浏览器不支持内联SVG。)
   </svg>
 </body>
</html>

To transform the colors in the offset image, use the <feColorMatrix> element.

Example of the<feColorMatrix> element:

<!DOCTYPE html>
<html>
 <head>
   <title>Title of the document</title>
 </head>
 <body>
   <svg height="200" width="200">
     <defs>
       <filter id="filter" x="0" y="0" width="150%" height="150%">
         <feOffset result="offOut" in="SourceGraphic" dx="25" dy="25" />
         <feColorMatrix result="matrixOut" in="offOut" type="matrix" 
                        values="0.2 0 0 0 0 0 0.2 0 0 0 0 0 0.2 0 0 0 0 0 1 0" />
(values = "0.2 0 0 0 0 0 0.2 0 0 0 0 0.2 0 0 0 0 0 0 1 0"/>)
         <feGaussianBlur result="blurOut" in="matrixOut" stdDeviation="9" />
         <feBlend in="SourceGraphic" in2="blurOut" mode="normal" />
       </filter>
     </defs>
     <rect width="150" height="150" stroke="purple" stroke-width="5" fill="lightblue" 
           filter="url(#filter)" /> 
      Sorry, your browser doesn't support inline SVG.
(抱歉,您的浏览器不支持内联SVG。)
   </svg>
 </body>
</html>


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部