-
Notifications
You must be signed in to change notification settings - Fork 0
/
wheel_of_fortune.html
199 lines (157 loc) · 5.42 KB
/
wheel_of_fortune.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
<!DOCTYPE html>
<html>
<head>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.js"></script>
</head>
<body>
<script>
let centerX, centerY, arrow;
let sectors = [];
let sectorsN = 10;
const sectorAngle = 360 / sectorsN;
let angleStart = 0;
let angleStop = 0;
let begin = true;
function setup() {
createCanvas(900, 600);
angleMode(DEGREES);
rectMode(CENTER);
centerX = width / 2;
centerY = height / 2;
arrow = new Arrow(centerX, centerY, -90);
for (let i = 0; i < sectorsN; i++) {
sectors[i] = new Sector(centerX, centerY, i * sectorAngle);
}
}
function draw() {
background(0);
stroke(255);
strokeWeight(1);
noFill();
ellipse(centerX, centerY, centerY * 1.7);
for (let s of sectors) {
s.draw();
}
arrow.update();
arrow.overlap();
arrow.draw();
strokeWeight(10);
point(centerX, centerY);
}
function mousePressed() {
let mouseAngle = atan2(mouseY - centerY, mouseX - centerX);
angleStart = realAngle(mouseAngle);
}
function mouseReleased() {
let mouseAngle = atan2(mouseY - centerY, mouseX - centerX);
angleStop = realAngle(mouseAngle);
if (begin && angleStart >= 350 && angleStop < 180) {
angleStart = 0;
begin = false;
} else if (begin && angleStart <= 10 && angleStop > 180) {
angleStart += 360;
begin = false;
}
let diffAngles = angleStop - angleStart;
// console.log(angleStart, angleStop, diffAngles)
arrow.changeAcceleration(diffAngles / 10);
}
function mouseDragged() {
let mouseAngle = atan2(mouseY - centerY, mouseX - centerX);
arrow.changeAngle(mouseAngle);
}
function realAngle(angle) {
if (angle > 270) {
return ((angle + 90) % 360);
} else {
if (angle >= -90 && angle <= 180) {
return ((angle + 90) % 360);
} else if (angle > -360 && angle < -90) {
return (270 + (180 + angle));
} else if (angle <= -360) {
if (Math.sign(angle) === -1) {
return angle % 360;
}
return angle % 360;
} else if (angle > 180 && angle < 270) {
return (270 + (angle - 180));
}
}
}
class Arrow {
constructor(x, y, angle) {
this.pos = createVector(x, y);
this.angle = angle;
this.angleV = 0;
this.angleA = 0;
this.force = 0;
this.realAngle = realAngle(angle);
this.sign = 1;
}
update() {
if (this.angleA !== 0) {
this.angleV = this.angleA;
if (this.angleA > -0.1 && this.angleA < 0.1) {
this.angleA = 0;
} else {
this.angleA += 0.1 * (this.sign * -1);
}
this.angle += this.angleV;
if (this.angle < -360) {
this.angle %= 360;
}
}
}
changeAngle(angle) {
this.angle = angle;
}
changeAcceleration(val) {
this.angleA = val;
this.sign = Math.sign(val);
}
overlap() {
this.realAngle = realAngle(this.angle);
let curSector = parseInt(this.realAngle / sectorAngle);
// console.log(curSector, this.realAngle, this.angle)
for (let [i, s] of sectors.entries()) {
if (s.alpha === 70 && i !== curSector) {
sectors[i].changeColor(10);
}
}
if (this.realAngle > 0) {
sectors[curSector].changeColor(70);
}
}
draw() {
push();
translate(this.pos);
rotate(this.angle);
noStroke();
fill(150, 255);
rect(0, 0, this.pos.y * 1.3, 20);
triangle(this.pos.y * 0.648, -25, this.pos.y * 0.648, 25, this.pos.y * 0.8, 0);
ellipse(-this.pos.x * 0.48, 0, 50);
pop();
}
}
class Sector {
constructor(x, y, angle) {
this.pos = createVector(x, y);
this.angle = angle;
this.alpha = 10;
}
changeColor(alpha) {
this.alpha = alpha;
}
draw() {
push();
translate(this.pos);
rotate(270);
fill(255, this.alpha);
arc(0, 0, this.pos.y * 1.7, this.pos.y * 1.7, this.angle, this.angle + sectorAngle, PIE);
pop();
}
}
</script>
</body>
</html>