Silverlight - animate Bezier curves line drawing?
I am building a small Silverlight application. In my application I need to draw lines, similar to what's开发者_StackOverflow shown in the attached image.
I understood that the best way to draw the arched connecting lines (green ones) would be with Bezier curves.
My question is, how do I animate the drawing of the lines (getting them to start from a starting (X,Y) coordinate, to the target ones)?
Attached Image:
I've spent a bit of time playing with this and it is possible. The trick is that you don't animate the path. Instead, you initially clip the path to a bounding area of zero dimension and then you essentially animate the height and width of the clipping area. The final effect looks like the path is being animated from Point A to Point B.
Take a look at the XAML sample below:
<Canvas x:Name="LayoutRoot" Background="White">
<Path Stroke="Blue" StrokeThickness="2" StrokeDashArray="10,2" >
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigureCollection>
<PathFigure StartPoint="50,50">
<PathFigure.Segments>
<PathSegmentCollection>
<BezierSegment
Point1="50,20"
Point2="120,170"
Point3="350,150"
/>
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
</PathFigureCollection>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
<Path.Clip>
<PathGeometry>
<PathFigure IsClosed="true">
<LineSegment x:Name="seg1" Point="50,50"/>
<LineSegment x:Name="seg2" Point="0,0"/>
<LineSegment x:Name="seg3" Point="0,0"/>
<LineSegment x:Name="seg4" Point="0,0"/>
</PathFigure>
</PathGeometry>
</Path.Clip>
<Path.Triggers>
<EventTrigger RoutedEvent="Path.Loaded">
<BeginStoryboard>
<Storyboard>
<PointAnimation Storyboard.TargetName="seg2" Storyboard.TargetProperty="point" To="350,50" Duration="0:0:2" />
<PointAnimation Storyboard.TargetName="seg3" Storyboard.TargetProperty="point" To="350,150" Duration="0:0:2" />
<PointAnimation Storyboard.TargetName="seg4" Storyboard.TargetProperty="point" To="50,1500" Duration="0:0:2" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Path.Triggers>
</Path>
<Path Fill="Gold" Stroke="Black" StrokeThickness="1">
<Path.Data>
<EllipseGeometry Center="50,50" RadiusX="20" RadiusY="20" />
</Path.Data>
</Path>
<Path Fill="Gold" Stroke="Black" StrokeThickness="1">
<Path.Data>
<EllipseGeometry Center="350,150" RadiusX="20" RadiusY="20" />
</Path.Data>
</Path>
</Canvas>
精彩评论