-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.html
219 lines (190 loc) · 6.42 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<!-- based from https://codepen.io/barney-parker/pen/OPyYqy -->
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="vue.min.js"></script>
<link rel="stylesheet" href="bulma.css">
</head>
<body>
<section class="hero is-dark">
<div class="hero-body">
<div class="container">
<h1 class="title">
VueRoulette
</h1>
<h2 class="subtitle">
A simple roulette using VueJS
</h2>
</div>
</div>
</section>
<section class="section">
<div class="container" id="app">
<div class="columns">
<div class="column">
<div class="columns is-centered">
<input type="button" class="button is-dark" value="SPIN ROULETTE" id='spin' v-on:click="spin"/>
</div>
<div class="columns is-centered">
<canvas id="canvas" width="500" height="500"></canvas>
</div>
</div>
<div class="column">
<div class="columns is-multiline">
<div class="column is-half">
<div class="field">
<input class="input" type="text" placeholder="Input More Prize" v-model="new_option" v-on:keyup.enter="addOptions">
</div>
</div>
<div class="column is-half">
<button class="button is-primary" v-on:click="addOptions">Add Prize</button>
</div>
<div class="column is-one-quarter" v-for="option in options" :key="option">
<button class="button is-danger" v-on:click="removeOptions(option)">x</button>
<span> {{option}} </span>
</div>
</div>
</div>
</div>
</div>
</section>
</body>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
options: ['Try Again'],
new_option: '',
startAngle: 0,
startAngleStart: 0,
spinTimeout: null,
spinArcStart: 10,
spinTime: 0,
spinTimeTotal: 0,
ctx: ''
},
computed:{
arc: function () {
return Math.PI / (this.options.length / 2);
}
},
methods: {
byte2Hex: function (n) {
var nybHexString = "0123456789ABCDEF";
return String(nybHexString.substr((n >> 4) & 0x0F,1)) + nybHexString.substr(n & 0x0F,1);
},
RGB2Color: function (r,g,b) {
return '#' + this.byte2Hex(r) + this.byte2Hex(g) + this.byte2Hex(b);
},
getColor: function (item, maxitem) {
var phase = 0;
var center = 128;
var width = 127;
var frequency = Math.PI*2/maxitem;
red = Math.sin(frequency*item+2+phase) * width + center;
green = Math.sin(frequency*item+0+phase) * width + center;
blue = Math.sin(frequency*item+4+phase) * width + center;
return this.RGB2Color(red,green,blue);
},
addOptions: function () {
this.options.push(this.new_option);
this.new_option = '';
this.drawRouletteWheel()
},
removeOptions: function (option) {
let idx = this.options.indexOf(option) || 0;
this.options.splice(idx, 1);
this.drawRouletteWheel()
},
drawRouletteWheel: function () {
var canvas = document.getElementById("canvas");
if (canvas.getContext) {
var outsideRadius = 200;
var textRadius = 160;
var insideRadius = 125;
this.ctx = canvas.getContext("2d");
this.ctx.clearRect(0,0,500,500);
this.ctx.strokeStyle = "black";
this.ctx.lineWidth = 2;
this.ctx.font = 'bold 12px Helvetica, Arial';
for(var i = 0; i < this.options.length; i++) {
var angle = this.startAngle + i * this.arc;
//this.ctx.fillStyle = colors[i];
this.ctx.fillStyle = this.getColor(i, this.options.length);
this.ctx.beginPath();
this.ctx.arc(250, 250, outsideRadius, angle, angle + this.arc, false);
this.ctx.arc(250, 250, insideRadius, angle + this.arc, angle, true);
this.ctx.stroke();
this.ctx.fill();
this.ctx.save();
this.ctx.shadowOffsetX = -1;
this.ctx.shadowOffsetY = -1;
this.ctx.shadowBlur = 0;
this.ctx.shadowColor = "rgb(220,220,220)";
this.ctx.fillStyle = "black";
this.ctx.translate(250 + Math.cos(angle + this.arc / 2) * textRadius,
250 + Math.sin(angle + this.arc / 2) * textRadius);
this.ctx.rotate(angle + this.arc / 2 + Math.PI / 2);
var text = this.options[i];
this.ctx.fillText(text, -this.ctx.measureText(text).width / 2, 0);
this.ctx.restore();
}
//Arrow
this.ctx.fillStyle = "black";
this.ctx.beginPath();
this.ctx.moveTo(250 - 4, 250 - (outsideRadius + 5));
this.ctx.lineTo(250 + 4, 250 - (outsideRadius + 5));
this.ctx.lineTo(250 + 4, 250 - (outsideRadius - 5));
this.ctx.lineTo(250 + 9, 250 - (outsideRadius - 5));
this.ctx.lineTo(250 + 0, 250 - (outsideRadius - 13));
this.ctx.lineTo(250 - 9, 250 - (outsideRadius - 5));
this.ctx.lineTo(250 - 4, 250 - (outsideRadius - 5));
this.ctx.lineTo(250 - 4, 250 - (outsideRadius + 5));
this.ctx.fill();
}
},
spin: function () {
this.spinAngleStart = Math.random() * 10 + 10;
this.spinTime = 0;
this.spinTimeTotal = Math.random() * 3 + 4 * 1000;
this.rotateWheel();
},
rotateWheel: function () {
this.spinTime += 30;
if(this.spinTime >= this.spinTimeTotal) {
this.stopRotateWheel();
return;
}
var spinAngle = this.spinAngleStart - this.easeOut(this.spinTime, 0, this.spinAngleStart, this.spinTimeTotal);
this.startAngle += (spinAngle * Math.PI / 180);
this.drawRouletteWheel();
let _this = this
this.spinTimeout = setTimeout(function() {
_this.rotateWheel()
}, 30);
},
stopRotateWheel: function () {
clearTimeout(this.spinTimeout);
var degrees = this.startAngle * 180 / Math.PI + 90;
var arcd = this.arc * 180 / Math.PI;
var index = Math.floor((360 - degrees % 360) / arcd);
this.ctx.save();
this.ctx.font = 'bold 30px Helvetica, Arial';
var text = this.options[index]
console.log(index, text, this.options)
this.ctx.fillText(text, 250 - this.ctx.measureText(text).width / 2, 250 + 10);
this.ctx.restore();
},
easeOut: function (t, b, c, d) {
var ts = (t/=d)*t;
var tc = ts*t;
return b+c*(tc + -3*ts + 3*t);
}
},
mounted() {
this.drawRouletteWheel();
}
});
</script>
</html>