forked from NOAA-ORR-ERD/compass-rose-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compass-rose-ui.js
359 lines (307 loc) · 13.8 KB
/
compass-rose-ui.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* Compass Rose UI - a javascript control for selecting velocity and direction
* for wind and water.
*/
(function( $ ) {
$.fn.compassRoseUI = function(options, arg) {
return this.each(function() {
var _this = this;
// Public API methods
// clear the front canvas (arrow and line shapes)
if (options === 'reset') {
this.resetCanvas();
return;
}
// update the plugin properties
if (options === 'update') {
if (!this.frontcanv) {
return;
}
this.resetCanvas();
var newDirection = parseInt(arg.direction);
var newSpeed = parseInt(arg.speed);
var coordinates = this.flipXY(this.polarToCartesian(newDirection, newSpeed));
coordinates.x *= this.frontcanv.px_per_unit;
coordinates.y *= -this.frontcanv.px_per_unit;
coordinates.x += this.frontcanv.width / 2;
coordinates.y += this.frontcanv.height / 2;
_this.drawArrow(coordinates);
if(this.frontcanv.parentElement.settings.move && arg.trigger_move === true){
this.frontcanv.parentElement.settings['move'](newSpeed, newDirection);
}
return;
}
this.settings = $.extend( {
'arrow-direction' : 'out',
// we can optionally set a function which translates the angle to its nearest cardinal value
'cardinal-name' : function(angle) {
var dirNames = ['N', 'NNE', 'NE', 'ENE',
'E', 'ESE', 'SE', 'SSE',
'S', 'SSW', 'SW', 'WSW',
'W', 'WNW', 'NW', 'NNW'];
return dirNames[Math.floor((+(angle) + 360 / 32) / (360 / 16.0) % 16)];
},
// we can optionally set a function which translates the cardinal direction to its angle
'cardinal-angle' : function(name) {
var dirNames = ['N', 'NNE', 'NE', 'ENE',
'E', 'ESE', 'SE', 'SSE',
'S', 'SSW', 'SW', 'WSW',
'W', 'WNW', 'NW', 'NNW'];
var idx = dirNames.indexOf(name.toUpperCase());
if (idx === -1) {
return null;
} else {
return (360.0 / 16) * idx;
}
},
// we can optionally set a function which happens on a move
'move' : null,
// we can optionally set a function which happens after a mouse drag
'change' : null,
}, options);
this.resetCanvas = function() {
if (!this.frontcanv) {
return;
}
this.frontcanv.getContext('2d').clearRect(
0, 0, this.frontcanv.width, this.frontcanv.height);
};
this.polarToCartesian = function(angle, magnitude) {
var radians = angle * Math.PI / 180;
return {
x: magnitude * Math.cos(radians),
y: magnitude * Math.sin(radians)
};
};
this.cartesianToPolar = function(x, y) {
return {
magnitude: Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)),
angle: Math.atan2(x, y) * 180 / Math.PI
};
};
this.flipXY = function(coords) {
var tmp = coords.x;
coords.x = coords.y;
coords.y = tmp;
return coords;
};
this.getMousePosition = function(ev){
// XXX: Do these values get used?
var coordinates;
var offset = $(this.frontcanv).offset();
if (ev.originalEvent['layerX'] != undefined) {
coordinates = {
x: ev.originalEvent.pageX - offset.left,
y: ev.originalEvent.pageY - offset.top
};
} else {
// in IE, we use this property
coordinates = {
x: ev.originalEvent.x,
y: ev.originalEvent.y
};
}
return coordinates;
};
this.checkPath = function(ctx, coords, path){
return ctx.isPointInPath(path, coords.x, coords.y);
};
this.drawArrow = function(coords) {
var canvas = this.frontcanv;
var ctx = canvas.getContext('2d');
var xmag = (coords.x - canvas.width / 2);
var ymag = -(coords.y - canvas.height / 2);
canvas.pmag = Math.sqrt(Math.pow(xmag, 2) + Math.pow(ymag, 2));
canvas.pmag /= canvas.px_per_unit;
var temp = canvas.pmag;
// Capping maximum magnitude of speed vector to 30
(canvas.pmag > 30) ? canvas.pmag = 30 : canvas.pmag;
// Convert degrees to radians
canvas.pangle = Math.atan2(xmag, ymag) * 180 / Math.PI;
if (canvas.pangle < 0) {
canvas.pangle += 360;
}
if (temp > 30) {
coords = this.flipXY(this.polarToCartesian(canvas.pangle, 30));
coords.y = -coords.y;
coords.x *= canvas.px_per_unit;
coords.y *= canvas.px_per_unit;
coords.x += canvas.width / 2;
coords.y += canvas.height / 2;
}
// draw a line from the center
ctx.lineWidth = 2;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.moveTo(canvas.width / 2, canvas.height / 2);
ctx.lineTo(coords.x, coords.y);
ctx.stroke();
ctx.closePath();
// draw our arrow point
ctx.beginPath();
if (canvas.parentElement.settings &&
canvas.parentElement.settings['arrow-direction'] === 'in')
{
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.rotate((canvas.pangle + 180) * Math.PI / 180);
} else {
ctx.translate(coords.x, coords.y);
ctx.rotate(canvas.pangle * Math.PI / 180);
}
ctx.moveTo(0, 0);
ctx.quadraticCurveTo(0, 8, 8, 15);
ctx.lineTo(0, 8);
ctx.moveTo(0, 0);
ctx.quadraticCurveTo(0, 8, -8, 15);
ctx.lineTo(0, 8);
ctx.stroke();
ctx.closePath();
if (canvas.parentElement.settings &&
canvas.parentElement.settings['arrow-direction'] === 'in')
{
ctx.rotate(-(canvas.pangle + 180) * Math.PI / 180);
ctx.translate(-canvas.width / 2, -canvas.height / 2);
} else {
ctx.rotate(-canvas.pangle * Math.PI / 180);
ctx.translate(-coords.x, -coords.y);
}
ctx.beginPath();
ctx.closePath();
};
$(this).load(function(ev) {
var event = ev;
});
// check if our object is hidden
$(this).css({'visibility':'hidden', 'display':'block'});
// here we create our background canvas
if (this['backcanv'] === undefined) {
$(this).append('<canvas id="' + this.id + '-back"></canvas>');
this.backcanv = $('canvas#' + this.id + '-back')[0];
if (window['G_vmlCanvasManager'] != undefined) {
// older versions of IE don't natively have canvas functionality
G_vmlCanvasManager.initElement(this.backcanv);
}
}
var backcanv = this.backcanv;
backcanv.width = $(this).width();
backcanv.height = $(this).height();
backcanv.style.zIndex = "0";
backcanv.style.position = 'absolute';
// here we create our foreground canvas
if (this['frontcanv'] === undefined) {
$(this).append('<canvas id="' + this.id + '-front"></canvas>');
this.frontcanv = $('canvas#' + this.id + '-front')[0];
if (window['G_vmlCanvasManager'] != undefined) {
G_vmlCanvasManager.initElement(this.frontcanv);
}
}
var frontcanv = this.frontcanv;
frontcanv.width = $(this).width();
frontcanv.height = $(this).height();
frontcanv.style.zIndex = "1";
frontcanv.style.position = 'absolute';
// if our div was hidden, return it to its original state
$(this).removeAttr('style');
//
// here we draw the background canvas
//
var ctx = backcanv.getContext('2d');
var maxradius = ((backcanv.width > backcanv.height)
? backcanv.height / 2 - 1
: backcanv.width / 2 - 1) * 0.75;
// here is the backround white target
var path = this.path = new Path2D();
this.path.arc(backcanv.width / 2, backcanv.height / 2,
maxradius + 1, 0, Math.PI * 2, true);
ctx.fillStyle = 'rgba(255, 255, 255, .8)';
ctx.fill(path);
// here are the concentric circles in the target
// TODO: Add a setting for scale.
frontcanv.px_per_unit = maxradius / 30;
ctx.beginPath();
for (var i = maxradius / 3; i <= maxradius; i += maxradius / 3) {
ctx.moveTo(backcanv.width / 2 + i, backcanv.height / 2);
ctx.arc(backcanv.width / 2, backcanv.height / 2,
i, 0, 2 * Math.PI);
}
ctx.closePath();
ctx.stroke();
// here are the direction indicator letters
ctx.fillStyle = 'rgba(0, 0, 0, .8)';
ctx.translate(backcanv.width / 2, backcanv.height / 2);
var fontsize = backcanv.height / 20 + 1;
var fontpad = backcanv.height / 50;
if (window['G_vmlCanvasManager'] != undefined) {
ctx.font= 'bold ' + fontsize + 'px Arial';
} else {
ctx.font= 'bold ' + fontsize + 'px Arial';
}
ctx.fillText('N', -5, -((backcanv.height / 2) - fontsize));
ctx.fillText('S', -5, (backcanv.height / 2) - fontpad);
ctx.fillText('W', -(backcanv.width / 2 - fontpad), 5);
ctx.fillText('E', (backcanv.width / 2 - fontsize), 5);
var numAngles = 8;
for (var i = 0; i < numAngles; i++) {
var angleSize = 360 / numAngles;
var txt = (angleSize * i).toString();
var txtWidth = ctx.measureText(txt).width;
ctx.fillText(txt, -txtWidth / 2,
-(backcanv.height / 2 - (fontsize * 2) - fontpad + 1));
ctx.rotate(angleSize * Math.PI / 180);
}
//
// here we draw the front canvas
// which we will be drawing on
//
ctx = frontcanv.getContext('2d');
ctx.fillRect(20, 20, 150, 100);
ctx.clearRect(20, 20, 150, 100);
frontcanv.pressed = false;
frontcanv.moved = false;
$(frontcanv).mousedown(function (ev) {
this.pressed = true;
var coordinates = _this.getMousePosition(ev);
var path = _this.path;
var inPath = _this.checkPath(ctx, coordinates, path);
if (inPath) {
_this.drawArrow(coordinates);
// pass our values to the configured move function
if (this.parentElement.settings &&
this.parentElement.settings['move'] !== null)
{
this.parentElement.settings['move'](this.pmag, this.pangle);
}
}
});
$(frontcanv).mousemove(function (ev) {
if (!this.pressed) {
return;
}
this.moved = true;
var coordinates = _this.getMousePosition(ev);
var path = _this.path;
var inPath = _this.checkPath(ctx, coordinates, path);
if (inPath) {
_this.drawArrow(coordinates);
// pass our values to the configured move function
if (this.parentElement.settings &&
this.parentElement.settings['move'] !== null)
{
this.parentElement.settings['move'](this.pmag, this.pangle);
}
}
});
$(frontcanv).mouseup(function (ev) {
if (this.pressed && this.moved) {
// pass our values to the configured change function
if (this.parentElement.settings &&
this.parentElement.settings['change'] !== null)
{
this.parentElement.settings['change'](this.pmag, this.pangle);
}
}
this.pressed = this.moved = false;
});
});
};
})( jQuery );