[cairo] Problems erasing a line.

Govind Salinas govindsalinas at gmail.com
Mon Mar 19 00:31:41 PDT 2007


Hey guys,

I am writing a small project to familiarize myself with c# and all the
new stuff that has come into the gnome-related world lately,
specifically Cairo.  One thing I was trying to do was make a
Gtk.DrawingArea.  When I get a left mouse click I start a rubber band
line to the current mouse location (mouse move events).  As the cursor
moves, I white out my background, erase my old line and draw a new one.
However I keep seeing artifacts of my previous line.

Here is the relevant code:

void draw (Cairo.Context gr, double x, double y, double width, double
height)
{
   gr.Color = m_fillColor; //white

   gr.Rectangle(x, y, width, height);
   gr.Fill();

   if (m_oldTrackX != m_trackX || m_oldTrackY != m_trackY)            
   {
      gr.MoveTo(m_clickX, m_clickY);
      gr.LineTo(m_oldTrackX, m_oldTrackY);
      gr.Stroke();
   }

   gr.Color = m_drawColor; //black
   if (m_clickX > 0 && m_trackX > 0)
   {
      gr.MoveTo(m_clickX, m_clickY);
      gr.LineTo(m_trackX, m_trackY);
      gr.Stroke();
   }
}

This more or less works other than I get artifacts of previous lines.

As an experiment I tried this:

void draw (Cairo.Context gr, double x, double y, double width, double
height)
{
   gr.Color = m_fillColor; //white

   gr.Rectangle(x, y, width, height);
   gr.Fill();

   gr.Color = m_drawColor; //black
   if (m_clickX > 0 && m_trackX > 0)
   {
      gr.MoveTo(m_clickX, m_clickY);
      gr.LineTo(m_trackX, m_trackY);
      gr.StrokePreserve();
      gr.Color = m_fillColor; //white
      gr.Stroke();
   }
}

What I get from this is the outside of the line I was drawing (I
successfully erased the middle of the line but left two thinner lines in
its place).  I would expect that this leaves me with a blank canvas.  Is
there some fundamental thing that I am missing that will get this to
work?  The only way I have gotten this to draw cleanly is to invalidate
the whole area (QueueDraw() for example) and then draw the new line.
While this works its seems like an expensive way to erase a line.  Can
someone tell me where I am going wrong?


Thanks,
Govind.



More information about the cairo mailing list