-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
170 lines (142 loc) · 4.53 KB
/
index.html
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
<html>
<head>
<script type="text/javascript">
var options = [0, 32, 15, 19, 4, 21, 2, 25, 17, 34, 6, 27, 13, 36, 11, 30, 8, 23, 10, 5, 24, 16, 33, 1, 20, 14, 31, 9, 22, 18, 29, 7, 28, 12, 35, 3, 26];
var startAngle = 0;
var arc = Math.PI / (options.length / 2);
var spinTimeout = null;
var spinArcStart = 10;
var spinTime = 0;
var spinTimeTotal = 0;
var resize = 1;
var ctx;
function getColor(item) {
var erPartall = (item % 2 == 0);
var erNull = item == 0;
if (erNull) {
return "#008b0f";
}
if (erPartall) {
return "#FF0000";
}
return "#000000";
}
function drawRouletteWheel() {
var offsetX = 250*resize;
var offsetY = 250*resize;
var canvas = document.getElementById("canvas");
if (canvas.getContext) {
var outsideRadius = 200*resize;
var textRadius = 160*resize;
var insideRadius = 125*resize;
ctx = canvas.getContext("2d");
ctx.clearRect(0,0,500*resize,500*resize);
ctx.strokeStyle = "black";
ctx.lineWidth = 2;
ctx.font = 'normal '+(16*resize)+'px Helvetica, Arial';
for(var i = 0; i < options.length; i++) {
var angle = startAngle + i * arc;
//ctx.fillStyle = colors[i];
ctx.fillStyle = getColor(i);
ctx.beginPath();
ctx.arc(offsetX, offsetY, outsideRadius, angle, angle + arc, false);
ctx.arc(offsetX, offsetY, insideRadius, angle + arc, angle, true);
ctx.stroke();
ctx.fill();
ctx.save();
ctx.fillStyle = "white";
ctx.translate(offsetX + Math.cos(angle + arc / 2) * textRadius,
offsetY + Math.sin(angle + arc / 2) * textRadius);
ctx.rotate(angle + arc / 2 + Math.PI / 2);
var text = options[i];
ctx.fillText(text, -ctx.measureText(text).width / 2, 0);
ctx.restore();
}
//Arrow
ctx.fillStyle = "#ccc";
ctx.beginPath();
ctx.moveTo(offsetX - 4*resize, offsetY - (outsideRadius + 5*resize));
ctx.lineTo(offsetX + 4*resize, offsetY - (outsideRadius + 5*resize));
ctx.lineTo(offsetX + 4*resize, offsetY - (outsideRadius - 5*resize));
ctx.lineTo(offsetX + 9*resize, offsetY - (outsideRadius - 5*resize));
ctx.lineTo(offsetX + 0*resize, offsetY - (outsideRadius - 13*resize));
ctx.lineTo(offsetX - 9*resize, offsetY - (outsideRadius - 5*resize));
ctx.lineTo(offsetX - 4*resize, offsetY - (outsideRadius - 5*resize));
ctx.lineTo(offsetX - 4*resize, offsetY - (outsideRadius + 5*resize));
ctx.fill();
}
}
function spin() {
spinAngleStart = Math.random() * 10 + 10;
spinTime = 0;
spinTimeTotal = Math.random() * 3 + 4 * 1000;
rotateWheel();
}
function rotateWheel() {
spinTime += 30;
if(spinTime >= spinTimeTotal) {
stopRotateWheel();
return;
}
var spinAngle = spinAngleStart - easeOut(spinTime, 0, spinAngleStart, spinTimeTotal);
startAngle += (spinAngle * Math.PI / 180);
drawRouletteWheel();
spinTimeout = setTimeout('rotateWheel()', 30);
}
function stopRotateWheel() {
clearTimeout(spinTimeout);
var degrees = startAngle * 180 / Math.PI + 90;
var arcd = arc * 180 / Math.PI;
var index = Math.floor((360 - degrees % 360) / arcd);
ctx.save();
ctx.shadowOffsetX = -2;
ctx.shadowOffsetY = -2;
ctx.shadowBlur = 0;
ctx.shadowColor = "rgb(220,220,220)";
ctx.fillStyle = getColor(index);
var boxX = 250*resize - ctx.measureText(text).width / 2 + 7*resize;
var boxY = 250*resize - 30*resize;
ctx.fillRect(boxX, boxY, 55*resize, 55*resize);
ctx.restore();
ctx.font = 'bold '+40*resize+'px Helvetica, Arial';
var text = options[index];
ctx.fillStyle = 'white';
ctx.fillText(text, 250*resize - ctx.measureText(text).width / 2, 250*resize + 10*resize);
ctx.restore();
}
function easeOut(t, b, c, d) {
var ts = (t/=d)*t;
var tc = ts*t;
return b+c*(tc + -3*ts + 3*t);
}
function onKeyPress(e){
// look for window.event in case event isn't passed in
e = e || window.event;
if (e.keyCode >= 0)
{
spin();
return false;
}
return true;
}
</script>
</head>
<body onkeypress="return onKeyPress(event)">
<canvas id="canvas" width="500" height="500"></canvas>
<script type="text/javascript">
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight|| e.clientHeight|| g.clientHeight;
var size = (y > x) ? x : y;
var canvas = document.getElementById("canvas");
canvas.height = size;
canvas.width = size;
resize = size / 500;
drawRouletteWheel();
</script>
<!-- Based on Barney Parkers code pen: http://codepen.io/barney-parker/pen/OPyYqy -->
</body>
</html>