-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcandy.html
141 lines (121 loc) · 3.38 KB
/
candy.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>The Big Wheel - With Candy!</title>
<style>
.center-screen {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
min-height: 100vh;
}
</style>
<script type="text/javascript">
// https://stackoverflow.com/a/29190009
// https://stackoverflow.com/a/5400970
// https://stackoverflow.com/a/12646864
// https://stackoverflow.com/a/35626468
var words = ["Buzz Like a Bee", "Stick Your Tongue Out", "New First Letter",
"Stand On One Foot", "Rub Belly, Pat Head", "Teachers vs Kids",
"Three Musketeers", "Solo For Rolo", "Class Act",
"Staccato", "Soft", "Fast"];
var colors = ["Red", "YellowGreen", "MediumPurple",
"Orange", "CornFlowerBlue", "Yellow",
"Red", "YellowGreen", "MediumPurple",
"Orange", "CornFlowerBlue", "Yellow"];
var rotation = 0;
var delta = 0;
var radius = 240;
/**
* Randomize array element order in-place.
* Using Durstenfeld shuffle algorithm.
*/
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
function init() {
shuffleArray(words);
canvas = document.getElementById("testCanvas");
canvas.addEventListener('click', () => {
play();
delta = 0.18 + Math.random() * 0.04;
requestAnimationFrame(drawWheel);
});
context = canvas.getContext("2d");
drawWheel(false);
drawTriangle();
}
function drawSector(text, angle, subtend) {
context.save();
context.rotate(angle);
context.beginPath();
context.moveTo(0, 0);
context.arc(0, 0, radius, -subtend/2, subtend/2, false);
context.fillStyle = colors[i];
context.fill();
context.textAlign = "center";
context.fillStyle = "Black";
context.font = context.font.replace(/\d+px/, "16px");
context.fillText(text, radius * 0.6, 6);
context.restore();
}
function drawWheel(animate) {
// reset transforms before clearing
context.save();
// translate and rotate an absolute rotation value
context.translate(300, 300);
context.rotate(rotation);
subtend = 2 * Math.PI / words.length;
// draw arcs
for (i = 0; i < words.length; i++) {
drawSector(words[i], i*subtend, subtend);
}
context.beginPath();
context.arc(0, 0, radius, 0, Math.PI * 2, false);
context.lineWidth = 8;
context.stroke();
context.beginPath();
context.arc(0, 0, radius / 10.0, 0, Math.PI * 2, false);
context.lineWidth = 8;
context.fillStyle = "Gray";
context.fill();
context.restore();
// update rotation value and request new frame
if (animate) {
rotation += delta;
delta -= 0.001
requestAnimationFrame(()=>drawWheel(delta > 0))
}
}
function drawTriangle() {
context.beginPath();
context.moveTo(540, 300);
context.lineTo(595, 330);
context.lineTo(595, 270);
context.lineTo(540, 300);
context.fillStyle = "gray";
context.lineWidth = 5;
context.fill();
context.stroke();
}
function play(){
var audio = document.getElementById("audio");
audio.play();
}
</script>
</head>
<body onload="init()">
<canvas class="center-screen" id="testCanvas" width="600" height="600">
Canvas not supported
</canvas>
<audio id="audio" src="sound.mp3" ></audio>
</body>
</html>