16.1. One-Second SpotlightAttention, Effect, EyeCandy, Fade, Fading, Graphics, Refresh, Update, YellowFadeTechnique Figure 16-1. One-Second Spotlight16.1.1. Goal StoryTracy is checking out her portfolio, which shows each stock in its own row. Suddenly, "Neverland Mining Co." stock plummets, and its row lights up to reflect the change. Tracy's attention is drawn instantly to this visual effect, and she immediately begins to offload her stake in the company. 16.1.2. ProblemHow can you direct the user's attention to spots on the page? 16.1.3. Forces
16.1.4. SolutionWhen a display element undergoes a value changeor any other significant changedynamically increase its brightness for about a second. As a device for communicating system activity, the One-Second Spotlight accomplishes several things:
This pattern is a generalization of the technique 37signals introduced as the Yellow Fade Technique (http://www.37signals.com/svn/archives/000558.php). With that, an item lights up for a second after the user has edited it, and the lighting quickly fades away. Unlike a conventional application, in which a page refresh would occur, it's often unclear when an XMLHttpRequest Call has occurred. So a quick visual effect is almost essential for the user to understand what's happening. From a technical perspective, the Yellow Fade Technique is usually achieved by suddenly switching the element's background-color style from white to yellow, then using Scheduling to run through a continuous spectrum of background colors, starting with yellow and ending with white. In more general terms, One-Second Spotlight encompasses several types of animation. One or more of the following CSS styles are affected during the spotlight period:
The possibilities are endless, but there are a few common effects. Firstly, there are straightforward transitions from one style setting to another:
The other category of effects involves rapid oscillation and includes the following:
One typical implementation combines Display Morphing with Scheduling. To fade an item out, for instance, the following algorithm may be used:
The algorithm is based on interpolation. In moving from color A to color B, there are a number of discrete transitions that occur, say, once every 100 milliseconds. At each stage, the algorithm decides where on the spectrum from A to B the color should be. Since a color consists of three componentsR, G, and Bthe calculation occurs separately for each component. So, let's say you're shifting from #ffffff to #000044. First, it's useful to represent these as decimal percentage settings: (100%, 100%, 100%) moving to (0%, 0%, 25%). And assume we're going to make 10 transitions. The red and green components will shift down 10 percent each step, and the blue component will shift down 7.5 percent each time. So it's just a matter of dropping the value for each color component at each transition, rounding off, and redisplaying the element. If you want to make a transition to the background of the element, you can shift opacity instead of color. (Be aware that there are portability issues with opacity [http://www.quirksmode.org/css/opacity.html].) The algorithm assumes a linear interpolation, where each transition drops by the same amount. You could also experiment with other transitions. An exponentially increasing interval, for example, would give an effect of an initially slow transition, leading to a quick burst at the end. A similar phenomenon is quite common with flash effects, where the flashing starts off slowly and speeds up at the end. You probably won't have to hand code many of these effects because libraries like Scriptaculous (http://script.aculo.us; discussed below) are quite flexible and easy to incorporate. 16.1.5. Decisions16.1.5.1. What events will trigger a spotlight?An Ajax App undergoes multiple events; which warrant a spotlight? A spotlight can serve any of the following purposes:
Here are some examples:
16.1.5.2. What color will be used?Yellow, popularized by 37signals, is becoming something of a standard for fading effects. In general, though, the color has to take into consideration which element is affected, as well as surrounding elements and the page appearance in general. Additionally, multiple colors can be used to good effect. For instance, each color can be used to represent a different type of event. In a multiuser context, JotSpot Live (http://jotlive.com) illustrates how each user can be associated with a different color so that their recent edits appear as distinctly-colored fade effects. 16.1.5.3. How long should the spotlight last? How long will each transition be?Asking how long the spotlight should last might seem a bit ridiculous for a pattern named "One-Second Spotlight." But the name, of course, only hints at one reasonable value. You'll have to decide on the precise duration of the overall effect and also on how many transitions will take place in that time. Here are some considerations:
As far as transitions go, the trade-off is fairly clear: faster transitions look smoother but slow down the application. Is the user likely to leave your application in a background tab and only check it once an hour? If so, avoid slowing down the whole browser with high-granularity effects. Likewise if the application is already process-intensive. This is an area where different browsers will act differently. For instance, what's the lowest possible transition time you can set? That will depend on the user's system and the browser in question. You can experiment with different parameters on the Time Spotlight Demo (http://ajaxify.com/run/time/periodicRefresh/spotlightCustom/). 16.1.6. Real-World Examples16.1.6.1. 37signals Backpack37signals Backpack (http://www.backpackit.com/) maintains items in a list (Figure 16-2). When you click the Edit button, the read-only item morphs into a textarea. On saving, it becomes read-only again and is accompanied by a yellow Fade Out effect. All quite fitting for the company that coined the term, "Yellow Fade Technique." Figure 16-2. Backpack16.1.6.2. chat.appchat.app (http://gamma.nic.fi/~jmp/chat/app.html) shows text messages as you or others enter them. Each new message is highlighted with a yellow Fade Out. 16.1.6.3. Digg SpyDigg Spy (http://digg.com/spy) shows new stories as they appear on the Web. Each time a block of new stories appears, it appears with a Fade Out effectthe background shifting from grey to white. 16.1.6.4. ColoirColoir (http://www.couloir.org) is a slideshow application. Each time you click next, an animated "loading" icon appears and a new photo fades in. 16.1.6.5. OdeoOdeo (http://odeo.com) is a podcast subscription service. You can force Odeo to grab the latest feed for a podcast; during the delay, the entire document fades. An effect like this can be achieved with a partially transparent Popup element covering the whole document. It feels similar to the way Windows fades the entire screen when you're about to log out. 16.1.6.6. Scriptaculous libraryScriptaculous (http://script.aculo.us) is an excellent JavaScript library supporting many Ajax UI patterns. Its visual effects library is based on four fundamental effects:
Combining these leads to any number of animation sequences, and many of the useful sequences are prepackaged. The visual effects demo (http://script.aculo.us/visual-effects) provides many spotlight effects. 16.1.6.7. Fade Anything Technique (FAT) libraryFAT (http://www.axentric.com/posts/default/7) is a library allowing easy scripting of a transition from one color to another. 16.1.7. Code Refactoring: AjaxPatterns Spotlight TimeThe Time Periodic Refresh demo (http://www.ajaxpatterns.org/run/time/periodicRefresh/) shows the server time, which is updated every few seconds. It's not clear exactly when the time has changed, however, so this refactoring adds a spotlight effect each time a new result arrives from the server. There are actually a couple of refactorings here: one is hand-built and the other illustrates reuse of the Scriptaculous library. 16.1.7.1. Hand-built shift effectThe Time Spotlight Demo (http://www.ajaxpatterns.org/run/time/periodicRefresh/spotlightCustom) lets the user set a few parameters and applies the corresponding effect (Figure 16-3). A fader object encapsulates the fading logic. The fade duration and transition interval are modifiable attributes: Figure 16-3. Time Spotlight Demofader.durationTime = parseInt($("fadeDuration").value); fader.transitionInterval = parseInt($("transitionInterval").value); The application asks fader to fade the div as soon as it's been modified: function showCustomTime(text) { var customTimeLabel = $("customTimeLabel"); customTimeLabel.innerHTML = text + "." + callingContext; fader.fade(customTimeLabel, $("startColor").value, $("endColor").value); } fader then kicks off the first transition, passing in a timeSoFar parameter of zero. The function is simpler using an array of percentage color values, but hex is more common, so a simple conversion takes place: fade: function(element, startRGB, endRGB) { this.nextTransition( element, this.rgbToPercents(startRGB), this.rgbToPercents(endRGB),0); } Each transition involves deciding how far along the fade it is and calculating the corresponding color style using a linear interpolation (as explained in the Solution for this pattern): nextTransition: function(element, startColor, endColor, timeSoFar) { ... var currentColor = "rgb("; for (component=0; component<3; component++) { var currentComponent = Math.round(startColor[component] + proportionSoFar * (endColor[component] - startColor[component])); currentColor+=currentComponent + "%" + (component<2 ? "," : ")"); } element.style.backgroundColor = currentColor; ... } Then it increments timeSoFar and finishes the fade if fade time has expired: timeSoFar+=this.transitionInterval; if (timeSoFar>=this.durationTime) { this.durationTime+ "\n"; element.style.backgroundColor = "rgb("+endColor[0]+"%,"+endColor[1]+"%,"+endColor[2]+"%)"; return; } var nextCall = function( ) { fader.nextTransition(element, startColor, endColor, timeSoFar); } setTimeout(nextCall, this.transitionInterval); fader is coded for simplicity, and it is possible to make a couple of improvements. First, the repeated color calculation would be more effective if it worked on the pretransition delta instead of calculating the absolute color each time. Also, the timing could be adjusted to take into account the time taken to run the function itself. 16.1.7.2. Fading with ScriptaculousThe Time Scriptaculous Demo (http://www.ajaxpatterns.org/run/time/periodicRefresh/spotlightScriptaculous) shows how easy it is to reuse an existing effects library. Here, the time displays show a Materialize effect when a new result appears. They transition from completely invisible (showing only the document background color) to the appearance determined by their class's style. The refactoring is trivial. Include the Scriptaculous effects library, effects.js, and prototype.js, a general-purpose JavaScript utility used by Scriptaculous: <script type="text/javascript" src="/books/2/755/1/html/2/prototype.js"></script> <script type="text/javascript" src="/books/2/755/1/html/2/effects.js"></script> Then, as with the hand-built fader presented earlier, invoking the effect is a one-liner: new Effect.Appear("defaultTimeLabel"); 16.1.8. Alternatives16.1.8.1. One-Second Mutation and One-Second MotionOne-Second Mutation and One-Second Motion (see both later in this chapter) are also used to draw attention to a significant event. One-Second Spotlight can be used as both a general attention-getting device as well as to hint at a specific type of event, whereas those two patterns tend to be slightly more about depicting specific events. The effects can also be combined, e.g., shrinking an object as it fades away. 16.1.9. Related Patterns16.1.9.1. Periodic RefreshA change arising from a Periodic Refresh (Chapter 10) is often marked with aOne-Second Spotlight. 16.1.9.2. TimeoutA Timeout (Chapter 17) event, and any prior warnings, can be marked with a One-Second Spotlight. In community sites that show details about other users, the effect can also be used to mark changes to users' online status. 16.1.10. MetaphorThe pattern's name is the key here: a spotlight can be used to direct audience attention. 16.1.11. Acknowledgments37signals introduced the Yellow Fade Technique into its products, and its Yellow Fade Technique article (http://www.37signals.com/svn/archives/000558.php) helped spread the meme. |