html5 loading 效果来了

Html5在移动设备上表现抢眼,几乎所有稍微高端一点的设备(乔帮主的iPad,iPhone和Andriod的平板手机等)的浏览器都支持Html5。而且据我个人的测试这些支持html5的设备对canvas标签的支持是相当的好。
       大家都知道web2.0以来大量的使用ajax,loading的小图标也有很多很多种,甚至还有专门提供loading图片的网站。所以我就想能不能让html5解决一下这个以前用gif文件解决的问题。没想到非常的简单,只用了不到一小时的时间就搞定了两个,而且这样做出来的loading图标是可定制的,既可以定制颜色,也可以定制大小等属性。
看看效果吧:
http://f200-8.bbs.hexun.com/e/111219/loading.htm
http://f200-8.bbs.hexun.com/e/111219/loading2.htm
       第一个带着小尾巴转动的loading图标画图的思路是,首先画一个圆,然后在圆的边上按顺序画大小逐渐减小的小圆点,在每次刷新画布时改变这一系列的小圆点在大圆边上的位置。

Html代码
  1. <!doctype html>  
  2. <html>  
  3.   <head>  
  4.     <meta http-equiv="content-type" content="GBK"/>  
  5.     <title>loading</title>  
  6.     <script type="text/javascript">  
  7.        
  8.     function loading(canvas,options){   
  9.       this.canvas = canvas;   
  10.       if(options){   
  11.         this.radius = options.radius||12;   
  12.         this.circleLineWidth = options.circleLineWidth||4;   
  13.         this.circleColor = options.circleColor||'lightgray';   
  14.         this.dotColor = options.dotColor||'gray';   
  15.       }else{         
  16.         this.radius = 12;   
  17.         this.circelLineWidth = 4;   
  18.         this.circleColor = 'lightgray';   
  19.         this.dotColor = 'gray';   
  20.       }   
  21.     }   
  22.     loading.prototype = {   
  23.       show:function (){   
  24.         var canvas = this.canvas;   
  25.         if(!canvas.getContext)return;   
  26.         if(canvas.__loading)return;   
  27.         canvas.__loading = this;   
  28.         var ctx = canvas.getContext('2d');   
  29.         var radius = this.radius;         
  30.         var rotators = [{angle:0,radius:1.5},{angle:3/radius,radius:2},{angle:7/radius,radius:2.5},{angle:12/radius,radius:3}];         
  31.         var me = this;   
  32.         canvas.loadingInterval = setInterval(function(){   
  33.           ctx.clearRect(0,0,canvas.width,canvas.height);            
  34.           var lineWidth = me.circleLineWidth;   
  35.           var center = {x:canvas.width/2 - radius,y:canvas.height/2-radius};             
  36.           ctx.beginPath();   
  37.           ctx.lineWidth = lineWidth;   
  38.           ctx.strokeStyle = me.circleColor;   
  39.           ctx.arc(center.x,center.y,radius,0,Math.PI*2);   
  40.           ctx.closePath();   
  41.           ctx.stroke();   
  42.           for(var i=0;i<rotators.length;i++){           
  43.             var rotatorAngle = rotators[i].currentAngle||rotators[i].angle;               
  44.             //在圆圈上面画小圆   
  45.             var rotatorCenter = {x:center.x-(radius)*Math.cos(rotatorAngle) ,y:center.y-(radius)*Math.sin(rotatorAngle)};               
  46.             var rotatorRadius = rotators[i].radius;   
  47.             ctx.beginPath();   
  48.             ctx.fillStyle = me.dotColor;   
  49.             ctx.arc(rotatorCenter.x,rotatorCenter.y,rotatorRadius,0,Math.PI*2);   
  50.             ctx.closePath();   
  51.             ctx.fill();   
  52.             rotators[i].currentAngle = rotatorAngle+4/radius;   
  53.           }   
  54.         },50);   
  55.       },   
  56.       hide:function(){   
  57.         var canvas = this.canvas;   
  58.         canvas.__loading = false;   
  59.         if(canvas.loadingInterval){   
  60.           window.clearInterval(canvas.loadingInterval);   
  61.         }   
  62.         var ctx = canvas.getContext('2d');   
  63.         if(ctx)ctx.clearRect(0,0,canvas.width,canvas.height);   
  64.       }   
  65.     };   
  66.         
  67.     </script>  
  68.   </head>  
  69.   <body>  
  70.     <canvas id="canvas" width="300" height="100" style="border:1px solid #69c"></canvas>  
  71.     <p>  
  72.     <input type="button" onclick="loadingObj.hide()" value="HideLoading"/>  
  73.     <input type="button" onclick="loadingObj.show()" value="showLoading"/>  
  74.     </p>  
  75.     <script>  
  76.     var loadingObj = new loading(document.getElementByIdx_x('canvas'),{radius:8,circleLineWidth:3});   
  77.     loadingObj.show();   
  78.     </script>  
  79.   </body>  
  80. </html>  

       第二个较为简单,在一个圆环上有一个相同圆心相同半径的圆弧在不停的转动。画图的步骤是首先画一个圆环,然后画一个不同颜色相同圆心半径的圆弧,在每次刷新画布时改变圆弧的起始角度。

