2

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
2
  • On the basis that your "imaginary code" would be your preference you haven't make a very good case for not simply using ani.Duration = new Duration(TimeSpan.FromSeconds(200/300)) ? Please add more detail regarding your objection to this approach. Commented May 15, 2011 at 20:14
  • There is no objection. As I said above, it feels clumsy.
    – Dercsár
    Commented May 15, 2011 at 20:53

1 Answer 1

0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.