Tutorials Books Videos Forums

Change the theme! Search!
Rambo ftw!

Customize Theme


Color

Background


Done

Introduction to Blend: Animation

by kirupa   | filed under .NET and C#

This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.

One of the cool features in Windows Presentation Foundation (WPF) is its support for animation. While the animation support is great, creating animations manually by writing XAML requires some effort. To simplify creating your own animations in WPF, you can use a graphical editor such as Blend instead. Blend features, as you will find out, an intuitive interface for creating and modifying animations. The XAML is automatically written in the background for you.

For a demo of what you will create, click here or on the following screenshot to launch the application. If you are using Firefox, be sure to download the FFClickOnce extension to be able to run ClickOnce apps directly without having to download the files first:

For this tutorial, you should be familiar enough with Blend to easily draw and customize objects. If you feel you need more practice, click here to visit/re-visit my Brushes tutorial that covers these basic tasks.

On the Menu

This tutorial covers a lot of topics, so I've broken it down into smaller sections for easy digestion:

  1. Using the Label Control to Display Text
  2. Simplified Look at Animation in WPF
  3. Animating the Text
  4. Loose Ends
     
    1. Leaving and Entering the Recording Mode
    2. Adding Label Control without Specifying Size
  5. Conclusion

While there seem to be a lot of topics, this should be a fun tutorial. The output is nifty also, so let's get started by first creating the text that you will animate.

Using the Label Control to Display Text

The first thing we want to do is animate some text. In order to do that, we first need some text to animate. The following steps will explain how to use the Label component to create the text that you will animate.

  1. Launch Blend and create a new .exe project from the Create New Project window. Give your project any name you want. I will be calling my project SimpleAnimation:

[ create a new exe project in Blend ]

Press OK to close the Create New Project window and to actually create your project.

  1. With your project created, you should see a new window displayed in your Artboard. Select your window and make some modifications. From the Properties panel, reduce its width and height to 400 by 300, give it a dark black/blue gradient background color, and rename the title to Simple Animation:

[ make some modifications to your current window ]

  1. The last few steps should have been a review for you. From here on out, I'll go a bit more slowly and explain the steps in greater detail. First, we want to create the text that slides in from right to left.

    From the Toolbox on the left, click on the icon containing the text controls. It should be the fourth item from the bottom, and when you click and hold that icon, a drop down menu should appear.

    From this drop-down menu click on the Label control:

[ select the Label control from the list of text-related controls ]

The Label control is often used for displaying text, and by default, it doesn't display a border or any extra formatting that the other text controls employ. A label is perfect for what we are trying to do - which is, just display some text.

  1. After you click on the Label control, go to your Artboard and click and drag your mouse to draw a rectangle that will form the size of your text label:

[ draw the appropriate size for your label control ]

Draw a label that is large enough to hold a few words using a large font size, but we can change that easily later. Once you have the label rectangle sized, release the left mouse button and your Label control will be added:

[ when you release the left mouse button, a label with your specified size will be drawn ]

You are almost done with creating the text label. In the next section, we will finish up work on the Label and begin discussing how to think about animations  in WPF.

In the previous section, you started to create the Label component to display some text. We'll finish up work on our label and discuss animation.

  1. Right now, the text in our Label is too small to read, and the dark text color is not visible on our dark background. Let's change that. Make sure your label is selected, and look at your Properties panel on the right-hand side. Find the panel marked Text:

