14 July 2009

Inkscape Drop Shadow Using Filters

I like Inkscape. It's a great free program for vector graphics, but it can be a bit confusing at times to do certain things. Like the ubiquitous drop shadow. It ought to be as easy as clicking a button to apply a drop shadow to an image, but alas things are rarely that simple unless you are using a mac, but thats a post for another day.

So how do we do drop shadows in inkscape?

The easy way is to use a duplicate object. So start by creating an object, I just created some text and coloured it red:



Then duplicate that object, Edit -> Duplicate, or Ctrl-D:



Using the fill and stroke options, change the colour to a nice mid tone grey with a bit of transparency:



Use 'pagedown' or the 'lower' icon, to drop the grey text below the red text, and use the magical gaussian blur slider in the fill and stroke options to give us a nice hazy shadow:



Using a little repositioning we get a nice shadow:



But what happens if we need to change the text or if there is a typo? Its a bit of a pain to go through the whole process again, just to reproduce the same effect.



There is a better way using filters

Start with your text object again, then bring up the filters dialog (Object ->Filter Effects), its a bit weird and confusing at first, so we'll go through it step by step. On the left is a list of filters, in the middle are the effects that make up that filter, and on the right are the possible 'input sources' for the effects. At the bottom are the parameters for a particular effect.



Lets create a new filter, click the new button, a new filter will be created, right click and rename it to something more memorable, e.g 'drop shadow'.



Now select the gaussian blur filter from the effects selection list, and click add effect. That was easy, we created our first filter, but we can achieve the same thing using the blur slider in the fill and stroke options panel, but we can do so much more with filters, because we can combine them to produce better and more complicated effects.



There is a line connecting the gaussian blur effect to the 'source graphic' input. In this tutorial we are only concerned with the source graphic and source alpha inputs. With only one effect the source of the blur cannot be changed.

If the text object is selected, then checking the box next to the drop shadow filter will enable or disable the gaussian blur filter on the text.

Ok, we have blurred text, but thats not what we want, we want a shadow, so from the effects list select the 'merge effect', and click add:



Now we have a blur effect and a merge effect, but the text hasn't changed. This is because we need to 'wire up' the inputs of each effect. Attached to each effect is a little triangle, pointing at the text, if you hover the mouse over this triangle you will see it changes colour, indicating it is selectable. If you click and drag on this triangle, a line is drawn to the mouse cursor. This allows you to link the effect with either a different 'input' or another effect. If you click and drag the triangle on the blur effect and 'drop' the line on the 'source alpha' input column, the text will turn fuzzy black, indicating that the gaussian blur is affecting the alpha channel of the text instead of the text itself.



Now we need to wire up the merge effect. Drag the arrow from the merge effect and drop it on the gaussian blur. A little angled line should link the two effects, and a new triangle should appear on the merge effect, drag this second triangle to the 'Source Graphic' input column on the right.



We should now have some red text with a hazy black shadow! This is much better. We can edit the text and the shadow adjusts with the text as it changes. The shadow can be turned on an off with the checkbox next to the filter name. This same filter can be applied to other objects on the page. So if we have any other text or objects on the page, select them and check the drop shadow filter, and hey presto they have shadows too.

But the drop shadow is still not quite right, it needs to be offset from the text, and I want it to be transparent grey not black. To achieve this we need add more effects into the filter. Moving the shadow is easy.

Select the 'Offset' effect, and click 'add effect'. Change the Delta X and Delta Y parameters to 5 pixels or however much you want to offset the shadow by. The offset effect is added at the bottom of the filter, and takes the merge effect as an input, so the result of the merge, ie. the whole graphic, is offset. This is not what we want, we only want to offset the blur.



First, drag the offset effect from the bottom and drop it just underneath the blur, the order of the effects should now be changed, but the connections are messed up.



Drag the connection that goes from the merge effect to the blur effect, and drop it on the offset effect instead, et voila, just the blur is shifted, giving us a nice offset shadow effect.



The shadow is still the wrong colour though. At this stage it seems logical to add the 'Color Matrix' filter to change the colour of the shadow, unfortunately there a 2 problems with this, the first one is that the color matrix filter doesn't have a colorize with alpha option, and second, I cant understand how to use the matrix option as it's too complicated, and I'm not a maths guru. So instead we can use 2 other filters to make the process easier.

Add the 'Flood Filter' and set its colour and alpha values as required, the input will default to the 'Source Graphic', leave this alone. Next add a 'Composite' filter and set its operator to 'In', select the Flood filter as its first input, then the 'Source Graphic' as its second input, then feed this into the blur then offset effects, then finally merge with the source graphic.



Finally we have a drop shadow that can be enabled by the click of a button, and it doesn't matter if we need to change the text or modify the object shape.

If we have an object on the page which requires a different kind of shadow, the easiest thing is to right click on the filter name to duplicate it, rename it eg 'drop shadow 2', modify the settings as appropriate, then apply the second filter to the new object.

The only drawback with these filters is that it isn't possible to save the filter, which is a shame, as there could be a list of predefined filters which could be reused.

Update: Actually you can save the filter as a custom one. Check out Elaine's comment below.