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

目前从移动设备对Html5的支持来看,html5大有可为。

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

本文转自HTML5中国官方网站:www.html5cn.org/article-1557-1.html