Section 7.2. Using an Animation to Create a Fade Effect


7.2. Using an Animation to Create a Fade Effect

You can create a nice fade effect by changing the opacity of an element. Let's start with the programmatic approach. In the pageLoad() function, we create a new Sys.UI.FadeAnimation object:

 var ani = new Sys.UI.FadeAnimation(); 

Then we set the target element: a label element (<span>) we created on the page:

 ani.set_target($("Label1").control); 

The default behavior for the fading animation is that the element fades in. However, the Sys.UI.FadeEffect enumeration defines two options, FadeIn and FadeOut, which you can change by calling the set_effect() method:

 ani.set_effect(Sys.UI.FadeEffect.FadeOut); 

We then define how long the animation should run. The default value is one second; the following code triples that:

 ani.set_duration(3); 

Finally, we run the animation:

 ani.play(); 

This is all illustrated in Example 7-1.

Example 7-1. Using a fading animation

 FadeAnimation.aspx <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">   <title>Atlas</title>   <script language="JavaScript" type="text/javascript">   function pageLoad() {     var ani = new Sys.UI.FadeAnimation();     ani.set_target($("Label1").control);     ani.set_effect(Sys.UI.FadeEffect.FadeOut);     ani.set_duration(3);     ani.play();   }   </script> </head> <body>   <form  runat="server">     <atlas:ScriptManager runat="server" >       <Scripts>         <atlas:ScriptReference ScriptName="AtlasUIGlitz" />       </Scripts>     </atlas:ScriptManager>     <div>       <label  style="display: inline-block; background-color: Red;">         See me fading ...</label>     </div>   </form>   <script type="text/xml-script">     <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">       <components>         <label  />       </components>     </page>   </script> </body> </html> 

Note that the display: inline-block CSS command is used; otherwise, Internet Explorer will not show the animation (for reasons I have been unable to determine). When the page is loaded, the element fades over the course of three seconds. Figure 7-1 shows how the page appears as the Label control is fading.

Figure 7-1. The label is fading into the background


Naturally, this effect can also be implemented in a declarative way. As always, you create an xml-script element whose name is a camel-case version of the class, so FadeAnimation becomes a <fadeAnimation> element. It is important to provide an ID for the animation, because you need to be able to refer to it to start it.

You can start it not only with code, but also using xml-script, as follows:

 <application>   <load>     <invokeMethod target="ani" method="play" />   </load> </application> 

This approach is explained in greater detail in Chapter 9.

Example 7-2 shows the complete code, with important page elements highlighted.

Example 7-2. Implementing a fading animation with xml-script

 FadeAnimationDeclarative.aspx <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">   <title>Atlas</title> </head> <body>   <form  runat="server">     <atlas:ScriptManager runat="server" >     <Scripts>     <atlas:ScriptReference ScriptName="AtlasUIGlitz" />     </Scripts>     </atlas:ScriptManager>     <div>     <label  style="display: inline-block; background-color: Red;">     See me fading ...</label>     </div>   </form>   <script type="text/xml-script">     <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">     <components>     <label  />     <fadeAnimation  target="Label1" effect="FadeOut" />     <application>     <load>     <invokeMethod target="ani" method="play" />     </load>     </application>     </components>     </page>   </script> </body> </html> 

7.2.1. Using an Animation to Move an Element

Changing an element's opacity is a special kind of animation. A more general animation provided by Atlas is one that simply increments the value of a number at set intervals. You can then use the changing number value in some useful way, typically to set an element property. One example that immediately comes to mind is animating an element by continually changing left and top properties.

The Atlas Sys.UI.NumberAnimation class animates numbers from a start value to an end value. By setting the animation's duration and frames-per-second values, you control the number of intermediate steps and how long the whole animation takes.

We will again use a Label control as an example. The code instantiates the Sys.UI.NumberAnimation class and sets the required properties, except for the frames per second, where the default value of 25 is used:

 var ani = new Sys.UI.NumberAnimation(); ani.set_target($("Label1").control); ani.set_startValue(0); ani.set_endValue(300); ani.set_duration(3); 

