/
RotateAnimationUsingPathExample.xaml
164 lines (144 loc) · 7.39 KB
/
RotateAnimationUsingPathExample.xaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<!-- RotateAnimationUsingPath.XAML
This example shows how to use PathGeometry and DoubleAnimationUsingPath to animate a rectangle
along a path. In addition, the rectangle is rotated.-->
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowTitle="Rotate Animation Using Path Example"
Background="{StaticResource MyGridBrushResource}">
<DockPanel Margin="20">
<Border Background="#99FFFFFF" BorderBrush="#CCCCFF" BorderThickness="2"
Margin="0,0,0,20" Padding="20" DockPanel.Dock="Top">
<TextBlock Width="750">
This example animates the X, Y and Angle transform properties using a path geometry. This results in a rectangle animating along
a path across the screen and rotating to follow the contours of the path. The DoubleAnimationUsingPath object is needed because
the X, Y, and Angle properties use double values.
</TextBlock>
</Border>
<!-- This StackPanel renders the example on the left and the code example on the right -->
<StackPanel DockPanel.Dock="Left">
<!-- Canvas Containing the animated object and the path it follows during animation -->
<Canvas HorizontalAlignment="Left" Margin="0" Width="340" Height="215" >
<!-- This Path is only to show the path that the animated object will follow. -->
<Path VerticalAlignment="Top" Margin="15,15,15,15"
Data="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100"
Stroke="Black" StrokeThickness="2"
Stretch="None" />
<!-- This geometry renders the rectangle that is animated across the screen. -->
<Path Fill="Blue" Margin="0,0,15,15">
<Path.Data>
<RectangleGeometry x:Name="MyAnimatedRectGeometry" Rect="0,0 30 30" >
<RectangleGeometry.Transform>
<TransformGroup>
<RotateTransform x:Name="MyRotateTransform" Angle="0" CenterX="15" CenterY="15" />
<TranslateTransform x:Name="MyTranslateTransform" X="10" Y="100" />
</TransformGroup>
</RectangleGeometry.Transform>
</RectangleGeometry>
</Path.Data>
</Path>
</Canvas>
<!-- Create button to restart the animations. -->
<Button Width="80" HorizontalAlignment="Left" >
Start Animations
<!-- Trigger and StoryBoard to initiate the animation when the button is clicked. -->
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard Name="MyBeginStoryboard">
<Storyboard>
<!-- Animates the angle along the path. -->
<DoubleAnimationUsingPath
Storyboard.TargetName="MyRotateTransform"
Storyboard.TargetProperty="Angle"
Source="Angle"
Duration="0:0:5"
RepeatBehavior="Forever" AutoReverse="True" >
<DoubleAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100" />
</DoubleAnimationUsingPath.PathGeometry>
</DoubleAnimationUsingPath>
<!-- Animates the rectangle horizotally along the path. -->
<DoubleAnimationUsingPath
Storyboard.TargetName="MyTranslateTransform"
Storyboard.TargetProperty="X"
Source="X"
Duration="0:0:5"
RepeatBehavior="Forever" AutoReverse="True" >
<DoubleAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100" />
</DoubleAnimationUsingPath.PathGeometry>
</DoubleAnimationUsingPath>
<!-- Animates the rectangle vertically along the path. -->
<DoubleAnimationUsingPath
Storyboard.TargetName="MyTranslateTransform"
Storyboard.TargetProperty="Y"
Source="Y"
Duration="0:0:5"
RepeatBehavior="Forever" AutoReverse="True" >
<DoubleAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100" />
</DoubleAnimationUsingPath.PathGeometry>
</DoubleAnimationUsingPath>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</StackPanel>
<Border Background="#EEEEEE" BorderBrush="Black" BorderThickness="1">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<TextBlock Style="{StaticResource CodeSnippetParagraph}" xml:space="preserve">
...
<!-- This geometry renders the rectangle that is animated across the screen. -->
<Path Fill="Blue" Margin="0,0,15,15">
<Path.Data>
<RectangleGeometry x:Name="MyAnimatedRectGeometry" Rect="0,0 30 30" >
<RectangleGeometry.Transform>
<TransformGroup>
<RotateTransform x:Name="MyRotateTransform" Angle="0" CenterX="15" CenterY="15" />
<TranslateTransform x:Name="MyTranslateTransform" X="10" Y="100" />
</TransformGroup>
</RectangleGeometry.Transform>
</RectangleGeometry<
</Path.Data>
</Path>
...
<Storyboard>
<Bold><!-- Animates the angle along the path. -->
<DoubleAnimationUsingPath
Storyboard.TargetName="MyRotateTransform"
Storyboard.TargetProperty="Angle"
Duration="0:0:5"
RepeatBehavior="Forever" AutoReverse="True" >
<DoubleAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100" />
</DoubleAnimationUsingPath.PathGeometry>
</DoubleAnimationUsingPath>
<!-- Animates the rectangle horizotally along the path. -->
<DoubleAnimationUsingPath
Storyboard.TargetName="MyTranslateTransform"
Storyboard.TargetProperty="X"
Source="X"
Duration="0:0:5"
RepeatBehavior="Forever" AutoReverse="True" >
<DoubleAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100" />
</DoubleAnimationUsingPath.PathGeometry>
</DoubleAnimationUsingPath>
<!-- Animates the rectangle vertically along the path. -->
<DoubleAnimationUsingPath
Storyboard.TargetName="MyTranslateTransform"
Storyboard.TargetProperty="Y"
Source="Y"
Duration="0:0:5"
RepeatBehavior="Forever" AutoReverse="True" >
<DoubleAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100" />
</DoubleAnimationUsingPath.PathGeometry>
</DoubleAnimationUsingPath></Bold>
</Storyboard>
...
</TextBlock>
</ScrollViewer>
</Border>
</DockPanel>
</Page>