星空网 > 软件开发 > Java

利用canvas实现抽奖转盘

之前做过的项目中,有需要抽奖转盘功能的。项目已经完工一段时间了,也没出现什么严重的bug,所以现在拎出来分享给大家。

 
功能需求
  1. 转盘要美观,转动效果流畅。
  2. 转盘上需要显示奖品图片,并且奖品是后台读取的照片和名字。
  3. 转动动画完成后要有相应提示。
  4. 获取的奖品具体算法在数据库里操作,前端只提供最后的效果展示。
 

知识要点
  1. 引用了一个jq插件:awardRotate,用来实现更智能化的转动(插件下载:http://www.jqcool.net/jquery-jqueryrotate.html)。
  2. 使用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   else8   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 || postaction25   }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/loading.gif' data-original="../images/chouzhang12.png"/>  <imgclass="pointer"src='/images/loading.gif' data-original="../images/hianji .png"/>  </div>  </div>

 


效果图:

利用canvas实现抽奖转盘
点击事件执行代码:
 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的朋友们
自己也是将代码分享出来有利于自己的代码总结和梳理。
 
 






原标题:利用canvas实现抽奖转盘

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

跨境小学:https://www.goluckyvip.com/tag/6097.html
法国esmod申请:https://www.goluckyvip.com/tag/60971.html
跨境电商之家:https://www.goluckyvip.com/tag/6098.html
赶跟卖被威胁:https://www.goluckyvip.com/tag/6099.html
深圳市税务局:https://www.goluckyvip.com/tag/61.html
Meesho融资:https://www.goluckyvip.com/tag/610.html
天坛最佳攻略 天坛必玩景点:https://www.vstour.cn/a/408240.html
央视新址为什么会找回:https://www.vstour.cn/a/408241.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流