|
| 1 | +/** |
| 2 | + * |
| 3 | + * @authors Your Name ([email protected]) |
| 4 | + * @date 2017-10-30 11:13:20 |
| 5 | + * @version $Id$ |
| 6 | + */ |
| 7 | +$(function(){ |
| 8 | + const color = ["#626262","#787878","rgba(0,0,0,0.5)","#DCC722","white","#FF4350"], |
| 9 | + info = ["谢谢参与"," 1000"," 10"," 500"," 100"," 4999"," 1"," 20"], |
| 10 | + info1 = ['再接再厉',' 元',' 元',' 淘金币',' 元',' 淘金币',' 元',' 淘金币']; |
| 11 | + let clickNum=5,//可抽奖次数 |
| 12 | + rotateNum=0,//旋转次数 |
| 13 | + angles,//旋转角度 |
| 14 | + notice;//中奖公告 |
| 15 | + $('#btn').on('click',function(){ |
| 16 | + if(clickNum>=1){ |
| 17 | + clickNum--; |
| 18 | + runCup(); |
| 19 | + //转盘旋转过程“开始抽奖”按钮无法点击 |
| 20 | + $("#btn").attr('disabled',true); |
| 21 | + rotateNum++; |
| 22 | + //“开始抽奖”按钮无法点击恢复点击 |
| 23 | + setTimeout(function(){ |
| 24 | + alert(notice); |
| 25 | + $('#btn').removeAttr("disabled", true); |
| 26 | + },6000); |
| 27 | + }else{ |
| 28 | + alert("亲,抽奖次数已用光!"); |
| 29 | + |
| 30 | + } |
| 31 | + }); |
| 32 | + //转盘旋转 |
| 33 | + function runCup(){ |
| 34 | + probability(); |
| 35 | + const degValue = 'rotate('+angles+'deg'+')'; |
| 36 | + $("#canvas").css('transform',degValue); |
| 37 | + } |
| 38 | + //各奖项对应的旋转角度及中奖公告内容 |
| 39 | + function probability(){ |
| 40 | + //获取随机数 |
| 41 | + let num=parseInt(Math.random()*7); |
| 42 | + switch (num) { |
| 43 | + case 0: angles=2160*rotateNum+ 1800;notice = info[0] + info1[0]; |
| 44 | + break; |
| 45 | + case 1: angles=2160*rotateNum+ 1845;notice = info[7] + info1[7]; |
| 46 | + break; |
| 47 | + case 2: angles=2160*rotateNum+ 1890;notice = info[6] + info1[6]; |
| 48 | + break; |
| 49 | + case 3: angles=2160*rotateNum+ 1935;notice = info[5] + info1[5]; |
| 50 | + break; |
| 51 | + case 4: angles=2160*rotateNum+ 1980;notice = info[4] + info1[4]; |
| 52 | + break; |
| 53 | + case 5: angles=2160*rotateNum+ 2025;notice = info[3] + info1[3]; |
| 54 | + break; |
| 55 | + case 6: angle=2160*rotateNum+ 2070;notice = info[2] + info1[2]; |
| 56 | + break; |
| 57 | + case 7: angles=2160*rotateNum+ 2115;notice = info[1] + info1[1]; |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + circle(); |
| 62 | + function circle(){ |
| 63 | + const ctx=$("#canvas")[0].getContext('2d'), |
| 64 | + ctx1=$("#canvas1")[0].getContext('2d'), |
| 65 | + ctx2=$("#canvas2")[0].getContext('2d'), |
| 66 | + ctx3=$("#canvas3")[0].getContext('2d'); |
| 67 | + console.log($("#canvas"));//$("#canvas")获取到的是所有id为canvas的元素,是一个集合 |
| 68 | + createCricle(); |
| 69 | + createCirText(); |
| 70 | + initPoint(); |
| 71 | + //绘制外面的圆 |
| 72 | + function createCricle(){ |
| 73 | + let startAngle=0;//扇形开始的角度 |
| 74 | + let endAngle=0;//扇形结束的角度 |
| 75 | + for(let i=0;i<8;i++){ |
| 76 | + startAngle=Math.PI*(i/4-1/8); |
| 77 | + endAngle=startAngle+Math.PI*(1/4); |
| 78 | + ctx.save(); |
| 79 | + ctx.beginPath(); |
| 80 | + ctx.arc(150,150,100,startAngle,endAngle,false); |
| 81 | + ctx.lineWidth=120; |
| 82 | + i%2==0?(ctx.strokeStyle=color[0]):(ctx.strokeStyle=color[1]); |
| 83 | + ctx.stroke(); |
| 84 | + ctx.restore(); |
| 85 | + } |
| 86 | + } |
| 87 | + function createCirText(){ |
| 88 | + ctx.textAlign='start'; |
| 89 | + ctx.textBaseline='middle'; |
| 90 | + ctx.fillStyle=color[3]; |
| 91 | + const step=Math.PI/4; |
| 92 | + for(let i=0;i<8;i++){ |
| 93 | + ctx.save(); |
| 94 | + ctx.beginPath(); |
| 95 | + ctx.translate(150,150); |
| 96 | + ctx.rotate(i*step); |
| 97 | + ctx.font = " 20px Microsoft YaHei"; |
| 98 | + ctx.fillStyle = color[3]; |
| 99 | + ctx.fillText(info[i],-30,-115,60); |
| 100 | + ctx.font = " 14px Microsoft YaHei"; |
| 101 | + ctx.fillText(info1[i],-30,-95,60); |
| 102 | + ctx.closePath(); |
| 103 | + ctx.restore(); |
| 104 | + } |
| 105 | + } |
| 106 | + //转盘的指针 |
| 107 | + function initPoint(){ |
| 108 | + //箭头指针 |
| 109 | + ctx1.beginPath(); |
| 110 | + ctx1.moveTo(100,24); |
| 111 | + ctx1.lineTo(90,62); |
| 112 | + ctx1.lineTo(110,62); |
| 113 | + ctx1.lineTo(100,24); |
| 114 | + ctx1.fillStyle = color[5]; |
| 115 | + ctx1.fill(); |
| 116 | + ctx1.closePath(); |
| 117 | + //中间小圆 |
| 118 | + ctx3.beginPath(); |
| 119 | + ctx3.arc(100,100,40,0,Math.PI*2,false); |
| 120 | + ctx3.fillStyle = color[5]; |
| 121 | + ctx3.fill(); |
| 122 | + ctx3.closePath(); |
| 123 | + //小圆文字 |
| 124 | + ctx3.font = "Bold 20px Microsoft YaHei"; |
| 125 | + ctx3.textAlign='start'; |
| 126 | + ctx3.textBaseline='middle'; |
| 127 | + ctx3.fillStyle = color[4]; |
| 128 | + ctx3.beginPath(); |
| 129 | + ctx3.fillText('开始',80,90,40); |
| 130 | + ctx3.fillText('抽奖',80,110,40); |
| 131 | + ctx3.fill(); |
| 132 | + ctx3.closePath(); |
| 133 | + //中间圆圈 |
| 134 | + ctx2.beginPath(); |
| 135 | + ctx2.arc(75,75,75,0,Math.PI*2,false); |
| 136 | + ctx2.fillStyle = color[2]; |
| 137 | + ctx2.fill(); |
| 138 | + ctx2.closePath(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | +}) |
0 commit comments