茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    javascrip+HTML5 Canvas繪制轉(zhuǎn)盤抽獎(jiǎng)
    來源:易賢網(wǎng) 閱讀:1428 次 日期:2016-07-08 09:54:03
    溫馨提示:易賢網(wǎng)小編為您整理了“javascrip+HTML5 Canvas繪制轉(zhuǎn)盤抽獎(jiǎng)”,方便廣大網(wǎng)友查閱!

    這篇文章主要介紹了javascrip+HTML5 Canvas繪制轉(zhuǎn)盤抽獎(jiǎng)的相關(guān)資料,需要的朋友可以參考下

    之前做過的項(xiàng)目中,有需要抽獎(jiǎng)轉(zhuǎn)盤功能的。項(xiàng)目已經(jīng)完工一段時(shí)間了,也沒出現(xiàn)什么嚴(yán)重的bug,所以現(xiàn)在拎出來分享給大家。

    功能需求

    1、轉(zhuǎn)盤要美觀,轉(zhuǎn)動(dòng)效果流暢。

    2、轉(zhuǎn)盤上需要顯示獎(jiǎng)品圖片,并且獎(jiǎng)品是后臺(tái)讀取的照片和名字。

    3、轉(zhuǎn)動(dòng)動(dòng)畫完成后要有相應(yīng)提示。

    4、獲取的獎(jiǎng)品具體算法在數(shù)據(jù)庫里操作,前端只提供最后的效果展示。 

    知識(shí)要點(diǎn)

    1、引用了一個(gè)jq插件:awardRotate,用來實(shí)現(xiàn)更智能化的轉(zhuǎn)動(dòng)(插件下載:http://www.jqcool.net/jquery-jqueryrotate.html)。

    2、使用canvas標(biāo)簽和對(duì)應(yīng)的html5 api 進(jìn)行操作。(canvas中文手冊(cè)可以查看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)盤獎(jiǎng)品名稱

     lucky:[],//獎(jiǎng)品內(nèi)容

     colors:[],//大轉(zhuǎn)盤獎(jiǎng)品區(qū)塊對(duì)應(yīng)背景顏色

     goodsimgArr:[],//獎(jiǎng)品圖片頁面標(biāo)簽

     outsideRadius:175,//大轉(zhuǎn)盤外圓的半徑

     textRadius:140,//大轉(zhuǎn)盤獎(jiǎng)品位置距離圓心的距離

     insideRadius:65,//大轉(zhuǎn)盤內(nèi)圓的半徑

     startAngle:0,//開始角度

     bRotate:false//false:停止;ture:旋轉(zhuǎn)

     };

     由參數(shù)可知,我們需要從服務(wù)端獲取相應(yīng)的獎(jiǎng)品名稱,獎(jiǎng)品內(nèi)容,獎(jiǎng)品圖片頁面標(biāo)簽等信息,再對(duì)大轉(zhuǎn)盤進(jìn)行渲染。

    所以我們的第一步操作就是向服務(wù)端發(fā)送請(qǐng)求獲取對(duì)應(yīng)的獎(jiǎ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是我獲取來的獎(jiǎng)品json數(shù)據(jù):

    [

     {

     "data0":"一等獎(jiǎng)",

     "data1":"iphone6s",

     "data2":"0",

     "data3":"0",

     "data4":"201510161406303384.png",

     "data5":"XXXX網(wǎng)絡(luò)科技",

     "data6":"浙江省衢州市柯城區(qū)XXXXX",

     "data7":"0570-XXXXXX"

     },......

     ]

    由于客戶要求獎(jiǎng)品沒有“謝謝參與”,所以最低獎(jiǎng)品也為“優(yōu)勝獎(jiǎng)”,所以在遍歷獎(jiǎng)品之后,插入有關(guān)“優(yōu)勝獎(jiǎng)”的渲染描述即可:

    turnplate.goodsimgArr.push('../images/hongbao.png')

     turnplate.restaraunts.push("優(yōu)勝獎(jiǎng)");

     turnplate.colors.push("#5fcbd4");

     //頁面所有元素加載完畢后執(zhí)行drawRouletteWheel()方法對(duì)轉(zhuǎn)盤進(jìn)行渲染

     preloadimages(turnplate.goodsimgArr).done(function(images){

     drawRouletteWheel();

     });

    因?yàn)閳D片加載需要時(shí)間,而使用canvas復(fù)制圖片需要圖片加載完成后才能繪制,所以我使用了preloadimages,讓所有獎(jiǎng)品圖片都加載完畢后進(jìn)行大轉(zhuǎn)盤的渲染工作:

    //對(duì)獎(jiǎng)品圖片預(yù)加載

     function preloadimages(arr){

     var newimages =[], loadedimages =0

     var postaction =function(){}//此處增加了一個(gè)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ù)傳遞進(jìn)去

     }

     }

     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{//此處返回一個(gè)空白對(duì)象的done方法

     done:function(f){

     postaction = f || postaction

     }

     }

     }

    繪制轉(zhuǎn)盤代碼:

    function drawRouletteWheel(){

     var canvas = document.getElementById("wheelcanvas");

     if(canvas.getContext){

     //根據(jù)獎(jiǎng)品個(gè)數(shù)計(jì)算圓周角度

     var arc =Math.PI /(turnplate.restaraunts.length /2);

     var ctx = canvas.getContext("2d");

     //在給定矩形內(nèi)清空一個(gè)矩形

     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)前獎(jiǎng)品索引 計(jì)算繪制的扇形開始弧度

     var angle = turnplate.startAngle + i * arc;

     //根據(jù)獎(jiǎng)品參數(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();

     //----繪制獎(jiǎng)品開始----

     //獎(jiǎng)品默認(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);

     //繪制獎(jiǎng)品圖片

     var img =newImage();

     img.src = turnplate.goodsimgArr[i];

     ctx.drawImage(img,-17,35);

     //由于設(shè)計(jì)的轉(zhuǎn)盤色塊是交錯(cuò)的,所以這樣可以實(shí)現(xiàn)相鄰獎(jiǎng)品區(qū)域字體顏色不同

     if(i %2==0){

     ctx.fillStyle ="#f7452f";

     }

     //將字體繪制在對(duì)應(yīng)坐標(biāo)

     ctx.fillText(text,-ctx.measureText(text).width /2,0);

     //設(shè)置字體

     ctx.font =' 14px Microsoft YaHei';

     //繪制獎(jiǎng)品名稱

     if(text !="優(yōu)勝獎(jiǎng)"){

     ctx.fillText(lukyname,-ctx.measureText(lukyname).width /2,25);

     }else{

     ctx.fillText("優(yōu)麥幣",-ctx.measureText("優(yōu)麥幣").width /2,25);

     }

     //把當(dāng)前畫布返回(插入)到上一個(gè)save()狀態(tài)之前

     ctx.restore();

     ctx.save();

     //----繪制獎(jiǎng)品結(jié)束----

     }

     }

     }

    每一步基本上都有注釋,對(duì)于canvas方法有不理解的可以百度,或者查詢我上面分享的中文手冊(cè)。

    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>

     效果圖:

    名單

    點(diǎn)擊事件執(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)配置錯(cuò)誤,1091用戶未登陸或用戶數(shù)據(jù)異常,1092用戶剩余積分不足,1093未中獎(jiǎng)

     hideInput("code",data.code)

     if(data.code.toString()=="1090"){

     iosalert("系統(tǒng)配置錯(cuò)誤")

     }elseif(data.code.toString()=="1091"){

     iosalert("用戶未登陸或用戶數(shù)據(jù)異常")

     }elseif(data.code.toString()=="1092"){

     iosalert("用戶剩余積分不足")

     }elseif(data.code.toString()=="1094"){

     iosalert("超過每日抽獎(jiǎng)次數(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)勝獎(jiǎng)!");

     }

     }

     })

     });

    上面的代碼實(shí)現(xiàn)了基本上的邏輯,還需要一個(gè)轉(zhuǎn)動(dòng)轉(zhuǎn)盤的方法來響應(yīng)服務(wù)端傳過來的結(jié)果:

    //旋轉(zhuǎn)轉(zhuǎn)盤 item:獎(jiǎng)品位置; txt:提示語;

     var rotateFn =function(item, txt){

     //根據(jù)傳進(jìn)來的獎(jiǎng)品序號(hào) 計(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;

     }

     //強(qiáng)制停止轉(zhuǎn)盤的轉(zhuǎn)動(dòng)

     $('#wheelcanvas').stopRotate();

     //調(diào)用轉(zhuǎn)動(dòng)方法,設(shè)置轉(zhuǎn)動(dòng)所需參數(shù)和回調(diào)函數(shù)

     $('#wheelcanvas').rotate({

     //起始角度

     angle:0,

     //轉(zhuǎn)動(dòng)角度 +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())

     }

     }

     });

     };

    好了 主要的功能代碼都已分享完畢了,還有些工具方法不理解的,可以留言 我會(huì)補(bǔ)充進(jìn)去的。

    總結(jié)

    canvas是html5很強(qiáng)大的一張王牌,可以實(shí)現(xiàn)許多絢麗的效果,希望本文可以幫到一些正在學(xué)習(xí)使用canvas的朋友們。

    更多信息請(qǐng)查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機(jī)網(wǎng)站地址:javascrip+HTML5 Canvas繪制轉(zhuǎn)盤抽獎(jiǎng)
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2026上岸·考公考編培訓(xùn)報(bào)班

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)