近期项目开发一款抽奖活动页面,有需要的请带走,如有不足之处,提出来,感谢各位!
项目特点: 一、canvas画图没有任何插件
二、自适应
三、动态创建圆盘份数及文字表述等等 先看效果图
话不多说,直接上代码
html部分
HTML5 Canvas自适应动态生成圆盘抽奖应用
*{
padding: 0px;
margin: 0px;
font-size: 16px;
font-family: "Microsoft YaHei";
}
.turnplate_box{
width: 400px;
height: 400px;
margin: 100px auto;
position: relative;
}
#turnplate_box{
width:400px;
height:400px;background:url(../disk1.jpg) no-repeat center center;
background-size:100% 100%;
position: relative;
border-radius: 100%;
}
.box{
width:300px;
height: 300px;
position: absolute;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
margin:auto;
}
.turnplate_box canvas{
position: absolute;
}
#myCanvas{
background-color: white;
border-radius: 100%;
}
#myCanvas01,#myCanvas03{
z-index: 30;
}
#myCanvas02{
z-index: 20;
}
#myCanvas01,#myCanvas03,#myCanvas02,#tupBtn{
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
margin:auto;
}
#turnplate_box{
-o-transform: transform 6s;
-ms-transform: transform 6s;
-moz-transform: transform 6s;
-webkit-transform: transform 6s;
transition: transform 6s;
-o-transform-origin: 50% 50%;
-ms-transform-origin: 50% 50%;
-moz-transform-origin: 50% 50%;
-webkit-transform-origin: 50% 50%;
transform-origin: 50% 50%;
}
#myCanvas{
-o-transform: transform 6s;
-ms-transform: transform 6s;
-moz-transform: transform 6s;
-webkit-transform: transform 6s;
transition: transform 6s;
-o-transform-origin: 50% 50%;
-ms-transform-origin: 50% 50%;
-moz-transform-origin: 50% 50%;
-webkit-transform-origin: 50% 50%;
transform-origin: 50% 50%;
}
.turnplatw_btn{
width: 60px;
height: 60px;
left: 120px;
top: 120px;
border-radius: 100%;
position: absolute;
cursor: pointer;
border: none;
background: transparent;
outline: none;
z-index: 40;
}
js部分
$(document).ready(function(){
//旋转角度
var angles;
//可抽奖次数
var clickNum = 5;
//旋转次数
var rotNum = 0;
//划分分数
var X = 8;
//中奖公告
var notice = null;
//转盘初始化
var color = ["#626262","#787878","rgba(0,0,0,0.5)","#fff","white","#FF4350"];
var info = ["谢谢参与","一等奖","二等奖","三等奖","四等奖","五等奖","六等奖","七等奖"];
var info1 = ['再接再厉','1000元','800元','500元','400元','300元','200元','100元']
canvasRun();
$('#tupBtn').bind('click',function(){
if (clickNum >= 1) {
//可抽奖次数减一
clickNum = clickNum-1;
//转盘旋转
runCup();
//转盘旋转过程“开始抽奖”按钮无法点击
$('#tupBtn').attr("disabled", true);
//旋转次数加一
rotNum = rotNum + 1;
//“开始抽奖”按钮无法点击恢复点击
setTimeout(function(){
alert(notice);
$('#tupBtn').removeAttr("disabled", true);
},6000);
}
else{
alert("亲,抽奖次数已用光!");
}
});
//计算自适应
function zsy(){
var Width = document.documentElement.clientWidth;
var Height = document.documentElement.clientHeight;
$(".turnplate_box").css({height:Width-20+"px",width:Width-20+"px"});
$("#turnplate_box").css({height:Width-20+"px",width:Width-20+"px"});
$(".box").css({height:0.8*Width-20+"px",width:0.8*Width-20+"px"});
$("#myCanvas").css({height:0.8*Width-20+"px",width:0.8*Width-20+"px"});
$("#myCanvas01").css({height:0.5*Width+"px",width:0.5*Width+"px"});
$("#myCanvas03").css({height:0.5*Width+"px",width:0.5*Width+"px"});
$("#myCanvas02").css({height:0.4*Width+"px",width:0.4*Width+"px"});
$("#tupBtn").css({height:0.16*Width+"px",width:0.16*Width+"px"});
}zsy();
//转盘旋转
function runCup(){
probability();
var degValue = 'rotate('+angles+'deg'+')';
$('#turnplate_box').css('-webkit-transform',degValue);
$('#myCanvas').css('-o-transform',degValue); //Opera
$('#myCanvas').css('-ms-transform',degValue); //IE浏览器
$('#myCanvas').css('-moz-transform',degValue); //Firefox
$('#myCanvas').css('-webkit-transform',degValue); //Chrome和Safari
$('#myCanvas').css('transform',degValue);
}
//各奖项对应的旋转角度及中奖公告内容
function probability(){
//获取随机数
var num = parseInt(Math.random()*(7 - 0 + 0) + 0);
if(X-num==8){
angles = 2160 * rotNum + 1800+360/X*num;
notice = info[0] + info1[0];
}else{
angles = 2160 * rotNum + 1800+360/X*num;
notice = info[X-num] + info1[X-num];
}
}
//绘制转盘
function canvasRun(){
var canvas=document.getElementById('myCanvas');
var canvas01=document.getElementById('myCanvas01');
var canvas03=document.getElementById('myCanvas03');
var canvas02=document.getElementById('myCanvas02');
var ctx=canvas.getContext('2d');
var ctx1=canvas01.getContext('2d');
var ctx3=canvas03.getContext('2d');
var ctx2=canvas02.getContext('2d');
createCircle();
createCirText();
initPoint();
//外圆
function createCircle(){
var startAngle = 0;//扇形的开始弧度
var endAngle = 0;//扇形的终止弧度
//画一个8等份扇形组成的圆形
for (var i = 0; i