DrawBox.js 1.8 KB

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