In this case, the animation takes three seconds and there are 25 frames per second, so for each step the value increases by 4. (Three seconds with 25 frames each makes 75 animation steps; since the number is animated from 0 to 300, this leads to a step size of 4.) Therefore, all values are whole numbersthat is, integral. However there are cases in which the relationship of duration and intervals does not result in integral values. Since we want to position the label only at integral positions, the resulting values must be rounded. The NumberAnimation class has a built-in support for that in the form of the integralValues property:


 ani.set_integralValues(true); 

Because the NumberAnimation class is generic, so to speakthere are no assumptions about how you will use the changing numeric valuesit does not implement a method that you can call directly to translate the numeric values into an element property. Instead, you set the NumberAnimation class's setValue property to a function that performs the work you want to do. This has the advantage that you can manipulate the numeric values as needed. For example, some browsers (like the Mozilla-based ones) only accept values for positioning that include a unit, such as "20px" instead of just "20", so your setMethod() function can add a unit to the number.

One challenge is referencing the element to be animated without making the code too specific (for instance, with document.getElementById() and a fixed ID). The animation class enables you to get a reference to the target object using get_target(), and the result's element property grants access to the associated DOM element. You can combine this reference with your implementation of setValue() and then start the animation. Your code might look like the following:

 ani.setValue = function(value) {   this.get_target().element.style.left = value + "px";   this.get_target().element.style.top = value + "px"; } ani.play(); 

Example 7-3 shows a complete listing for a page that animates a Label control, moving it around on the page.

Example 7-3. Moving an element with an animation

 NumberAnimation.aspx <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">   <title>Atlas</title>   <script language="JavaScript" type="text/javascript">   function pageLoad() {     var ani = new Sys.UI.NumberAnimation();     ani.set_target($("Label1").control);     ani.set_startValue(0);     ani.set_endValue(300);     ani.set_duration(3);     ani.set_integralValues(true);     ani.setValue = function(value) {     this.get_target().element.style.left = value + "px";     this.get_target().element.style.top = value + "px";     }     ani.play();   }   </script> </head> <body>   <form  runat="server">     <atlas:ScriptManager runat="server" >       <Scripts>         <atlas:ScriptReference ScriptName="AtlasUIGlitz" />       </Scripts>     </atlas:ScriptManager>     <div>       <label  style="background-color: Red; position: relative;">         See me moving ...</label>     </div>   </form>   <script type="text/xml-script">     <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">       <components>         <label  />       </components>     </page>   </script> </body> </html> 

Once the page has been loaded, the label element moves across the page at a 45-degree angle. Notice how the position: relative CSS property is used to make this possible. Figure 7-2 is a snapshot of the result.

Figure 7-2. The element moves across the screen


7.2.2. Using a Length Animation to Move an Element

The preceding code can also be written declaratively. As noted, to provide cross-browser support, the top and left properties of an element must not be set to a number but must contain a unit. The NumberAnimation class can provide the unit only when you create a custom setValue() method.

However, Atlas also provides a class called LengthAnimation that is capable of performing the task more directly.

It works like NumberAnimation, with two differences:

  • The values for each animation step are always rounded.

  • The value of the unit property (the default is "px") is appended to the numeric value.

So, the LengthAnimation class looks like a "better" way to move an element than the NumberAnimation class from the previous example. However, both work, that's why both are shown here.

Still, using the LengthAnimation class to animate a Label control is a bit tricky. The left and top properties are part of the element's style, which is not directly accessible as properties. However, a behavior called <layout> provides access to style this information, and therefore to the positioning values.

To animate a Label control, therefore, when you define the label in xml-script, add the <layout> behavior to it and assign an ID:

 <label >   <behaviors>     <layout  />   </behaviors> </label> 

Then create an animationor two, since we are modifying two style values:

 <lengthAnimation  target="Label1Style" duration="3" property="left" startValue="0" endValue="300" /> <lengthAnimation  target="Label1Style" duration="3" property="top" startValue="0" endValue="300" /> 

