首页 > 代码库 > canvas转盘抽奖

canvas转盘抽奖

  1 <!DOCTYPE html>
  2 <html>
  3 <head>
  4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5 <title>html5 canvas圆形转盘抽奖特</title>
  6 </head>
  7 <body>
  8     <div style="width:400px;margin:40px auto 0 auto;">
  9         <canvas id=‘bg‘></canvas>
 10     </div>
 11     <script type="text/javascript">
 12         var fillStyle = 
 13                 //[‘rgb(255,2,0)‘,‘rgb(246,150,30)‘,‘rgb(255,255,1)‘,‘rgb(154,205,52)‘,‘rgb(34,145,103)‘,‘rgb(30,156,144)‘,‘rgb(2,128,255)‘,‘rgb(53,52,154)‘,‘rgb(128,1,128)‘,‘rgb(200,23,131)‘]
 14                 [‘rgb(220,0,140)‘,‘rgb(250,210,50)‘,‘rgb(220,0,140)‘,‘rgb(250,210,50)‘,‘rgb(220,0,140)‘,‘rgb(250,210,50)‘,‘rgb(220,0,140)‘,‘rgb(250,210,50)‘,‘rgb(220,0,140)‘,‘rgb(250,210,50)‘]
 15             ,fillText = [‘一等奖‘,‘二等奖‘,‘三等奖‘,‘四等奖‘,‘五等奖‘,‘六等奖‘,‘七等奖‘,‘八等奖‘,‘九等奖‘,‘十等奖‘]
 16             ,width = 400
 17             ,height = 400
 18             ,c_x = 200
 19             ,c_y =200
 20             ,radius = 180 // 圆盘半径
 21             ,canvas = document.getElementById(‘bg‘)
 22             ,index =0
 23             ,timer = null
 24             ,running = false // 是否运行中
 25             ,speed = 300 // 速度
 26             ,isBeginPrize = false // 是否开始抽奖
 27             ,stepping=0 // 步数,经过一个扇形为1步
 28             ,basecircle = 3 // 点击开始时,圆盘旋转的圈数,旋转玩指定圈数之后,再根据selected的值确定奖项
 29             ,selected =0; // 最终选中第几个扇形,也就是确定几等奖
 30             
 31         function setup(){
 32             drawCircle(false);
 33         }
 34         
 35         function drawCircle(isRunning){
 36             var deg = Math.PI/300;
 37             var startAngle = 0;
 38             var endAngle = 58;
 39             canvas.height = height;
 40             canvas.width = width;
 41             var ctx=canvas.getContext(‘2d‘);
 42             for(var i=0;i<10;i++){
 43                 ctx.beginPath();
 44                 // 正在运行的时候,改变当前扇形的颜色
 45                 if(isRunning && index==i)
 46                 {
 47                     ctx.fillStyle = ‘rgb(255,0,0)‘;
 48                 }
 49                 else
 50                 {
 51                     ctx.fillStyle = fillStyle[i];
 52                 }
 53         
 54                 // 绘制扇形
 55                 ctx.moveTo(c_x,c_y);
 56                 ctx.arc(c_x,c_y,radius,deg*startAngle,deg*endAngle,false);
 57                 ctx.fill();
 58         
 59                 // 绘制扇形上的文字
 60                 ctx.font="12px Microsoft YaHei";
 61                 ctx.fillStyle = ‘#fff‘;
 62                 ctx.textAlign = "center";
 63                 ctx.fillText(fillText[i],200+Math.cos(deg*(startAngle+30))*150,200+Math.sin(deg*(startAngle+30))*150);
 64                 startAngle +=60;
 65                 endAngle +=60;
 66             }
 67         
 68             // 绘制中心圆
 69             ctx.beginPath();
 70             ctx.arc(200,200,50,0,2*Math.PI,1);
 71             ctx.fillStyle = ‘rgb(255,255,255)‘;
 72             ctx.fill();
 73             // 绘制中心圆
 74             ctx.font="15px Microsoft YaHei";
 75             // 创建渐变
 76             var gradient=ctx.createLinearGradient(0,0,width,0);
 77             gradient.addColorStop("0","magenta");
 78             gradient.addColorStop("0.2","blue");
 79             gradient.addColorStop("0.4","red");
 80             // 用渐变填色
 81             ctx.textAlign = "center";
 82             ctx.fillStyle=gradient;
 83             ctx.fillText("start",200,195+0);
 84             ctx.fillText("立即抽奖",200,200+20);
 85             // 绘制中心园边框
 86             ctx.strokeStyle = ‘rgb(0,0,0)‘;
 87             ctx.lineWidth = 1;
 88             ctx.stroke();
 89         }
 90         
 91         function rotate(){
 92             if(stepping==5){ // 4步之后开始加速
 93                 clearTimer();
 94                 speed = +100;
 95                 timer = setInterval(rotate,speed);
 96             }
 97         
 98             if(basecircle>0 && index ==10){ // 基本圈数结束以后,开始随机抽奖
 99                 index = 0;
100                 basecircle--;
101                 if(basecircle == 0) // 开始随机抽奖
102                 {
103                     clearTimer();
104                     speed = 300;
105                     timer = setInterval(rotate,speed);
106                     isBeginPrize = true;              
107                 }
108             }
109         
110             if(isBeginPrize && selected > 0) // 开始抽奖
111             {
112                 if(--selected == 0) //到了选择的奖项之后
113                 {
114                     clearTimer();
115                     isStop = true;
116                 }
117                 else
118                 {
119                     clearTimer();
120                     speed += 100;
121                     timer = setInterval(rotate,speed);
122                 }
123             }
124             drawCircle(true);
125             index++;
126             stepping++;
127         }
128         
129         // 初始化抽奖参数
130         function init()
131         {
132             basecircle = 5;
133              selected =  (Math.floor(Math.random() * 10) + 1 );//输出1-10的随机数
134             running= false;
135             isBeginPrize = false;
136             index = index++;
137             stepping = 0;
138             speed = 300;
139         }
140         
141         function mouseDown_Start(e){
142             var local = getPointOnCanvas(canvas, e.pageX, e.pageY);
143             if(local.x > 100 && local.x < 300 && local.y>100 && local.y<300) // 中心圆区域
144             {
145                 if(running){
146                     return;
147                 }
148                 init();
149                 timer = setInterval(rotate,speed);
150             }
151         }
152         
153         function clearTimer(){
154             clearInterval(timer);
155             timer = null;
156         }
157         
158         function getPointOnCanvas(canvas, x, y) {
159             var bbox =canvas.getBoundingClientRect();
160             return { x:x- bbox.left *(canvas.width / bbox.width),
161                 y:y - bbox.top  * (canvas.height / bbox.height)
162             };
163         }
164         setup();
165         canvas.addEventListener("mousedown",mouseDown_Start,false);
166     </script>
167 </div>
168 </body>
169 </html>

 

canvas转盘抽奖