DrawTextField.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { getContext2dOrThrow } from '../dom/getContext2dOrThrow';
  2. import { resolveInput } from '../dom/resolveInput';
  3. export var AnchorPosition;
  4. (function (AnchorPosition) {
  5. AnchorPosition["TOP_LEFT"] = "TOP_LEFT";
  6. AnchorPosition["TOP_RIGHT"] = "TOP_RIGHT";
  7. AnchorPosition["BOTTOM_LEFT"] = "BOTTOM_LEFT";
  8. AnchorPosition["BOTTOM_RIGHT"] = "BOTTOM_RIGHT";
  9. })(AnchorPosition || (AnchorPosition = {}));
  10. var DrawTextFieldOptions = /** @class */ (function () {
  11. function DrawTextFieldOptions(options) {
  12. if (options === void 0) { options = {}; }
  13. var anchorPosition = options.anchorPosition, backgroundColor = options.backgroundColor, fontColor = options.fontColor, fontSize = options.fontSize, fontStyle = options.fontStyle, padding = options.padding;
  14. this.anchorPosition = anchorPosition || AnchorPosition.TOP_LEFT;
  15. this.backgroundColor = backgroundColor || 'rgba(0, 0, 0, 0.5)';
  16. this.fontColor = fontColor || 'rgba(255, 255, 255, 1)';
  17. this.fontSize = fontSize || 14;
  18. this.fontStyle = fontStyle || 'Georgia';
  19. this.padding = padding || 4;
  20. }
  21. return DrawTextFieldOptions;
  22. }());
  23. export { DrawTextFieldOptions };
  24. var DrawTextField = /** @class */ (function () {
  25. function DrawTextField(text, anchor, options) {
  26. if (options === void 0) { options = {}; }
  27. this.text = typeof text === 'string'
  28. ? [text]
  29. : (text instanceof DrawTextField ? text.text : text);
  30. this.anchor = anchor;
  31. this.options = new DrawTextFieldOptions(options);
  32. }
  33. DrawTextField.prototype.measureWidth = function (ctx) {
  34. var padding = this.options.padding;
  35. return this.text.map(function (l) { return ctx.measureText(l).width; }).reduce(function (w0, w1) { return w0 < w1 ? w1 : w0; }, 0) + (2 * padding);
  36. };
  37. DrawTextField.prototype.measureHeight = function () {
  38. var _a = this.options, fontSize = _a.fontSize, padding = _a.padding;
  39. return this.text.length * fontSize + (2 * padding);
  40. };
  41. DrawTextField.prototype.getUpperLeft = function (ctx, canvasDims) {
  42. var anchorPosition = this.options.anchorPosition;
  43. var isShiftLeft = anchorPosition === AnchorPosition.BOTTOM_RIGHT || anchorPosition === AnchorPosition.TOP_RIGHT;
  44. var isShiftTop = anchorPosition === AnchorPosition.BOTTOM_LEFT || anchorPosition === AnchorPosition.BOTTOM_RIGHT;
  45. var textFieldWidth = this.measureWidth(ctx);
  46. var textFieldHeight = this.measureHeight();
  47. var x = (isShiftLeft ? this.anchor.x - textFieldWidth : this.anchor.x);
  48. var y = isShiftTop ? this.anchor.y - textFieldHeight : this.anchor.y;
  49. // adjust anchor if text box exceeds canvas borders
  50. if (canvasDims) {
  51. var width = canvasDims.width, height = canvasDims.height;
  52. var newX = Math.max(Math.min(x, width - textFieldWidth), 0);
  53. var newY = Math.max(Math.min(y, height - textFieldHeight), 0);
  54. return { x: newX, y: newY };
  55. }
  56. return { x: x, y: y };
  57. };
  58. DrawTextField.prototype.draw = function (canvasArg) {
  59. var canvas = resolveInput(canvasArg);
  60. var ctx = getContext2dOrThrow(canvas);
  61. var _a = this.options, backgroundColor = _a.backgroundColor, fontColor = _a.fontColor, fontSize = _a.fontSize, fontStyle = _a.fontStyle, padding = _a.padding;
  62. ctx.font = fontSize + "px " + fontStyle;
  63. var maxTextWidth = this.measureWidth(ctx);
  64. var textHeight = this.measureHeight();
  65. ctx.fillStyle = backgroundColor;
  66. var upperLeft = this.getUpperLeft(ctx, canvas);
  67. ctx.fillRect(upperLeft.x, upperLeft.y, maxTextWidth, textHeight);
  68. ctx.fillStyle = fontColor;
  69. this.text.forEach(function (textLine, i) {
  70. var x = padding + upperLeft.x;
  71. var y = padding + upperLeft.y + ((i + 1) * fontSize);
  72. ctx.fillText(textLine, x, y);
  73. });
  74. };
  75. return DrawTextField;
  76. }());
  77. export { DrawTextField };
  78. //# sourceMappingURL=DrawTextField.js.map