亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    H5動(dòng)畫–canvas繪制圓環(huán)百分比進(jìn)度的實(shí)例

    1.效果圖

    H5動(dòng)畫--canvas繪制圓環(huán)百分比進(jìn)度的實(shí)例
    2.原理
    第一步繪制一個(gè)整圓顏色自定義,第二部繪制一個(gè)內(nèi)圓,其半徑要小于外圓顏色自定
    最后一步按照百分比繪制第三個(gè)圓,顏色自定。
    要實(shí)現(xiàn)動(dòng)態(tài)繪制第三步的效果,只要添加一個(gè)定時(shí)器的功能,每隔一段時(shí)間繪制一段距離,設(shè)定一個(gè)閥值
    當(dāng)大于這個(gè)閥值得時(shí)候就清空這個(gè)定時(shí)器,這個(gè)閥值其實(shí)就是要顯示的百分比值。每次繪制0.01.
    注意:在定時(shí)器內(nèi)繪制時(shí),要把第二步繪制內(nèi)圓,空白圓也在定時(shí)器中繪制。

    3.知識(shí)點(diǎn)
    繪制公式:arc(x, y, radius, startRad, endRad, anticlockwise)
    在canvas畫布上繪制以坐標(biāo)點(diǎn)(x,y)為圓心、半徑為radius的圓上的一段弧線。這段弧線的起始弧度是startRad,結(jié)束弧度是endRad。這里的弧度是以x軸正方向(時(shí)鐘三點(diǎn)鐘)為基準(zhǔn)、進(jìn)行順時(shí)針旋轉(zhuǎn)的角度來計(jì)算的。anticlockwise表示是以逆時(shí)針方向還是順時(shí)針方向開始繪制,如果為true則表示逆時(shí)針,如果為false則表示順時(shí)針。anticlockwise參數(shù)是可選的,默認(rèn)為false,即順時(shí)針。

    4.js源代碼

    <script src="jquery.min.js"></script><script>function circleProgress(value,average){      var canvas = document.getElementById("yuan");    var context = canvas.getContext('2d');    var _this = $(canvas),      value= Number(value),// 當(dāng)前百分比,數(shù)值      average = Number(average),// 平均百分比      color = "",// 進(jìn)度條、文字樣式      maxpercent = 100,//最大百分比,可設(shè)置      c_width = _this.width(),// canvas,寬度      c_height =_this.height();// canvas,高度      // 判斷設(shè)置當(dāng)前顯示顏色      if( value== maxpercent ){          color="#29c9ad";      }else if( value> average ){          color="#27b5ff";      }else{          color="#ff6100";      }    // 清空畫布      context.clearRect(0, 0, c_width, c_height);    // 畫初始圓      context.beginPath();    // 將起始點(diǎn)移到canvas中心      context.moveTo(c_width/2, c_height/2);    // 繪制一個(gè)中心點(diǎn)為(c_width/2, c_height/2),半徑為c_height/2,起始點(diǎn)0,終止點(diǎn)為Math.PI * 2的 整圓      context.arc(c_width/2, c_height/2, c_height/2, 0, Math.PI * 2, false);      context.closePath();      context.fillStyle = '#ddd'; //填充顏色      context.fill();    // 繪制內(nèi)圓      context.beginPath();      context.strokeStyle = color;      context.lineCap = 'square';      context.closePath();      context.fill();      context.lineWidth = 10.0;//繪制內(nèi)圓的線寬度        function draw(cur){          // 畫內(nèi)部空白            context.beginPath();            context.moveTo(24, 24);            context.arc(c_width/2, c_height/2, c_height/2-10, 0, Math.PI * 2, true);            context.closePath();            context.fillStyle = 'rgba(255,255,255,1)';  // 填充內(nèi)部顏色          context.fill();        // 畫內(nèi)圓          context.beginPath();        // 繪制一個(gè)中心點(diǎn)為(c_width/2, c_height/2),半徑為c_height/2-5不與外圓重疊,          // 起始點(diǎn)-(Math.PI/2),終止點(diǎn)為((Math.PI*2)*cur)-Math.PI/2的 整圓cur為每一次繪制的距離          context.arc(c_width/2, c_height/2, c_height/2-5, -(Math.PI / 2), ((Math.PI * 2) * cur ) - Math.PI / 2, false);          context.stroke();        //在中間寫字            context.font = "bold 18pt Arial";  // 字體大小,樣式          context.fillStyle = color;  // 顏色          context.textAlign = 'center';  // 位置          context.textBaseline = 'middle';            context.moveTo(c_width/2, c_height/2);  // 文字填充位置          context.fillText(value+"%", c_width/2, c_height/2-20);          context.fillText("正確率", c_width/2, c_height/2+20);      }    // 調(diào)用定時(shí)器實(shí)現(xiàn)動(dòng)態(tài)效果      var timer=null,n=0;    function loadCanvas(nowT){          timer = setInterval(function(){              if(n>nowT){                  clearInterval(timer);              }else{                  draw(n);                  n += 0.01;              }          },15);      }      loadCanvas(value/100);      timer=null;  };   </script>

    最后需用調(diào)用circleProgress這個(gè)方法,并把相應(yīng)的參數(shù)穿進(jìn)去。博主是這樣寫的,通過點(diǎn)擊按鈕觸發(fā)。。。

    <input onclick="circleProgress(10,50)" value="畫圓" type="button"><canvas id="yuan"></canvas>

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)