Html代码
  1. <!doctype html>  
  2. <html>  
  3. <head>  
  4.   <meta http-equiv="content-type" content="text/html;charset=gbk"/>  
  5.   <title>loading</title>  
  6.   <script>  
  7.       
  8.     function loading(canvas,options){   
  9.       this.canvas = canvas;   
  10.       if(options){   
  11.         this.radius = options.radius||12;   
  12.         this.circleLineWidth = options.circleLineWidth||4;   
  13.         this.circleColor = options.circleColor||'lightgray';   
  14.         this.moveArcColor = options.moveArcColor||'gray';   
  15.       }else{         
  16.         this.radius = 12;   
  17.         this.circelLineWidth = 4;   
  18.         this.circleColor = 'lightgray';   
  19.         this.moveArcColor = 'gray';   
  20.       }   
  21.     }   
  22.     loading.prototype = {   
  23.       show:function (){   
  24.         var canvas = this.canvas;   
  25.         if(!canvas.getContext)return;   
  26.         if(canvas.__loading)return;   
  27.         canvas.__loading = this;   
  28.         var ctx = canvas.getContext('2d');   
  29.         var radius = this.radius;         
  30.         var me = this;   
  31.         var rotatorAngle = Math.PI*1.5;   
  32.         var step = Math.PI/6;   
  33.         canvas.loadingInterval = setInterval(function(){   
  34.           ctx.clearRect(0,0,canvas.width,canvas.height);            
  35.           var lineWidth = me.circleLineWidth;   
  36.           var center = {x:canvas.width/2 - radius,y:canvas.height/2-radius};             
  37.           ctx.beginPath();   
  38.           ctx.lineWidth = lineWidth;   
  39.           ctx.strokeStyle = me.circleColor;   
  40.           ctx.arc(center.x,center.y,radius,0,Math.PI*2);   
  41.           ctx.closePath();   
  42.           ctx.stroke();   
  43.           //在圆圈上面画小圆   
  44.           ctx.beginPath();   
  45.           ctx.strokeStyle = me.moveArcColor;   
  46.           ctx.arc(center.x,center.y,radius,rotatorAngle,rotatorAngle+Math.PI*.45);   
  47.           ctx.stroke();   
  48.           rotatorAngle+=step;   
  49.              
  50.         },50);   
  51.       },   
  52.       hide:function(){   
  53.         var canvas = this.canvas;   
  54.         canvas.__loading = false;   
  55.         if(canvas.loadingInterval){   
  56.           window.clearInterval(canvas.loadingInterval);   
  57.         }   
  58.         var ctx = canvas.getContext('2d');   
  59.         if(ctx)ctx.clearRect(0,0,canvas.width,canvas.height);   
  60.       }   
  61.     };   
  62.         
  63.     </script>  
  64.   </head>  
  65.   <body>  
  66.     <canvas id="canvas" width="300" height="100" style="border:1px solid #69c">您的浏览器不支持html5哟</canvas>  
  67.     <p>  
  68.     <input type="button" onclick="loadingObj.hide()" value="HideLoading"/>  
  69.     <input type="button" onclick="loadingObj.show()" value="showLoading"/>  
  70.     </p>  
  71.     <script>  
  72.     var loadingObj = new loading(document.getElementByIdx_x('canvas'),{radius:8,circleLineWidth:3});   
  73.     loadingObj.show();   
  74.     </script>  
  75.   </body>  
  76. </html>  

       目前从移动设备对Html5的支持来看,html5大有可为。
       天下大势,合久必分,分久必和。PC开发时web应用在很大程度上统一了客户端程序;而现在移动开发使用不同的系统不同的语言,将来大多数应用必然会统一到一种语言。这种语言必然是html5+javascript.

 
 
 
本文转自HTML5中国官方网站:www.html5cn.org/article-1557-1.html
posted @ 2012-01-13 18:11  HTML5中国  阅读(1097)  评论(0编辑  收藏  举报