使用html5 canvas 绘制的圆盘抽奖程序

效果图:

贴上全部代码:  1 <!DOCTYPE html>

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

HTML5 canvas 圆盘抽奖的更多相关文章

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

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

  2. html5 canvas 圆形抽奖的实例

    年底没啥,抽空学习了canvas,写了个html5抽奖的代码,造了个轮子,有用的童鞋可以拿走. 其中,canvas.onclick触发抽奖行为,概率可以在core.lottery()函数上添加,美化也 ...

  3. HTML5 Canvas绘制转盘抽奖

    新项目:完整的Canvas转盘抽奖代码 https://github.com/givebest/GB-canvas-turntable 演示 http://blog.givebest.cn/GB-ca ...

  4. canvas转盘抽奖

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

  5. HTML5 程序设计 - 使用HTML5 Canvas API

    请你跟着本篇示例代码实现每个示例,30分钟后,你会高喊:“HTML5 Canvas?!在哥面前,那都不是事儿!” 呵呵.不要被滚动条吓到,很多都是代码和图片.我没有分开写,不过上面给大家提供了目录,方 ...

  6. 赠书:HTML5 Canvas 2d 编程必读的两本经典

    赠书:HTML5 Canvas 2d 编程必读的两本经典 这两年多一直在和HTML5 Canvas 打交道,也带领团队开发了世界首款基于HTML5 Canvas 的演示文档工具---AxeSlide( ...

  7. 如何开发一个简单的HTML5 Canvas 小游戏

    原文:How to make a simple HTML5 Canvas game 想要快速上手HTML5 Canvas小游戏开发?下面通过一个例子来进行手把手教学.(如果你怀疑我的资历, A Wiz ...

  8. html5 canvas常用api总结(一)

    1.监听浏览器加载事件. window.addEventListener("load",eventWindowLoaded,false); load事件在html页面加载结束时发生 ...

  9. html5 canvas首屏自适应背景动画循环效果代码

    模板描述:html5 canvas首屏自适应背景动画循环效果代码 由于动态图太大,怕以后服务器受不了,所以现在都改为静态图了,大家点击演示地址一样的,希望大家喜欢,你们的支持就是小海的动力!! 欢迎大 ...

随机推荐

  1. 自顶向下理解Java集合框架(三)Map接口

    Map基本概念 数据结构中Map是一种重要的形式.Map接口定义的是查询表,或称查找表,其用于储存所谓的键/值对(key-value pair),其中key是映射表的索引. JDK结构中还存在实现Ma ...

  2. 爆料!如何在Visual Studio 2017上体验五星级云服务

    2017 年 3 月初,号称宇宙最强 IDE 之一的 Visual Studio 发布了最新的 2017 版本,遥想自己使用 VC++ 6.0 的当年,看着现在已然稀疏的头发,真是一入 IT 似海深, ...

  3. python3线程介绍01(如何启动和调用线程)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import osimport time,randomimport threading # 1-进程说明# 进程 ...

  4. IOS 封装View的fram(X Y W H )

    @interface UIView (Extension) @property (nonatomic, assign) CGFloat x; @property (nonatomic, assign) ...

  5. 【BZOJ3622】已经没有什么好害怕的了(动态规划+广义容斥)

    点此看题面 大致题意: 有\(n\)个糖果和\(n\)个药片,各有自己的能量.将其两两配对,求糖果比药片能量大的组数恰好比药片比糖果能量大的组数多\(k\)组的方案数. 什么是广义容斥(二项式反演) ...

  6. 使用 NetBackup 命令创建 Hyper-V 策略(命令创建其他策略也是如此)

    Veritas NetBackup™ for Hyper-V 管理指南 Product(s): NetBackup (8.1) 使用 NetBackup 命令创建 Hyper-V 策略 本主题介绍如何 ...

  7. 【造轮子】开发vue组件库MeowMeowUI

    项目示例 github ​ 1. 创建项目 # 全局安装 vue-cli $ npm install --global vue-cli # 创建一个基于 webpack 模板的新项目 $ vue in ...

  8. 第十五章 函数————函数的递归、生成器send 、匿名函数

    1.生成器send方法 send的工作原理 1.send发生信息给当前停止的yield 2.再去调用__next__()方法,生成器接着往下指向,返回下一个yield值并停止 例: persons=[ ...

  9. hadoop分类输出

    import org.apache.hadoop.io.Text; import java.io.IOException;import java.util.Iterator;import java.u ...

  10. 网际协议 IP

    网际协议 网际协议(internet  protocol),简称IP; 概念:TCP/IP网络体系结构中网际层的协议.用以提供无连接的数据服务. 1.IP地址的概念及组成 概念:IP地址就是用来唯一标 ...