Skip to content

Commit

Permalink
fix: restore context after drawing overlay + rename Overlay -> Graphi…
Browse files Browse the repository at this point in the history
…cOverlay
  • Loading branch information
bmatthieu3 committed May 21, 2024
1 parent e764866 commit 05d5dc1
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 39 deletions.
2 changes: 1 addition & 1 deletion examples/al-footprints.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
A.init.then(() => {
// Start up Aladin Lite
aladin = A.aladin('#aladin-lite-div', {survey: "CDS/P/DSS2/color", target: 'M 1', fov: 0.2, showContextMenu: true, fullScreen: true});
var overlay = A.graphicOverlay({color: '#ee2345', lineWidth: 3});
var overlay = A.graphicOverlay({color: '#ee2345', lineWidth: 3, lineDash: [2, 2]});
aladin.addOverlay(overlay);
overlay.addFootprints([
A.polygon([[83.64287, 22.01713], [83.59872, 22.01692], [83.59852, 21.97629], [83.64295, 21.97629]], {hoverColor: 'green'}),
Expand Down
6 changes: 3 additions & 3 deletions src/js/A.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*****************************************************************************/

import { MOC } from "./MOC.js";
import { Overlay } from "./Overlay.js";
import { GraphicOverlay } from "./Overlay.js";
import { Circle } from "./shapes/Circle.js";
import { Ellipse } from "./shapes/Ellipse.js";
import { Polyline } from "./shapes/Polyline.js";
Expand Down Expand Up @@ -326,7 +326,7 @@ A.vector = function (ra1, dec1, ra2, dec2, options) {
* var overlay = A.graphicOverlay({ color: '#ee2345', lineWidth: 3, lineDash: [2, 4]});
*/
A.graphicOverlay = function (options) {
return new Overlay(options);
return new GraphicOverlay(options);
};

/**
Expand Down Expand Up @@ -377,7 +377,7 @@ A.coo = function (longitude, latitude, prec) {
* @returns {Array.<Polyline|Circle>} Returns a list of shapes from the STC-S string
*/
A.footprintsFromSTCS = function (stcs, options) {
var footprints = Overlay.parseSTCS(stcs, options);
var footprints = GraphicOverlay.parseSTCS(stcs, options);
return footprints;
}

Expand Down
6 changes: 3 additions & 3 deletions src/js/Aladin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import { version } from "./../../package.json";
import { View } from "./View.js";
import { Utils } from "./Utils";
import { Overlay } from "./Overlay.js";
import { GraphicOverlay } from "./Overlay.js";
import { Logger } from "./Logger.js";
import { ProgressiveCat } from "./ProgressiveCat.js";
import { Sesame } from "./Sesame.js";
Expand Down Expand Up @@ -1837,7 +1837,7 @@ export let Aladin = (function () {
* Get list of overlays layers
*
* @memberof Aladin
* @returns {MOC[]|Catalog[]|ProgressiveCat[]|Overlay[]} - Returns the ordered list of image layers. Items can be {@link ImageHiPS} or {@link ImageFITS} objects.
* @returns {MOC[]|Catalog[]|ProgressiveCat[]|GraphicOverlay[]} - Returns the ordered list of image layers. Items can be {@link ImageHiPS} or {@link ImageFITS} objects.
*/
Aladin.prototype.getOverlays = function () {
return this.view.allOverlayLayers;
Expand Down Expand Up @@ -1879,7 +1879,7 @@ export let Aladin = (function () {
* Please use {@link A.graphicOverlay} instead
*/
Aladin.prototype.createOverlay = function (options) {
return new Overlay(options);
return new GraphicOverlay(options);
};

// Select corresponds to rectangular selection
Expand Down
66 changes: 46 additions & 20 deletions src/js/Overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,32 @@ import { Utils } from './Utils';
import A from "./A.js";
import { Color } from './Color';


export let Overlay = (function() {
let Overlay = function(options) {
/**
* @typedef {Object} GraphicOverlayOptions
* @description Options for configuring the graphic overlay
*
* @property {Object} options - Configuration options for the MOC.
* @property {string} [options.name="overlay"] - The name of the catalog.
* @property {string} [options.color] - A string parsed as CSS <color> value. See {@link https://developer.mozilla.org/en-US/docs/Web/CSS/color_value| here}
* @property {number} [options.lineWidth=3] - The line width in pixels
* @property {Array.<number>} [options.lineDash=[]] - Dash line option. See the segments property {@link https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash#segments| here}
*/

/**
* Represents an overlay containing Footprints, whether it is
*
* @namespace
* @typedef {Object} GraphicOverlay
*/
export let GraphicOverlay = (function() {
/**
* Constructor function for creating a new graphical overlay instance.
*
* @constructor
* @memberof GraphicOverlay
* @param {GraphicOverlayOptions} options - Configuration options for the overlay.
*/
let GraphicOverlay = function(options) {
options = options || {};

this.uuid = Utils.uuidv4();
Expand All @@ -45,7 +68,7 @@ export let Overlay = (function() {
this.name = options.name || "overlay";
this.color = options.color || Color.getNextColor();

this.lineWidth = options["lineWidth"] || 2;
this.lineWidth = options["lineWidth"] || 3;
this.lineDash = options["lineDash"] || [];

//this.indexationNorder = 5; // at which level should we index overlays?
Expand All @@ -59,7 +82,7 @@ export let Overlay = (function() {


// TODO : show/hide methods should be integrated in a parent class
Overlay.prototype.show = function() {
GraphicOverlay.prototype.show = function() {
if (this.isShowing) {
return;
}
Expand All @@ -70,7 +93,7 @@ export let Overlay = (function() {
this.reportChange();
};

Overlay.prototype.hide = function() {
GraphicOverlay.prototype.hide = function() {
if (! this.isShowing) {
return;
}
Expand All @@ -81,7 +104,7 @@ export let Overlay = (function() {
this.reportChange();
};

Overlay.prototype.toggle = function() {
GraphicOverlay.prototype.toggle = function() {
if (! this.isShowing) {
this.show()
} else {
Expand All @@ -90,7 +113,7 @@ export let Overlay = (function() {
};

// return an array of Footprint from a STC-S string
Overlay.parseSTCS = function(stcs, options) {
GraphicOverlay.parseSTCS = function(stcs, options) {
options = options || {};

var footprints = [];
Expand Down Expand Up @@ -151,7 +174,7 @@ export let Overlay = (function() {
};

// ajout d'un tableau d'overlays (= objets Footprint, Circle ou Polyline)
Overlay.prototype.addFootprints = function(overlaysToAdd) {
GraphicOverlay.prototype.addFootprints = function(overlaysToAdd) {
for (var k=0, len=overlaysToAdd.length; k<len; k++) {
this.add(overlaysToAdd[k], false);
}
Expand All @@ -160,7 +183,7 @@ export let Overlay = (function() {
};

// TODO : item doit pouvoir prendre n'importe quoi en param (footprint, circle, polyline)
Overlay.prototype.add = function(item, requestRedraw) {
GraphicOverlay.prototype.add = function(item, requestRedraw) {
requestRedraw = requestRedraw !== undefined ? requestRedraw : true;

//if (item instanceof Footprint) {
Expand All @@ -178,7 +201,7 @@ export let Overlay = (function() {


// return a footprint by index
Overlay.prototype.getFootprint = function(idx) {
GraphicOverlay.prototype.getFootprint = function(idx) {
if (idx<this.footprints.length) {
return this.footprints[idx];
}
Expand All @@ -187,21 +210,22 @@ export let Overlay = (function() {
}
};

Overlay.prototype.setView = function(view) {
GraphicOverlay.prototype.setView = function(view) {
this.view = view;
};

Overlay.prototype.removeAll = function() {
GraphicOverlay.prototype.removeAll = function() {
// TODO : RAZ de l'index
//this.overlays = [];
this.overlayItems = [];
};

Overlay.prototype.draw = function(ctx) {
GraphicOverlay.prototype.draw = function(ctx) {
if (!this.isShowing) {
return;
}

ctx.save();
// simple drawing
ctx.strokeStyle= this.color;
ctx.lineWidth = this.lineWidth;
Expand Down Expand Up @@ -233,9 +257,11 @@ export let Overlay = (function() {
for (var k=0; k<this.overlayItems.length; k++) {
this.overlayItems[k].draw(ctx, this.view);
}

ctx.restore();
};

Overlay.increaseBrightness = function(hex, percent){
GraphicOverlay.increaseBrightness = function(hex, percent){
// strip the leading # if it's there
hex = hex.replace(/^\s*#|\s*$/g, '');

Expand All @@ -254,25 +280,25 @@ export let Overlay = (function() {
((0|(1<<8) + b + (256 - b) * percent / 100).toString(16)).substr(1);
};

Overlay.prototype.setColor = function(color) {
GraphicOverlay.prototype.setColor = function(color) {
this.color = color;
this.reportChange();
};

Overlay.prototype.setLineWidth = function(lineWidth) {
GraphicOverlay.prototype.setLineWidth = function(lineWidth) {
this.lineWidth = lineWidth;
this.reportChange();
};

Overlay.prototype.setLineDash = function(lineDash) {
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
Overlay.prototype.reportChange = function() {
GraphicOverlay.prototype.reportChange = function() {
this.view && this.view.requestRedraw();
};

return Overlay;
return GraphicOverlay;
})();
6 changes: 3 additions & 3 deletions src/js/shapes/Circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*****************************************************************************/

import { Utils } from "./../Utils";
import { Overlay } from "./../Overlay.js";
import { GraphicOverlay } from "./../Overlay.js";

/**
* Represents an circle shape
Expand Down Expand Up @@ -254,10 +254,10 @@ export let Circle = (function() {
if(this.selectionColor) {
ctx.strokeStyle = this.selectionColor;
} else {
ctx.strokeStyle = Overlay.increaseBrightness(baseColor, 50);
ctx.strokeStyle = GraphicOverlay.increaseBrightness(baseColor, 50);
}
} else if (this.isHovered) {
ctx.strokeStyle = this.hoverColor || Overlay.increaseBrightness(baseColor, 25);
ctx.strokeStyle = this.hoverColor || GraphicOverlay.increaseBrightness(baseColor, 25);
} else {
ctx.strokeStyle = baseColor;
}
Expand Down
6 changes: 3 additions & 3 deletions src/js/shapes/Ellipse.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*****************************************************************************/

import { Utils } from "./../Utils";
import { Overlay } from "./../Overlay.js";
import { GraphicOverlay } from "./../Overlay.js";

/**
* @typedef {Object} ShapeOptions
Expand Down Expand Up @@ -294,10 +294,10 @@ export let Ellipse = (function() {
if(this.selectionColor) {
ctx.strokeStyle = this.selectionColor;
} else {
ctx.strokeStyle = Overlay.increaseBrightness(baseColor, 50);
ctx.strokeStyle = GraphicOverlay.increaseBrightness(baseColor, 50);
}
} else if (this.isHovered) {
ctx.strokeStyle = this.hoverColor || Overlay.increaseBrightness(baseColor, 25);
ctx.strokeStyle = this.hoverColor || GraphicOverlay.increaseBrightness(baseColor, 25);
} else {
ctx.strokeStyle = baseColor;
}
Expand Down
6 changes: 3 additions & 3 deletions src/js/shapes/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*****************************************************************************/
import { Polyline } from "./Polyline.js";
import { Utils } from '../Utils';
import { Overlay } from "../Overlay.js";
import { GraphicOverlay } from "../Overlay.js";
import { Ellipse } from "./Ellipse.js";

/**
Expand Down Expand Up @@ -131,9 +131,9 @@ export let Line = (function() {
}

if (this.isSelected) {
ctx.strokeStyle = this.selectionColor || Overlay.increaseBrightness(baseColor, 50);
ctx.strokeStyle = this.selectionColor || GraphicOverlay.increaseBrightness(baseColor, 50);
} else if (this.isHovered) {
ctx.strokeStyle = this.hoverColor || Overlay.increaseBrightness(baseColor, 25);
ctx.strokeStyle = this.hoverColor || GraphicOverlay.increaseBrightness(baseColor, 25);
} else {
ctx.strokeStyle = baseColor;
}
Expand Down
6 changes: 3 additions & 3 deletions src/js/shapes/Polyline.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*****************************************************************************/

import { Utils } from '../Utils';
import { Overlay } from "../Overlay.js";
import { GraphicOverlay } from "../Overlay.js";
import { ProjectionEnum } from "../ProjectionEnum.js";

/**
Expand Down Expand Up @@ -267,10 +267,10 @@ export let Polyline = (function() {
if(this.selectionColor) {
ctx.strokeStyle = this.selectionColor;
} else {
ctx.strokeStyle = Overlay.increaseBrightness(baseColor, 50);
ctx.strokeStyle = GraphicOverlay.increaseBrightness(baseColor, 50);
}
} else if (this.isHovered) {
ctx.strokeStyle = this.hoverColor || Overlay.increaseBrightness(baseColor, 25);
ctx.strokeStyle = this.hoverColor || GraphicOverlay.increaseBrightness(baseColor, 25);
} else {
ctx.strokeStyle = baseColor;
}
Expand Down

0 comments on commit 05d5dc1

Please sign in to comment.