? ? ? ?HTML5在移動設備上表現(xiàn),相信已經(jīng)不用我多說了,干掉了Flash之后,它已經(jīng)坐上了移動應用程序的第一把交椅。幾乎所有稍微高端一點的設備(喬幫主的iPad,iPhone和Andriod的平板手機等)的瀏覽器都支持HTML5,而且據(jù)權威人士測試,這些支持HTML5的設備對Canvas標簽的支持也是相當?shù)暮谩?br>
? ? ? ??大家都知道Web2.0以來,應用程序的實現(xiàn)使用了大量Ajax,而Loading的小圖標也有很多,甚至還有專門提供Loading圖片的網(wǎng)站,所以我就想能不能讓HTML5一并解決這個以前用gif文件才能解決的問題。出乎我意料的是,實現(xiàn)的過程非常簡單,只用了不到一小時的時間我就用HTML5實驗出了兩個Loading效果,而且這樣做出來的Loading圖標是可定制的,既可以定制顏色,也可以定制大小等屬性。
? ? ? ??第一個帶著小尾巴轉動的loading圖標畫圖的思路是,首先畫一個圓,然后在圓的邊上按順序畫大小逐漸減小的小圓點,在每次刷新畫布時改變這一系列的小圓點在大圓邊上的位置。
? ? ? ??這里是案例的演示代碼:
- ??
- ? ?
- ? ?loading
- ? ?
- ? ? 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
- ? ?? ?? ?? ?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><br>
</script>
- ? ? var loadingObj = new loading(document.getElementById('canvas'),{radius:8,circleLineWidth:3});
- ? ? loadingObj.show();
- ? ?
- ??
-
復制代碼
? ? ? ??演示地址:
http://f200-8.bbs.hexun.com/e/111219/loading.htm? ? ? ??第二個較為簡單,在一個圓環(huán)上有一個相同圓心相同半徑的圓弧在不停的轉動。畫圖的步驟是首先畫一個圓環(huán),然后畫一個不同顏色相同圓心半徑的圓弧,在每次刷新畫布時改變圓弧的起始角度。
? ? ? ??這里是案例的演示代碼:
- ??
- ??loading
- ??<script><br>
</script>
- ? ?/*
- ? ? html5 loading 鎺т歡
- ? ? 浣滆