HTML 5如何实现缓冲效果
HTML5在移动设备上表现,相信已经不用我多说了,干掉了Flash之后,它已经坐上了移动应用程序的第一把交椅。几乎所有稍微高端一点的设备(乔帮主的iPad,iPhone和Andriod的平板手机等)的浏览器都支持HTML5,而且据权威人士测试,这些支持HTML5的设备对Canvas标签的支持也是相当的好。
本文引用地址://m.amcfsurvey.com/article/136716.htm大家都知道Web2.0以来,应用程序的实现使用了大量Ajax,而Loading的小图标也有很多,甚至还有专门提供Loading图片的网站,所以我就想能不能让HTML5一并解决这个以前用gif文件才能解决的问题。出乎我意料的是,实现的过程非常简单,只用了不到一小时的时间我就用HTML5实验出了两个Loading效果,而且这样做出来的Loading图标是可定制的,既可以定制颜色,也可以定制大小等属性。
第一个带着小尾巴转动的loading图标画图的思路是,首先画一个圆,然后在圆的边上按顺序画大小逐渐减小的小圆点,在每次刷新画布时改变这一系列的小圆点在大圆边上的位置。
这里是案例的演示代码:
- >
- <html>
- <head>
- <metahttp-equiv="content-type"content="GBK"/>
- <title>loadingtitle>
- <scripttype="text/javascript">
- 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 (){
- varcanvas=this.canvas;
- if(!canvas.getContext)return;
- if(canvas.__loading)return;
- canvas.__loading=this;
- varctx=canvas.getContext('2d');
- varradius=this.radius;
- varrotators= [{angle:0,radius:1.5},{angle:3/radius,radius:2},{angle:7/radius,radius:2.5},{angle:12/radius,radius:3}];
- varme=this;
- canvas.loadingInterval=setInterval(function(){
- ctx.clearRect(0,0,canvas.width,canvas.height);
- varlineWidth=me.circleLineWidth;
- varcenter= {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(vari=0;i<rotators.length;i++){
- varrotatorAngle=rotators[i].currentAngle||rotators[i].angle;
- //在圆圈上面画小圆
- varrotatorCenter= {x:center.x-(radius)*Math.cos(rotatorAngle) ,y:center.y-(radius)*Math.sin(rotatorAngle)};
- varrotatorRadius=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(){
- varcanvas=this.canvas;
- canvas.__loading=false;
- if(canvas.loadingInterval){
- window.clearInterval(canvas.loadingInterval);
- }
- varctx=canvas.getContext('2d');
- if(ctx)ctx.clearRect(0,0,canvas.width,canvas.height);
- }
- };
- script>
- head>
- <body>
- <canvasid="canvas"width="300"height="100"style="border:1px solid #69c">canvas>
- <p>
- <inputtype="button"onclick="loadingObj.hide()"value="HideLoading"/>
- <inputtype="button"onclick="loadingObj.show()"value="showLoading"/>
- style>
- <p>
- <script>
- varloadingObj=newloading(document.getElementById('canvas'),{radius:8,circleLineWidth:3});
- loadingObj.show();
- script>
- body>
- html>
第二个较为简单,在一个圆环上有一个相同圆心相同半径的圆弧在不停的转动。画图的步骤是首先画一个圆环,然后画一个不同颜色相同圆心半径的圆弧,在每次刷新画布时改变圆弧的起始角度。
这里是案例的演示代码:
- >
- <html>
- <head>
- <metahttp-equiv="content-type"content="text/html;charset=gbk"/>
- <title>loadingtitle>
- <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 (){
- varcanvas=this.canvas;
- if(!canvas.getContext)return;
- if(canvas.__loading)return;
- canvas.__loading=this;
- varctx=canvas.getContext('2d');
- varradius=this.radius;
- varme=this;
- varrotatorAngle=Math.PI*1.5;
- varstep=Math.PI/6;
- canvas.loadingInterval=setInterval(function(){
- ctx.clearRect(0,0,canvas.width,canvas.height);
- varlineWidth=me.circleLineWidth;
- varcenter= {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(){
- varcanvas=this.canvas;
- canvas.__loading=false;
- if(canvas.loadingInterval){
- window.clearInterval(canvas.loadingInterval);
- }
- varctx=canvas.getContext('2d');
- if(ctx)ctx.clearRect(0,0,canvas.width,canvas.height);
- }
- };
- script>
- head>
- <body>
- <canvasid="canvas"width="300"height="100"style="border:1px solid #69c">您的浏览器不支持html5哟canvas>
- <p>
- <inputtype="button"onclick="loadingObj.hide()"value="HideLoading"/>
- <inputtype="button"onclick="loadingObj.show()"value="showLoading"/>
- p>
- <script>
- varloadingObj=newloading(document.getElementById('canvas'),{radius:8,circleLineWidth:3});
- loadingObj.show();
- script>
- body>
- html>
目前从移动设备对HTML5的支持来看,HTML5将来必定大有可为。
天下大势,合久必分,分久必和。PC开发时Web应用在很大程度上统一了客户端程序;而现在移动开发使用不同的系统不同的语言,将来大多数应用必然会统一到一种语言,这种语言必然是html5加Javascript。
评论