Canvas animation in windows store apps

Introduction:
Animations are key features to make an app interactive and lively. It can occur between Opacity, change in X & Y coordinates, Bounce effect, Size changes etc. Storyboards are the most effective way to implement an animation.

Description:
In this example we will go through an animation, which would make a rectangular box move from left to right with position and time span being specified.
To make such kind of animation first of all it is required to animate the Canvas.Left property of an element using the DoubleAnimation class. This class makes use of the Duration specified in the format “hour:minute:second” and animates the Canvas.Left property of the element according to the time period specified. Other property includes AutoReverse, which reverses the animation (the same animation in opposite manner)and RepeatBehavior, which specifies how many times the animation can be repeated.

Here’s the XAML code piece illustrating the same.

<Page x:Class="App5.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App5" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
    <Page.Resources>
        <Storyboard Storyboard.TargetName="rectImageLogoContainer" x:Name="sbAnimateBottom" RepeatBehavior="Forever">
            <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:5" From="100" To="400" x:Name="daAnimateBottom"></DoubleAnimation>
        </Storyboard>
    </Page.Resources>
    <Grid x:Name="gdParContent">
        <Canvas VerticalAlignment="Center" HorizontalAlignment="Stretch">
            <Rectangle x:Name="rectImageLogoContainer" Height="150" Width="150" Canvas.Left="100" Fill="Red" />
        </Canvas>
    </Grid>
</Page>

Here’s the c# code that would start the animation.

/// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            sbAnimateBottom.Begin();
        }

Summary:
Using story board we can achieve animation of position on X axis or Y axis. Time duration can be manually specified and in this example, since we have provided the repeat behaviors it will make a continuous animation by animating the box from Canvas.Left -100 to 400. The parameters of the DoubleAnimation can be specified from the code behind also if required.

Written By: Anobik Dey, Software Developer, Mindfire Solutions

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s