MOBILE

Programming the Mobile Web : WebKit CSS Extensions (part 4) - Animations

1/20/2011 3:34:44 PM

6. Animations

Transitions are great and are the simplest way to create animations for iPhone- and Android-based devices. If you need finer animation control at the keyframe level, you can do this using the CSS animation framework. To be completely honest, I thought this was too much to be handled only by CSS, a nonprocedural and non-markup language, but it works great.

WebKit animations are done with the shortcut property -webkit-animation, which has the following syntax:

-webkit-animation: name duration timing_function delay iteration_count
direction

As you’ve probably guessed, there are also specific properties for each possible value, listed in Table 3.

Table 3. WebKit animation CSS properties
PropertyDescription
-webkit-animation-nameProvides the name of the animation to be used by the keyframes.
-webkit-animation-durationSpecifies the duration of the animation, in seconds or milliseconds.
-webkit-animation-timing-function:Defines the function used to calculate intermediate values between the initial and final values of the property. You can use the CSS cubic-bezier function or any of the following constants: ease, linear, ease-in, ease-out, and ease-in-out (the most commonly used.)
-webkit-animation-delayDefines the offset delay of the animation beginning from the time when the property was changed. This can be defined in seconds or milliseconds, and the default value is 0. If a negative value is used, the animation starts immediately but with some of the animation already done.
-webkit-animation-iteration-countDefines how many times the animation will be repeated. This can be 1 (the default value), any integer value, the special constant infinite, or a float value.
-webkit-animation-directionDefines whether the animation will play in forward direction (normal) or in alternate mode, playing forward on even iterations and in reverse on odd iterations.

After reading this list of properties you are probably asking yourself, where is the animation defined? What will be animating? For these, the WebKit keyframe extensions come into play.


Note:

If you are moving or scaling an object and you want it to be animated, it is better to use the performance-accelerated -webkit-transform property rather than the properties specified in the CSS standards.


6.1. Keyframe at-rule

To define how the animation will work and what it will do, we need to define a special CSS at-rule called @-webkit-keyframes. This rule is followed by the animation name (the one specified in -webkit-animation-name).

Inside the keyframe at-rule, we need to specify as many selectors or animation groups as keyframes we want. The selector is defined by a percentage value or the constants from (equivalent to 0%) and to (equivalent to 100%). Inside each selector, we define all the properties and values that we want at that point in the animation. We can also define the timing to use in every animation group using -webkit-transition-timing-function.


Warning:

When the animation finishes, the original values are restored. The elements will not maintain the last keyframe values after the animation stops.


For example, the following sample moves a div in a square path:

<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN"
"http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Fade Sample</title>
<style>

#box {
width: 200px;
height: 200px;
background-color: red;
position: absolute;
top: 0px;
left: 0px;
}

.squareAnimation {
-webkit-animation-name: squarePath;
-webkit-animation-duration: 4s;
-webkit-animation-timing-function: linear;
-webkit-animation-iteration-count: infinite;
}

@-webkit-keyframes squarePath {

/* We can use 0% or "from" as selector */
from {
top: 0px;
left: 0px;
}

25% {
top: 0px;
left: 100px;
}

50% {
top: 100px;
left: 100px;
}

75% {
top: 100px;
left: 0px;
}

/* We can use 100% or "to" as selector */
100% {
top: 0px;
left: 0px;
}

}

</style>
<script type="text/javascript">
function start() {
// When we apply the -webkit-animation attributes, the animation starts
document.getElementById("box").className = "squareAnimation";
}
</script>

</head>
<body onload="start()">

<h1>Moving over a square path</h1>
<div id="box">
</div>
</body>
</html>



Warning:

If we define the -webkit-animation attributes in the element from the beginning, the animation will begin when the page loads. The best solution is to define animations as classes and, when we want to start an animation, apply that class to the element.


