新聞中心

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

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

        作者: 時間:2012-09-12 來源:51CTO 收藏

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

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

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

          這里是案例的演示代碼:

        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>   

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

          這里是案例的演示代碼:

        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ā)布或使用此控件,請保留作者聲明   
        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>   

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

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



        關(guān)鍵詞: HTML5

        評論


        相關(guān)推薦

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

        關(guān)閉
        主站蜘蛛池模板: 磐安县| 东光县| 双牌县| 高邑县| 重庆市| 湖口县| 兴义市| 古丈县| 正安县| 高尔夫| 长海县| 会东县| 澄江县| 石屏县| 五寨县| 江城| 湟源县| 长春市| 木兰县| 新绛县| 紫云| 茶陵县| 文登市| 平邑县| 德清县| 正镶白旗| 吕梁市| 兴海县| 丰都县| 冀州市| 全州县| 布尔津县| 荥阳市| 剑河县| 额济纳旗| 建瓯市| 仁怀市| 锦屏县| 班玛县| 乐都县| 晴隆县|