年底没啥,抽空学习了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. mysql case when 条件过滤

    [1].[代码] 使用CASE WHEN进行字符串替换处理 跳至 [1] [2] [3] [4] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2 ...

  2. wikioi 1154 能量项链

    题目描述 Description 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对于相邻的两颗珠子 ...

  3. Ping批量函数

    function pingm ($file){ $ips = gc $file foreach ($ip in $ips) { $cmdline +="ping " + $ip + ...

  4. 无废话C#设计模式系列文章

    不错的系列文章 原文:http://www.cnblogs.com/lovecherry/archive/2007/10/17/927728.html 本系列文章从公司内部的知识分享修改而来,有错误或 ...

  5. UOJ #142. 【UER #5】万圣节的南瓜灯 并查集

    #142. [UER #5]万圣节的南瓜灯 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/142 Descrip ...

  6. [WebGL] Setting Up WebGL

    In this lesson we cover setting up WebGL for use, including creating a canvas, getting the WebGL ren ...

  7. 免费的天气预报API--谷歌,雅虎,中央气象台

    Google Weather API 仅仅支持美国地区使用邮政编码进行查询,比如:  http://www.google.com/ig/api?hl=zh-cn&weather=94043  ...

  8. android141 360 安装软件管理java代码

    AppManagerActivity package com.itheima52.mobilesafe.activity; import android.app.Activity; import an ...

  9. Outlook2010 移动数据文件到其它地方

            您可以将数据文件移到计算机的其他文件夹中.移动文件的一个原因在于可使备份更容易并且可以让默认的outlook邮件文件不在存在C盘,导致装系统不见或者文件过大而撑死了C盘.例如,如果数据 ...

  10. Opacity多浏览器透明度兼容处理(转)

    用来设定元素透明度的 Opacity 是CSS 3里的一个属性.当然现在还只有少部分浏览器支持. 不过各个浏览器都有自己的私有属性来支持,其中包括老版本的Mozilla和Safari: IE: fil ...