年底没啥,抽空学习了canvas,写了个html5抽奖的代码,造了个轮子,有用的童鞋可以拿走。

其中,canvas.onclick触发抽奖行为,概率可以在core.lottery()函数上添加,美化也可以替换上图,嘿嘿,给大家做个事例,如有bug,请以评论的形式提出。

 代码如下 复制代码

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>html5圆形抽奖 - zkeyword.com</title>
</head>
<body>
<canvas id="canvas" style="border:1px solid #ddd"></canvas>
</body>
<script>
var lottery = (function(){

var canvas     = document.getElementById('canvas'),
            ctx        = canvas.getContext('2d'),
            width      = 400,
            height     = 400,
            x          = 200,
            y          = 200,
            color      = ['#a00','#0a0','#00a','#aa0','#a16','#0aa','#a5a','#f31','#784','#a43','#34a','#786'],
            deg        = Math.PI / 180,
            isClick    = true,
            startTimer = null,
            shopTimer  = null,
            i          = 0,
            len        = 12
            speed      = 340;

canvas.width  = width;
 canvas.height = height;

var core = {
  init: function(i){
   core.bg();
   core.proint(i);
   core.btn();
  },

bg: function(){
   var i = 0;

ctx.save();
   for(; i < len; i++){
    ctx.beginPath();
    ctx.fillStyle = color[i];
    ctx.moveTo(x, y);
    ctx.arc(x, y, 180, deg * i * 30,  deg * (i+1) * 30);
    ctx.fill();
    ctx.closePath();
   }
   ctx.restore();
  },

proint: function(i){
   ctx.save();
   ctx.beginPath();
   ctx.fillStyle = '#333';
   ctx.moveTo(x, y);
   ctx.arc(x, y, 180, deg * i * 30,  deg * (i+1) * 30);
   ctx.fill();
   ctx.closePath();
   ctx.restore();
  },

btn: function(){
   ctx.beginPath();
   ctx.fillStyle = '#555';
   ctx.arc(x, y, 50, 0, Math.PI*2);
   ctx.fill();
   ctx.closePath();
   ctx.restore();
  },www.111cn.net

clear: function(){
   ctx.clearRect(0, 0, 250, 250);
  },

/*开始加速*/
  start: function(){
   i          = (i === 12) ? 0 : i;
   speed     = (speed !== 100) ? (speed - 20) : 100;
   startTimer = setTimeout(function(){
                                        core.start();
                                     }, speed);

core.clear();
   core.init(i);
   i++;
   isClick = false;
  },

/*匀速运动,指定指针*/
  move: function(h){
   var val  = 12 - Math.abs(h + 1 - i);

if( startTimer ) clearTimeout(startTimer);
   if( val !== 12 ){
    i = (i === 12) ? 0 : i;
    var timer = setTimeout(function(){
                                                core.move(h);
                                     }, speed);

core.clear();
    core.init(i);
    i++;
   }else{
    core.stop();
   }
  },

/*停止减速*/
  stop: function(){
   if( speed !== 340 ){
    i = (i === 12) ? 0 : i;
    speed += 20;
    shopTimer = setTimeout(function(){
                                                core.stop();
         }, speed);

core.clear();
    core.init(i);
    i++;
   }else{
    if( shopTimer ) clearTimeout(shopTimer);
    core.callback(i);
    i = 0;
    isClick = true;
   }
  },

callback: function(i){
   console.log(i,'中奖了')
  },

random: function(min, max){
   return Math.floor(min + Math.random()*(max-min));
  },

/*抽奖,概率算法*/
  lottery: function(){
   var s = core.random(1, 10),
       y = core.random(1, 1000);

if( s === 1 ){
       core.move(1);
   }else if( y === 1 ){
                            core.move(1);
   }else{
       core.move(0);
   }
   console.log(s, y)
  }
 };

core.init(0);

canvas.onclick = function(e){
  if( isClick ){
   core.start();
   setTimeout(function(){
       core.lottery();
       }, 5000);
  }
  //else if( !isClick && speed === 100 ){
  //}
 }

})();
</script>
</html>

 
你可能感兴趣的文章

html5 canvas 圆形抽奖的实例的更多相关文章

  1. HTML5 Canvas圆盘抽奖应用(适用于Vue项目)

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...

  2. HTML5 Canvas图片操作简单实例1

    1.加载显示图片 <canvas id="canvasOne" class="myCanvas" width="500" height ...

  3. HTML5 canvas 圆盘抽奖

    使用html5 canvas 绘制的圆盘抽奖程序 效果图: 贴上全部代码:  1 <!DOCTYPE html> <html> <head> <meta ch ...

  4. html5 canvas 圆形径向渐变

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. HTML5 Canvas Text文本居中实例

    1.代码: <canvas width="700" height="300" id="canvasOne" class="c ...

  6. HTML5 Canvas显示本地图片实例1、Canvas预览图片实例1

    1.前台代码: <input id="fileOne" type="file" /> <canvas id="canvasOne&q ...

  7. canvas转盘抽奖

    1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" ...

  8. 16个富有创意的HTML5 Canvas动画特效集合

    HTML5技术正在不断的发展和更新,越来越多的开发者也正在加入HTML5阵营,甚至在移动开发上HTML5的地位也是越来越重要了.HTML5中的大部分动画都是通过Canvas实现,因为Canvas就像一 ...

  9. 16个非常有趣的HTML5 Canvas动画特效集合

    HTML5技术正在不断的发展和更新,越来越多的开发者也正在加入HTML5阵营,甚至在移动开发上HTML5的地位也是越来越重要了.HTML5中的大部分动画都是通过Canvas实现,因为Canvas就像一 ...

随机推荐

  1. 通过 监听器获取sessionId

    package shopping; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpSess ...

  2. 在DWZ框架中整合kindeditor复文本框控件

    今天上午在DWZ框架中整合kindeditor复文本框控件,发现上传图片是老是提示 “上传中,请稍候...”,上网查看别人说可能是文件路径问题,在想以前在其他项目中用这个控件一直没问题,到这里怎么会出 ...

  3. 看京东如何把Intel RealSense技术用在物流上

    提起Intel RealSense 3D 深度摄像头,总是离不开无人机.机器人等前沿硬件产品,比如 CES 2016 上让人眼前一亮的自动避障无人机Yuneec “台风 H”,Segway.Nineb ...

  4. Android学习笔记(3)

    今天我试着往应用里添加广告,结果adView一操作就闪退,换了很多种方法都不行. 最后解决过程有点坑爹,原来是还没setcontentview就开始adview了,哈哈 虽然我现在菜得不行,还没入门. ...

  5. Codeforces Round #185 (Div. 2) B. Archer 水题

    B. Archer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/312/problem/B D ...

  6. POJ 2406 Power Strings KMP运用题解

    本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都认为是能够利用next数组的.可是自己想了非常久没能这么简洁地总结出来,也仅仅能查查他人代码才恍 ...

  7. ListView添加项目带序列

    ListView添加项目带序列     function AddSelItems(listview1:TListView;ListView2:TListView):Boolean;var  s: st ...

  8. PhoneTutorial

    https://github.com/navasmdc/PhoneTutorial PhoneTutorial-master.zip

  9. 基于Linux的owncloud搭建

    为了保证一个纯净的环境,我重新安装了一台centos系统 [root@localhost httpd-2.2.23]# lsb_release -a LSB Version:    :base-4.0 ...

  10. [AngularJS] Javascript scope and AngularJS $scope

    Scope resolution of our Angular documents works exactly the same way scope resolution works in plain ...