Wednesday, September 22, 2010

Loading spinner animation using CSS and WebKit

It’s hard to stifle a smile any time I get a glimpse of the future thanks to WebKit-based browsers like Safari and Chrome (and their mobile counterparts on iOS and Android devices). That happened today when I discovered a way to make an iPhone-style spinner without any images, just CSS.
First attempt

I have been working on a project that targets mobile WebKit browsers so it was natural to explore using -webkit-animation to handle the image rotation. My initial idea was to use a single PNG image as a mask over a colored background which makes it possible to change the color of the spinner in code. Here’s how that looked in CSS:

p#spinner {
height: 62px;
width: 62px;
overflow: hidden;
background: #000;
-webkit-mask-image: url("data:image/png[...]");
-webkit-mask-size: 62px 62px;
-webkit-animation-name: rotate;
-webkit-animation-duration: 1.5s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
}

This is how it looks in a browser: Image mask rotation demo

The examples in this post all target WebKit browsers so I’d suggest you fire up Chrome or Safari for viewing the demos. Better yet, try a mobile WebKit browser.

It felt like a win to use a single-frame PNG image instead of an animated GIF but it didn’t look right. Webkit animations smoothly tween rotations so I ended up with what looked like a rotating image instead of the sharp ticking I was going for. Still, masking is an impressive technique that I’m sure will come in handy in another project.
CSS-only, no images

After several failed attempts to make the animation step-based, I still couldn’t get the effect right. I decided to look again into how I could do it without the image. A quick search led to a post by Kilian Valkhof who had a good idea for the basic technique. The first step is to create the bars of the animation by sizing and rotating them around an axis.





[...]



Each of the twelve bars is rotated 30° from the one previous and given an animation delay equal to 1/12 of a second. This ensures that the bars highlight, then fade in order. All that is left is to define and call the animation for each bar to fade.

@-webkit-keyframes fade {
from {opacity: 1;}
to {opacity: 0.25;}
}

div.spinner div {
[...]
-webkit-animation: fade 1s linear infinite;
}



Related Link's

http://www.classicclashes.co.za/Community/members/Vivina2354/default.aspx
http://beyondrelational.com/members/Vivina2354/default.aspx
http://ronua.ro/CS/members/Vivina2354/default.aspx
https://www.bisque.com/sc/members/Vivina2354/default.aspx
http://gameinformer.com/members/Vivina2354/default.aspx?
http://www.summsoft.com/members/Vivina2354/default.aspx
http://scug.dk/members/Vivina2354/default.aspx
http://www.travelweekly.co.uk/travelhub/members/Vivina2354/default.aspx
http://www.crochetme.com/members/Vivina2354/default.aspx
http://www.knittingdaily.com/members/Vivina2354/default.aspx
http://www.lexisnexis.com/COMMUNITY/DOWNLOADANDSUPPORTCENTER/members/Vivina2354/default.aspx
http://3isoft.com/members/Vivina2354/default.aspx
http://community.infragistics.com/members/Vivina2354/default.aspx
http://forums.gamesforwindows.com/members/Vivina2354/default.aspx
http://forums.techsoup.org/cs/members/Vivina2354/default.aspx
http://hollywoodharvradio.com/members/Vivina2354/default.aspx
http://juliocasal.com/members/Vivina2354/default.aspx
http://technetbrunei.net/members/Vivina2354/default.aspx
http://thwack.com/members/Vivina2354/default.aspx
http://www.possibilityspace.com/members/Vivina2354/default.aspx
http://community.nachoordinary.com/members/Vivina2354/default.aspx
http://www.classicclashes.co.za/Community/members/vinak2354/default.aspx
http://beyondrelational.com/members/vinak2354/default.aspx
http://ronua.ro/CS/members/vinak2354/default.aspx
https://www.bisque.com/sc/members/vinak2354/default.aspx
http://gameinformer.com/members/vinak2354/default.aspx?
http://www.summsoft.com/members/vinak2354/default.aspx
http://scug.dk/members/vinak2354/default.aspx
http://www.travelweekly.co.uk/travelhub/members/vinak2354/default.aspx
http://www.crochetme.com/members/vinak2354/default.aspx
http://www.knittingdaily.com/members/vinak2354/default.aspx
http://www.lexisnexis.com/COMMUNITY/DOWNLOADANDSUPPORTCENTER/members/vinak2354/default.aspx
http://3isoft.com/members/vinak2354/default.aspx
http://community.infragistics.com/members/vinak2354/default.aspx
http://forums.gamesforwindows.com/members/vinak2354/default.aspx
http://forums.techsoup.org/cs/members/vinak2354/default.aspx
http://hollywoodharvradio.com/members/vinak2354/default.aspx
http://juliocasal.com/members/vinak2354/default.aspx
http://technetbrunei.net/members/vinak2354/default.aspx
http://thwack.com/members/vinak2354/default.aspx
http://www.possibilityspace.com/members/vinak2354/default.aspx
http://community.nachoordinary.com/members/vinak2354/default.aspx