DrawBox.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var classes_1 = require("../classes");
  4. var getContext2dOrThrow_1 = require("../dom/getContext2dOrThrow");
  5. var DrawTextField_1 = require("./DrawTextField");
  6. var DrawBoxOptions = /** @class */ (function () {
  7. function DrawBoxOptions(options) {
  8. if (options === void 0) { options = {}; }
  9. var boxColor = options.boxColor, lineWidth = options.lineWidth, label = options.label, drawLabelOptions = options.drawLabelOptions;
  10. this.boxColor = boxColor || 'rgba(0, 0, 255, 1)';
  11. this.lineWidth = lineWidth || 2;
  12. this.label = label;
  13. var defaultDrawLabelOptions = {
  14. anchorPosition: DrawTextField_1.AnchorPosition.BOTTOM_LEFT,
  15. backgroundColor: this.boxColor
  16. };
  17. this.drawLabelOptions = new DrawTextField_1.DrawTextFieldOptions(Object.assign({}, defaultDrawLabelOptions, drawLabelOptions));
  18. }
  19. return DrawBoxOptions;
  20. }());
  21. exports.DrawBoxOptions = DrawBoxOptions;
  22. var DrawBox = /** @class */ (function () {
  23. function DrawBox(box, options) {
  24. if (options === void 0) { options = {}; }
  25. this.box = new classes_1.Box(box);
  26. this.options = new DrawBoxOptions(options);
  27. }
  28. DrawBox.prototype.draw = function (canvasArg) {
  29. var ctx = getContext2dOrThrow_1.getContext2dOrThrow(canvasArg);
  30. var _a = this.options, boxColor = _a.boxColor, lineWidth = _a.lineWidth;
  31. var _b = this.box, x = _b.x, y = _b.y, width = _b.width, height = _b.height;
  32. ctx.strokeStyle = boxColor;
  33. ctx.lineWidth = lineWidth;
  34. ctx.strokeRect(x, y, width, height);
  35. var label = this.options.label;
  36. if (label) {
  37. new DrawTextField_1.DrawTextField([label], { x: x - (lineWidth / 2), y: y }, this.options.drawLabelOptions).draw(canvasArg);
  38. }
  39. };
  40. return DrawBox;
  41. }());
  42. exports.DrawBox = DrawBox;
  43. //# sourceMappingURL=DrawBox.js.map