-
Notifications
You must be signed in to change notification settings - Fork 0
/
turntable.js
174 lines (158 loc) · 6.31 KB
/
turntable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
(function ($) {
/**
* @param {Object} options
* @param {Array} options.list 存储奖品的的列表,example [{1:{name:'谢谢参与',image:'1.jpg'}}]
* @param {Object} options.outerCircle {color:'#df1e15'} 外圈颜色,默认红色
* @param {Object} options.innerCircle {color:'#f4ad26'} 里圈颜色,默认黄色
* @param {Array} options.dots ['#fbf0a9', '#fbb936'] 装饰点颜色 ,默认深黄浅黄交替
* @param {Array} options.disk ['#ffb933', '#ffe8b5', '#ffb933', '#ffd57c', '#ffb933', '#ffe8b5', '#ffd57c'] 中心奖盘的颜色,默认7彩
* @param {Object} options.title {color:'#5c1e08',font:'19px Arial'} 奖品标题颜色
*/
$.fn.WheelSurf = function (options) {
var _default = {
outerCircle: {
color: '#df1e15'
},
innerCircle: {
color: '#f4ad26'
},
dots: ['#fbf0a9', '#fbb936'],
disk: ['#ffb933', '#ffe8b5', '#ffb933', '#ffd57c', '#ffb933', '#ffe8b5', '#ffd57c'],
title: {
color: '#5c1e08',
font: '19px Arial'
}
}
$.extend(_default,options)
// 画布中心移动到canvas中心
var _this = this[0],
width = _this.width,
height = _this.height,
ctx = _this.getContext("2d"),
imgs = [],
awardTitle = [],
awardPic = []
for (var item in _default.list) {
awardTitle.push(_default.list[item].name)
imgs.push(_default.list[item].image)
}
var num = imgs.length
// 圆心
var x = width / 2
var y = height / 2
ctx.translate(x, y)
return {
init: function (angelTo) {
angelTo = angelTo || 0;
ctx.clearRect(-this.width, -this.height, this.width, this.height);
// 平分角度
var angel = (2 * Math.PI / 360) * (360 / num);
var startAngel = 2 * Math.PI / 360 * (-90)
var endAngel = 2 * Math.PI / 360 * (-90) + angel
// 旋转画布
ctx.save()
ctx.rotate(angelTo * Math.PI / 180);
// 画外圆
ctx.beginPath();
ctx.lineWidth = 25;
ctx.strokeStyle = _default.outerCircle.color;
ctx.arc(0, 0, 243, 0, 2 * Math.PI)
ctx.stroke();
// 画里圆
ctx.beginPath();
ctx.lineWidth = 23;
ctx.strokeStyle = _default.innerCircle.color;
ctx.arc(0, 0, 218, 0, 2 * Math.PI)
ctx.stroke();
// 装饰点
var dotColor = _default.dots
for (var i = 0; i < 12; i++) {
// 装饰点 圆心 坐标计算
ctx.beginPath();
var radius = 230;
var xr = radius * Math.cos(startAngel)
var yr = radius * Math.sin(startAngel)
ctx.fillStyle = dotColor[i % dotColor.length]
ctx.arc(xr, yr, 11, 0, 2 * Math.PI)
ctx.fill()
startAngel += (2 * Math.PI / 360) * (360 / 12);
}
// 画里转盘
var colors = _default.disk
for (var i = 0; i < num; i++) {
ctx.beginPath();
ctx.lineWidth = 208;
ctx.strokeStyle = colors[i % colors.length]
ctx.arc(0, 0, 104, startAngel, endAngel)
ctx.stroke();
startAngel = endAngel
endAngel += angel
}
// 添加奖品
function loadImg() {
var dtd = $.Deferred()
var countImg = 0
if (awardPic.length) {
return dtd.resolve(awardPic);
}
for (var i = 0; i < num; i++) {
var img = new Image()
awardPic.push(img)
img.src = imgs[i]
img.onload = function () {
countImg++
if (countImg == num) {
dtd.resolve(awardPic);
}
}
}
return dtd.promise()
}
$.when(loadImg()).done(function (awardPic) {
startAngel = angel / 2
for (var i = 0; i < num; i++) {
ctx.save();
ctx.rotate(startAngel)
ctx.drawImage(awardPic[i], -48, -48 - 130);
ctx.font = _default.title.font;
ctx.fillStyle = _default.title.color
ctx.textAlign = "center";
ctx.fillText(awardTitle[i], 0, -170);
startAngel += angel
ctx.restore();
}
})
ctx.restore();
},
/**
* @param angel 旋转角度
* @param callback 转完后的回调函数
*/
lottery: function (angel, callback) {
angel = angel || 0
angel = 360-angel
angel += 720
// 基值(减速)
var baseStep = 30
// 起始滚动速度
var baseSpeed = 0.3
// 步长
var count = 1;
var _this = this
var timer = setInterval(function () {
_this.init(count)
if (count == angel) {
clearInterval(timer)
if (typeof callback == "function") {
callback()
}
}
count = count + baseStep * (((angel - count) / angel) > baseSpeed ? baseSpeed : ((angel - count) / angel))
if (angel - count < 0.5) {
count = angel
}
}, 25)
}
}
}
}(jQuery))