1.16. SVG Shadow

Release time :2023-12-07 23:00:02 UTC      

Note: Internet Explorer and Safari do not support SVG filters!

1.16.1. <defs> and <filter>

All SVG filters for the Internet are defined in <defs> element. <defs> element definitions are short and contain special element definitions, such as filters.

<filter> tags are used to define SVG filters. <filter> the tag usesthe required id attribute to define which filter to apply to the drawing?

1.16.2. SVG <feOffset>

Example 1

<feOffset> elements are used to create shadow effects. Our idea is to take a SVG graphic (image or element) and move it a little bit on the xy plane.

The first example is offset by a rectangle (with <feOffset> ), and then blend and offset the top of the image (including <feBlend> ):

Imgage0

Here is the SVG code:

Example

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <defs>
    <filter id="f1" x="0" y="0" width="200%" height="200%">
      <feOffset result="offOut" in="SourceGraphic" dx="20" dy="20" />
      <feBlend in="SourceGraphic" in2="offOut" mode="normal" />
    </filter>
  </defs>
  <rect width="90" height="90" stroke="green" stroke-width="3"
  fill="yellow" filter="url(#f1)" />
</svg>

Code parsing:

  • <filter> the element id attribute defines a unique name for a filter

  • <rect> the filter attribute of the element is used to link the element to the “F1” filter

Example 2

Now, the offset image can be blurred (including <feGaussianBlur> ):

Imgage1

Here is the SVG code:

Example

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <defs>
    <filter id="f1" x="0" y="0" width="200%" height="200%">
      <feOffset result="offOut" in="SourceGraphic" dx="20" dy="20" />
      <feGaussianBlur result="blurOut" in="offOut" stdDeviation="10" />
      <feBlend in="SourceGraphic" in2="blurOut" mode="normal" />
    </filter>
  </defs>
  <rect width="90" height="90" stroke="green" stroke-width="3"
  fill="yellow" filter="url(#f1)" />
</svg>

Code parsing:

  • The stdDeviation attribute of the < feGaussianBlur > element defines the fuzzy amount

Example 3

Now, make a black shadow:

Imgage2

Here is the SVG code:

Example

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <defs>
    <filter id="f1" x="0" y="0" width="200%" height="200%">
      <feOffset result="offOut" in="SourceAlpha" dx="20" dy="20" />
      <feGaussianBlur result="blurOut" in="offOut" stdDeviation="10" />
      <feBlend in="SourceGraphic" in2="blurOut" mode="normal" />
    </filter>
  </defs>
  <rect width="90" height="90" stroke="green" stroke-width="3"
  fill="yellow" filter="url(#f1)" />
</svg>

Code parsing:

  • The attribute of the < feOffset > element is changed to “SourceAlpha” to use the residual shadow in the Alpha channel instead of the entire RGBA pixel.

Example 4

Now put a layer of color on the shadow:

Imgage3

Here is the SVG code:

Example

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <defs>
    <filter id="f1" x="0" y="0" width="200%" height="200%">
      <feOffset result="offOut" in="SourceGraphic" dx="20" dy="20" />
      <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" />
      <feGaussianBlur result="blurOut" in="matrixOut" stdDeviation="10"
/>
      <feBlend in="SourceGraphic" in2="blurOut" mode="normal" />
    </filter>
  </defs>
  <rect width="90" height="90" stroke="green" stroke-width="3"
  fill="yellow" filter="url(#f1)" />
</svg>

Code parsing:

  • <feColorMatrix> the filter is used to convert the offset image closer tothe black color. All three values of the ‘0.2’ matrix are multiplied by red, green and blue channels. Reduce the color brought by its value to black(black is 0)

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.