An Elegant App - Part 2

by
Bryan Maynard, Software Engineer
Object Computing, Inc. (OCI)

Introduction

This is the second installment in a four part series on building an elegant application for the web. In the previous article we looked at designing our application's user interface for mobile devices first and scaling up to larger, more capable devices. In this article we will be looking at how CSS Animations can augment our application's user interface, making our application more pleasant to use and intuitive.

Why do we need animations?

In our profession we spend a lot of time with software. This makes it easy to lose sight of how complex and bewildering our application interfaces can be to users who haven't spent as much time with them as we have. Building interfaces the average person can understand and intuit requires looking beyond our familiarities.

Let's forget software for just a minute and talk about the real world. In the real world, do things appear out of thin air? How would you respond if you paid for a Clif Bar and it moved from the checkout lane to your pocket quicker than you could blink your eyes? If you pushed a door to walk into Starbuck's and the door disappeared, what would you do?

While software applications are not physical objects, our application's user interfaces do need to follow some of the real world's conventions. In the real world things have state transition gradually instead of switching instantly. For a Clif bar to make it to my pocket, it must first be moved from the register. When I push on a door to enter a Starbuck's, that door swings open.

The more time a user spends dealing with real world objects, the more they expect our user interfaces to follow the conventions of real world objects. They will expect forward progress through a set of steps to be conveyed via right-to-left visual motions. They will expect important content to be revealed, not merely shown.

In the real world everything comes from somewhere. To help our users maintain the association between different parts of our application, we must provide visual clues in our interface as to where each part of our application comes from and how those parts relate to each other.

For example, sliding content up from the bottom of the page would indicate that the new content is more specific, or more narrowly focused, than the content that preceded it. In a multi-step wizard, sliding the screen right-to-left as the user progresses indicates forward progress.

What are CSS animations?

CSS Animations are a portion of the W3C's CSS3 specification that govern gradually changing an element's style over time from one value to another.

CSS Animations are defined by specifying at least two keyframes:

@keyframes my-first {
0% { background: red; }
100% { background: yellow; }
}

The animation my-first would then be bound to div elements as follows:

div {
animation: my-first 5s;
}

This rule specifies that the my-first animation should take 5 seconds to complete. Since we have not provided any classes or attribute values to match against, this CSS Animation will begin executing on a div as soon as it is added to the document - including the initial page load.

You may specify more than two keyframes. If you do, each keyframe must be assigned a point in the animation:

@keyframes my-second {
0% { background: red; }
25% { background: yellow; }
50% { background: blue; }
100% { background: green; }
}

This animation defines four distinct stages and may be bound to an element in the same way as the first example.

When are animations appropriate?

CSS Animations allow elements of an application's ui to "come from" somewhere, giving them purpose and context. They provide visual links between pieces of our application.

A CSS Animation is appropriate to the extent that it satisfies one or more of the following criteria:

CSS Animations do not need to be long or drawn out. A fraction of a second is often enough to do what is needed. We do not want to slow our user down by adding animations to our interface.

Snappy, sensible animations make our user interface feel more approachable, understandable, and polished. Slow, overblown, flashy animations frustrate users and make our interface feel gaudy and cheap.

This article will show how AiOIM's CSS Animations for the user list, chat session window, and user messages are defined.

A non-standard standard

One thing needs to be noted about CSS Animations before we proceed: they are currently implemented via vendor prefixes. This means that to provide support for all modern browsers (Safari, Google Chrome, IE, Opera, etc) CSS files containing animations become rather large. To support the widest range of users, our first example animation would need to be written as follows:

// Official W3C syntax
@keyframes my-first {
0% { background: red; }
100% { background: yellow; }
}
// For WebKit (Google Chrome, Safari, etc)
@-webkit-keyframes my-first {
0% { background: red; }
100% { background: yellow; }
}
// For Firefox
@-moz-keyframes my-first {
0% { background: red; }
100% { background: yellow; }
}
// For IE
@-ms-keyframes my-first {
0% { background: red; }
100% { background: yellow; }
}
// For Opera
@-o-keyframes my-first {
0% { background: red; }
100% { background: yellow; }
}

Tools like Stylus can minimize the need for repetitious typing, but nothing except careful use of animations can mitigate the danger of bloated CSS files that take a long time to load.

In my experience, sensible use of simple animations does not cause any noticeable impact of page load time. This does, however, present a powerful reason to stay away from needless, overly flashy CSS Animations.

In this article, to save space, I will show only the official W3C syntax for animations.

User list

The user list animation is defined as follows:

