-
Notifications
You must be signed in to change notification settings - Fork 42
/
Overlay.js
394 lines (336 loc) · 11.9 KB
/
Overlay.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright 2015 - UDS/CNRS
// The Aladin Lite program is distributed under the terms
// of the GNU General Public License version 3.
//
// This file is part of Aladin Lite.
//
// Aladin Lite is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// Aladin Lite is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// The GNU General Public License is available in COPYING file
// along with Aladin Lite.
//
/******************************************************************************
* Aladin Lite project
*
* File Overlay
*
* Description: a plane holding overlays (footprints, polylines, circles)
*
* Author: Thomas Boch[CDS]
*
*****************************************************************************/
import { Utils } from './Utils';
import A from "./A.js";
import { Color } from './Color';
/**
* @typedef {Object} GraphicOverlayOptions
* @description Options for configuring the graphic overlay
*
* @property {string} [name="overlay"] - The name of the catalog.
* @property {string} [color] - A string parsed as CSS <color> value. See {@link https://developer.mozilla.org/en-US/docs/Web/CSS/color_value| here}
* @property {number} [lineWidth=3] - The line width in pixels
* @property {Array.<number>} [lineDash=[]] - Dash line option. See the segments property {@link https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash#segments| here}
*/
export let GraphicOverlay = (function() {
/**
* Represents an overlay containing Footprints, whether it is
*
* @class
* @constructs GraphicOverlay
* @param {GraphicOverlayOptions} options - Configuration options for the overlay.
*/
let GraphicOverlay = function(options) {
options = options || {};
this.uuid = Utils.uuidv4();
this.type = 'overlay';
this.name = options.name || "overlay";
this.color = options.color || Color.getNextColor();
this.lineWidth = options["lineWidth"] || 3;
this.lineDash = options["lineDash"] || [];
//this.indexationNorder = 5; // at which level should we index overlays?
//this.overlays = [];
this.overlayItems = []; // currently Circle or Polyline
//this.hpxIdx = new HealpixIndex(this.indexationNorder);
//this.hpxIdx.init();
this.isShowing = true;
};
// TODO : show/hide methods should be integrated in a parent class
/**
* Show the graphic overlay
*
* @memberof GraphicOverlay
*/
GraphicOverlay.prototype.show = function() {
if (this.isShowing) {
return;
}
this.isShowing = true;
// Dispatch to the child shapes
this.overlayItems.forEach((item) => item.show())
this.reportChange();
};
/**
* Hide the graphic overlay
*
* @memberof GraphicOverlay
*/
GraphicOverlay.prototype.hide = function() {
if (! this.isShowing) {
return;
}
this.isShowing = false;
// Dispatch to the child shapes
this.overlayItems.forEach((item) => item.hide())
this.reportChange();
};
/**
* Toggle on/off the graphic overlay
*
* @memberof GraphicOverlay
*/
GraphicOverlay.prototype.toggle = function() {
if (! this.isShowing) {
this.show()
} else {
this.hide()
}
};
// return an array of Footprint from a STC-S string
/**
* Parse a STCS string and returns a list of footprints (only circles, polygons and ellipses given in ICRS frame are handled).
*
* @memberof GraphicOverlay
*
* @returns {Circle[]|Polyline[]|Ellipse[]} The list of mixed circles, polygons and ellipses
*/
GraphicOverlay.parseSTCS = function(stcs, options) {
options = options || {};
var footprints = [];
var parts = stcs.match(/\S+/g);
var k = 0, len = parts.length;
while(k<len) {
var s = parts[k].toLowerCase();
if(s=='polygon') {
var curPolygon = [];
k++;
frame = parts[k].toLowerCase();
if (Utils.isNumber(frame)) {
frame = 'icrs'
k--;
}
if (frame=='icrs' || frame=='j2000' || frame=='fk5') {
while(k+2<len) {
var ra = parseFloat(parts[k+1]);
if (isNaN(ra)) {
break;
}
var dec = parseFloat(parts[k+2]);
curPolygon.push([ra, dec]);
k += 2;
}
options.closed = true;
footprints.push(A.polygon(curPolygon, options));
}
}
else if (s=='circle') {
var frame;
k++;
frame = parts[k].toLowerCase();
if (Utils.isNumber(frame)) {
frame = 'icrs'
k--;
}
if (frame=='icrs' || frame=='j2000' || frame=='fk5') {
var ra, dec, radiusDegrees;
ra = parseFloat(parts[k+1]);
dec = parseFloat(parts[k+2]);
radiusDegrees = parseFloat(parts[k+3]);
footprints.push(A.circle(ra, dec, radiusDegrees, options));
k += 3;
}
} else if (s=='ellipse') {
var frame;
k++;
frame = parts[k].toLowerCase();
if (Utils.isNumber(frame)) {
frame = 'icrs'
k--;
}
if (frame=='icrs' || frame=='j2000' || frame=='fk5') {
var ra, dec, a, b, theta;
ra = parseFloat(parts[k+1]);
dec = parseFloat(parts[k+2]);
a = parseFloat(parts[k+3]);
b = parseFloat(parts[k+4]);
theta = parseFloat(parts[k+5]);
footprints.push(A.ellipse(ra, dec, a, b, theta, options));
k += 5;
}
}
k++;
}
return footprints;
};
/**
* Add an array (or single) shapes (i.e. Footprint, Circle, Polyline, Ellipse, Vector, ...)
*
* @memberof GraphicOverlay
*
* @param {Footprint[]|Circle[]|Polyline[]|Ellipse[]|Vector[]} overlaysToAdd - a list (or single) shapes to add to the overlay
*/
GraphicOverlay.prototype.addFootprints = function(overlaysToAdd) {
overlaysToAdd = [].concat(overlaysToAdd)
for (var k=0, len=overlaysToAdd.length; k<len; k++) {
this.add(overlaysToAdd[k], false);
}
};
// TODO : item doit pouvoir prendre n'importe quoi en param (footprint, circle, polyline)
GraphicOverlay.prototype.add = function(item, requestRedraw) {
requestRedraw = requestRedraw !== undefined ? requestRedraw : true;
//if (item instanceof Footprint) {
// this.overlays.push(item);
//}
//else {
this.overlayItems.push(item);
//}
item.setOverlay(this);
if (requestRedraw) {
this.view.requestRedraw();
}
};
/**
* Returns a shape by an index
*
* @memberof GraphicOverlay
*
* @param {number} idx - The index of the shape to retrieve
*
* @returns {Footprint|Circle|Polyline|Ellipse|Vector} The shape
*/
GraphicOverlay.prototype.getFootprint = function(idx) {
if (idx<this.footprints.length) {
return this.footprints[idx];
}
else {
return null;
}
};
GraphicOverlay.prototype.setView = function(view, idx) {
this.view = view;
this.view.overlays.push(this);
this.view.insertOverlay(this, idx);
};
/**
* Clear the overlay of all its shapes
*
* @memberof GraphicOverlay
*/
GraphicOverlay.prototype.removeAll = function() {
// TODO : RAZ de l'index
//this.overlays = [];
this.overlayItems = [];
};
GraphicOverlay.prototype.draw = function(ctx) {
if (!this.isShowing) {
return;
}
ctx.save();
// simple drawing
ctx.strokeStyle= this.color;
ctx.lineWidth = this.lineWidth;
ctx.setLineDash(this.lineDash);
// 1. Drawing polygons
// TODO: les overlay polygons devrait se tracer lui meme (methode draw)
//ctx.lineWidth = this.lineWidth;
//ctx.beginPath();
/*var xyviews = [];
for (var k=0, len = this.overlays.length; k<len; k++) {
xyviews.push(this.drawFootprint(this.overlays[k], ctx, width, height));
}*/
//ctx.stroke();
// selection drawing
/*ctx.strokeStyle= Overlay.increaseBrightness(this.color, 50);
ctx.beginPath();
for (var k=0, len = this.overlays.length; k<len; k++) {
if (this.overlays[k].isSelected) {
this.drawFootprintSelected(ctx, xyviews[k]);
}
}
ctx.stroke();*/
// 2. Circle and polylines drawing
for (var k=0; k<this.overlayItems.length; k++) {
this.overlayItems[k].draw(ctx, this.view);
}
ctx.restore();
};
/**
* Increase the brightness of a color by a percentage
*
* @memberof GraphicOverlay
*
* @param {string} hex - The color given in hexadecimal e.g. '#ffa0bb'
* @param {number} percent - The percentage to increase the brightness of
*
* @returns {string} The new color given as an hexadecimal string
*/
GraphicOverlay.increaseBrightness = function(hex, percent){
// strip the leading # if it's there
hex = hex.replace(/^\s*#|\s*$/g, '');
// convert 3 char codes --> 6, e.g. `E0F` --> `EE00FF`
if(hex.length == 3){
hex = hex.replace(/(.)/g, '$1$1');
}
var r = parseInt(hex.substr(0, 2), 16),
g = parseInt(hex.substr(2, 2), 16),
b = parseInt(hex.substr(4, 2), 16);
return '#' +
((0|(1<<8) + r + (256 - r) * percent / 100).toString(16)).substr(1) +
((0|(1<<8) + g + (256 - g) * percent / 100).toString(16)).substr(1) +
((0|(1<<8) + b + (256 - b) * percent / 100).toString(16)).substr(1);
};
/**
* Set the color of the shapes inside the overlay
*
* @memberof GraphicOverlay
*
* @param {string} color - the new color in hexadecimal e.g. '#ff00ff'
*/
GraphicOverlay.prototype.setColor = function(color) {
this.color = color;
this.reportChange();
};
/**
* Set the line width of the shapes inside the overlay
*
* @memberof GraphicOverlay
*
* @param {number} lineWidth - the new line width in pixels
*/
GraphicOverlay.prototype.setLineWidth = function(lineWidth) {
this.lineWidth = lineWidth;
this.reportChange();
};
/**
* Set the dash line property
*
* @memberof GraphicOverlay
*
* @param {Array.<number>} [lineDash=[]] - See the segments property {@link https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash#segments| here}
*/
GraphicOverlay.prototype.setLineDash = function(lineDash) {
this.lineDash = lineDash;
this.reportChange();
}
// callback function to be called when the status of one of the footprints has changed
GraphicOverlay.prototype.reportChange = function() {
this.view && this.view.requestRedraw();
};
return GraphicOverlay;
})();