论坛首页 Web前端技术论坛

html5 loading 效果来了

浏览 29020 次
精华帖 (1) :: 良好帖 (3) :: 新手帖 (0) :: 隐藏帖 (6)
作者 正文
   发表时间:2011-12-20   最后修改:2011-12-20
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图标画图的思路是,首先画一个圆,然后在圆的边上按顺序画大小逐渐减小的小圆点,在每次刷新画布时改变这一系列的小圆点在大圆边上的位置。
<!doctype html>
<html>
  <head>
    <meta http-equiv="content-type" content="GBK"/>
    <title>loading</title>
    <script type="text/javascript">
    /*
    html5 loading 控件
    作者:玉开 博客:http://www.cnblogs.com/yukaizhao/
    发布或使用此控件,请保留作者声明
    */
    function loading(canvas,options){
      this.canvas = canvas;
      if(options){
        this.radius = options.radius||12;
        this.circleLineWidth = options.circleLineWidth||4;
        this.circleColor = options.circleColor||'lightgray';
        this.dotColor = options.dotColor||'gray';
      }else{      
        this.radius = 12;
        this.circelLineWidth = 4;
        this.circleColor = 'lightgray';
        this.dotColor = 'gray';
      }
    }
    loading.prototype = {
      show:function (){
        var canvas = this.canvas;
        if(!canvas.getContext)return;
        if(canvas.__loading)return;
        canvas.__loading = this;
        var ctx = canvas.getContext('2d');
        var radius = this.radius;      
        var rotators = [{angle:0,radius:1.5},{angle:3/radius,radius:2},{angle:7/radius,radius:2.5},{angle:12/radius,radius:3}];      
        var me = this;
        canvas.loadingInterval = setInterval(function(){
          ctx.clearRect(0,0,canvas.width,canvas.height);         
          var lineWidth = me.circleLineWidth;
          var center = {x:canvas.width/2 - radius,y:canvas.height/2-radius};          
          ctx.beginPath();
          ctx.lineWidth = lineWidth;
          ctx.strokeStyle = me.circleColor;
          ctx.arc(center.x,center.y,radius,0,Math.PI*2);
          ctx.closePath();
          ctx.stroke();
          for(var i=0;i<rotators.length;i++){        
            var rotatorAngle = rotators[i].currentAngle||rotators[i].angle;            
            //在圆圈上面画小圆
            var rotatorCenter = {x:center.x-(radius)*Math.cos(rotatorAngle) ,y:center.y-(radius)*Math.sin(rotatorAngle)};            
            var rotatorRadius = rotators[i].radius;
            ctx.beginPath();
            ctx.fillStyle = me.dotColor;
            ctx.arc(rotatorCenter.x,rotatorCenter.y,rotatorRadius,0,Math.PI*2);
            ctx.closePath();
            ctx.fill();
            rotators[i].currentAngle = rotatorAngle+4/radius;
          }
        },50);
      },
      hide:function(){
        var canvas = this.canvas;
        canvas.__loading = false;
        if(canvas.loadingInterval){
          window.clearInterval(canvas.loadingInterval);
        }
        var ctx = canvas.getContext('2d');
        if(ctx)ctx.clearRect(0,0,canvas.width,canvas.height);
      }
    };
     
    </script>
  </head>
  <body>
    <canvas id="canvas" width="300" height="100" style="border:1px solid #69c"></canvas>
    <p>
    <input type="button" onclick="loadingObj.hide()" value="HideLoading"/>
    <input type="button" onclick="loadingObj.show()" value="showLoading"/>
    </p>
    <script>
    var loadingObj = new loading(document.getElementById('canvas'),{radius:8,circleLineWidth:3});
    loadingObj.show();
    </script>
  </body>
</html>

