Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Making an "Control" follow a Path?
14 replies. Latest Post by Qbus on November 10, 2008.
(0)
Qbus
Member
607 points
269 Posts
10-27-2008 6:39 PM |
Hi
I have a Path object. When I hover the mouse over this Path I want a little "glowing" object thing to move on the lines of the Path. Like a pulse effekt or something.
No mather what, I need an "Control" (Ellipse, UserControl, what-ever) to follow a Path, is that possible and how?
Thanks in advance,
preishuber
Contributor
3570 points
655 Posts
10-27-2008 9:06 PM |
you have to move the object by code. Best use some mouse event for that
bartczer...
3838 points
666 Posts
10-27-2008 10:55 PM |
You can use a mask to do that..You can have "glowing rectangle" pass under the whole path and it can be masked by that path. It will give the appearance that the glow is following the path. There are some examples of this on the web.
10-28-2008 6:48 AM |
preishuber: you have to move the object by code. Best use some mouse event for that
Ok, but to do that I need to get all the points of the current line i want my object to follow, how do i do that?
10-28-2008 9:53 AM |
you can attacht the mousemove event
then you need the "formular" which describes the object
then you have to decide which is your control parameter (x or y position)
btw: i have done that not bevore in that matter
10-28-2008 10:13 AM |
I think we misunderstand each other :)
I have a Path, lets say it has 4 "points", you know like a rectagle. When I hover my mouse over it I what something to happen. This is easy, I set the MouseEnter event on my Path and that is fired when I take my mouse over it. So far so good.
When my mouse enter this Path, or "hover" over it if you want, I want a little Ellipse to move along the Path. Lets say my Path has corners A,B,C,D. I then want my Ellipse to go from A to B, then B to C etc. etc.
I think I can do this if I can find a way to retrieve all the "points" of a Path. I looked at the Path obejct yesterday, but I couldn't find any method or property to help me here...
10-28-2008 11:04 AM |
not really
lets talk about a sinus. The formular is y= sin(x). With that you can calculate each point in the 2d. Mouse move can input one of the parameters. You can also use eg a slider to control the movement. I have implemented that for a carousell www.adc08.de
The only open point is, how to get the formular.
gabouy
219 points
45 Posts
10-28-2008 12:14 PM |
I think this is a very interesting problem, which has been proposed before. Seems it's not natively supported right now. I've tried to do it by animating an object programmatically with a timer, but couldn't.
My problem lies in that I can't determine the function value of my path for a given x value (assuming my path can be described as a function). You can also think of it as detecting the intersection points of your path and a vertical line on a given x (left) position.
This is my shot at it:
"LayoutRoot" Background="White"> "MyPath" Height="139" Width="266" Canvas.Left="19.5" Canvas.Top="128.5" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Data="M20,211 C89,211 96,159 96,159 L101,140 L113,129 L121,133 L125,148 C125,148 125,172 125,173 C125,174 131,260 131,260 L133,267 L139,262 L143,234 L146,174 L149,157 L160,150 L167,158 L170,166 L173,178 C173,178 163,210 285,210"/> "SomeCtrl" Height="11" Width="10" Canvas.Left="9.5" Canvas.Top="182" Fill="#FFFDFF04"/> "btnGo" Height="23" Width="35" Canvas.Left="321" Canvas.Top="21" Content="Go" Click="btnGo_Click"/>
with code behind,
private DispatcherTimer _timer = new DispatcherTimer(); private List pathCoordinates; private int _index; public Page() { InitializeComponent(); _index = 0; pathCoordinates = GetPathAsPointList(MyPath); _timer.Interval = TimeSpan.FromMilliseconds(200); _timer.Tick += new EventHandler(_timer_Tick); } void _timer_Tick(object sender, EventArgs e) { SomeCtrl.SetValue(Canvas.TopProperty, (double)pathCoordinates[_index].Y); SomeCtrl.SetValue(Canvas.LeftProperty, (double)pathCoordinates[_index].X); _index++; } private void btnGo_Click(object sender, RoutedEventArgs e) { _timer.Start(); } private List GetPathAsPointList(Path p) { List discretePath = new List(); double left = p.Data.Bounds.Left; double top = p.Data.Bounds.Top; double pwidth = p.Data.Bounds.Width; double pheight = p.Data.Bounds.Height; p.StrokeThickness = 2; for(int i = 0;ifor(int j = 0;jnew Point(left + i, top + j); IEnumerable hits = VisualTreeHelper.FindElementsInHostCoordinates(p1, LayoutRoot); if(hits.Count()>0) { discretePath.Add(p1); } } } return discretePath; }
but the discretePath.Add(p1) never gets invoked :(
damonpayne
304 points
75 Posts
10-28-2008 12:33 PM |
This has been brought up before...
WPF has a DoubleAnimationUsingPath class that is missing in Silverlight. Of all the things left out of Silverlight, especially in terms of competing with Flash, this one seems an odd omission.
The Silverlight APIs do expose the high level means of creating curves/paths using control points, but not for generating all of the points that would fall on these curves. The math for Bezier curves and such is readily available, but I have to wonder if there are not some potential differences in interpretation? This would be an interesting exercise to attempt.
10-28-2008 5:34 PM |
Have you tried running the:
pathCoordinates = GetPathAsPointList(MyPath);
In your Page_Loaded event/method instead of the constructor?
If we can get this to work, that could be a good solution :)
10-28-2008 7:34 PM |
I see one with the same problem here:
https://silverlight.net/forums/p/39163/113594.aspx
surbhiydv
Participant
1134 points
218 Posts
10-31-2008 6:08 AM |
If this is what you are looking for, you can do it easily through blend.
<UserControl.Resources> <Storyboard x:Name="Storyboard1"> <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"> <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/> <SplineDoubleKeyFrame KeyTime="00:00:01.4000000" Value="290.487"/> <SplineDoubleKeyFrame KeyTime="00:00:02.2000000" Value="288.443"/> <SplineDoubleKeyFrame KeyTime="00:00:04" Value="6.3880000114440918"/> <SplineDoubleKeyFrame KeyTime="00:00:05" Value="-73.058"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"> <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/> <SplineDoubleKeyFrame KeyTime="00:00:01.4000000" Value="4.59"/> <SplineDoubleKeyFrame KeyTime="00:00:02.2000000" Value="107.07"/> <SplineDoubleKeyFrame KeyTime="00:00:04" Value="146.94599914550781"/> <SplineDoubleKeyFrame KeyTime="00:00:05" Value="14.946"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </UserControl.Resources> <Grid> <Path Margin="109,115.5,317.5,35.5" Fill="{x:Null}" Stretch="Fill" Stroke="#FFEA1414" StrokeThickness="5" Data="M186,116 L480,120 L481.5,229.5 L195.48396,262.85306 L109.62923,117.48838" MouseEnter="MouseHover" MouseLeave="MouseOut"/> <Ellipse Height="32" HorizontalAlignment="Left" Margin="171.768,100.963,0,0" VerticalAlignment="Top" Width="32" Stroke="{x:Null}" StrokeThickness="5" x:Name="ellipse" RenderTransformOrigin="0.5,0.5"> <Ellipse.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <RotateTransform/> <TranslateTransform/> </TransformGroup> </Ellipse.RenderTransform> <Ellipse.Fill> <RadialGradientBrush> <GradientStop Color="#FF000000" Offset="1"/> <GradientStop Color="#FFFFFFFF" Offset="0"/> </RadialGradientBrush> </Ellipse.Fill> </Ellipse> </Grid>
Begin/Stop this storyboard on MouseEnter/Leave of your path.
RamsZone
160 points
159 Posts
10-31-2008 8:33 AM |
Hi Qbus,
Me too had the same need for making a control to follow specified path.
We can do it easily for WPF by using "Convert to Motion Path" option in blend.
Unfortunately we can't have this option for silverlight.
But we can achieve this with little more effort.
Please check this link
http://blogs.msdn.com/jaimer/archive/2007/06/20/using-blend-to-creating-motion-path-animations-for-silverlight.aspx
In that link jaime created user control to convert the WPF animation to keyframes.
We can copy this key frames to our silverlight application.
But this will be useful only for static animations.
Regards,
Rams.
11-06-2008 8:34 AM |
Looks like this could do the actual "animation" of moving from one point to another.
http://msdn.microsoft.com/en-us/library/system.windows.media.animation.pointanimationusingkeyframes(VS.95).aspx
But I still need to points from the Path, which isn't possible to retrieve in an easy way.
11-10-2008 5:41 AM |
Ok I saw this on my RSS reader, haven't tried it out yet, but it seems like what Im asking for :)
http://www.codeproject.com//KB/silverlight/PathAnimation.aspx