-
Notifications
You must be signed in to change notification settings - Fork 2
/
pizza.js
332 lines (286 loc) · 11.2 KB
/
pizza.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
function Pizza(pizzaX, pizzaY, innerSize, sizeRatio, numSlices, colorPalette) {
this.innerSize = innerSize; // radius in pixels of the inner circle
this.sizeRatio = sizeRatio; // ratio for increasing the size of circles
// position of center of pizza
this.pizzaX = pizzaX;
this.pizzaY = pizzaY;
this.numSlices = numSlices;
this.colorPalette = colorPalette;
// have an array to store all the pizza slices to access later
this.pizzaSlicesArr = [];
// draw the pizza!
// outer circle
this.pizzaSlicesArr.push(new PizzaSlices(4, this.numSlices, this.pizzaX, this.pizzaY, this.innerSize + this.innerSize * this.sizeRatio * 3, this.colorPalette));
// middle circle
this.pizzaSlicesArr.push(new PizzaSlices(3, this.numSlices, this.pizzaX, this.pizzaY, this.innerSize + this.innerSize * this.sizeRatio * 2, this.colorPalette));
// inner circle
this.pizzaSlicesArr.push(new PizzaSlices(2, this.numSlices, this.pizzaX, this.pizzaY, this.innerSize + this.innerSize * this.sizeRatio, this.colorPalette));
// call this function when change the slice number or something
this.updatePizza = function(){
for (let i=0; i< this.pizzaSlicesArr.length; i++){
this.pizzaSlicesArr[i].numSlices = this.numSlices;
this.pizzaSlicesArr[i].updatePizzaSlices();
//this.pizzaSlicesArr[i].increaseAngle = 2 * Math.PI / this.pizzaSlicesArr[i].numSlices;
}
}
this.drawPizza = function() {
push();
// draw the slices of pizza
this.pizzaSlicesArr.forEach(function(slice) {
slice.drawPizzaSlices();
});
this.pizzaSlicesArr.forEach(function(slice) {
slice.drawShapes();
});
this.pizzaSlicesArr.forEach(function(slice) {
slice.drawNodes();
});
// inner circle of pizza
noStroke();
fill(this.colorPalette.pizzaCenter.r, this.colorPalette.pizzaCenter.g,this.colorPalette.pizzaCenter.b);
ellipse(this.pizzaX, this.pizzaY, this.innerSize, this.innerSize);
push()
// show type text
fill(this.colorPalette.text.r, this.colorPalette.text.g, this.colorPalette.text.b);
noStroke();
textAlign(CENTER, CENTER);
textStyle(BOLD);
textSize(18);
text(currentInst.name, this.pizzaX, this.pizzaY);
pop();
// outer numbers
this.pizzaSlicesArr[2].drawNum = true;
pop();
}
// Function that gets called whenever the pizza has been clicked on
// Check if a node has been clicked on
// If true - update the node and return its array indices
this.clickPizza = function(clickX, clickY) {
let layerVal = this.determineLayer(clickX, clickY);
if (!layerVal) {return false;}
if (layerVal == 1) {
this.changeCenter();
return false;
}
let layerIndex = 2 - (layerVal - 2);
return (this.pizzaSlicesArr[layerIndex].clickSlice(clickX, clickY));
}
// Returns the layer that was clicked on
// If the mouse was outside of the pizza - returns false
this.determineLayer = function(clickX, clickY) {
let clickDistance = dist(clickX, clickY, this.pizzaX, this.pizzaY);
let distanceRatio = (clickDistance - (this.innerSize / 2)) / (this.sizeRatio * (this.innerSize / 2));
if (distanceRatio > 3) {return false;} // Assuming pizza has 3 layers
else {return floor(distanceRatio + 2);}
}
this.changeCenter = function() {
console.log('Center was clicked');
return false;
}
this.updateColor = function(colorPalette) {
this.colorPalette = colorPalette;
this.pizzaSlicesArr.forEach(function(slice) {
slice.updateColor(colorPalette);
});
}
}
function PizzaSlices(layer, numSlices, sliceX, sliceY, sliceSize, colorPalette) {
this.layer = layer;
this.numSlices = numSlices;
this.sliceX = sliceX;
this.sliceY = sliceY;
this.sliceSize = sliceSize;
this.colorPalette = colorPalette;
this.drawNum = false; // set to true on outer layer
// to draw
this.increaseAngle = 2 * Math.PI / this.numSlices;
//this.startAngle = 0; // 22.5 if 16 slices
//this.startAngle = (270 * Math.PI/180) - (this.increaseAngle / 2);
this.increaseAngle = (2 * Math.PI) / this.numSlices;
this.startAngle = (270 * Math.PI/180) - (this.increaseAngle / 2);
// have an array to store all the pizza nodes to access later
this.pizzaNodesArr = [];
// this create the initial slices
for (var i = 0; i < this.numSlices; i++) {
let nodeAngle = this.startAngle + this.increaseAngle / 2;
this.startAngle += this.increaseAngle;
// create the nodes on each slice!
let nodeX = this.sliceX + (((this.sliceSize - 100) / 2) * Math.cos(nodeAngle));
let nodeY = this.sliceY + (((this.sliceSize - 100) / 2) * Math.sin(nodeAngle));
let currentPizzaNode = new PizzaNode(i + 1, this.layer, nodeX, nodeY, colorPalette);
this.pizzaNodesArr.push(currentPizzaNode);
}
// call update when slices num is changed or something
this.updatePizzaSlices = function(){
this.increaseAngle = 2 * Math.PI / this.numSlices;
this.startAngle = (270 * Math.PI/180) - (this.increaseAngle / 2);
// updating pizza nodes here
for (var i = 0; i < this.numSlices; i++) {
let nodeAngle = this.startAngle + this.increaseAngle / 2;
this.startAngle += this.increaseAngle;
// update the nodes position!
let nodeX = this.sliceX + (((this.sliceSize - 100) / 2) * Math.cos(nodeAngle));
let nodeY = this.sliceY + (((this.sliceSize - 100) / 2) * Math.sin(nodeAngle));
this.pizzaNodesArr[i].updatePizzaNode(nodeX, nodeY);
}
}
this.drawPizzaSlices = function() {
this.increaseAngle = 2 * Math.PI / this.numSlices;
this.startAngle = (270 * Math.PI/180) - (this.increaseAngle / 2);
push();
// change angle mode to RADIANS!
angleMode(RADIANS);
// stroke color grey
stroke(100);
let layerColor;
if (this.layer == 4){
layerColor = this.colorPalette.outerRing;
}
else if (this.layer == 3){
layerColor = this.colorPalette.middleRing;
}
else if (this.layer == 2){
layerColor = this.colorPalette.innerRing;
}
fill(layerColor.r, layerColor.g, layerColor.b);
// draw num slices
for (let i = 0; i < this.numSlices; i++) {
arc(this.sliceX, this.sliceY, this.sliceSize, this.sliceSize, this.startAngle, this.startAngle + this.increaseAngle, PIE);
//this.pizzaNodesArr[i].drawPizzaNode();
let nodeAngle = this.startAngle + this.increaseAngle / 2;
// if it's layer 4 and we draw the numbers around
if (this.drawNum === true) {
push();
noStroke();
textStyle(BOLD);
textSize(18);
textAlign(CENTER, CENTER);
fill(this.colorPalette.text.r, this.colorPalette.text.g, this.colorPalette.text.b);
let textX = this.sliceX + ((this.sliceSize + 50) * Math.cos(nodeAngle));
let textY = this.sliceY + ((this.sliceSize + 50) * Math.sin(nodeAngle));
text(this.pizzaNodesArr[i].slice, textX, textY);
pop();
}
this.startAngle += this.increaseAngle;
}
pop();
}
this.drawShapes = function(){
let shapeColor;
if (this.layer == 4){
shapeColor = this.colorPalette.outerShape;
}
else if (this.layer == 3){
shapeColor = this.colorPalette.middleShape;
}
else if (this.layer == 2){
shapeColor = this.colorPalette.innerShape;
}
push();
strokeWeight(5);
stroke(shapeColor.r, shapeColor.g, shapeColor.b, 150);
//fill(this.colorPalette.outerShape);
fill(shapeColor.r, shapeColor.g, shapeColor.b, 150);
beginShape();
for (let i=0; i<this.numSlices; i++){
if (this.pizzaNodesArr[i].isActive){
// console.log(this.pizzaNodesArr[i]);
vertex(this.pizzaNodesArr[i].nodeX, this.pizzaNodesArr[i].nodeY);
}
}
endShape(CLOSE);
pop();
}
this.drawNodes = function(){
for (let i = 0; i < this.numSlices; i++) {
// draw the nodes on each slice!
this.pizzaNodesArr[i].drawPizzaNode();
}
}
// If a node is clicked, it will call its chageState function and return
// the results
// Otherwise, it will return false
this.clickSlice = function(clickX, clickY) {
for (let i = 0; i < this.pizzaNodesArr.length; i++) {
let currentNode = this.pizzaNodesArr[i];
if (dist(clickX, clickY, currentNode.nodeX, currentNode.nodeY) < currentNode.nodeSize) {
return currentNode.changeState();
}
}
return false;
}
this.updateColor = function(colorPalette) {
this.colorPalette = colorPalette;
this.pizzaNodesArr.forEach(function(node) {
node.updateColor(colorPalette);
});
}
}
function PizzaNode(slice, layer, nodeX, nodeY, colorPalette) {
this.slice = slice;
this.layer = layer;
if (this.layer==4){
this.nodeOnColor = colorPalette.outerNodeOn;
}
else if (this.layer==3){
this.nodeOnColor = colorPalette.middleNodeOn;
}
else if (this.layer==2){
this.nodeOnColor = colorPalette.innerNodeOn;
}
this.nodeX = nodeX;
this.nodeY = nodeY;
this.nodeSize = 15;
this.fillColor = {
true: this.nodeOnColor,
false: colorPalette.nodeOff
};
this.isActive = false;
this.isHighlighted = false;
this.drawPizzaNode = function() {
// draw the node!
push();
if (this.isHighlighted){
stroke(255);
strokeWeight(this.nodeSize/3);
}
else{
noStroke();
}
fill(this.fillColor[this.isActive].r, this.fillColor[this.isActive].g, this.fillColor[this.isActive].b);
ellipse(this.nodeX, this.nodeY, this.nodeSize, this.nodeSize);
pop();
}
this.updatePizzaNode = function(_x, _y){
this.nodeX = _x;
this.nodeY = _y;
}
// Swaps the isActive variable of the node
// Returns the node's layer, slice, and new state of isActive - this is
// used for updating the beats array
this.changeState = function() {
this.isActive = !this.isActive;
return ([this.layer, this.slice, this.isActive]);
}
this.highlight = function(){
this.isHighlighted = true;
}
this.notHighlight = function(){
this.isHighlighted = false;
}
this.updateColor = function(colorPalette) {
if (this.layer==4){
this.nodeOnColor = colorPalette.outerNodeOn;
}
else if (this.layer==3){
this.nodeOnColor = colorPalette.middleNodeOn;
}
else if (this.layer==2){
this.nodeOnColor = colorPalette.innerNodeOn;
}
this.fillColor = {
true: this.nodeOnColor,
false: colorPalette.nodeOff
}
}
}