第二个较为简单,在一个圆环上有一个相同圆心相同半径的圆弧在不停的转动。画图的步骤是首先画一个圆环,然后画一个不同颜色相同圆心半径的圆弧,在每次刷新画布时改变圆弧的起始角度。
<!doctype html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html;charset=gbk"/>
  <title>loading</title>
  <script>
   /*
    html5 loading 控件
    作者:玉开 博客:http://www.cnblogs.com/yukaizhao/
    发布或使用此控件,请保留作者声明
    */
    function loading(canvas,options){
      this.canvas = canvas;
      if(options){
        this.radius = options.radius||12;
        this.circleLineWidth = options.circleLineWidth||4;
        this.circleColor = options.circleColor||'lightgray';
        this.moveArcColor = options.moveArcColor||'gray';
      }else{      
        this.radius = 12;
        this.circelLineWidth = 4;
        this.circleColor = 'lightgray';
        this.moveArcColor = 'gray';
      }
    }
    loading.prototype = {
      show:function (){
        var canvas = this.canvas;
        if(!canvas.getContext)return;
        if(canvas.__loading)return;
        canvas.__loading = this;
        var ctx = canvas.getContext('2d');
        var radius = this.radius;      
        var me = this;
        var rotatorAngle = Math.PI*1.5;
        var step = Math.PI/6;
        canvas.loadingInterval = setInterval(function(){
          ctx.clearRect(0,0,canvas.width,canvas.height);         
          var lineWidth = me.circleLineWidth;
          var center = {x:canvas.width/2 - radius,y:canvas.height/2-radius};          
          ctx.beginPath();
          ctx.lineWidth = lineWidth;
          ctx.strokeStyle = me.circleColor;
          ctx.arc(center.x,center.y,radius,0,Math.PI*2);
          ctx.closePath();
          ctx.stroke();
          //在圆圈上面画小圆
          ctx.beginPath();
          ctx.strokeStyle = me.moveArcColor;
          ctx.arc(center.x,center.y,radius,rotatorAngle,rotatorAngle+Math.PI*.45);
          ctx.stroke();
          rotatorAngle+=step;
          
        },50);
      },
      hide:function(){
        var canvas = this.canvas;
        canvas.__loading = false;
        if(canvas.loadingInterval){
          window.clearInterval(canvas.loadingInterval);
        }
        var ctx = canvas.getContext('2d');
        if(ctx)ctx.clearRect(0,0,canvas.width,canvas.height);
      }
    };
     
    </script>
  </head>
  <body>
    <canvas id="canvas" width="300" height="100" style="border:1px solid #69c">您的浏览器不支持html5哟</canvas>
    <p>
    <input type="button" onclick="loadingObj.hide()" value="HideLoading"/>
    <input type="button" onclick="loadingObj.show()" value="showLoading"/>
    </p>
    <script>
    var loadingObj = new loading(document.getElementById('canvas'),{radius:8,circleLineWidth:3});
    loadingObj.show();
    </script>
  </body>
</html>


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

   发表时间:2011-12-20  
可惜没有办法发iframe让大家直接看效果呀
0 请登录后投票
   发表时间:2011-12-20  

按着你的思路也写了一下这个效果,没想到我的loading圆圈显示出来之后是椭圆。查找了很长时间的原因,才发现我的canvas标签是这样写的:

<canvas id="canvas" style="width:300px; height:100px; border:1px solid #ccc;"></canvas>这样写,会令canvas标签中的内容被变形拉伸。

如果换成你那种写法的话,其中的内容就不会被拉伸。无意中发现的一个小技巧,向楼主卖弄一下。。

0 请登录后投票
   发表时间:2011-12-20  
有时候也在想,html5写法是不是太复杂了
0 请登录后投票
   发表时间:2011-12-20  
踏月流星 写道

按着你的思路也写了一下这个效果,没想到我的loading圆圈显示出来之后是椭圆。查找了很长时间的原因,才发现我的canvas标签是这样写的:

<canvas id="canvas" style="width:300px; height:100px; border:1px solid #ccc;"></canvas>这样写,会令canvas标签中的内容被变形拉伸。

如果换成你那种写法的话,其中的内容就不会被拉伸。无意中发现的一个小技巧,向楼主卖弄一下。。

这个问题我也遭遇过,canvas的style中的宽度和高度是不可以设置的,如果设置的话就会出现拉伸或压缩的情况。

0 请登录后投票
   发表时间:2011-12-20  
zhangdaiping 写道
有时候也在想,html5写法是不是太复杂了


其实一点都不复杂,使用起来和gif图片一样好控制。
0 请登录后投票
   发表时间:2011-12-20  
其实使用一个Gif图片最简单,为什么一定要多此一举。不能为了技术而技术。
0 请登录后投票
   发表时间:2011-12-20  
myten 写道
其实使用一个Gif图片最简单,为什么一定要多此一举。不能为了技术而技术。


对 这么大段代码 还不如一个gif 简洁明了

有时候常常走向误区呀
0 请登录后投票
   发表时间:2011-12-20   最后修改:2011-12-20
这样会有更好的表现能力。其实使用起来一点都不复杂。

另外用js做起来比做一个loading效果的gif图片更简单。

楼上两位的看法可能是因为已经习惯了从网上下载个图片就用了,根本没有想过gif是怎么做出来的。
0 请登录后投票
   发表时间:2011-12-20  
用图片不一定好,如果做移动网站,多费流量啊(*^__^*) 嘻嘻…

换个角度想,上去几年,连圆角都是用图片拼接实现的,现在已经能通过CSS搞定(当然需浏览器支持)
HTML5真普及起来,谁会料到能发生什么事情呢
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics