[cairo] drag selection box over image surface

Rick W. Chen stuffcorpse at gmail.com
Sat Mar 8 20:12:13 PST 2008


Hi Peter, thanks for your help. I've got it working now. I use a buffer
surface in on_size_allocate. that way it always gets created at the
right size and clipping and scaling are done once. And in on_expose I
just paint the buffer surface without doing anything else. The selection
box runs great, very smooth!

Peter Groves said the following on 08/03/08 18:22:
> On Sat, 08 Mar 2008 18:30:28 +1300
> "Rick W. Chen" <stuffcorpse at gmail.com> wrote:
> 
> 
> This is as good a place as any :)
> 
> 
> I have an application that does something very similar. Unfortunately it's using the ruby bindings so I won't attach it. What I can tell you though is that your approach is correct except you need to save the background image in a buffer in it's final form (with all scaling and clipping done) and repaint that (plus the rectangles) on each mouse motion event. The most straightforward way is to draw the background to an ImageSurface and then draw that and then the rectangle onto the DrawingArea's context on each motion event. You can also create the surface to use as a buffer using create_similar_surface, but that's an optimization you may not need to bother with if ImageSurface performs well enough.
> 
> Since I get good performance (no flickering) even when using the ruby bindings (ruby is a slow language), I'm sure you'll get at least decent performance.
> 
> The complicated part is that you have to make sure the saved ImageSurface is replaced and redrawn every time the gtk window is resized with a new ImageSurface of the correct dimensions. 
> 
> Also, I should mention that if you do all the background drawing onto an ImageSurface, you only need to scale the cairo context when you're drawing to that. When you draw the ImageSurface onto the DrawingArea and then draw the rectangles, you should be able to stay in the default pixel coordinates. This will also reduce the amount of work cairo has to do on each redraw from the mouse motion events.
> 
> Let me know if that doesn't make sense. Good luck.
> 
> -Peter
> 


-- 
  Rick


More information about the cairo mailing list