Click here to Skip to main content
Click here to Skip to main content

Motion class to animate your controls

By , 25 Jul 2003
 

Sample Image - tween.jpg

Introduction

Have you ever needed to animate a control in your programs? In other words, animate anything on your screen? Read on....

This method will animate any control, anywhere on the screen, anyhow you want, according to the parameters passed. Parameters are destination X position, destination Y position, elapsed time and 10 animation types to choose from.

Background

When developing Macromedia Flash applications, I have come across several instances where I needed to animate objects "from here to there". Animating in Flash is relatively easy, but scripted animation is a bit trickier. A clever concept inspired by Robert Penner and Zeh Fernando, led me to write my own customized method in Actionscript (Macromedia's Flash Scripting Language), which I now always use.

But recently I have been digging some more into C# and have been surprised by the similarity to Actionscript and ease of the language, while still respectful of its depth and power. Curious on how hard or easy this would be, here is an attempt to bring some Flash style animation programming into C#.

Using the code

Simply take the following steps:

  • add this class (tween.cs) to your project (ensure the namespace matches your project namespace)
  • instantiate an object from the class.
  • call startTweenEvent method from your events and pass the parameters.

It is that easy! Parameters required are:

void TweenLibrary.startTweenEvent(object sender
        [control you want to animate, can be sender or any other],
        int X Destination,
        int Y Destination,
        string Animation Type,
        int Duration in ticks)

or as described in example below.

This code snippet will instantiate two tween objects and start tweening the two button controls upon program launch. Since these are two independent objects, they will animate simultaneously!

<CODE>public</CODE> TweenLibrary tween1 = <CODE>new</CODE> TweenLibrary();
<CODE>public</CODE> TweenLibrary tween2 = <CODE>new</CODE> TweenLibrary();

<CODE>public</CODE> Form1(){
    InitializeComponent();
    tween1.startTweenEvent(<CODE>this</CODE>.button1, 300, 12, "linear", 40);
    tween2.startTweenEvent(<CODE>this</CODE>.button2, 188, 90, "linear", 40);
}

Note how in the previous example the object is hardcoded (button1 and button2) In the following example the snippet shows how to add motion to a control with an eventhandler that can come from any control that points to the same handler. (an instance (tween1) of the tween class must have been created prior to calling this method.)

The object to animate here is the sender and therefore can be any control!

<CODE>private void</CODE> button1_Click(<CODE>object</CODE> sender, System.EventArgs e) {
    tween1.startTweenEvent(sender, 65, 132, "easeinoutcubic", 5);
}

You can choose from the following 10 animation types:

  • linear //simple linear tweening - no easing
  • easeinquad //quadratic easing in - accelerating from zero velocity
  • easeoutquad //quadratic easing out - decelerating to zero velocity
  • easeinoutquad //quadratic easing in/out - acceleration until halfway, then deceleration
  • easeincubic //cubic easing in - accelerating from zero velocity
  • easeoutcubic //cubic easing out - decelerating to zero velocity
  • easeinoutcubic //cubic easing in/out - acceleration until halfway, then deceleration
  • easeinquart //quartic easing in - accelerating from zero velocity
  • easeinexpo //exponential easing in - accelerating from zero velocity
  • easeoutexpo //exponential easing out - decelerating to zero velocity

The provided source files include two .cs files. One (Form1.cs) is the Form with the Main() method and the other (tween.cs) is the tween class, containing the required methods to make it work. The example consists of a form with some random controls, which have all the same event Handling method. It also generates a random X and Y position and a dropdown (top left) allows you to set the animiation type for demonstration purposes.

To apply this code, all you would need to do is to include the tween.cs file in your project; change the namespace to match your namespace and instantiate an object from the tween class. Then just call the method and pass your parameters.

Points of Interest

You can find more info about the author at http://www.mxer.com/. Any feedback, questions or comments please write to miguel@mxer.com

All code written by Miguel Moreno. Thanks to Robert Penner for supplying his truly exceptional easing equations in Actionscript, and to Zeh Fernando for inspiring me with his Flash tween(); method to create this experiment in C#.

History

  • Version 1.0
    • 10 animation types. (will add more in next revision)
    • no "end of tween" function call. (will add in next revision)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

mijalis
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionthanks!memberJayson Ragasa27 Aug '12 - 6:05 
awesome code! thanks for sharing!
Software Developer
Jayzon Ragasa
Baguio City, Philippines

GeneralNicely done!memberRavi Bhavnani18 Apr '12 - 19:22 
5 Thumbs Up | :thumbsup:
 
/ravi
My new year resolution: 2048 x 1536
Home | Articles | My .NET bits | Freeware
ravib(at)ravib(dot)com

GeneralAnimate cartoonmemberumeed.e.sahar11 Sep '08 - 7:07 
Hi,
 
I need help or any code sample for animating a .X file in C#. I already loaded few sample .X files in C#. Want to achieve interactivity to animate any 3D character by giving inputs on the fly.
 
This code can only move controls. What if i want to animate arms of a cartoon picture??
Any expert out there who can help in this regard.
 
Safia
GeneralNeeds to be time basedmemberburstingfist30 Aug '06 - 10:52 
The original mc_tween2 script (I have used and loved for years) that this project was derived from, was time based, which was nice because any tween would take the same amount of time no matter how powerful the cpu. This code should function in the same manner.
 
It also should use reflection to allow tweening of any numeric property. I have re-written this behavior.
 
I also added a tween completion event (necessary for sequenced animations).
 
I will be implementing the time based aspect next. If anyone is interested in these enhancements, let me know and they could be shared somehow.
GeneralRe: Needs to be time basedmemberarmax752 Jan '07 - 6:35 
Great,
 
can I have your modifies
 
I need your suggestions.
 
Thanks
 
Armando
GeneralRe: Needs to be time basedmemberMike Tuersley13 Sep '07 - 3:26 
I would be interested as well.
 
_____________________________
Mike Tuersley

GeneralGreat Code ... Well Donememberpiksale29 Apr '06 - 10:36 
Great Code ... Well Done
 
Pikel
GeneralVery Nicememberevidor25 Apr '05 - 16:21 
This is a great little class, how about adding the ability of changing the size of the object as it moves.
QuestionNotify when animation is complete?memberHez17 Feb '05 - 23:22 

Great article and code! Cool | :cool:
 
Very helpful class, I am currently using it to try and improve the animation on a .NET style ToolBox side bar control. One question, is there anyway to determine when the animation has completed? This would be handy as I could then hide the panel which has been replaced.
 
Thanks!
Hez
QuestionCan it be used to move more controls at the same time?memberDan Bunea27 Aug '03 - 2:59 
Can this be used to move more controls at the same time?
Thank you,
Dan Bunea

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 26 Jul 2003
Article Copyright 2003 by mijalis
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid