[cairo] 'erasing' a surface

Carl Worth cworth at cworth.org
Mon Apr 18 22:36:36 PDT 2005

On Tue, 19 Apr 2005 17:18:30 +1200, Jonathan Roewen wrote:
> I've been running myself around in circles trying to figure out how to
> effectively erase a surface.
> Basically, I want to take a cairo object, and draw an opaque black
> rectangle over a given dimension.

That should be as simple as:

	cairo_rectangle (cr, x, y, width, height);
	cairo_set_source_rgb (cr, 0, 0, 0);
	cairo_fill (cr);

(This is using the API as in CVS right now[*]).

Opaque erasing is easier than "erase to transparent" which would look
something like this:

	cairo_save (cr);
	cairo_rectangle (cr, x, y, width, height);
	cairo_set_source_rgba (cr, 0, 0, 0, 0);
	cairo_set_operator (cr, CAIRO_OPERATOR_SRC);
	cairo_fill (cr);
	cairo_restore (cr);

Where the "SRC" operator copies data directly from the transparent
source color rather then blending it "OVER" the background. The extra
cairo_save/cairo_restore just avoids later code inadvertently using
the "SRC" operator rather than the default "OVER".

And, for the future, I'm very close to having a patch done for the new
cairo_paint API which will simplify this operation, (when wanting to
erase all of the target surface, or at least all of the current
clip). Using cairo_paint instead of cairo_fill will allow the
cairo_rectangle call to be dropped, (which also means the user won't
have to worry about the surface/clip dimensions.

> I'm using the OCaml bindings, and every which way I've tried it so far
> doesn't work. Either I get this massive white rectangle that doesn't
> fit with anything I've coded, or whatever I do doesn't have any
> noticable effect.

I don't know what cairo-ocaml code looks like, but hopefully the above
snippets are easy enough to translate for you.

Good luck,


[*] Back in 0.4.0, one would use cairo_set_rgb_color instead of
cairo_set_source_rgb, and cairo_set_rgb_color/cairo_set_alpha instead
of cairo_set_source_rgba.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.freedesktop.org/archives/cairo/attachments/20050419/070838ef/attachment.pgp

More information about the cairo mailing list