Dercsár
Dercsár

Reputation: 1694

silverlight animation with constant speed

I would like to create animations where Duration is not set, but instead it is calculated based on an absolute speed setting. E.g. I want the animation to happen at 100 pixels/second and the duration is calculated automatically based on To and From values. If the path is 350 pixels, the animation will take 3.5 seconds to finish.

Duration.Automatic is NOT for this. Also Animation.SpeedRatio is a different thing.

I can of course calculate the duration from the path length, but I will have many objects moving on the screen, each created and removed procedural way and personally find it clumsy to bother with this.

What is a nice solution? Is there any built-in behaviour for this in Silverlight 4 or later?

Imaginary code:

DoubleAnimation ani = new DoubleAnimation();
ani.From = 0;
ani.To = 200;
ani.AbsoluteSpeed = "300 pixels / sec";
storyBoard1.Begin(); // now my animation will take 0.66 sec

Upvotes: 2

Views: 247

Answers (1)

AnthonyWJones
AnthonyWJones

Reputation: 189447

Use code like:-

ani.Duration = new Duration(TimeSpan.FromSeconds(200 / myPixelsPerSecond))

This is not at all "clumsy".

If the use of such a fairly straight forward expression would merit the addition of new property what would happen if the same approach were applied to the rest of the available API? The set of these "helpful" properties would expand to unmanagable levels. The API would be crushed by the weight of zillions of similar properties all performing fairly simple expressions for setting the true fundemental properties.

Elegance (the opposite of clusmy) is having a small set of carefully chosen properties that can be combined in a zillions ways using simple expressions. Exactly as above.

Upvotes: 0

Related Questions