@keyframes slide_down-fade_in {
0% {
top: 2em;
opacity: 0;
box-shadow; 0 0 0 black;
}
100% {
top: 2.5em;
opacity: 1;
box-shadow: 0 3px 15px black;
}
}
@keyframes slide_up-fade_out {
0% {
top: 2.5em;
opacity: 1;
box-shadow: 0 3px 15px black;
}
100% {
top: 2em;
opacity: 0;
box-shadow: 0 0 0 black;
}
}

Sliding down as the user list appears communicates that the list "came from" the search box. This strengthens the connection between these two UI elements for the use.

Also keep in mind that our user does not know exactly what to expect when they start typing in AiOIM's user search field. Flashing a user list into view as a result of their typing would be surprising. Fading the list into view gives the user time to realize what's happening and relate the user list to the text they entered.

New messages

The animations for messages are:

@keyframes slide_in-self-message {
0% {
opacity: 0;
right: -19em;
}
100% {
opacity: 1;
right: 0em;
}
}
@keyframes slide_in-other-message {
0% {
opacity: 0;
left: -19em;
}
100% {
opacity: 1;
left: 0em;
}
}

This define two animations: one that fades a message in as it slides from left to right (a message from someone other that our user) and one that fades a message in as it slides from right to left (a message from our user).

As people, we understand that conversations between two people are bi-directional. Films translate this idea to a two-dimensional space by assigning conversation participants areas, or regions, of the screen. This helps viewers quickly identify who is who as the camera changes focus from person to person. In a conversation with two participants, one is assigned the left side of the screen and one the right.

AiOIM already has a slight notion of this convention. Messages we send are indented slightly so they appear on the "right side" of the chat window compared to messages sent from others - which appear to be more on the "left side".

To follow the film convention for conversations even more closely, we will apply the animations created above to new messages. Once these animations are applied, messages from others will slide into view from the left - effectively placing other user on the left side of our chat screen. Messages from us will slide in from the right - effectively placing us on the right side of our chat screen.

Chat session window

The animations for the chat session window are:

@keyframes slide_down-fade_in_messages {
0% {
margin-top: 0;
opacity: 0;
box-shadow:
inset 0 2px 5px rgba(0,0,0, .5),
0 0 0 rgba(0,0,0, .7);
}
50% {
margin-top: 0;
opacity: 0;
box-shadow:
inset 0 2px 5px rgba(0,0,0, .5),
0 0 0 rgba(0,0,0, .7);
}
100% {
top: .5em;
opacity: 1;
box-shadow:
inset 0 2px 5px rgba(0,0,0, .5),
0 5px 10px rgba(0,0,0, .7);
}
}
@keyframes slide_in-user_name {
0% {
margin-left: -.1em;
opacity: 0;
text-shadow:
0 0 0 transparent,
0 0 0 transparent;
}
100% {
margin-left: .4em;
opacity: 1;
text-shadow: 0 -1px #FFD8A6, 0 1px 0 #A66B1F;
}
}

To support the relationship between the user search box and a chat session visually we make sure that selected user's name slides into view before the chat window appears. By making it appear that the user's name "comes from" the search box instead of the user list we are communicating to our user that the search box, not the user list, is how they find users to chat with.

Additionally, by waiting until after the user's name has faded in to show the chat window we communicate that the chat "comes from" the user we found. We are also using the "slide down" animation consistently.

Staggered animations

The chat window animations introduce an interesting technique for managing animations across multiple elements: staggered animations.

A staggered animation has one main element, and one or more dependent animations. The main animation in our choreography is slide_in-user_name. The dependent animation is slide_down-fade_in_messages. We do not want the chat window to begin fading into view or sliding down until the user's name has faded into view. To achieve this, we assign the animations as follows:

#aioim .chatting.with li.active .user.name {
animation slide_in-user_name .33s
}
#aioim .chatting.with li.active .messages {
animation slide_down-fade_in_messages .66s
}

This means that when a conversation (represented by an li HTML element) has the active class applied both the slide_in-user_name and slide_down-fade_in_messages will begin executing simultaneously. When the slide_in-user_name has completed the slide_down-fade_in_messages animation will at 50%.

A staggered animation may have more than one dependent animation, and dependency chains may be setup if needed.

In conclusion

CSS Animations are a powerful tool. By following a few simple guidelines we can use CSS Animations to make our user interfaces more intuitive, less intimidating, and more attractive.

To see the results of the code shown above, please go to aioim.bryanmaynard.com


Valid XHTML 1.0 Strict [Valid RSS]
RSS
Top