Open In App

SVG <feDistantLight> Element

SVG stands for Scalable Vector Graphics. It basically defines vector-based graphics in XML format. SVG graphics do NOT lose any quality if they are zoomed or resized. Every element and every attribute in SVG files can be animated.

The SVG <feDiffuseLighting> filter primitive lights an image using the alpha channel as a bump map. Using diffuse lighting the sides of the object facing the light are brighter and the sides facing away are darker and in shadow.



The two channels of <feDiffuseLighting> are as follows.

Attribute Used:



azimuth attribute: It is used to specify the direction angle for the light source on the XY plane (clockwise), in degrees from the x-axis.

elevation: It is used to specify the direction angle for the light source from the XY plane towards the z-axis, in degrees.

Example 1: In the below code, we will make use of the <feDistantLight> element.




<!DOCTYPE html>
<html>
  
<body>
    <center>
        <h1 style="color:green;">GeeksforGeeks</h1>
        <h3>A computer science portal for geeks</h3>
    </center>
    <svg viewBox="0 0 1000 1000">
        <filter id="lightMe2">
            <feDiffuseLighting in="SourceGraphic"
                  surfaceScale="1" diffuseConstant="2"
                  kernelUnitLength="2">
  
                <fePointLight x="90" y="60" z="60" />
  
                <feDistantLight azimuth="140" elevation="220" />
            </feDiffuseLighting>
  
            <feComposite in="SourceGraphic" in2="light"
                 operator="arithmetic" k1="1" k2="0"
                 k3="0" k4="0" />
        </filter>
  
        <rect x="20" y="20" width="200" height="200"
              fill="green" style="filter: url(#lightMe2);" />
    </svg>      
</body>
</html>

Output:

 

Example 2: In the below code, we will make use of the <feDistantLight> element.




<!DOCTYPE html>
<html>
  
<body>
    <center>
    <h1 style="color:green;">GeeksforGeeks</h1>
    <h3>A computer science portal for geeks</h3>
    </center>
    <svg viewBox="0 0 1000 1000">
        <filter id="lightMe3">
            <feDiffuseLighting in="BackgroundImage"
                surfaceScale="4" diffuseConstant="2"
                kernelUnitLength="2">
  
                <feSpotLight x="50" y="60" z="70"
                    limitingConeAngle="40"
                    pointsAtX="200" pointsAtY="200"
                    pointsAtZ="0" />
  
                <fePointLight x="80" y="80" z="40" />
            </feDiffuseLighting>
  
            <feComposite in="SourceGraphic"
                in2="light" operator="arithmetic"
                k1="14" k2="0" k3="1" k4="0" />
        </filter>
  
        <rect x="20" y="20" width="200" height="200"
              fill="blue" style="filter: url(#lightMe3);" />
    </svg>
</body>
</html>

Output:

 

Reference: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/feDistantLight


Article Tags :