[cairo] Creating an opaque fade-in effect with Cairo

Andrea Canciani ranma42 at gmail.com
Thu Jan 20 14:26:37 PST 2011


On Thu, Jan 20, 2011 at 10:54 PM, Tristan Schmelcher
<tschmelcher at google.com> wrote:
> Hello,
>
> I am developing a Cairo-based application and I'd like to fade-in an
> opaque image on top of a background. i.e., I want the image to start
> out as opaque black and gradually fade-in to its full brightness. But
> I can't figure out how to efficiently paint my image surface to the
> screen and attenuate the RGB channels at the same time. Ideally I'm
> looking for an API that lets me paint my source surface (the image)
> and takes a fractional brightness parameter in the range [0, 1] to
> multiply with each of the R, G, and B channels. So far the best that I
> have come up with is to first paint with CAIRO_OPERATOR_CLEAR and then
> paint with CAIRO_OPERATOR_OVER and cairo_mask(), but that seems like a
> lot of computational work. Is there a more efficient way to do this
> with Cairo?

If your destination is always opaque, you can do everything like this:

cairo_set_operator (cr, CAIRO_OPERATOR_IN);
cairo_set_source (cr, the_fading_surface);
cairo_paint_with_alpha (cr, the_fade_parameter);

This should be sufficient to completely replace the old content assuming that
the destination has a content of type CAIRO_CONTENT_COLOR.
It is slightly less flexible than your code, because this will only allow
fading from/to black, but I expect it to be faster, because it performs only
one compositing operation.

You should obviously profile your application to check if this improves
its performance or not and you might also want to check what are the hot
functions, so that they might possibly be improved.

Andrea


More information about the cairo mailing list