HTML 5如何實現(xiàn)緩沖效果
HTML5在移動設備上表現(xiàn),相信已經不用我多說了,干掉了Flash之后,它已經坐上了移動應用程序的第一把交椅。幾乎所有稍微高端一點的設備(喬幫主的iPad,iPhone和Andriod的平板手機等)的瀏覽器都支持HTML5,而且據(jù)權威人士測試,這些支持HTML5的設備對Canvas標簽的支持也是相當?shù)暮谩?/p>本文引用地址:http://butianyuan.cn/article/136716.htm
大家都知道Web2.0以來,應用程序的實現(xiàn)使用了大量Ajax,而Loading的小圖標也有很多,甚至還有專門提供Loading圖片的網(wǎng)站,所以我就想能不能讓HTML5一并解決這個以前用gif文件才能解決的問題。出乎我意料的是,實現(xiàn)的過程非常簡單,只用了不到一小時的時間我就用HTML5實驗出了兩個Loading效果,而且這樣做出來的Loading圖標是可定制的,既可以定制顏色,也可以定制大小等屬性。
第一個帶著小尾巴轉動的loading圖標畫圖的思路是,首先畫一個圓,然后在圓的邊上按順序畫大小逐漸減小的小圓點,在每次刷新畫布時改變這一系列的小圓點在大圓邊上的位置。
這里是案例的演示代碼:
- <!doctype html>
- <html>
- <head>
- <meta http-equiv="content-type" content="GBK"/>
- <title>loading</title>
- <script type="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 (){
- 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"/>
- </style>
- <p>
- <script>
- var loadingObj = new loading(document.getElementById('canvas'),{radius:8,circleLineWidth:3});
- loadingObj.show();
- </script>
- </body>
- </html>
第二個較為簡單,在一個圓環(huán)上有一個相同圓心相同半徑的圓弧在不停的轉動。畫圖的步驟是首先畫一個圓環(huán),然后畫一個不同顏色相同圓心半徑的圓弧,在每次刷新畫布時改變圓弧的起始角度。
這里是案例的演示代碼:
- <!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/
- 發(fā)布或使用此控件,請保留作者聲明
- */
- 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開發(fā)時Web應用在很大程度上統(tǒng)一了客戶端程序;而現(xiàn)在移動開發(fā)使用不同的系統(tǒng)不同的語言,將來大多數(shù)應用必然會統(tǒng)一到一種語言,這種語言必然是html5加Javascript。
評論