Graphics - is there a way to erase graph?

Hello,
I want to draw some graphics with Graphics and later (after something happen) to make some part of that graphic (but not whole) to be transparent, but I want to avoid drawing again. Is there easy way to do that with Graphics?

I tried to use for that reduceClipRegion but doesn’t work like that.

Have you tried excludeClipRegion?

To be honest I didn’t tried that because it accepts only Rectangle<int> as a parameter, but I need other shapes like ellipse.
And due to description it doesn’t erase region but only prevent to further drawing in that region.

It sounds to me like you just want to draw new shapes with the background color?

That’s right reFX, but the background in my case is at first not simple colour (there could be some elements in background with unexpected shapes), and the second problem is I don’t know what the background will be at which moment, there are more than 5 possible backgrounds. So for me the easiest way would be to just erase my Graphics instead drawing new shape with unknown background.

Every call to paint() will take a blank graphics context for you to draw to.

When you get a call to paint(), you could simply check the state of your component and draw the appropriate background.

When the component’s state changes, you should call repaint() to trigger an asynchronous paint callback.

1 Like

Exactly, paint() is a snapshot in time. Don’t think about paint() in terms of an animation. In that case you need to create multiple paint() calls that result in a changing scene.

Just correcting this:

The Graphics context is not blank but contains garbage, probably from previous paints, but could be anything really. That’s why not implementing paint() in a component with setOpaque (true) will assert. In opaque components you need to fill the background, either with a g.fillAll() call or if you know for a fact you will cover all pixels you can do that as well.

If you need the background from the previous call you can draw in an Image and use that in the next call. I didn’t understand your flow exactly, but something like that could work:

void paint (juce::Graphics& g) override
{
    juce::Image canvas (juce::Image::ARGB, getWidth(), getHeight(), false);
    juce::Graphics cg (canvas);
    cg.drawImageAt (m_lastImage, 0, 0); // copy from last drawing
    // draw your stuff on cg here
    // ...

    // draw the virtual image
    g.drawImageAt (m_lastImage, 0, 0);

    // save the canvas
    m_lastImage = canvas;
}