sigplot-ts
Version:
TypeScript-based Integration Library for SigPlot 2.0
236 lines • 8.14 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var sigplot = require("sigplot");
/**
* This is an alternate Boxes plugin that allows control over
* which sides of the box to draw.
*
* This class mirrors closely the version found in SigPlot.
* However more methods have been added for supporting
* updating, etc. as well as drawing partial boxes.
*/
var Boxes2Plugin = /** @class */ (function () {
function Boxes2Plugin(options) {
if (options === void 0) { options = {}; }
this.options = options;
/** Map of the boxes to draw */
this.boxes = new Map();
if (this.options.display === undefined) {
this.options.display = true;
}
this.resetStats();
}
Object.defineProperty(Boxes2Plugin.prototype, "minX", {
/** Minimum X position */
get: function () { return this._minX; },
enumerable: true,
configurable: true
});
Object.defineProperty(Boxes2Plugin.prototype, "maxX", {
/** Maximum X position */
get: function () { return this._maxX; },
enumerable: true,
configurable: true
});
Object.defineProperty(Boxes2Plugin.prototype, "minY", {
/** Minimum Y position */
get: function () { return this._minY; },
enumerable: true,
configurable: true
});
Object.defineProperty(Boxes2Plugin.prototype, "maxY", {
/** Maximum Y Position */
get: function () { return this._maxY; },
enumerable: true,
configurable: true
});
/**
* Initialize the plugin for the plot
* @param plot
*/
Boxes2Plugin.prototype.init = function (plot) {
this.plot = plot;
};
/**
* Menu callback
*/
Boxes2Plugin.prototype.menu = function () {
var _this = this;
return {
text: 'Boxes2...',
menu: {
title: 'BOXES_2',
items: [{
text: 'Display',
checked: this.options.display,
style: 'checkbox',
handler: function () {
_this.options.display = !_this.options.display;
_this.plot.redraw();
}
}, {
text: 'Clear All',
handler: function () { _this.clear_boxes(); }
}]
}
};
};
/**
* Add a box to the view
* @param id The unique ID for the box
* @param box The box parameters to draw
* @param redraw Whether or not to redraw with this call
*/
Boxes2Plugin.prototype.add_box = function (id, box, redraw) {
if (redraw === void 0) { redraw = true; }
this.boxes.set(id, box);
if (redraw) {
this.plot.redraw();
}
};
/**
* Update a box in the view
* @param id The unique ID for the box
* @param box The box parameters to draw
* @param redraw Whether or not to redraw with this call
*/
Boxes2Plugin.prototype.update_box = function (id, box, redraw) {
if (redraw === void 0) { redraw = true; }
// TODO: Change this to a merge?
this.add_box(id, box, redraw);
};
/**
* Remove a box from the view
* @param id The unique ID for the box
* @param redraw Whether or not to redraw with this call
*/
Boxes2Plugin.prototype.remove_box = function (id, redraw) {
if (redraw === void 0) { redraw = true; }
this.boxes.delete(id);
if (redraw) {
this.plot.redraw();
}
};
/**
* Clears all boxes
* @param redraw Whether or not to redraw with this call
*/
Boxes2Plugin.prototype.clear_boxes = function (redraw) {
if (redraw === void 0) { redraw = true; }
this.boxes.clear();
if (redraw) {
this.plot.redraw();
}
};
/**
* Refresh all the drawn blocks
* @param canvas
*/
Boxes2Plugin.prototype.refresh = function (canvas) {
var _this = this;
if (!this.options.display) {
return;
}
var Gx = this.plot._Gx;
var Mx = this.plot._Mx;
// Get context, and ensure we don't draw outside
var ctx = canvas.getContext('2d');
ctx.save();
ctx.beginPath();
ctx.rect(Mx.l, Mx.t, Mx.r - Mx.l, Mx.b - Mx.t);
ctx.clip();
var x;
var y;
var height;
var width;
this.resetStats();
this.boxes.forEach(function (box, id) {
// Update statistics
_this._minX = Math.min(_this._minX, box.x);
_this._maxX = Math.max(_this._maxX, box.x + box.width);
_this._minY = Math.min(_this._minY, box.y - box.height);
_this._maxY = Math.max(_this._maxY, box.y);
// Calculate drawing position
if (box.absolutePlacement) {
x = box.x + Mx.l;
y = box.y + Mx.t;
width = box.width;
height = box.height;
}
else {
var ul = sigplot.mx.real_to_pixel(Mx, box.x, box.y);
var lr = sigplot.mx.real_to_pixel(Mx, box.x + box.width, box.y + box.height);
x = ul.x;
y = ul.y;
width = lr.x - ul.x;
height = ul.y - lr.y;
}
// If fillStyle is set, create the rectangle first.
if (box.fill) {
var alpha = ctx.globalAlpha;
var fill = ctx.fillStyle;
ctx.beginPath();
ctx.globalAlpha = box.fill.alpha || alpha;
ctx.fillStyle = box.fill.style || fill;
ctx.fillRect(x, y, width, height);
// Restore alpha, fill
ctx.globalAlpha = alpha;
ctx.fillStyle = fill;
}
// Helper function for drawing a generic line
var drawSide = function (x1, y1, x2, y2, feature) {
if (feature === void 0) { feature = {}; }
ctx.beginPath();
ctx.lineCap = feature.lineCap || 'butt';
ctx.strokeStyle = feature.strokeStyle || '#000000';
ctx.lineWidth = feature.lineWidth || 1;
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
};
// Draw each side: left, top, right, bottom
if (box.leftSide) {
drawSide(x, y, x, y - height, box.leftSide);
}
if (box.topSide) {
drawSide(x, y - height, x + width, y - height, box.topSide);
}
if (box.rightSide) {
drawSide(x + width, y, x + width, y - height, box.rightSide);
}
if (box.bottomSide) {
drawSide(x, y, x + width, y, box.bottomSide);
}
// If text is defined, add the annotation
if (box.text) {
ctx.beginPath();
ctx.font = box.text.font || Mx.text_H + 'px Courier New, monospace';
ctx.globalAlpha = 1;
ctx.textAlign = 'end';
ctx.fillStyle = ctx.strokeStyle;
x -= Mx.text_w;
y -= (Mx.text_h / 3);
var textWidth = ctx.measureText(box.text.value).width;
if (x - textWidth < Mx.l) {
x += width;
}
ctx.fillText(box.text.value, x, y);
}
});
// Restore
ctx.restore();
};
Boxes2Plugin.prototype.dispose = function () {
this.plot = undefined;
this.boxes.clear();
};
Boxes2Plugin.prototype.resetStats = function () {
this._minX = Infinity;
this._maxX = -Infinity;
this._minY = Infinity;
this._maxY = -Infinity;
};
return Boxes2Plugin;
}());
exports.Boxes2Plugin = Boxes2Plugin;
//# sourceMappingURL=boxes2-plugin.js.map
;