So, to start the animation we apply the class, and if we want to stop it before it reaches the ending value we should assign an empty value to the -webkit-transform-name property.

We can define one animation that changes several properties, or use different animations with different names at the same time, each changing a single property.

6.2. Animation events

As with transitions, we can listen for the events webkitAnimationStart, webkitAnimationIteration, and webkitAnimationEnd. When fired, they will send a WebKitAnimationEvent object as a parameter. There is no event to capture each keyframe change.

The event object has the special properties animationName and elapsedTime, whose value is given in seconds.

Other  
  •  Windows Phone 7 Development : Debugging Application Exceptions (part 2) - Debugging a Web Service Exception
  •  Windows Phone 7 Development : Debugging Application Exceptions (part 1) - Debugging Page Load Exceptions
  •  Programming the Mobile Web : JavaScript Libraries
  •  Programming the Mobile Web : Ajax Support
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 5) - Deploying the Service to Windows Azure
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 4) - Coding NotepadViewModel
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 3) - Coding the BoolToVisibilityConvert
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 2) - Coding MainPage
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 1) - Building the User Interface
  •  Building Android Apps : Detecting Browsers with WURFL
  •  Building Android Apps : Submitting Your App to the Android Market - Preparing a Release Version of Your App
  •  Windows Phone 7 Development : Creating a Cloud Service to Access the Cloud Database (part 2) - Implementing a WCF Service to Access the SQL Azure Database
  •  Windows Phone 7 Development : Creating a Cloud Service to Access the Cloud Database (part 1) - Generating an Object Model to Access the Cloud Database
  •  Windows Phone 7 Development : Using Cloud Services As Data Stores - Creating a Cloud Database
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 5) - Implementing the View Controller Logic
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 4) - Hiding the Keyboard
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 3) - Creating Styled Buttons
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 2) - Adding Text Views
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 1) - Adding Text Fields
  •  Building Android Apps : Controlling the Phone with JavaScript (part 3) - Accelerometer
  •  
    Top 10
    Quiet PC Serenity Gamer - Take On All Challengers
    Roccat Kone Pure – Accurate & Comfortable
    Samsung 840 Pro Series SSD 512GB
    Scan 3XS Performance GTX - The Most Powerful Gaming PC
    Motorola MotoSmart - Low-Cost Handset
    Motorola RAZRi - Excellent UI Skin
    The Xperia T - Remarkable In How Unremarkable It Is (Part 2)
    The Xperia T - Remarkable In How Unremarkable It Is (Part 1)
    Magnepan DWM Woofer Review
    Livescribe Sky - Sync Your Handwritten Notes
    Most View
    Graphics Card Shootout - Budget FPS (Part 2)
    Excel Programmer : Fix Misteakes
    Adobe InDesign CS5 : Applying and Editing a Photoshop Clipping Path in InDesign, Creating and Editing an InDesign Clipping Path
    Upgrade Power - Guidelines For PSU Buyers (Part 2) - Antec HCP-850, Cooler Master Silent Pro Hybrid 1050W
    Install Windows Server 2008
    Microsoft & Apple rejoin the os battle
    SteelSeries Kinzu V2 - Reacquainting With The Kinzu
    Remove Internet Limits With A VPN (Part 4)
    Programming the iPhone : Standard Control Types (part 5) - Search Bars
    Network Programming with Windows Sockets : In-Process Servers
    BlackBerry Java Application Development : installing other JDE component packages over-the-air
    iphone SDK : Using the ABPersonViewController Class, Using the ABNewPersonViewController Class
    OKI MB461: Speed Is Its Middle Name
    Touchy Curve : BlackBerry Curve 9380
    Asus Eee Pad Transformer Prime
    The choices of mobile computing for SOHO users (part 1)
    Nikon Launched The D800 And D800E
    6 Ways To Program Your Life (Part 1)
    Norton 360 2013 - Is Three The Magic Number?
    Essential Camera Skills Crash Course (Part 3)