[ the Text panel allows you to modify your Label control's properties ]

In this Text panel, we will change the text size. Notice that the font-size drop-down menu currently displays the default size of 12. Change the value from 12 to 72 by either typing 72 after clicking on the 12 or by selecting 72 from the drop-down menu:

[ increase the font size to 72 ]

  1. After changing the font size to 72, let's change the text color to a shade of white. With your Label still selected, look in your Brushes panel and change the Foreground color to White:

[ change your text's color by modifying the Foreground property ]

  1. Right now, your label's text should both be larger and lightly colored than it was a few steps earlier. If you set your label's initial size to be too small, you'll find that the text in your Label doesn't fully display:

[ if you initially sized your Label control too small, the text will be cut-off ]

To fix the sizing issue, there are two things you can do. You can make your font size smaller, or you can resize your Label control to display all of your text. For this tutorial, if your text is cut-off, resize the label control to make sure all of the size 72 text fits:

[ resize the label control to make sure all of your text fits ]

  1. We are almost done customizing our Label control. The last thing to do is change the default text from Label to something else. Right click on your Label control and select Edit Text:

[ select Edit Text to change the text that is displayed in your Label control ]

Your text will now be selected. Type the word Blend and click anywhere else in the Artboard to accept the change:

[ change the text inside your label to Blend ]

The previous section of this tutorial will describe how to insert a Label control where the control resizes itself automatically based on the text inside it.

Your Label component is now ready. The next step is to animate this control, but before we dive right in, I think it is useful to think about how animation works in WPF.

Simplified Look at Animation in WPF

When creating animations, you need an object to animate. In our animation example, some of the animated objects included text and various circles. An object alone does not make an animation, though. You need to also specify how your object will vary at different time states. For example, your object may start off small, but at a later time, it is made much larger. The animation would involve showing how the object grows from its initial small size to its final large size.

The actual animation part is handled by Blend itself. All you have to do is specify how your object looks at a beginning time and an ending time. One way of looking at this is you take a snapshot of how your object is at Time 0. You then make various modifications to your object and take another snapshot at a later time such as Time 1:

As shown in the above image, at Time 0, you have a square that you have tilted on a corner. At Time 1, you take the same square, rotate it, scale it down in size, make it more transparent, and move it to your right. By themselves, both of these object states are pretty boring. When animated though, things become interested. Don't worry, though, WPF takes care of the animation where all the intermediate frames are generated for you automatically.

Internally, the following image shows how WPF perceives and generates the intermediate sequences of your square between Times 0 and 1:

Times 0 and 1 are arbitrary times that I picked to describe times for the starting and ending states. WPF's animations are time-driven, so if your animation needs to take 2 seconds, then it will try its best to start and end the animation at exactly two seconds. The frame rate during this phase isn't fixed at a particular number, but it varies depending on what the computer is capable of.

The duration you specify for your animation determines how fast your animation actually will seem. If your time between states is really short, then your animation will seem very fast because you have little time to go from the initial to the final state:

If your duration is large, then your animation will take longer to complete because you now have more time to go from the initial state to the final state:

With the longer duration, WPF generates more intermediate frames because the eye will need to perceive more intermediate changes to simulate a smooth transition.

Note - Frame Rates and Visual Perception

For information on frame rates and what causes our eyes to see smooth animation, check out my blog post on this topic here.

To summarize, you first need an object that needs to be animated. It may be a square as in the above images, or it could be some text or circles as it was in the example on the first page. Once you find an object that you wish to animate, you need to specify the starting and ending states and the duration/running time your animation will follow. Don't sweat the details (yet!), for the following sections will guide you through creating a simple animation using Blend.

In the previous section, you finished adding your Label control, and more importantly, you received a brief introduction on animations in WPF. Let's put what you learned into practice by animating the text you placed in your Label control.

Animating the Text

The following instructions will explain how to use Blend's animation features to animate your text:

  1. Animations are dependent on timelines, so what we need to do is first create a timeline. From your Objects and Timeline panel, click the + button to add a new Timeline:

[ press the + button to create a new timeline ]

  1. Once you clicked the + button, the Create Storyboard Resource window will appear. Change the Resource name (Key) value to TextAnimation and press OK:

[ give your Storyboard the name TextAnimation ]

  1. After you clicked OK, the Create Storyboard Resource window will disappear, and your Objects and Timeline panel will change to display a timeline, and you will find yourself in the Recording mode.

    The recording mode is where you get to create and edit animations. You'll see that you are in the recording mode by seeing the red border surrounding your Artboard with the header of your Artboard indicating that Timeline recording is on:

[ the Timeline recording indicator ]

More importantly, you will see a timeline appear in your Objects and Timeline panel:

[ your Objects and Timeline panel now display a timeline ]

The timeline is where you specify the starting and ending points of your animation. If it hasn't already been done, select your text label to select it. When selected, you'll see it highlighted in gray as shown in the above image.

With your label now selected, click on the Record Keyframe button to insert a keyframe at time 0:

[ the Record keyframe button adds a marker at the specified time ]

After you clicked on Record Keyframe, you will see a keyframe marker displayed next to your label object in the tree:

[ the keyframe marker is now displayed in your timeline ]

  1. For our animation, we want our text to scroll in from right to left. Select your Label control (the one with the Blend text) in the Artboard, and use your right arrow key (or Shift + right arrow key to move faster) to move it beyond the right edge of your Window:

[ move your Label to the right side side of the screen initially ]

  1. Right now, your Blend text is positioned to the right of your project's Window. Like mentioned earlier, we want the final position of our text to be in the visible area on the left.

    Go back to your Objects and Timeline panel. Right now, your timeline's play head (the yellow triangle and line) is on frame 0:

[ the playhead marker indicates the time your Artboard currently is in ]

Click on the play head and drag it over to the 1 second point in your timeline:

[ drag the play head marker to the 1 second mark ]

  1. With your play head now at the the 1 second mark, click on the Record Keyframe button again to record a keyframe marker at the 1 second mark:

[ insert another keyframe at the 1 second mark to specify the end of the animation ]

After you hit the Record Keyframe button when your playhead is at the 1 second mark, you'll see both the keyframe marker appear in your timeline at time 1 as well as seeing a gray highlight appear between your keyframes on time 0 and time 1.

  1. With your playhead still on time 1, select your Label control on your Artboard and use your left arrow key to move it back over the dark background of your application Window:

[ at the 1 second mark, move your Blend label to the left ]

Notice that when you move your text back over your application window, you see a guide path consisting of small blue circles (see above image) that indicate the path your object is taking from where it was in an earlier time. Think of it as a breadcrumb trail!

  1. In your Objects and Timeline panel, if you click and drag your playhead between the keyframes at times 0 and 1, you will see your Blend text smoothly transition between the left and right sides.

    If you press F5, you can preview your animation and see your animation in action:

[ what your animation now looks like after it has finished running ]

Phew. You have now successfully created a basic animation that moves text from one side of the window to another. In the next section, I will cover some tips that might save you some time in the future and wrap this tutorial up.

In the previous section, you learned how to apply all of your work from the first two pages into creating a working animation. In this page, I will cover some details that, while not relevant to what was done in this tutorial, will come in handy when you are creating your own animations.

Loose Ends

When providing instructions, there are certain details that I did not emphasize in the main tutorial. This section contains some of those details that you may find useful.

Leaving and Entering the Recording Mode

In Blend, when modifying your timeline, you are known to be in the Timeline recording mode. You already know about the timeline that magically appears in your Timeline and Objects panel, and you may also have noticed a red box outlining your Artboard with the text "Timeline recording is on" surrounding your Artboard:

[ the Timeline recording indicator ]

You got into this mode when you decided to create a new timeline by pressing the plus button representing the action for creating a new timeline. If you want to leave the Recording mode, go back to your Objects and Timeline panel and click on the timeline drop-down menu to select the Default timeline:

[ selecting the Default timeline exits the Recording mode ]

When you select the Default timeline, you will no longer be in recording mode, but instead, be back in your default editing view. Likewise, to go back to the recording mode, you can either create a new timeline like you did in the previous section, or you can click on the same timeline drop-down menu and select the timeline you are interested in re-visiting.

Adding a Label Control without Specifying Size

Earlier, when you were adding the Label control, you drew a rectangle that set the boundaries of your label. When the text you were adding was larger than the space afforded by the Label control, your text would get cut off. I suggested resizing the control to make the text fully appear, but you really do not have to that.

You can actually draw a default sized Label control that automatically adjusts its dimensions based on the size of the text contained inside it. First, select the Label control like you did earlier:

[ select your Label control ]

After you have selected the Label control, your toolbox will now display the Label as one of the recently selected controls.

[ double click on the control to add it to your Artboard at the default size ]

You can double click on the Label control directly from the Toolbox. This time, the width and height of your control varies depending on the content inside it. This trick is not just limited to Labels, so feel free to use this approach on other controls as you see fit.

Conclusion

This introductory tutorial should provide you with the basic skills needed to create animations in WPF using Blend. There is a lot more to animation that was not covered in this tutorial, but subsequent tutorials will dig deeper into some more animation techniques that you will need to know.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--