so_brave 发表于 2011-12-20 23:46

html5 loading 效果来了


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.    html5 loading 控件

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

12.    function loading(canvas,options){
13.      this.canvas = canvas;
14.      if(options){
15.      this.radius = options.radius||12;
16.      this.circleLineWidth = options.circleLineWidth||4;
17.      this.circleColor = options.circleColor||'lightgray';
18.      this.moveArcColor = options.moveArcColor||'gray';
19.      }else{      
20.      this.radius = 12;
21.      this.circelLineWidth = 4;
22.      this.circleColor = 'lightgray';
23.      this.moveArcColor = 'gray';
24.      }
25.    }
26.    loading.prototype = {
27.      show:function (){
28.      var canvas = this.canvas;
29.      if(!canvas.getContext)return;
30.      if(canvas.__loading)return;
31.      canvas.__loading = this;
32.      var ctx = canvas.getContext('2d');
33.      var radius = this.radius;      
34.      var me = this;
35.      var rotatorAngle = Math.PI*1.5;
36.      var step = Math.PI/6;
37.      canvas.loadingInterval = setInterval(function(){
38.          ctx.clearRect(0,0,canvas.width,canvas.height);         
39.          var lineWidth = me.circleLineWidth;
40.          var center = {x:canvas.width/2 - radius,y:canvas.height/2-radius};            
41.          ctx.beginPath();
42.          ctx.lineWidth = lineWidth;
43.          ctx.strokeStyle = me.circleColor;
44.          ctx.arc(center.x,center.y,radius,0,Math.PI*2);
45.          ctx.closePath();
46.          ctx.stroke();
47.          //在圆圈上面画小圆
48.          ctx.beginPath();
49.          ctx.strokeStyle = me.moveArcColor;
50.          ctx.arc(center.x,center.y,radius,rotatorAngle,rotatorAngle+Math.PI*.45);
51.          ctx.stroke();
52.          rotatorAngle+=step;
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">您的浏览器不支持html5哟</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.getElementById('canvas'),{radius:8,circleLineWidth:3});
77.    loadingObj.show();
78.    </script>
79.</body>
80.</html>
目前从移动设备对Html5的支持来看,html5大有可为。
天下大势,合久必分,分久必和。PC开发时web应用在很大程度上统一了客户端程序;而现在移动开发使用不同的系统不同的语言,将来大多数应用必然会统一到一种语言。这种语言必然是html5+javascript.

小鬼萌萌控 发表于 2011-12-21 22:00

学习鸟 谢谢分享
页: [1]
查看完整版本: html5 loading 效果来了