Home > Article > Web Front-end > javascript+HTML5 Canvas drawing carousel lottery_javascript skills
Among the projects I have done before, some of them required the lottery wheel function. The project has been completed for a while, and there are no serious bugs, so I am sharing it with everyone.
Functional requirements
1. The turntable should be beautiful and the rotation effect should be smooth.
2. The prize picture needs to be displayed on the carousel, and the prize is the photo and name read in the background.
3. There should be corresponding prompts after the rotation animation is completed.
4. The specific algorithm of the prizes obtained is operated in the database, and the front end only provides the final effect display.
Key points of knowledge
1. Referenced a jq plug-in: awardRotate, used to achieve more intelligent rotation (plug-in download: http://www.jqcool.net/jquery-jqueryrotate.html).
2. Use the canvas tag and the corresponding html5 api to operate. (Canvas Chinese manual can be viewed http://javascript.ruanyifeng.com/htmlapi/canvas.html
Text
Quote big carousel style
.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%;}
Parameters required for the turntable plug-in:
var turnplate ={ restaraunts:[],//大转盘奖品名称 lucky:[],//奖品内容 colors:[],//大转盘奖品区块对应背景颜色 goodsimgArr:[],//奖品图片页面标签 outsideRadius:175,//大转盘外圆的半径 textRadius:140,//大转盘奖品位置距离圆心的距离 insideRadius:65,//大转盘内圆的半径 startAngle:0,//开始角度 bRotate:false//false:停止;ture:旋转 };
As can be seen from the parameters, we need to obtain the corresponding prize name, prize content, prize picture page label and other information from the server, and then render the big carousel.
So our first step is to send a request to the server to obtain the corresponding prize information, and traverse the array parameters required to generate the big carousel:
$.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 is the prize json data I obtained:
[ { "data0":"一等奖", "data1":"iphone6s", "data2":"0", "data3":"0", "data4":"201510161406303384.png", "data5":"XXXX网络科技", "data6":"浙江省衢州市柯城区XXXXX", "data7":"0570-XXXXXX" },...... ]
Since the customer requested the prize without "Thank you for participating", the minimum prize is also the "Winner Prize", so after traversing the prizes, just insert the rendering description of the "Winner Prize":
turnplate.goodsimgArr.push('../images/hongbao.png') turnplate.restaraunts.push("优胜奖"); turnplate.colors.push("#5fcbd4"); //页面所有元素加载完毕后执行drawRouletteWheel()方法对转盘进行渲染 preloadimages(turnplate.goodsimgArr).done(function(images){ drawRouletteWheel(); });
Because it takes time to load images, and using canvas to copy images requires the image to be loaded before it can be drawn, so I used preloadimages to render the big carousel after all the prize images have been loaded:
//对奖品图片预加载 function preloadimages(arr){ var newimages =[], loadedimages =0 var postaction =function(){}//此处增加了一个postaction函数 var arr =(typeof arr !="object")?[arr]: arr function imageloadpost(){ loadedimages++ if(loadedimages == arr.length){ postaction(newimages)//加载完成用我们调用postaction函数并将newimages数组做为参数传递进去 } } 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 } } }
Code for drawing the carousel:
function drawRouletteWheel(){ var canvas = document.getElementById("wheelcanvas"); if(canvas.getContext){ //根据奖品个数计算圆周角度 var arc =Math.PI /(turnplate.restaraunts.length /2); var ctx = canvas.getContext("2d"); //在给定矩形内清空一个矩形 ctx.clearRect(0,0,422,422); //strokeStyle 属性设置或返回用于笔触的颜色、渐变或模式 ctx.strokeStyle ="rgba(0,0,0,0)"; //font 属性设置或返回画布上文本内容的当前字体属性 ctx.font ='bold 18px Microsoft YaHei'; for(var i =0; i < turnplate.restaraunts.length; i++){ //根据当前奖品索引 计算绘制的扇形开始弧度 var angle = turnplate.startAngle + i * arc; //根据奖品参数 绘制扇形填充颜色 ctx.fillStyle = turnplate.colors[i]; //开始绘制扇形 ctx.beginPath(); //arc(x,y,r,起始角,结束角,绘制方向) 方法创建弧/曲线(用于创建圆或部分圆) //绘制大圆 ctx.arc(212,212, turnplate.outsideRadius, angle, angle + arc,false); //绘制小圆 ctx.arc(212,212, turnplate.insideRadius, angle + arc, angle,true); ctx.stroke(); ctx.fill(); //锁画布(为了保存之前的画布状态) ctx.save(); //----绘制奖品开始---- //奖品默认字体颜色 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方法旋转当前的绘图 ctx.rotate(angle + arc /2+Math.PI /2); //绘制奖品图片 var img =newImage(); img.src = turnplate.goodsimgArr[i]; ctx.drawImage(img,-17,35); //由于设计的转盘色块是交错的,所以这样可以实现相邻奖品区域字体颜色不同 if(i %2==0){ ctx.fillStyle ="#f7452f"; } //将字体绘制在对应坐标 ctx.fillText(text,-ctx.measureText(text).width /2,0); //设置字体 ctx.font =' 14px Microsoft YaHei'; //绘制奖品名称 if(text !="优胜奖"){ ctx.fillText(lukyname,-ctx.measureText(lukyname).width /2,25); }else{ ctx.fillText("优麦币",-ctx.measureText("优麦币").width /2,25); } //把当前画布返回(插入)到上一个save()状态之前 ctx.restore(); ctx.save(); //----绘制奖品结束---- } } }
Each step is basically commented. If you don’t understand the canvas method, you can use Baidu, or consult the Chinese manual I shared above.
The html code is:
<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>
Rendering:
Click event execution code:
$('.lunck_draw_wrap').delegate("img.pointer","click",function(){ if(turnplate.bRotate)return; turnplate.bRotate =!turnplate.bRotate; $.getJSON("../AJAX/lottery.ashx","",function(data){ //1090系统配置错误,1091用户未登陆或用户数据异常,1092用户剩余积分不足,1093未中奖 hideInput("code",data.code) if(data.code.toString()=="1090"){ iosalert("系统配置错误") }elseif(data.code.toString()=="1091"){ iosalert("用户未登陆或用户数据异常") }elseif(data.code.toString()=="1092"){ iosalert("用户剩余积分不足") }elseif(data.code.toString()=="1094"){ iosalert("超过每日抽奖次数") } 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,"恭喜获得优胜奖!"); } } }) });
The above code implements the basic logic, and also requires a method of rotating the turntable to respond to the results passed by the server:
//旋转转盘 item:奖品位置; txt:提示语; var rotateFn =function(item, txt){ //根据传进来的奖品序号 计算相应的弧度 var angles = item *(360/ turnplate.restaraunts.length)-(360/(turnplate.restaraunts.length *2)); if(angles <270){ angles =270- angles; }else{ angles =360- angles +270; } //强制停止转盘的转动 $('#wheelcanvas').stopRotate(); //调用转动方法,设置转动所需参数和回调函数 $('#wheelcanvas').rotate({ //起始角度 angle:0, //转动角度 +1800是为了多转几圈 animateTo: angles +1800, duration:8000, callback:function(){ iosSuccess(txt); turnplate.bRotate =!turnplate.bRotate; if($("#code").val()!="1093"){ delayLoad(getHttpPrefix +"graphicdetails.html?lukyid="+ $("#code").val()) } } }); };
Okay, the main function codes have been shared. If there are still some tools and methods that you don’t understand, you can leave a message and I will add them.
Summary
Canvas is a very powerful trump card of HTML5 and can achieve many gorgeous effects. I hope this article can help some friends who are learning to use canvas.