In the <application><load> section, you must start both animations, of course. Example 7-4 shows the resulting listing.

Example 7-4. Moving an element with xml-script

 LengthAnimation.aspx <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">   <title>Atlas</title> </head> <body>   <form  runat="server">     <atlas:ScriptManager runat="server" >       <Scripts>         <atlas:ScriptReference ScriptName="AtlasUIGlitz" />       </Scripts>     </atlas:ScriptManager>     <div>       <label  style="background-color: Red; position: relative;">         See me moving ...</label>     </div>   </form>   <script type="text/xml-script">     <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">       <components>         <label >           <behaviors>             <layout  />           </behaviors>         </label>     <lengthAnimation  target="Label1Style" duration="3"     property="left" startValue="0" endValue="300" />     <lengthAnimation  target="Label1Style" duration="3"     property="top" startValue="0" endValue="300" />     <application>     <load>     <invokeMethod target="ani1" method="play" />     <invokeMethod target="ani2" method="play" />     </load>     </application>       </components>     </page>   </script> </body> </html> 

7.2.3. Compositing (Grouping) Animations

When the effect you want involves more than one animation, the markup can get ugly: you get several animations that start in sequence (but hopefully are executed in parallel). The preceding example (Example 7-4) contained two separate animations, one for the horizontal value and one for the vertical value, each of which you had to define separately, including their duration.

You can simplify things by grouping animations using the Sys.UI.CompositeAnimation class. Grouping animations helps make sure that animations execute in parallel.

You can do this using the xml-script <compositeAnimation> element. Within the element, the <animation> element contains the xml-script definitions for all animations that should be executed together. You can then specify an id attribute and a duration attribute for the <compositeAnimation> element that then apply to the group as a whole:

 <compositeAnimation  duration="3">   <animations>     <lengthAnimation target="Label1Style" property="left"                      startValue="0" endValue="300" />     <lengthAnimation target="Label1Style" property="top"                      startValue="0" endValue="300" />     <fadeAnimation target="Label1" effect="FadeOut" />   </animations> </compositeAnimation> 

You can start the composited animation using <invokeMethod>:

 <application>   <load>     <invokeMethod target="ani" method="play" />   </load> </application> 

Example 7-5 shows the complete code for a page that contains a set of grouped animations.

Example 7-5. Grouping animations on a page

 CompositeAnimation.aspx <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">   <title>Atlas</title> </head> <body>   <form  runat="server">     <atlas:ScriptManager runat="server" >       <Scripts>         <atlas:ScriptReference ScriptName="AtlasUIGlitz" />       </Scripts>     </atlas:ScriptManager>     <div>       <label  style="display: inline-block; background-color: Red; position: relative;">         See me fading and moving ...</label>     </div>   </form>   <script type="text/xml-script">     <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">       <components>         <label >           <behaviors>             <layout  />           </behaviors>         </label>         <compositeAnimation  duration="3">           <animations>             <lengthAnimation target="Label1Style" property="left"                              startValue="0" endValue="300" />             <lengthAnimation target="Label1Style" property="top"                              startValue="0" endValue="300" />             <fadeAnimation target="Label1" effect="FadeOut" />           </animations>         </compositeAnimation>         <application>           <load>             <invokeMethod target="ani" method="play" />           </load>         </application>       </components>     </page>   </script> </body> </html> 

This animation is composed of three subanimations that each complete at the same time (see Figure 7-3):

  • The element fades out.

  • The element is moved right.

  • The element is moved down.

Figure 7-3. The label moves and fades at the same time


And even though the real-world use of animations is a bit limited, Atlas makes it very convenient to add some eye candy to a web application. Since these features all reside in an external JavaScript file, the Atlas.js library itself is not bloated by including this functionality by default.




Programming Atlas
Programming Atlas
ISBN: 0596526725
EAN: 2147483647
Year: 2006
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net