javascrip+HTML5 Canvas繪制轉(zhuǎn)盤抽獎

字號:


    這篇文章主要介紹了javascrip+HTML5 Canvas繪制轉(zhuǎn)盤抽獎的相關(guān)資料,需要的朋友可以參考下
    之前做過的項目中,有需要抽獎轉(zhuǎn)盤功能的。項目已經(jīng)完工一段時間了,也沒出現(xiàn)什么嚴(yán)重的bug,所以現(xiàn)在拎出來分享給大家。
    功能需求
    1、轉(zhuǎn)盤要美觀,轉(zhuǎn)動效果流暢。
    2、轉(zhuǎn)盤上需要顯示獎品圖片,并且獎品是后臺讀取的照片和名字。
    3、轉(zhuǎn)動動畫完成后要有相應(yīng)提示。
    4、獲取的獎品具體算法在數(shù)據(jù)庫里操作,前端只提供最后的效果展示。 
    知識要點
    1、引用了一個jq插件:awardRotate,用來實現(xiàn)更智能化的轉(zhuǎn)動(插件下載:http://www.jqcool.net/jquery-jqueryrotate.html)。
    2、使用canvas標(biāo)簽和對應(yīng)的html5 api 進行操作。(canvas中文手冊可以查看http://javascript.ruanyifeng.com/htmlapi/canvas.html
    正文
    引用大轉(zhuǎn)盤樣式
    .lunck_draw_wrap{display:block;width:95%;margin-right:auto;}
     .lunck_draw_wrap .turnplate{display:block;width:106%; position:relative;}
      .lunck_draw_wrap .turnplate canvas.item{left:1px;
      position: relative;
      top:9px;
      width:100%;}
      .lunck_draw_wrap .turnplate img.pointer{ height:37.5%;
      left:34.6%;
      position: absolute;
      top:30%;
      width:31.5%;}
    轉(zhuǎn)盤插件所需參數(shù):
    var turnplate ={
     restaraunts:[],//大轉(zhuǎn)盤獎品名稱
     lucky:[],//獎品內(nèi)容
     colors:[],//大轉(zhuǎn)盤獎品區(qū)塊對應(yīng)背景顏色
     goodsimgArr:[],//獎品圖片頁面標(biāo)簽
     outsideRadius:175,//大轉(zhuǎn)盤外圓的半徑
     textRadius:140,//大轉(zhuǎn)盤獎品位置距離圓心的距離
     insideRadius:65,//大轉(zhuǎn)盤內(nèi)圓的半徑
     startAngle:0,//開始角度
     bRotate:false//false:停止;ture:旋轉(zhuǎn)
     };
     由參數(shù)可知,我們需要從服務(wù)端獲取相應(yīng)的獎品名稱,獎品內(nèi)容,獎品圖片頁面標(biāo)簽等信息,再對大轉(zhuǎn)盤進行渲染。
    所以我們的第一步操作就是向服務(wù)端發(fā)送請求獲取對應(yīng)的獎品信息,并且遍歷到生成大轉(zhuǎn)盤所需的數(shù)組參數(shù)里:
    $.each(data.list,function(key, value){
     turnplate.restaraunts.push(value.data0);
     turnplate.lucky.push(value.data1);
     turnplate.goodsimgArr.push(getLuckyImg + value.data4);
     if(key %2==0)
     turnplate.colors.push("#fff");
     else
     turnplate.colors.push("#5fcbd4");
     })
    data.list是我獲取來的獎品json數(shù)據(jù):
    [
     {
     "data0":"一等獎",
     "data1":"iphone6s",
     "data2":"0",
     "data3":"0",
     "data4":"201510161406303384.png",
     "data5":"XXXX網(wǎng)絡(luò)科技",
     "data6":"浙江省衢州市柯城區(qū)XXXXX",
     "data7":"0570-XXXXXX"
     },......
     ]
    由于客戶要求獎品沒有“謝謝參與”,所以最低獎品也為“優(yōu)勝獎”,所以在遍歷獎品之后,插入有關(guān)“優(yōu)勝獎”的渲染描述即可:
    turnplate.goodsimgArr.push('../images/hongbao.png')
     turnplate.restaraunts.push("優(yōu)勝獎");
     turnplate.colors.push("#5fcbd4");
     //頁面所有元素加載完畢后執(zhí)行drawRouletteWheel()方法對轉(zhuǎn)盤進行渲染
     preloadimages(turnplate.goodsimgArr).done(function(images){
     drawRouletteWheel();
     });
    因為圖片加載需要時間,而使用canvas復(fù)制圖片需要圖片加載完成后才能繪制,所以我使用了preloadimages,讓所有獎品圖片都加載完畢后進行大轉(zhuǎn)盤的渲染工作:
    //對獎品圖片預(yù)加載
     function preloadimages(arr){
     var newimages =[], loadedimages =0
     var postaction =function(){}//此處增加了一個postaction函數(shù)
     var arr =(typeof arr !="object")?[arr]: arr
     function imageloadpost(){
     loadedimages++
     if(loadedimages == arr.length){
     postaction(newimages)//加載完成用我們調(diào)用postaction函數(shù)并將newimages數(shù)組做為參數(shù)傳遞進去
     }
     }
     for(var i =0; i < arr.length; i++){
     newimages[i]=newImage()
     newimages[i].src = arr[i]
     newimages[i].onload =function(){
     imageloadpost()
     }
     newimages[i].onerror =function(){
     imageloadpost()
     }
     }
     return{//此處返回一個空白對象的done方法
     done:function(f){
     postaction = f || postaction
     }
     }
     }
    繪制轉(zhuǎn)盤代碼:
    function drawRouletteWheel(){
     var canvas = document.getElementById("wheelcanvas");
     if(canvas.getContext){
     //根據(jù)獎品個數(shù)計算圓周角度
     var arc =Math.PI /(turnplate.restaraunts.length /2);
     var ctx = canvas.getContext("2d");
     //在給定矩形內(nèi)清空一個矩形
     ctx.clearRect(0,0,422,422);
     //strokeStyle 屬性設(shè)置或返回用于筆觸的顏色、漸變或模式
     ctx.strokeStyle ="rgba(0,0,0,0)";
     //font 屬性設(shè)置或返回畫布上文本內(nèi)容的當(dāng)前字體屬性
     ctx.font ='bold 18px Microsoft YaHei';
     for(var i =0; i < turnplate.restaraunts.length; i++){
     //根據(jù)當(dāng)前獎品索引 計算繪制的扇形開始弧度
     var angle = turnplate.startAngle + i * arc;
     //根據(jù)獎品參數(shù) 繪制扇形填充顏色
     ctx.fillStyle = turnplate.colors[i];
     //開始繪制扇形
     ctx.beginPath();
     //arc(x,y,r,起始角,結(jié)束角,繪制方向) 方法創(chuàng)建弧/曲線(用于創(chuàng)建圓或部分圓)
     //繪制大圓
     ctx.arc(212,212, turnplate.outsideRadius, angle, angle + arc,false);
     //繪制小圓
     ctx.arc(212,212, turnplate.insideRadius, angle + arc, angle,true);
     ctx.stroke();
     ctx.fill();
     //鎖畫布(為了保存之前的畫布狀態(tài))
     ctx.save();
     //----繪制獎品開始----
     //獎品默認(rèn)字體顏色
     ctx.fillStyle ="#fff";
     var text = turnplate.restaraunts[i];
     var lukyname = turnplate.lucky[i];
     var line_height =17;
     //translate方法重新映射畫布上的 (0,0) 位置
     ctx.translate(212+Math.cos(angle + arc /2)* turnplate.textRadius,212+Math.sin(angle + arc /2)* turnplate.textRadius);
     //rotate方法旋轉(zhuǎn)當(dāng)前的繪圖
     ctx.rotate(angle + arc /2+Math.PI /2);
     //繪制獎品圖片
     var img =newImage();
     img.src = turnplate.goodsimgArr[i];
     ctx.drawImage(img,-17,35);
     //由于設(shè)計的轉(zhuǎn)盤色塊是交錯的,所以這樣可以實現(xiàn)相鄰獎品區(qū)域字體顏色不同
     if(i %2==0){
     ctx.fillStyle ="#f7452f";
     }
     //將字體繪制在對應(yīng)坐標(biāo)
     ctx.fillText(text,-ctx.measureText(text).width /2,0);
     //設(shè)置字體
     ctx.font =' 14px Microsoft YaHei';
     //繪制獎品名稱
     if(text !="優(yōu)勝獎"){
     ctx.fillText(lukyname,-ctx.measureText(lukyname).width /2,25);
     }else{
     ctx.fillText("優(yōu)麥幣",-ctx.measureText("優(yōu)麥幣").width /2,25);
     }
     //把當(dāng)前畫布返回(插入)到上一個save()狀態(tài)之前
     ctx.restore();
     ctx.save();
     //----繪制獎品結(jié)束----
     }
     }
     }
    每一步基本上都有注釋,對于canvas方法有不理解的可以百度,或者查詢我上面分享的中文手冊。
    html代碼為:
    <divclass="lunck_draw_wrap">
     <divclass="turnplate"style=" background-size:100%100%;">
     <canvasclass="item"id="wheelcanvas"width="422px"height="422px"></canvas>
     <imgclass="pointer"style="top:0px; left:0px; width:100%; height:100%;"src="../images/chouzhang12.png"/>
     <imgclass="pointer"src="../images/hianji .png"/>
     </div>
     </div>
     效果圖:
    名單
    點擊事件執(zhí)行代碼:
    $('.lunck_draw_wrap').delegate("img.pointer","click",function(){
     if(turnplate.bRotate)return;
     turnplate.bRotate =!turnplate.bRotate;
     $.getJSON("../AJAX/lottery.ashx","",function(data){
     //1090系統(tǒng)配置錯誤,1091用戶未登陸或用戶數(shù)據(jù)異常,1092用戶剩余積分不足,1093未中獎
     hideInput("code",data.code)
     if(data.code.toString()=="1090"){
     iosalert("系統(tǒng)配置錯誤")
     }elseif(data.code.toString()=="1091"){
     iosalert("用戶未登陸或用戶數(shù)據(jù)異常")
     }elseif(data.code.toString()=="1092"){
     iosalert("用戶剩余積分不足")
     }elseif(data.code.toString()=="1094"){
     iosalert("超過每日抽獎次數(shù)")
     }
     else{
     var upoint =0;
     upoint = parseInt($("#uPoint").html())- parseInt($("#sPoint").html());
     $("#uPoint").html(upoint);
     if(data.isWin =='true'){
     item = getArrayIndex(turnplate.restaraunts, data.name);
     rotateFn(item +1,"恭喜獲得,"+ turnplate.restaraunts[item]);
     }
     else{
     rotateFn(0,"恭喜獲得優(yōu)勝獎!");
     }
     }
     })
     });
    上面的代碼實現(xiàn)了基本上的邏輯,還需要一個轉(zhuǎn)動轉(zhuǎn)盤的方法來響應(yīng)服務(wù)端傳過來的結(jié)果:
    //旋轉(zhuǎn)轉(zhuǎn)盤 item:獎品位置; txt:提示語;
     var rotateFn =function(item, txt){
     //根據(jù)傳進來的獎品序號 計算相應(yīng)的弧度
     var angles = item *(360/ turnplate.restaraunts.length)-(360/(turnplate.restaraunts.length *2));
     if(angles <270){
     angles =270- angles;
     }else{
     angles =360- angles +270;
     }
     //強制停止轉(zhuǎn)盤的轉(zhuǎn)動
     $('#wheelcanvas').stopRotate();
     //調(diào)用轉(zhuǎn)動方法,設(shè)置轉(zhuǎn)動所需參數(shù)和回調(diào)函數(shù)
     $('#wheelcanvas').rotate({
     //起始角度
     angle:0,
     //轉(zhuǎn)動角度 +1800是為了多轉(zhuǎn)幾圈
     animateTo: angles +1800,
     duration:8000,
     callback:function(){
     iosSuccess(txt);
     turnplate.bRotate =!turnplate.bRotate;
     if($("#code").val()!="1093"){
     delayLoad(getHttpPrefix +"graphicdetails.html?lukyid="+ $("#code").val())
     }
     }
     });
     };
    好了 主要的功能代碼都已分享完畢了,還有些工具方法不理解的,可以留言 我會補充進去的。
    總結(jié)
    canvas是html5很強大的一張王牌,可以實現(xiàn)許多絢麗的效果,希望本文可以幫到一些正在學(xué)習(xí)使用canvas的朋友們。