Silverlight - animate Bezier curves line drawing?

人盡茶涼 提交于 2019-12-12 18:48:46

问题


I am building a small Silverlight application. In my application I need to draw lines, similar to what's 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:


回答1:


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>


来源:https://stackoverflow.com/questions/5547982/silverlight-animate-bezier-curves-line-drawing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!