Introduction
Before reading further, read the following articles.
Now let's proceed to understand the Drop shadow SVG filter.
SVG Drop Shadow
The tag <feOffset> creates drop shadow effects. The offset filter takes an input or SVG graphics and moves the input a little bit in the xy plane and the new position of the input is the output. Actually it can move the input up, down, left and right. It works like the translation transformation except it is done in a filter.
The <feOffset> contains the attributes dx and dy and its default value is (0,0) if its not specified in <feOffset>. The attribute “dx” is the amount to offset the input graphics along the x-axis and “dy” along the y-axis. It can be specified as only “dx” or “dy” or both or none.
Let's see an example to see its effect.
Example 1 - <html>
- <body>
- <svg height="300" width="500">
- <defs>
- <filter id="offsetFilter1" x="-20" y="-20" width="70" height="70">
- <feOffset in="SourceGraphic" />
- </filter>
- <filter id="offsetFilter2" x="-20" y="-20" width="70" height="70">
- <feOffset in="SourceGraphic" dx="230" />
- </filter>
- <filter id="offsetFilter3" x="-20" y="-20" width="70" height="70">
- <feOffset in="SourceGraphic" dx="270" dy="120"/>
- </filter>
- </defs>
- <rect x="20" y="20" width="100" height="100"
- style="stroke: blue; fill: none; " />
- <rect x="20" y="20" width="100" height="100"
- style="stroke: red; fill: none; filter: url(#offsetFilter1);" />
- <rect x="200" y="20" width="100" height="100"
- style="stroke: blue; fill: cyan; " />
- <rect x="200" y="20" width="100" height="100"
- style="stroke: red; fill: yellow; filter: url(#offsetFilter2);" />
- <rect x="350" y="20" width="100" height="100"
- style="stroke: blue; fill: none; stroke-dasharray: 10 3;" />
- <rect x="350" y="20" width="100" height="100"
- style="stroke: red; fill: none; stroke-dasharray: 10 5; filter: url(#offsetFilter3);" />
- </svg>
- </body>
- </html>
Output
In the preceding example, the positions of two rectangles in each set are the same. One of the rectangles has an offset applied that moves at various positions depending on the defined offset as only “dx”, “both dx and dy” and ”none”.
Example 2
Multiple rectangles in the same position.
- <html>
- <body>
- <svg height="300" width="500">
- <defs>
- <filter id="offsetFilter1" x="-20" y="-20" width="70" height="70">
- <feOffset in="SourceGraphic" />
- </filter>
- <filter id="offsetFilter2" x="-20" y="-20" width="70" height="70">
- <feOffset in="SourceGraphic" dx="180" />
- </filter>
- <filter id="offsetFilter3" x="-20" y="-20" width="70" height="70">
- <feOffset in="SourceGraphic" dy="130"/>
- </filter>
- <filter id="offsetFilter4" x="-20" y="-20" width="70" height="70">
- <feOffset in="SourceGraphic" dx="150" dy="110"/>
- </filter>
- <filter id="offsetFilter5" x="-20" y="-20" width="70" height="70">
- <feOffset in="SourceGraphic" dx="250" dy="250"/>
- </filter>
- </defs>
- <rect x="50" y="50" width="100" height="100"
- style="stroke: black; fill: none; " />
- <rect x="50" y="50" width="100" height="100"
- style="stroke: red; stroke-width: 3; fill: none; filter: url(#offsetFilter1);" />
- <rect x="50" y="50" width="200" height="150"
- style="stroke: blue; fill: orange; stroke-width: 4; filter: url(#offsetFilter2);" />
- <rect x="50" y="50" width="150" height="150"
- style="stroke: brown; stroke-width: 4; fill: none; stroke-dasharray: 10 7; filter: url(#offsetFilter3);" />
- <rect x="50" y="50" width="150" height="200"
- style="stroke: black; fill: lime; stroke-width: 3; stroke-dasharray: 10 7; filter: url(#offsetFilter4);" />
- <rect x="50" y="50" width="250" height="150"
- style="fill: cyan; filter: url(#offsetFilter5);" />
- </svg>
- </body>
- </html>
Output
Example 3
Offsetting the Alpha channel.
- <html>
- <body>
- <svg height="300" width="500">
- <defs>
- <filter id="offsetFilter1" x="-20" y="-20" width="70" height="70">
- <feOffset in="SourceAlpha" />
- </filter>
- <filter id="offsetFilter2" x="-20" y="-20" width="70" height="70">
- <feOffset in="SourceAlpha" dx="250" dy="120"/>
- </filter>
- </defs>
- <rect x="50" y="50" width="100" height="100"
- style="stroke: orange; fill: none; " />
- <rect x="50" y="50" width="100" height="100"
- style="stroke: blue; stroke-width: 3; fill: none; filter: url(#offsetFilter1);" />
- <rect x="200" y="50" width="100" height="100"
- style="stroke: green; fill: lightgreen; " />
- <rect x="200" y="50" width="100" height="100"
- style="stroke: blue; stroke-width: 3; fill: lightblue; filter: url(#offsetFilter2);" />
- </svg>
- </body>
- </html>
Output
We can see that the rectangles are filled by colors, but the output is Black and White due to the filter on the applied Alpha channel.
Summary
In this article, we learned about Scalable Vector Graphics - Filters 3.
Thank you, keep learning and sharing.