Animating your iPhone web application

May 29, 2009, 6:18 pm · 5 comments · Filed under: Apple, JavaScript, Web Development

Recently, Rebecca Murphey asked on Twitter why she was seeing choppy animation of CSS transitions in her iPhone web application. Initially, I had the same experience, but through some experimentation came to find the smoothness I craved.

andrew.hedges.name



Can your web browser do this?

You’ll never get rich digging a ditch, nor building Dashboard widgets.

A Kryptonite™ lock can be defeated in 11 seconds, but you still lock your bike, right?

Gaining Twitter followers is a little like losing weight. You have to try.

Over or under? It’s the age-old question when it comes to the orientation of toilet paper rolls.

Subscribe


Meta Me

I am a web developer, recently returned to the States after 3 years in New Zealand. I’m into my family, photography and frisbee sports.

Blip.fm Digg Facebook LinkedIn Stack Overflow Twitter Zooomr

Nothing will benefit human health and increase chances for survival of life on earth as much as the evolution to a vegetarian diet.
Albert Einstein


Topics

Apple · AppleScript · Business · Coda · CSS · Dashboard · Design · Google · InSTEDD · JavaScript · jQuery · Life · Marketing · Music · New Mexico · New Zealand · Open Source Software · Photography · PHP · Politics · Ruby on Rails · Scree · Subversion (SVN) · Twitter · Usability · Web Development · Widgets


Archives


Most Popular

CSS Fast Nav: Because (perception of) speed matters! · Personal Branding for Introverts · Stupid WebKit Tricks · Add an interactive legend to a MarkerManager managed Google Map · Dude. Mikeyy can’t even spell his own name. · Dashboard Widgets for Fun and Profit · Animating your iPhone web application · How-to recover from checksum mismatch errors in SVN · Why Apple can afford to charge so little for Snow Leopard · When is a global variable not a variable?


Most Recent

CSS Fast Nav: Because (perception of) speed matters! · When is a global variable not a variable? · Our misguided culture of cool · InSTEDD: Open Source Software that saves lives · Add an interactive legend to a MarkerManager managed Google Map · Personal Branding for Introverts · Moments of Rangitoto · Some Twitter conventions · Why Apple can afford to charge so little for Snow Leopard · Stupid WebKit Tricks


Twitshirt

Twitshirt is a tweet on a shirt. Buy the one below or check out my most recent tweets.

Love. Hope. Peace. Thanks.

See a random Twitshirt-worthy tweet.


Friends

80/20 · 90 Seven Design · Alyson Hurt · Andrew Nimick · Apps & Hats · Ben Young · Brian Arnold · Brian Warren · Carl Bolter · Chris Burgess · Christine Morris · Cristina Stoian · Daniel Lyons · Daniel Schwartz · David Hedges · Hamish Campbell · Jochen Daum · John Visser · Joseph McLaughlin · Joshua Sallach · Julian Pistorius · Justine Sanderson · Kalena Jordan · Katie Graham · Kelly Green · Kevin Potis · Mark Bixby · Matt Henry · Method Arts · Morgan Pyne · Peter Michaux · Philip Tellis · Piers Harding · Rebecca Murphey · Reid Givens · Rey Bango · Rhett Anderson · Richard Paul · Rob Pongsajapan · Robin Taylor · Ryan Park · Shaun Lee · Simon Young · Su Yin Khoo · Toni Barrett · Vaughan Rowsell · Vincent Thomé · Voom Studio


Recommended Books on
Web Development

My bias is for references over “cookbooks.” I want to know all of my options, not just one way to do something. Show me the why as well as the how and I am happy.

JavaScript: The Good Parts · Object-Oriented JavaScript: Create scalable, reusable high-quality JavaScript applications and libraries · JavaScript: The Definitive Guide · Designing with Web Standards · CSS: The Definitive Guide · Prioritizing Web Usability · The Elements of User Experience · Web ReDesign: Workflow that Works · Don't Make Me Think: A Common Sense Approach to Web Usability


Contact Info

Contact info for Andrew Hedges


I’ve hosted this website with pair Networks since 1997. They rock.

This blog is powered by…software I wrote.

Feeling generous? Knock yourself out!

I’m in the midst of some mobile web app development. Our first target platform is, of course, the iPhone. An issue of utmost importance when attempting to emulate a native application in an iPhone web view is rendering smooth animations for things like page transitions.

I spent today experimenting with different techniques and am here to share what I’ve learned. Below, I review 2 JavaScript-based techniques for animations as well as the use of WebKit CSS transitions and transforms. Which is best? Read on…

All of the animations use a duration of 250ms. Click the viewports (touch on your iPhone) to see the animations. Right-click on a viewport and select ‘View source’ to see how it’s done. If the animations don’t go, follow the link beside the viewport to see the stand-alone page.


The viewport to the left represents my custom attempt to provide a smooth animation experience. It combines a time-based, strictly linear animation algorithm with a small delay between invocations in an effort not to stress the wee iPhone CPU. It’s OK, but the lack of easing looks unfinished.

Stand-alone page


This example uses the excellent Animator class to handle the animation. It includes an easing algorithm that makes the animation look a little nicer. Not bad, but it doesn’t quite feel like a native application.

Stand-alone page


This example leverages WebKit’s native CSS transitions. Of the 3, this is the simplest to code and it also offers the best performance. The pages seem to ‘snap’ into place, unlike the other examples.

Stand-alone page

Honestly, I’m not sure what was vexing the examples Rebecca and I were looking at earlier. It could be that my first experiment with CSS transitions was trying to do too much (moving 4 divs while changing the opacity on 2 of them). The above examples are extremely simple cases, so they’re bound to perform better.

It stands to reason that native code would perform better than interpreted JavaScript code. Based on my first experiment, I rendered a verdict of unusable 4 now. After digging a little deeper, I stand corrected.

Update: CSS Transformations


Based on comments below by Sean Gilligan, developer of iUI, I have added a fourth example. This one uses -webkit-transform: translateX to achieve hardware acceleration on the iPhone. I’ve also slowed down the animations to 250ms to accentuate the differences between the various techniques.

I’m not sure it’s better, at least on my phone. What do you think?

Stand-alone page


Short URL to this article:



5 comments

For HW acceleration on iPhone you want to use:

-webkit-transform: translateX(-100%);

not:

left: -100%

and you’ll likely need translateX(0%); in your default style. There are also gotchas involving whether the browser can determine the width of the block.

Ah, interesting. I was just discussing with the client whether it made sense to just go with an existing framework (like yours, iUI) so we don’t have to go through the pain of discovering all of these tricks ourselves.

Are CSS animations integrated into iUI yet? The live apps I’ve looked at (from the PoweredBy wiki page) seem to have somewhat rough animations.

Smooth animation is not released yet - it is in Subversion (revision control) trunk and has been tested by a handful of developers on a handful of apps. You can run the music sample in the trunk to check it out.

The PoweredBy wiki page is pretty old - not updated in almost 2 years - probably not the best example of iUI apps.

p.s. I like the Markdown editor’s handling of links!

Always cool to learn what other are willing to share when it comes to the mobile web I am just now getting into learning how to create iPhone apps and look forward to utilizing what I have learned from you here.

@Sean: I tried the animations from the iui trunk on my 3GS, but it doesn’t feel right. The initial delay (before the animation starts) is too long and the animation itself is too fast (compared to native animations). I didn’t check on an older iPhone, so I can’t say, if this is related to the changed CPU-speed of the 3GS, but anyway…

Comments close automatically after 90 days.
Still have something to say? Drop me a line!

Possibly related posts