David Warwick

David Warwick

  • NA
  • 1
  • 692

Animate line endpoint over time

Nov 7 2015 10:46 AM
Hello all. I think I need a storyboard to do what I am trying to do, but I am not sure. I have been trying for a while now. I have run across many examples where I can get a rectangle to move across the screen for example, but what I want to do is different. I don't want to move the entire line. I just want to move the endpoint of a line to a specific coordinate on a canvas over a period of time. I want it to look like the line is being drawn on the screen. This will be a straight line. I would also like it to be done in C# instead of XAML. The canvas is defined in XAML, but the line will be created at runtime.
 
Can someone help me? This is for a Windows 10 Universal App.
 
Here is the code I have now, but it just moves the entire line:
 
private void MyAnimation()
{
double w = cnvML.ActualWidth;
double h = cnvML.ActualHeight;
Line l1 = new Line();
l1.StrokeThickness = 5;
l1.Stroke = blueBrush;
l1.X1 = 10;
l1.Y1 = 10;
l1.X2 = 200;
l1.Y2 = 200;
cnvML.Children.Add(l1);
Storyboard storyboard1 = new Storyboard();
((UIElement)l1).RenderTransform = (Transform)new TranslateTransform();
DoubleAnimation doubleAnimationY = new DoubleAnimation();
DoubleAnimation doubleAnimationX = new DoubleAnimation();
doubleAnimationY.Duration = new Duration(new TimeSpan(0, 0, 2));
doubleAnimationY.To = 100;
doubleAnimationY.AutoReverse = true;
doubleAnimationY.RepeatBehavior = RepeatBehavior.Forever;
Storyboard.SetTarget((Timeline)doubleAnimationY, (DependencyObject)l1.RenderTransform);
Storyboard.SetTargetProperty((Timeline)doubleAnimationY, "Y");
((ICollection<Timeline>)storyboard1.Children).Add((Timeline)doubleAnimationY);
doubleAnimationX.Duration = new Duration(new TimeSpan(0, 0, 2));
doubleAnimationX.To = 100;
doubleAnimationX.AutoReverse = true;
doubleAnimationX.RepeatBehavior = RepeatBehavior.Forever;
Storyboard.SetTarget((Timeline)doubleAnimationX, (DependencyObject)l1.RenderTransform);
Storyboard.SetTargetProperty((Timeline)doubleAnimationX, "X");
((ICollection<Timeline>)storyboard1.Children).Add((Timeline)doubleAnimationX);
storyboard1.Begin();
}
 
 
Thanks,
David