# Best way to "animate images" in Juce

**URL:** <https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856>\
**Category:** Audio Plugins\
**Created:** [June 12, 2019, 10:11am UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856 "2019-06-12T10:11:57Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![fr4nz909](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr4nz909/32/5853_2.png) [@fr4nz909](https://forum.juce.com/u/fr4nz909)\
**Post date:** [June 12, 2019, 10:11am UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/1 "2019-06-12T10:11:58Z")

</div>

Hey Folks,

while doing a bit of plugin development, I decided to make the LookAndFeel of the plugin I am doing right now quite funny.

So the idea is to have kind of an Avatar, thats “animated”. Animated in the sense of:

if (thisSlider == 0)  
Image = Image1  
else  
Image = Image2

(really simplified)

Think of SAUSAGE FATTENER and the SAUSAGE that corelates with the “fatness” knob. 😃

The avatar does different poses - These are all single .png Images for now.

First, I feel like the Images lose Quality when beeing rescaled…

And second, would there not be a simpler way to animate the Images, instead of Setting the Image to a different Image at different conditions and then “repaint()” ?

Maybe someone has an idea for that.

Thanks for reading,

Franz

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [June 12, 2019, 10:37am UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/2 "2019-06-12T10:37:23Z")

</div>

> [@fr4nz909](#):
>
> First, I feel like the Images lose Quality when beeing rescaled…

Yes, that is unfortunately the case. Plus you have only discrete versions of it.

> [@fr4nz909](#):
>
> And second, would there not be a simpler way to animate the Images, instead of Setting the Image to a different Image at different conditions and then “repaint()” ?

That is already the simplest. The repaint is a discrete event, and only you can provide a state.

The best way to start is to create a procedural version of primitives, where you can use a parameter. Pull it together with Graphics::drawXX commands, that way you can have smooth transitions. Later you can start becoming fancy, adding gradients, shadows etc.

Maybe start with a smiley and have the mouth like:

```auto
float smile = 1.0; // -1.0 .. 1.0
Path p;
p.addEllipse ( 50, 50, 30, 40);
p.addEllipse (120, 50, 30, 40);
p.startNewSubPath (50, 100);
p.cubicTo (100, 100 + 20 * smile,
           100, 100 + 20 * smile,
           150, 100);
g.setColour (Colours::grey);
g.strokePath (p, PathStrokeType (4, curved, rounded), AffineTransform::translated (-4, 4));
g.setColour (Colours::green);
g.strokePath (p, PathStrokeType (4, curved, rounded));

```

---

<div class="post-metadata">

**Author:** ![sparklingRhubarb](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@sparklingRhubarb](https://forum.juce.com/u/sparklingRhubarb)\
**Post date:** [June 12, 2019, 10:52am UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/3 "2019-06-12T10:52:29Z")

</div>

You can use “Filmstrips” - that is: One single image consisting of multiple equal-sized images stitched together horizontally or vertically. Then you can easily select the correct image from the filmstrip, based on the value of a float parameter.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [June 12, 2019, 11:37am UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/4 "2019-06-12T11:37:27Z")

</div>

> [@sparklingRhubarb](#):
>
> Then you can easily select the correct image from the filmstrip, based on the value of a float parameter.

float parameter is the keyword here. This technique exists and works, but only for discrete values (opposite of floating point). It works like a flip-book, so you won’t get smooth transitions between values.

But if you are ok with a discrete number of states and you are prepared to provide an image for each state, that is also a good technology.

---

<div class="post-metadata">

**Author:** ![sparklingRhubarb](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@sparklingRhubarb](https://forum.juce.com/u/sparklingRhubarb)\
**Post date:** [June 12, 2019, 11:40am UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/5 "2019-06-12T11:40:00Z")

</div>

> [@daniel](#):
>
> But if you are ok with a discrete number of states and you are prepared to provide an image for each state

From the question, I figured this would be the case. Drawing dynamically is not always possible or practical. Both in terms of speed and image fidelity/style.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [June 12, 2019, 11:46am UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/6 "2019-06-12T11:46:25Z")

</div>

You are absolutely right, didn’t mean to be disrespectful. That is a valid approach and often used.

I just have my preference 😉

---

<div class="post-metadata">

**Author:** ![fr4nz909](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr4nz909/32/5853_2.png) [@fr4nz909](https://forum.juce.com/u/fr4nz909)\
**Post date:** [June 12, 2019, 12:51pm UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/7 "2019-06-12T12:51:32Z")

</div>

Thanks for the answers, you two. 🙂

Both are good approaches.

@daniel Is the loss of Quality Happening for all Images in Juce? Or only for the rescaled ones?

How do you guys think did the sausage fattener People do this, with rendered Images or by drawing the sausage in the Code itsself? 😃

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [June 12, 2019, 1:22pm UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/8 "2019-06-12T13:22:14Z")

</div>

> [@fr4nz909](#):
>
> @daniel Is the loss of Quality Happening for all Images in Juce? Or only for the rescaled ones?

That is not JUCE specific, but all bitmap graphics will have artefacts when they are rescaled. They happen less noticeable for power of two scale factors.  
You can draw without scaling, but then you are bound to a certain size of your editor. You cannot have resizable UI, which is something to aim for nowadays.

For me the killer feature for vector is, you can scale it on any dpi, vs. with bitmaps, when machines use higher resolutions in the future, you might have to re-do the bitmaps.

In fairness, vector graphics also result in an antialias effect, which is also a slight blur, but since the mathematical model is known, it can still be rendered much more precise than the pixel scaling.

I haven’t used the Saussage Fattener myself. But most of the time, if it is comic art it is often vector graphics, if it is photo realistic, it is often bitmaps.

I have seen crafty people doing photorealistic vector graphics, that are amazing.

One link I found here:

> [@How do YOU make beautiful plugin gui's?](https://forum.juce.com/t/how-do-you-make-beautiful-plugin-guis/8483/24):
>
> I do not use any images anymore and also try to avoid svg’s where ever possible. I started to code the UI components. Following UI uses only the paint method and the JUCE graphics operations: [[image] ](https://tal-software.com//images/products/tal-dub-x.png) another one: [[image] ](https://tal-software.com//images/products/tal-u-no-lx-v2.png) I miss some blending and gradient options to make photo realistic UI’s, but i’m happy with the result. And scaling works great! p.s. The fonts in Juce is the main weak point in my optinion, but i think i…

There were more amazing screenshots I can’t find right now…

---

<div class="post-metadata">

**Author:** ![fr4nz909](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr4nz909/32/5853_2.png) [@fr4nz909](https://forum.juce.com/u/fr4nz909)\
**Post date:** [June 12, 2019, 2:42pm UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/9 "2019-06-12T14:42:37Z")

</div>

Thanks! That helps 🙂

You mentioned vector graphics. I know SVG as a vector graphic format… can I use .svg in JUCE as a Vector Graphic? Is this possible or do I really have to redraw that cartoon with code? 😅

Wow, that GUI is really awesome… and that all painted with JUCE…

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [June 12, 2019, 2:57pm UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/10 "2019-06-12T14:57:49Z")

</div>

Yes, you can use [Drawable::createFromImageData()](https://docs.juce.com/develop/classDrawable.html#aca3e8f4f84100faff7accb2398453e70). That reads SVG into a tree of Drawables from BinaryData, that you can draw using [drawable.drawAt (g, …);](https://docs.juce.com/develop/classDrawable.html#a1b662e41ac780e1bfa8cd381d293cb6a).

And they are much smaller in memory than bitmaps.

By using the AffineTransform, you can do the rotation for a knob for example…

---

<div class="post-metadata">

**Author:** ![fr4nz909](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr4nz909/32/5853_2.png) [@fr4nz909](https://forum.juce.com/u/fr4nz909)\
**Post date:** [June 12, 2019, 3:00pm UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/11 "2019-06-12T15:00:14Z")

</div>

Awesome. That’ll work for me.

Thanks for your time, folks 🙂

---

<div class="post-metadata">

**Author:** ![asimilon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/asimilon/32/4235_2.png) [@asimilon](https://forum.juce.com/u/asimilon)\
**Post date:** [June 12, 2019, 4:50pm UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/12 "2019-06-12T16:50:41Z")

</div>

> [@fr4nz909](#):
>
> I feel like the Images lose Quality when beeing rescaled

The rescaling is done with interpolation (and you can [choose different quality settings](https://docs.juce.com/master/classGraphics.html#a0f8160f9b0a6866e40cf1a2b45c3b892)). I found with using source images that are _much_ larger than you need and scaling down there isn’t a loss of quality (but maybe my threshold for what constitutes “loss of quality” is much higher than yours 😄 ).

---

<div class="post-metadata">

**Author:** ![sparklingRhubarb](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@sparklingRhubarb](https://forum.juce.com/u/sparklingRhubarb)\
**Post date:** [June 13, 2019, 6:56am UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/13 "2019-06-13T06:56:31Z")

</div>

> [@richie](#):
>
> there isn’t a loss of quality

We use 2x oversampled images for our knobs and sliders. That way, we can draw them on Hi PDI screens in their original resolution and scale them down for normal screens.

What I found is that scaling down by more than 2x can result in realy ugly moire artifacts when the image has small patterns that can not be resolved by the lower resolution. This was with the highest resampling quality selected. When I scale down the same image in Gimp to the same target resolution, the result was much prettier. Here, there was no moire, the pattern was simply blurred, as one would expect.  
I worked around that by scaling the original image down so that I never have to downsample by more than 2x, but I would prefer a better resampling algorithm.

---

<div class="post-metadata">

**Author:** ![fr4nz909](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr4nz909/32/5853_2.png) [@fr4nz909](https://forum.juce.com/u/fr4nz909)\
**Post date:** [June 13, 2019, 3:29pm UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/14 "2019-06-13T15:29:11Z")

</div>

hey again @daniel !

I wrote down the line “std::unique\_ptr testDrawable (Drawable::createFromImageData(BinaryData::testone\_svg, BinaryData::testone\_svgSize));”

But right now I cannot figure out how to use the drawAt function you mentioned…

I thought I would go with something like testDrawable.drawAt, but this does not seem to work o:

Franz

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [June 13, 2019, 4:10pm UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/15 "2019-06-13T16:10:38Z")

</div>

Can you specify what means “does not work?”  
Does not compile?  
Does not link?  
Doesn’t show up?  
Crashes?

since testDrawable is an unique\_ptr, `testDrawable.drawAt` wouldn’t compile, you would need to use `testDrawable->drawAt(...)`

---

<div class="post-metadata">

**Author:** ![fr4nz909](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr4nz909/32/5853_2.png) [@fr4nz909](https://forum.juce.com/u/fr4nz909)\
**Post date:** [June 13, 2019, 4:14pm UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/16 "2019-06-13T16:14:03Z")

</div>

Yeaah, just figured it out!  
Coming from C#, I forgot about the arrow that I need to use for a pointer, how dumb 😃

Thanks anyway 🙂

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [June 13, 2019, 4:15pm UTC](https://forum.juce.com/t/best-way-to-animate-images-in-juce/33856/17 "2019-06-13T16:15:34Z")

</div>

Ah cool, wasn’t sure, if that was literally what you were calling
