-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch_2.js
222 lines (176 loc) · 5.36 KB
/
sketch_2.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
let particles = [];
let result;
let font;
let currentX = 0;
let currentY = 0;
let dragState = false;
let offsetX = 0.0;
let offsetY = 0.0;
// Search
let inputElem;
let button;
// Zoom
let sf = 1.0;
// Switch button
let color_button;
let color = 0;
let position_button;
let position = 0;
class PopUp {
constructor(x, y, name, image, image_color) {
this.pos = createVector(x, y);
this.name = name;
this.image = image;
this.image_color = image_color;
}
show(){
let this_img;
if (color === 0)
this_img = this.image;
else
this_img = this.image_color;
push();
fill(255);
rect(this.pos.x - 10, this.pos.y - 30, this_img.width + 20, this_img.height + 40, 5);
fill(0);
text(this.name, this.pos.x, this.pos.y - 10);
image(this_img, this.pos.x, this.pos.y);
pop();
}
}
class Particle{
constructor(x, y, x_img, y_img, name, image, image_color){
this.pos = createVector(x, y);
this.pos_img = createVector(x_img, y_img);
this.name = name;
this.image = image;
this.image_color = image_color;
this.hovered = false;
}
update(){
let updated_x = -(currentX - mouseX + (windowWidth / 2));
let updated_y = -(currentY - mouseY + (windowHeight / 2));
if (dist(updated_x, updated_y, this.pos.x + (this.image.width / 2), this.pos.x + (this.image.height / 2)) < (this.image.height / 2)){
this.hovered = true;
this.popUp = new PopUp(this.pos.x, this.pos.y, this.name, this.image);
} else {
this.hovered = false;
this.popUp = null;
}
}
show(){
if (this.hovered){
this.popUp.show();
} else {
let this_pos;
let this_img;
if (color === 0)
this_img = this.image;
else
this_img = this.image_color;
if (position === 0)
this_pos = this.pos;
else
this_pos = this.pos_img;
push();
text(this.name, this_pos.x, this_pos.y - 10);
image(this_img, this_pos.x, this_pos.y, 0);
pop();
}
}
}
function preload(){
result = loadStrings("values.txt");
result_img = loadStrings("values_img.txt");
font = loadFont('roboto.ttf');
}
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
textFont(font);
fill(20);
if (result.length > 0 && result_img.length > 0) {
for (let i = 0; i < result.length - 1; i++) {
let splitString = split(result[i], ',');
let name = splitString[0];
let x = float(splitString[1]) * 80;
let y = float(splitString[2]) * 80;
let splitString_img = split(result_img[i], ',');
let x_img = float(splitString_img[1]) * 0.6;
let y_img = float(splitString_img[2]) * 0.6;
let img = loadImage("words/" + name + ".png");
let img_c = loadImage("words_3/" + name + ".png");
let p = new Particle(x, y, x_img, y_img, name, img, img_c);
particles.push(p);
}
}
cam = createCamera(0, 0, 700);
// Create input element
inputElem = createInput('');
inputElem.position(30, 60);
button = createButton('Search');
button.position(inputElem.x + inputElem.width, 60);
button.mousePressed(find);
color_button = createButton('Switch color');
color_button.position(inputElem.x, inputElem.y + inputElem.height);
color_button.mousePressed(switch_color);
position_button = createButton('Switch mapping');
position_button.position(color_button.x, color_button.y + color_button.height);
position_button.mousePressed(switch_position);
}
function draw() {
background("edf6f9");
// Adjust location if being dragged
if (dragState) {
currentX = mouseX + offsetX;
currentY = mouseY + offsetY;
}
cam.setPosition(-currentX, -currentY, 700 * sf);
for (let i = 0; i < particles.length; i++) {
// particles[i].update();
particles[i].show();
}
}
function find() {
let multiplier;
if (result.length > 0) {
for (let i = 0; i < result.length - 1; i++) {
let splitString;
if (position === 0) {
splitString = split(result[i], ',');
multiplier = 80;
} else {
splitString = split(result_img[i], ',');
multiplier = 0.6;
}
let name = splitString[0];
let x = float(splitString[1]) * multiplier;
let y = float(splitString[2]) * multiplier;
if (name === inputElem.value()){
currentX = -x;
currentY = -y;
}
}
}
}
function switch_color() {
color = 1 - color;
}
function switch_position() {
position = 1 - position;
}
function mousePressed() {
dragState = true;
// If so, keep track of relative location of click to corner of rectangle
offsetX = currentX - mouseX;
offsetY = currentY - mouseY;
}
//After the mouse is released, the silly state is restored
function mouseReleased() {
dragState = false;
}
window.addEventListener("wheel", function(e) {
if (e.deltaY > 0)
sf *= 1.02;
else
sf *= 0.98;
});