之前做過的項目中,有需要抽獎轉盤功能的。項目已經完工一段時間了,也沒出現什么嚴重的bug,所以現在拎出來分享給大家。
功能需求
- 轉盤要美觀,轉動效果流暢。
- 轉盤上需要顯示獎品圖片,並且獎品是后台讀取的照片和名字。
- 轉動動畫完成后要有相應提示。
- 獲取的獎品具體算法在數據庫里操作,前端只提供最后的效果展示。
知識要點
- 引用了一個jq插件:awardRotate,用來實現更智能化的轉動(插件下載:http://www.jqcool.net/jquery-jqueryrotate.html)。
- 使用canvas標簽和對應的html5 api 進行操作。(canvas中文手冊可以查看http://javascript.ruanyifeng.com/htmlapi/canvas.html
正文
引用大轉盤樣式
1 .lunck_draw_wrap{display:block;width:95%;margin-right:auto;} 2 .lunck_draw_wrap .turnplate{display:block;width:106%; position:relative;} 3 .lunck_draw_wrap .turnplate canvas.item{left:1px; 4 position: relative; 5 top:9px; 6 width:100%;} 7 .lunck_draw_wrap .turnplate img.pointer{ height:37.5%; 8 left:34.6%; 9 position: absolute; 10 top:30%; 11 width:31.5%;}
轉盤插件所需參數:
1 var turnplate ={ 2 restaraunts:[],//大轉盤獎品名稱 3 lucky:[],//獎品內容 4 colors:[],//大轉盤獎品區塊對應背景顏色 5 goodsimgArr:[],//獎品圖片頁面標簽 6 outsideRadius:175,//大轉盤外圓的半徑 7 textRadius:140,//大轉盤獎品位置距離圓心的距離 8 insideRadius:65,//大轉盤內圓的半徑 9 startAngle:0,//開始角度 10 bRotate:false//false:停止;ture:旋轉 11 };
由參數可知,我們需要從服務端獲取相應的獎品名稱,獎品內容,獎品圖片頁面標簽等信息,再對大轉盤進行渲染。
所以我們的第一步操作就是向服務端發送請求獲取對應的獎品信息,並且遍歷到生成大轉盤所需的數組參數里:
1 $.each(data.list,function(key, value){ 2 turnplate.restaraunts.push(value.data0); 3 turnplate.lucky.push(value.data1); 4 turnplate.goodsimgArr.push(getLuckyImg + value.data4); 5 if(key %2==0) 6 turnplate.colors.push("#fff"); 7 else 8 turnplate.colors.push("#5fcbd4"); 9 })
data.list是我獲取來的獎品json數據:
1 [ 2 { 3 "data0":"一等獎", 4 "data1":"iphone6s", 5 "data2":"0", 6 "data3":"0", 7 "data4":"201510161406303384.png", 8 "data5":"XXXX網絡科技", 9 "data6":"浙江省衢州市柯城區XXXXX", 10 "data7":"0570-XXXXXX" 11 },...... 12 ]
由於客戶要求獎品沒有“謝謝參與”,所以最低獎品也為“優勝獎”,所以在遍歷獎品之后,插入有關“優勝獎”的渲染描述即可:
1 turnplate.goodsimgArr.push('../images/hongbao.png') 2 turnplate.restaraunts.push("優勝獎"); 3 turnplate.colors.push("#5fcbd4"); 4 //頁面所有元素加載完畢后執行drawRouletteWheel()方法對轉盤進行渲染 5 preloadimages(turnplate.goodsimgArr).done(function(images){ 6 drawRouletteWheel(); 7 });
因為圖片加載需要時間,而使用canvas復制圖片需要圖片加載完成后才能繪制,所以我使用了preloadimages,讓所有獎品圖片都加載完畢后進行大轉盤的渲染工作:
1 //對獎品圖片預加載 2 function preloadimages(arr){ 3 var newimages =[], loadedimages =0 4 var postaction =function(){}//此處增加了一個postaction函數 5 var arr =(typeof arr !="object")?[arr]: arr 6 function imageloadpost(){ 7 loadedimages++ 8 if(loadedimages == arr.length){ 9 postaction(newimages)//加載完成用我們調用postaction函數並將newimages數組做為參數傳遞進去 10 } 11 } 12 for(var i =0; i < arr.length; i++){ 13 newimages[i]=newImage() 14 newimages[i].src = arr[i] 15 newimages[i].onload =function(){ 16 imageloadpost() 17 } 18 newimages[i].onerror =function(){ 19 imageloadpost() 20 } 21 } 22 return{//此處返回一個空白對象的done方法 23 done:function(f){ 24 postaction = f || postaction 25 } 26 } 27 }
繪制轉盤代碼:
1 function drawRouletteWheel(){ 2 var canvas = document.getElementById("wheelcanvas"); 3 if(canvas.getContext){ 4 //根據獎品個數計算圓周角度 5 var arc =Math.PI /(turnplate.restaraunts.length /2); 6 var ctx = canvas.getContext("2d"); 7 //在給定矩形內清空一個矩形 8 ctx.clearRect(0,0,422,422); 9 //strokeStyle 屬性設置或返回用於筆觸的顏色、漸變或模式 10 ctx.strokeStyle ="rgba(0,0,0,0)"; 11 //font 屬性設置或返回畫布上文本內容的當前字體屬性 12 ctx.font ='bold 18px Microsoft YaHei'; 13 for(var i =0; i < turnplate.restaraunts.length; i++){ 14 //根據當前獎品索引 計算繪制的扇形開始弧度 15 var angle = turnplate.startAngle + i * arc; 16 //根據獎品參數 繪制扇形填充顏色 17 ctx.fillStyle = turnplate.colors[i]; 18 //開始繪制扇形 19 ctx.beginPath(); 20 //arc(x,y,r,起始角,結束角,繪制方向) 方法創建弧/曲線(用於創建圓或部分圓) 21 //繪制大圓 22 ctx.arc(212,212, turnplate.outsideRadius, angle, angle + arc,false); 23 //繪制小圓 24 ctx.arc(212,212, turnplate.insideRadius, angle + arc, angle,true); 25 ctx.stroke(); 26 ctx.fill(); 27 //鎖畫布(為了保存之前的畫布狀態) 28 ctx.save(); 29 //----繪制獎品開始---- 30 //獎品默認字體顏色 31 ctx.fillStyle ="#fff"; 32 var text = turnplate.restaraunts[i]; 33 var lukyname = turnplate.lucky[i]; 34 var line_height =17; 35 //translate方法重新映射畫布上的 (0,0) 位置 36 ctx.translate(212+Math.cos(angle + arc /2)* turnplate.textRadius,212+Math.sin(angle + arc /2)* turnplate.textRadius); 37 //rotate方法旋轉當前的繪圖 38 ctx.rotate(angle + arc /2+Math.PI /2); 39 //繪制獎品圖片 40 var img =newImage(); 41 img.src = turnplate.goodsimgArr[i]; 42 ctx.drawImage(img,-17,35); 43 //由於設計的轉盤色塊是交錯的,所以這樣可以實現相鄰獎品區域字體顏色不同 44 if(i %2==0){ 45 ctx.fillStyle ="#f7452f"; 46 } 47 //將字體繪制在對應坐標 48 ctx.fillText(text,-ctx.measureText(text).width /2,0); 49 //設置字體 50 ctx.font =' 14px Microsoft YaHei'; 51 //繪制獎品名稱 52 if(text !="優勝獎"){ 53 ctx.fillText(lukyname,-ctx.measureText(lukyname).width /2,25); 54 }else{ 55 ctx.fillText("優麥幣",-ctx.measureText("優麥幣").width /2,25); 56 } 57 //把當前畫布返回(插入)到上一個save()狀態之前 58 ctx.restore(); 59 ctx.save(); 60 //----繪制獎品結束---- 61 } 62 } 63 }
每一步基本上都有注釋,對於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>
效果圖:

點擊事件執行代碼:
1 $('.lunck_draw_wrap').delegate("img.pointer","click",function(){ 2 if(turnplate.bRotate)return; 3 turnplate.bRotate =!turnplate.bRotate; 4 $.getJSON("../AJAX/lottery.ashx","",function(data){ 5 //1090系統配置錯誤,1091用戶未登陸或用戶數據異常,1092用戶剩余積分不足,1093未中獎 6 hideInput("code",data.code) 7 if(data.code.toString()=="1090"){ 8 iosalert("系統配置錯誤") 9 }elseif(data.code.toString()=="1091"){ 10 iosalert("用戶未登陸或用戶數據異常") 11 }elseif(data.code.toString()=="1092"){ 12 iosalert("用戶剩余積分不足") 13 }elseif(data.code.toString()=="1094"){ 14 iosalert("超過每日抽獎次數") 15 } 16 else{ 17 var upoint =0; 18 upoint = parseInt($("#uPoint").html())- parseInt($("#sPoint").html()); 19 $("#uPoint").html(upoint); 20 if(data.isWin =='true'){ 21 item = getArrayIndex(turnplate.restaraunts, data.name); 22 rotateFn(item +1,"恭喜獲得,"+ turnplate.restaraunts[item]); 23 } 24 else{ 25 rotateFn(0,"恭喜獲得優勝獎!"); 26 } 27 } 28 }) 29 });
上面的代碼實現了基本上的邏輯,還需要一個轉動轉盤的方法來響應服務端傳過來的結果:
1 //旋轉轉盤 item:獎品位置; txt:提示語; 2 var rotateFn =function(item, txt){ 3 //根據傳進來的獎品序號 計算相應的弧度 4 var angles = item *(360/ turnplate.restaraunts.length)-(360/(turnplate.restaraunts.length *2)); 5 if(angles <270){ 6 angles =270- angles; 7 }else{ 8 angles =360- angles +270; 9 } 10 //強制停止轉盤的轉動 11 $('#wheelcanvas').stopRotate(); 12 //調用轉動方法,設置轉動所需參數和回調函數 13 $('#wheelcanvas').rotate({ 14 //起始角度 15 angle:0, 16 //轉動角度 +1800是為了多轉幾圈 17 animateTo: angles +1800, 18 duration:8000, 19 callback:function(){ 20 iosSuccess(txt); 21 turnplate.bRotate =!turnplate.bRotate; 22 if($("#code").val()!="1093"){ 23 delayLoad(getHttpPrefix +"graphicdetails.html?lukyid="+ $("#code").val()) 24 } 25 } 26 }); 27 };
好了 主要的功能代碼都已分享完畢了,還有些工具方法不理解的,可以留言 我會補充進去的。
總結
canvas是html5很強大的一張王牌,可以實現許多絢麗的效果,希望本文可以幫到一些正在學習使用canvas的朋友們
自己也是將代碼分享出來有利於自己的代碼總結和梳理。