# Shadows for svg buttons

**URL:** <https://forum.juce.com/t/shadows-for-svg-buttons/49152>\
**Category:** General JUCE discussion\
**Created:** [December 9, 2021, 10:09am UTC](https://forum.juce.com/t/shadows-for-svg-buttons/49152 "2021-12-09T10:09:49Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [December 9, 2021, 10:09am UTC](https://forum.juce.com/t/shadows-for-svg-buttons/49152/1 "2021-12-09T10:09:49Z")

</div>

Hi, as juce doesn’t seem to render svg shadows (!) what’s the best practice for handling this?

Do people just use bitmaps or add drop shadows in the paint() method after all controls have been placed on the screen? Is there a better way to handle this?

Thanks

---

<div class="post-metadata">

**Author:** ![ImJimmi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/imjimmi/32/10843_2.png) [@ImJimmi](https://forum.juce.com/u/ImJimmi)\
**Post date:** [December 9, 2021, 10:27am UTC](https://forum.juce.com/t/shadows-for-svg-buttons/49152/2 "2021-12-09T10:27:02Z")

</div>

Shadows are a real pain-point for me, I’ve tried numerous times to find a neat solution but nothing ever sticks.

Currently, we have a `ShadowComponent` that can be attached to another component. They can be inner or outer shadows so they either place themselves on top or behind the component they’re attached to. For outer shadows, they need to be made a little bigger to show the shadow outside the bounds of the component being shadowed - this leads to all sorts of edge cases, like when the component being shadowed is against the edge of its parent’s bounds, so then the shadow gets clipped (unless you put the shadow even higher in the parent hierarchy but then it’ll likely get drawn behind the wrong things).

As for drawing them - the shadow component just uses `juce::DropShadowEffect` in its `paint()` method - since the things with shadows don’t change their size, the shadow only ever gets drawn once (as long as you call `setBufferedToImage(true)` on them). If they could be resized, then yeah drawing to an image and just scaling that image to fit the correct size is possibly the way to go - although then your blur radius won’t be consistent.

You can optimise the image method using [9-slice scaling](https://en.wikipedia.org/wiki/9-slice_scaling). If the component being shadowed doesn’t have any transparency - you can just use 8 images for the corners and edges, which saves you drawing a large chunk of pixels.

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [December 9, 2021, 10:29am UTC](https://forum.juce.com/t/shadows-for-svg-buttons/49152/3 "2021-12-09T10:29:14Z")

</div>

thanks - i’ve just played around with the shadowcomponent and it seems very “square” and unnatural. sounds like there’s going to be a lot of tweaking ahead from your description also.

---

<div class="post-metadata">

**Author:** ![ImJimmi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/imjimmi/32/10843_2.png) [@ImJimmi](https://forum.juce.com/u/ImJimmi)\
**Post date:** [December 9, 2021, 10:34am UTC](https://forum.juce.com/t/shadows-for-svg-buttons/49152/4 "2021-12-09T10:34:34Z")

</div>

We give the shadow component a path (to represent the outline of the component being drawn, as they usually have rounded corners), and a `juce::DropShadow`. The size of the shadow component is the size of the thing being shadowed, expanded by the blur radius, then offset the position of the shadow component with the offset from the drop shadow. When you draw the shadow to the shadow component, use an offset of (0, 0) as you’ve already account for that.

It can be a bit fiddly to get all the scaling, relative positions, etc. done right.

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [December 9, 2021, 10:48am UTC](https://forum.juce.com/t/shadows-for-svg-buttons/49152/5 "2021-12-09T10:48:37Z")

</div>

ah, interesting - i’ll give that a go - thanks for the tips

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [December 9, 2021, 11:34am UTC](https://forum.juce.com/t/shadows-for-svg-buttons/49152/6 "2021-12-09T11:34:17Z")

</div>

How do you convert the SVG to a JUCE path? thx

---

<div class="post-metadata">

**Author:** ![connorreviere](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/connorreviere/32/4890_2.png) [@connorreviere](https://forum.juce.com/u/connorreviere)\
**Post date:** [December 9, 2021, 5:41pm UTC](https://forum.juce.com/t/shadows-for-svg-buttons/49152/7 "2021-12-09T17:41:49Z")

</div>

[`Drawable::parseSVGPath()`](https://docs.juce.com/master/classDrawable.html#aebfffe1d689c9ab863c5c80526703d3f) should do the trick.

---

<div class="post-metadata">

**Author:** ![ImJimmi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/imjimmi/32/10843_2.png) [@ImJimmi](https://forum.juce.com/u/ImJimmi)\
**Post date:** [December 9, 2021, 6:07pm UTC](https://forum.juce.com/t/shadows-for-svg-buttons/49152/8 "2021-12-09T18:07:56Z")

</div>

IIRC that expects a `<path>` element from within an SVG, not the entire SVG document itself. I may be wrong but I recall coming across something along those lines before.

---

<div class="post-metadata">

**Author:** ![connorreviere](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/connorreviere/32/4890_2.png) [@connorreviere](https://forum.juce.com/u/connorreviere)\
**Post date:** [December 9, 2021, 9:17pm UTC](https://forum.juce.com/t/shadows-for-svg-buttons/49152/9 "2021-12-09T21:17:43Z")

</div>

Looks like you’re right, my mistake!

I thought I had used that in the past, but it looks like I ended up creating a `Drawable` object from the SVG data and calling `getOutlineAsPath()` to get its `Path`. Might give that a try if you haven’t already found a solution @leehu

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [December 11, 2021, 10:02am UTC](https://forum.juce.com/t/shadows-for-svg-buttons/49152/11 "2021-12-11T10:02:06Z")

</div>

thanks for all the info. i’ve got some basic shadows up and running now - doesn’t quite look as good as the shadows in the svg, but I guess it’ll do.

Any pointers on drawing internal shadows to give the highlight effect here:

![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/9/9a1a80d2bcf27ffbb1afa34a5b4a5c0864128996.png)

---

<div class="post-metadata">

**Author:** ![ttg](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ttg/32/14456_2.png) [@ttg](https://forum.juce.com/u/ttg)\
**Post date:** [December 12, 2021, 8:54am UTC](https://forum.juce.com/t/shadows-for-svg-buttons/49152/12 "2021-12-12T08:54:38Z")

</div>

With some exports we got from Adobe, the shadow was png embedded and didn’t properly draw.  
extracting the embedded png worked ok here.

Also, drawing a shadow using juce effect might have a performance hit. so it’s worth pre-rendering it.

---

<div class="post-metadata">

**Author:** ![yairadix](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yairadix/32/1114_2.png) [@yairadix](https://forum.juce.com/u/yairadix)\
**Post date:** [December 12, 2021, 11:05am UTC](https://forum.juce.com/t/shadows-for-svg-buttons/49152/13 "2021-12-12T11:05:38Z")

</div>

> [@leehu](#):
>
> Hi, as juce doesn’t seem to render svg shadows (!) what’s the best practice for handling this?

Afaik shadows aren’t a builtin feature of SVG, and may be implemented in several different ways, some of which JUCE might render correctly. So these sort of reports would probably work best with an example `.svg` file.
