Click here to Skip to main content
15,888,302 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm a newbie in WPF and please, guide me in the right direction for this problem.

I have built a WPF application which contains all the functionality of that of a road map view control. I.e. the road map can be zoomed in/out, panned in all directions using mouse, keyboard and the controls provided. I have mapped the roads as paths drawn using Expression Blend.

Currently I am looking for a way to animate a selected road, as if it was drawn by a pencil/pen/marker. Is this possible? So far, I've been able to animate the opacity and color of the path. I've search a lot for this functionality with no luck. May be I do not search for the correct terms. I hope someone of you could shed some light on to this matter.

Thanks in advance. Am sorry, if I sound crazy :) Programming is my way of being crazy :D
Posted

There are different levels of animation. I have a feeling that you mostly exhausted the possibilities of "default" animation features embedded in WPF and available via XAML, that's why you face the problem of expressive power of the method. Maybe your animation need more flexibility; it is more complex then the set of combined features of animation with the storyboard, etc.

If so, you need to step one level lower and design lower-level animation which would allow for nearly any motion. First, create a mathematical model of animation, express it in the form of data model. It could be a model of "physics" of the scene or something else — you named it.

On a next step, develop a rendering of your scene as a static picture on your canvas using your elements placed on the canvas. In other words, make the positions of all the objects and the transformations depending on your data model.

Next step: make a "rendering procedure". When the data model is updated, all your objects should relocate/transform according to the new data. This is the key: relationships between all the elements can be as complex as you need. Make it a single parameterless method, let's call it "Step".

Next step: add time to this picture. You can use a timer or a separate thread. I would advice to use a thread which is easier to implement and which is more reliable. First, you need to modify your data model depending on time. Here is a very important thing: with thread, you will need to use System.Threading.Thread.Sleep(frameDuration). Do not calculate time based on frameDuration! The actual thread timer would be somewhat random. Instead, you need to use "real" time. Take if from System.DateTime.Now or better use more accurate System.Diagnostics.Stopwatch, http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^]. I did not try to compare performance yet, but as you need to make this time method as fast as possible, I suspect Stopwatch will also perform faster. Please do some experiments/testing by yourself and pick the best method of timing.

From the above step, you will get some thread which "animates" the data model depending on real time, but the points of real may be not precisely equidistant, which will work quite fine, you will see it. What's more important, you need to have proper average frames per second (experiment with it by yourself) and model depending not on the number of frame, but on the "real" time; see the above paragraph.

If leaves for the last step: how to make the scene update. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher. In this case, you need to invoke your method Step I described above.

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
Thank you for the answer :) I am actually working on a XAML solution right now. I prefer not to go back to the old Windows applications development method. I love the new programming model and I want to develop a solution using the resources available already or writing a complex logic.

I've researched intensely and there is no existing solution for this. Let me work on it and if I succeed, it'll be worth posting and sharing, which I definitely will. Thank you again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900