[cairo] Shifting (scrolling) a translucent surface?

Stefan Salewski mail at ssalewski.de
Wed Mar 7 16:16:41 PST 2012

I just tried to make scrolling faster for a small toy application,
(http://www.ssalewski.de/PetEd.html.en). Indeed I was successful, I am
reusing the background grid (with correct offset of course) instead of
redrawing, and I draw the foreground some pixes larger, so that I can
copy it with some offset to the GTK drawing area, for small scrolls. For
larger scrolls I have to shift the cairo surface, before I paint to the
uncovered stripe. Works fine, but now I have to cleanup (and better
understand) the code...

For translating/shifting the foreground I have currently code like this

$ cat pet_canvas.rb
>elsif ((offx.abs < $OFFSET) and (offy.abs < $OFFSET))
>  return
>  t = cr.target
>  cr.identity_matrix
>  cr.push_group
>  cr.set_source(t, offx, offy)
>  cr.paint
>  cr.pop_group_to_source
>  cr.set_operator(Cairo::OPERATOR_CLEAR)
>  cr.paint
>  cr.set_operator(Cairo::OPERATOR_OVER)
>  cr.paint
>  offx = 0
>  offy = 0

I guess overlapping copies are not allowed for cairo, see


But is my code a good solution? My foreground picture is translucent, so
I need that paint with operator_clear to erase all before copying back
the translated content. Or is there an operator available which does the
same with only one paint? And, I wonder how the OPERATOR_CLEAR depends
on the source surface? First I tried cr.set_source_rgba(1,1,1,1) before
the clear operation, to get a solid source for clearing operation. But
it seems to be not necessary. Is the source ignored for clear operation?
May it be a better approach to allocate an new cairo context with empty
surface, copy the picture to that surface, discard the old and use the
new one? 

Best regards,

Stefan Salewski

More information about the cairo mailing list