html5 大轉盤

不太會寫文章,html5 大轉盤 源碼以下:javascript

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>大轉盤抽獎代碼</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="js/jquery-2.1.0.js"></script>
<script type="text/javascript" src="js/awardRotate.js"></script>
<script>

var turnplate = {
divID:"item",
Div: null,
restaraunts: [{text:"50M免費流量包",color:"#FFF4D6"},
{text:"10元",color:"#FFFFFF"},
{text:"謝謝參與",color:"#FFF4D6"},
{text:"5元",color:"#FFFFFF"},
{text:"10M免費流量包",color:"#FFF4D6"},
{text:"20M免費流量包",color:"#FFFFFF"},
{text:"20元",color:"#FFF4D6"},
{text:"30M免費流量包",color:"#FFFFFF"},
{text:"100M免費流量包",color:"#FFF4D6"},
{text:"2元",color:"#FFFFFF"}],
outsideRadius: 192, //大轉盤外圓的半徑
textRadius: 155, //大轉盤獎品位置距離圓心的距離
insideRadius: 68, //大轉盤內圓的半徑
startAngle: 0, //開始角度
bRotate: false, //false:中止;ture:旋轉
rotateFn: function (item) {
var txt = this.restaraunts[item - 1].text
var angles = item * (360 / turnplate.restaraunts.length) - (360 / (turnplate.restaraunts.length * 2));
if (angles < 270) {
angles = 270 - angles;
} else {
angles = 360 - angles + 270;
}
$('#wheelcanvas').stopRotate();
$('#wheelcanvas').rotate({
angle: 0,
animateTo: angles + 1800,
duration: 8000,
callback: function () {
plus.nativeUI.toast('您的抽獎結果是:' + txt + '!');
turnplate.setText('您的抽獎結果是:' + txt + '!');
turnplate.bRotate = !turnplate.bRotate;
}
});
},
setText: function (t) {
this.Div.innerHTML = t;
},
drawRouletteWheel: function () {
this.Div = document.getElementById(this.divID);
var canvas = document.getElementById("wheelcanvas");
if (canvas.getContext) {
//根據獎品個數計算圓周角度
var arc = Math.PI / (turnplate.restaraunts.length / 2);
var ctx = canvas.getContext("2d");
//在給定矩形內清空一個矩形
ctx.clearRect(0, 0, 240, 240);
//strokeStyle 屬性設置或返回用於筆觸的顏色、漸變或模式
ctx.strokeStyle = "#FFBE04";
//font 屬性設置或返回畫布上文本內容的當前字體屬性
ctx.font = '16px 宋體';
for (var i = 0; i < turnplate.restaraunts.length; i++) {
var angle = turnplate.startAngle + i * arc;
ctx.fillStyle = turnplate.restaraunts[i].color;
ctx.beginPath();
//arc(x,y,r,起始角,結束角,繪製方向) 方法建立弧/曲線(用於建立圓或部分圓)
ctx.arc(211, 211, turnplate.outsideRadius, angle, angle + arc, false);
ctx.arc(211, 211, turnplate.insideRadius, angle + arc, angle, true);
ctx.stroke();
ctx.fill();
//鎖畫布(爲了保存以前的畫布狀態)
ctx.save();
//----繪製獎品開始----
ctx.fillStyle = "#E5302F";
var text = turnplate.restaraunts[i].text;

var line_height = 17;
ctx.translate(211 + Math.cos(angle + arc / 2) * turnplate.textRadius, 211 + Math.sin(angle + arc / 2) * turnplate.textRadius);
ctx.rotate(angle + arc / 2 + Math.PI / 2);
if (text.indexOf("M") > 0) {//流量包
var texts = text.split("M");
for (var j = 0; j < texts.length; j++) {
ctx.font = j == 0 ? 'bold 20px 宋體' : '16px 宋體';
if (j == 0) {
ctx.fillText(texts[j] + "M", -ctx.measureText(texts[j] + "M").width / 2, j * line_height);
} else {
ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height);
}
}
} else if (text.indexOf("M") == -1 && text.length > 6) {//獎品名稱長度超過必定範圍
text = text.substring(0, 6) + "||" + text.substring(6);
var texts = text.split("||");
for (var j = 0; j < texts.length; j++) {
ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height);
}
} else {
ctx.fillText(text, -ctx.measureText(text).width / 2, 0);
}
ctx.restore();
}
}
},
//點擊開始抽獎
pointerOnclick: function () {css

var random = Math.floor(Math.random()*9+1);
turnplate.begin(random);html

},
begin: function (num) {
// if (turnplate.bRotate) return;
// turnplate.bRotate = !turnplate.bRotate;
turnplate.rotateFn(num);
turnplate.setText('');
}
};
html5


//
// var info =[{text:"50M免費流量包",color:"#FFF4D6"},
// {text:"10元",color:"#FFFFFF"},
// {text:"謝謝參與",color:"#FFF4D6"},
// {text:"5元",color:"#FFFFFF"},
// {text:"10M免費流量包",color:"#FFF4D6"},
// {text:"20M免費流量包",color:"#FFFFFF"},
// {text:"20元",color:"#FFF4D6"},
// {text:"30M免費流量包",color:"#FFFFFF"},
// {text:"100M免費流量包",color:"#FFF4D6"},
// {text:"2元",color:"#FFFFFF"}];java


window.onload=function(){
turnplate.drawRouletteWheel();
};
</script>
</head>
<body style="background:#e62d2d;overflow-x:hidden;">jquery

<img src="img/1.png" id="shan-img" style="display:none;" />
<img src="img/2.png" id="sorry-img" style="display:none;" />
<div class="banner">
<div class="turnplate" style="background-image:url(img/turnplate-bg.png);background-size:100% 100%;">
<canvas class="item" id="wheelcanvas" width="422px" height="422px"></canvas>
<img class="pointer" src="img/turnplate-pointer.png" onclick="turnplate.pointerOnclick();"/>
</div>
</div>canvas

</body>
</html>dom

相關文章
相關標籤/搜索