Activate your free membership today | Log-in

Saturday, September 29th, 2007

Canvas Loading Indicator for the iPhone and beyond

Category: iPhone, JavaScript, Library

<p>Adam van den Hoven wrote a Canvas Loading Indicator after he realised that animated gifs and the iPhone didn't mesh.

First he wrote the basic spinner:

JAVASCRIPT:
  1.  
  2. function getLoading(context, bars, center, innerRadius, size, color) {
  3. var animating = true,
  4.     currentOffset = 0;
  5.  
  6. function makeRGBA(){
  7.     return "rgba(" + [].slice.call(arguments, 0).join(",") + ")";
  8. }
  9. function drawBlock(ctx, barNo){
  10.     ctx.fillStyle = makeRGBA(color.red, color.green, color.blue, (bars+1-barNo)/(bars+1));
  11.     ctx.fillRect(-size.width/2, 0, size.width, size.height);
  12. }
  13. function calculateAngle(barNo){
  14.     return 2 * barNo * Math.PI / bars;
  15. }
  16. function calculatePosition(barNo){
  17.     angle = calculateAngle(barNo);
  18.     return {
  19.         y: (innerRadius * Math.cos(-angle)),
  20.         x: (innerRadius * Math.sin(-angle)),
  21.         angle: angle
  22.     };
  23. }
  24. function draw(ctx, offset) {
  25.     clearFrame(ctx);
  26.     ctx.save();
  27.     ctx.translate(center.x, center.y);
  28.     for(var i = 0; i<bars; i++){
  29.         var curbar = (offset+i) % bars,
  30.             pos = calculatePosition(curbar);
  31.         ctx.save();
  32.         ctx.translate(pos.x, pos.y);
  33.         ctx.rotate(pos.angle);
  34.         drawBlock(context, i);
  35.         ctx.restore();
  36.     }
  37.     ctx.restore();
  38. }
  39. function clearFrame(ctx) {
  40.     ctx.clearRect(0, 0, ctx.canvas.clientWidth, ctx.canvas.clientHeight);
  41. }
  42. function nextAnimation(){
  43.     if (!animating) {
  44.         return;
  45.     };
  46.     currentOffset = (currentOffset + 1) % bars;
  47.     draw(context, currentOffset);
  48.     setTimeout(nextAnimation, 50);
  49. }
  50. nextAnimation(0);
  51. return {
  52.     stop: function (){
  53.         animating = false;
  54.         clearFrame(context);
  55.     },
  56.     start: function (){
  57.         animating = true;
  58.         nextAnimation(0);
  59.     }
  60. };
  61. }
  62.  

And then to use it:

JAVASCRIPT:
  1.  
  2. var controller = getLoading(canvas.getContext("2d"), 9, {x:100, y:100}, 10, {width: 2, height:10}, {red: 0, green: 17, blue: 58});
  3.  

Related Content:

Posted by Dion Almaer at 8:30 am
7 Comments

+++--
3.2 rating from 24 votes

7 Comments »

Comments feed TrackBack URI

Adam, doesn’t see to have a demo currently, so for those who want immediate visual stimuli, I have thrown the example code up: Canvas Loading Example.

Pretty cool stuff, I didn’t know the iphone had Canvas support, opens up a whole new world of possibilities.

Comment by JP — September 29, 2007

I must be missing something here. A while back, I wrote a small app for the iPhone and it uses an animated GIF stolen from this page. Works fine on my iPhone…

Comment by Julien Lecomte — September 29, 2007

Good find.

Comment by Jordan — September 29, 2007

Canvas for displaying animated GIFs…!
Makes me think about the time I nuked a couple of insects in my backyard… ;)
Still impressive though :)

Comment by Thomas Hansen — September 30, 2007

Cool stuff.

Comment by DESIGNEXPANSE.COM — October 1, 2007

thanks good post

Comment by klip izle — November 11, 2007

Canvas is cool, but take a look at my approach using SVG animations. Works fine on every Webkit, including MobileSafari on the iPhone:
http://nickynubbel.blogspot.com/2010/06/coole-iphone-kompatible-loading.html, however this post is in German.
Use Google translater instead: http://translate.google.de/translate?u=http%3A%2F%2Fnickynubbel.blogspot.com%2F2010%2F06%2Fcoole-iphone-kompatible-loading.html&sl=de&tl=en&hl=&ie=UTF-8

Comment by nubbel — June 5, 2010

Leave a comment

You must be logged in to post a comment.