<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 專題 > HTML 5如何實(shí)現(xiàn)緩沖效果

          HTML 5如何實(shí)現(xiàn)緩沖效果

          作者: 時(shí)間:2012-09-12 來源:51CTO 收藏

            在移動(dòng)設(shè)備上表現(xiàn),相信已經(jīng)不用我多說了,干掉了Flash之后,它已經(jīng)坐上了移動(dòng)應(yīng)用程序的第一把交椅。幾乎所有稍微高端一點(diǎn)的設(shè)備(喬幫主的iPad,iPhone和Andriod的平板手機(jī)等)的瀏覽器都支持,而且據(jù)權(quán)威人士測(cè)試,這些支持的設(shè)備對(duì)Canvas標(biāo)簽的支持也是相當(dāng)?shù)暮谩?/p>本文引用地址:http://www.ex-cimer.com/article/136716.htm

            大家都知道Web2.0以來,應(yīng)用程序的實(shí)現(xiàn)使用了大量Ajax,而Loading的小圖標(biāo)也有很多,甚至還有專門提供Loading圖片的網(wǎng)站,所以我就想能不能讓HTML5一并解決這個(gè)以前用gif文件才能解決的問題。出乎我意料的是,實(shí)現(xiàn)的過程非常簡單,只用了不到一小時(shí)的時(shí)間我就用HTML5實(shí)驗(yàn)出了兩個(gè)Loading效果,而且這樣做出來的Loading圖標(biāo)是可定制的,既可以定制顏色,也可以定制大小等屬性。

            第一個(gè)帶著小尾巴轉(zhuǎn)動(dòng)的loading圖標(biāo)畫圖的思路是,首先畫一個(gè)圓,然后在圓的邊上按順序畫大小逐漸減小的小圓點(diǎn),在每次刷新畫布時(shí)改變這一系列的小圓點(diǎn)在大圓邊上的位置。

            這里是案例的演示代碼:

          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 id="canvas" width="300" height="100" style="border:1px solid #69c"></canvas>   
          69.     <p>   
          70.     <input type="button" onclick="loadingObj.hide()" value="HideLoading"/>   
          71.     <input type="button" onclick="loadingObj.show()" value="showLoading"/>   
          72. </style>   
          73.     <p>   
          74.     <script>   
          75.     var loadingObj = new loading(document.getElementById('canvas'),{radius:8,circleLineWidth:3});   
          76.     loadingObj.show();   
          77.     </script>   
          78.   </body>   
          79. </html>   

            第二個(gè)較為簡單,在一個(gè)圓環(huán)上有一個(gè)相同圓心相同半徑的圓弧在不停的轉(zhuǎn)動(dòng)。畫圖的步驟是首先畫一個(gè)圓環(huán),然后畫一個(gè)不同顏色相同圓心半徑的圓弧,在每次刷新畫布時(shí)改變圓弧的起始角度。

            這里是案例的演示代碼:

          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 控件   
          9.     作者:玉開 博客:http://www.cnblogs.com/yukaizhao/   
          10.     發(fā)布或使用此控件,請(qǐng)保留作者聲明   
          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.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.         },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 id="canvas" width="300" height="100" style="border:1px solid #69c">您的瀏覽器不支持html5喲</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.getElementById('canvas'),{radius:8,circleLineWidth:3});   
          75.     loadingObj.show();   
          76.     </script>   
          77.   </body>   
          78. </html>   

            目前從移動(dòng)設(shè)備對(duì)HTML5的支持來看,HTML5將來必定大有可為。

            天下大勢(shì),合久必分,分久必和。PC開發(fā)時(shí)Web應(yīng)用在很大程度上統(tǒng)一了客戶端程序;而現(xiàn)在移動(dòng)開發(fā)使用不同的系統(tǒng)不同的語言,將來大多數(shù)應(yīng)用必然會(huì)統(tǒng)一到一種語言,這種語言必然是html5加Javascript。



          關(guān)鍵詞: HTML5

          評(píng)論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();