BigBug
BigBug

Reputation: 6290

Drawing 2 lines

If i have 2 lines on a page

how would i animate the first line to reach the second line's position?

Upvotes: 4

Views: 386

Answers (2)

Likurg
Likurg

Reputation: 2760

If i'm not mistake you have blink, and this is your problem? Try use this BufferedGraphics small example

System.Drawing.Graphics g = this.CreateGraphics();
    System.Drawing.BufferedGraphicsContext dc = new BufferedGraphicsContext();
    BufferedGraphics backbuffer = dc.Allocate(g, new Rectangle(new Point(0, 0), g.VisibleClipBounds.Size.ToSize()));
    backbuffer.Graphics.DrawLine(Pens.White, 10, 10, 10, 100);
    backbuffer.Render(g);

Upvotes: 1

Phil Wright
Phil Wright

Reputation: 22906

Add a 'counter' variable that will be the percentage of the distance the line has travelled between the two locations. Initialize it to zero because it starts at the beginning position. Add a timer and each time it ticks you increment the 'counter' and invalidate the client area so that a repaint occurs. Once the 'counter' hits 100 and so it 100% of the way to the target you disable the timer as no longer being needed.

In the paint routine you already know the start position and the end position so just draw the line at the 'counter' percentage between the two. So given you know startX and you know endX your painted version is startX + ((endX - startX) / 100 * counter). Do this for each of the top, left, right and bottom of the line and you are done.

Upvotes: 2

Related Questions