-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweights_v2.js
313 lines (255 loc) · 6.76 KB
/
weights_v2.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
// Programmed By: Andrew Ciambrone
//Handling the Canvas
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
//Adding Listeners
canvas.addEventListener("mousedown", mausDown);
canvas.addEventListener("mouseup", mausUp);
canvas.addEventListener("mousemove", mausMove);
// canvasWidth and height variables
var canvasWidth = canvas.width;
var canvasHeight = canvas.height;
var BB = canvas.getBoundingClientRect();
var offsetX = BB.left;
var offsetY = BB.top;
var sectionHeight = 0;
var topHeight = 0;
var middleHeight = 0;
var bottomHeight = 0;
// Data structures
var weights = [];
//Constants
var numDataSets = 7;
var hradius = 0;
var wradius = 0;
var weightNames = ["Crime", "Cost of Living", "Education", "Unemployment", "Income", "Commute Time", "Health"];
var dragok = {i:-1};
function init() {
hradius = 40/2;
wradius = 20 *1.5;
//ADD the button
weights.push({x:canvasWidth/2, y:canvasHeight-hradius*2, isButton:true, label:""});
}
function draw()
{
clear();
drawLines();
drawWeights();
drawPopUp();
}
function drawLines () {
for (var i=0; i<weights.length; i++)
{
var weight = weights[i];
if (weight.isButton)
continue;
ctx.beginPath();
ctx.moveTo(canvasWidth*0.9, canvasHeight);
ctx.quadraticCurveTo(canvasWidth, weight.y - 20, weight.x, weight.y);
ctx.lineWidth = 5;
ctx.strokeStyle = "#2ecc71";
ctx.stroke();
}
}
function drawWeights() {
for (var i=0; i<weights.length; i++)
{
var weight = weights[i];
if (weight.isButton)
{
drawAddButton(weight.x, weight.y);
} else {
drawLabelCircle(weight.x, weight.y, weight.label);
}
}
}
function drawCircle(x, y) {
ctx.save();
ctx.translate(x,y);
ctx.scale(1.5, 1);
ctx.beginPath();
ctx.arc(0,0,hradius,hradius,0,2*Math.PI);
ctx.closePath();
ctx.restore();
ctx.fillStyle = "#2ecc71";
ctx.fill();
}
function drawAddButton(x, y) {
drawCircle(x,y);
ctx.beginPath();
ctx.moveTo(x,y - hradius/2);
ctx.lineTo(x,y + hradius/2);
ctx.closePath();
ctx.strokeStyle ="white";
ctx.lineWidth = 3;
ctx.stroke();
ctx.beginPath();
ctx.moveTo(x - hradius/2, y);
ctx.lineTo(x + hradius/2, y);
ctx.closePath();
ctx.strokeStyle ="white";
ctx.lineWidth = 3;
ctx.stroke();
}
function drawLabelCircle(x,y,label) {
drawCircle(x,y);
ctx.fillStyle = "white"; // font color to write the text with
var fontsize = hradius;
var font = "normal " + fontsize +"px serif";
ctx.font = font;
// Move it down by half the text height and left by half the text canvasWidth
var w = ctx.measureText(label).width;
while (w > (hradius*1.5)*2)
{
fontsize = fontsize*0.99;
font = "bold " + fontsize +"px serif";
ctx.font = font;
w = ctx.measureText(label).width;
}
var height = ctx.measureText("w").width; // this is a GUESS of height
ctx.fillText(label, x - (w/2) ,y + (height/2));
}
//Clear the context
function clear()
{
ctx.clearRect(0,0,canvasWidth+ 75,canvasHeight);
ctx.fillStyle = "#000000";
ctx.fillRect(0,0,canvasWidth,canvasHeight);
}
function distance(x1, x2, y1, y2) {
return d = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}
var popUp = null;
function createAddPopUp(x, y)
{
var font = "bold " + 15 +"px Verdana";
ctx.font = font;
var h = ctx.measureText("w").width*1.125;
popupHeight = (h)*weightNames.length;
var maxWidth = 0;
var section = [];
for (var i = 0; i < weightNames.length; i++)
{
var label = weightNames[i];
var w = ctx.measureText(label).width;
section.push({name:label, x:(x+hradius + 10), y:((y - hradius - 60) + i*(h*1.0)), h:h/1.125});
if (w > maxWidth)
{
maxWidth = w;
}
}
popUp = {xOrigin:x, yOrigin:y, x:(x+hradius + 10), y:(y - hradius - 60), width:(maxWidth*1.0), height:popupHeight, sections:section};
}
function drawPopUp()
{
if (popUp != null)
{
ctx.fillStyle= "#C0C0C0";
ctx.fillRect(popUp.x, popUp.y, popUp.width, popUp.height);
ctx.beginPath();
ctx.moveTo(popUp.xOrigin, popUp.yOrigin);
ctx.lineTo(popUp.x, popUp.y);
ctx.lineTo(popUp.x, (popUp.y + popUp.height));
ctx.fill();
var h = ctx.measureText("w").width; // this is a GUESS of height
for (var i = 0; i < weightNames.length; i++)
{
var rec = popUp.sections[i];
var label = weightNames[i];
ctx.fillStyle = "black"; // font color to write the text with
var font = "bold " + 15 +"px Verdana";
ctx.font = font;
// Move it down by half the text height and left by half the text width
var w = ctx.measureText(label).width;
ctx.fillText(label, popUp.x , popUp.y +h+ i*(h*1.1));
}
}
}
function inText(x, y, texts)
{
for (var i = 0; i < texts.length; i++)
{
var t = texts[i];
if ((x >= t.x && x <= t.x + popUp.width) && (y >= t.y && y <= t.y + t.h))
{
return t;
}
}
return null;
}
var counter = 0;
function mausDown(e) {
// tell the browser we're handling this mouse event
e.preventDefault();
e.stopPropagation();
var mx = parseInt(e.clientX-offsetX);
var my = parseInt(e.clientY-offsetY);
// For popUps
if (popUp != null)
{
//if Null clicked outside so clear
var clic = inText(mx, my, popUp.sections);
if (clic == null)
{
popUp = null;
} else {
weights.splice(weights.length - 1,1, {x:canvasWidth/2, y:popUp.yOrigin - hradius*2 , isButton:false, label:clic.name, isDragging:false});
if (counter < numDataSets-1)
{
weights.push({x:canvasWidth/2, y:canvasHeight-hradius*2, isButton:true, label:""});
}
var index = weightNames.indexOf(clic.name);
if (index !== -1)
{
weightNames.splice(index, 1);
}
counter = counter + 1;
popUp = null;
//updateMap();
}
return;
}
dragok = {i:-1};
for (var i=0; i<weights.length; i++)
{
var w = weights[i];
var d = (((mx - w.x)*(mx - w.x))/(wradius*wradius)) + (((my - w.y)*(my - w.y))/(hradius*hradius))
if (d <= 1)
{
if (w.isButton)
{
createAddPopUp(w.x+(hradius*0.8), w.y);
} else {
dragok = {i:i};
}
startX = mx;
startY = my;
return;
}
}
}
function mausUp(e) {
// tell the browser we're handling this mouse event
e.preventDefault();
e.stopPropagation();
dragok = {i:-1};
draw();
}
function mausMove(e) {
// tell the browser we're handling this mouse event
e.preventDefault();
e.stopPropagation();
if (dragok.i != -1) {
var mx=parseInt(e.clientX-offsetX);
var my=parseInt(e.clientY-offsetY);
var dx= mx - startX;
var dy= my - startY;
weights[dragok.i].x+=dx;
weights[dragok.i].y+=dy;
startX = mx;
startY = my;
draw();
}
}
init();
draw();