UNPKG

@discord-card/core

Version:
91 lines 2.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Text = void 0; const canvas_txt_1 = require("canvas-txt"); const lib_1 = require("./lib"); class Text { constructor(text, posX, posY) { this.text = text; this.rect = { x: posX, y: posY, w: 0, h: 0 }; this.multilineOn = false; this.strokeOn = false; } setFont(font) { this.font = font; return this; } setFontSize(size) { this.fontSize = size; return this; } setStyle(style) { this.style = style; return this; } setGradient(gradient) { this.gradient = gradient; return this; } stroke() { this.strokeOn = !this.strokeOn; return this; } /** Set the width and height of this text */ setRect(w, h) { this.rect.w = w; this.rect.h = h; return this; } multiline() { this.multilineOn = !this.multilineOn; return this; } draw(canvas) { // Save before style let ctx = canvas.getContext('2d'); ctx.save(); const canvasW = canvas.width, canvasH = canvas.height; if (this.rect.x < 1 && this.rect.y < 1) { this.rect.x *= canvasW; this.rect.y *= canvasH; } if (this.rect.w === 0) this.rect.w = canvasW - this.rect.x; if (this.rect.h === 0) this.rect.h = canvasH - this.rect.h; if (this.rect.w < 1 && this.rect.y < 1) { this.rect.w *= canvasW; this.rect.h *= canvasH; } if (this.textAlign) ctx.textAlign = this.textAlign; if (this.gradient) { this.style = this.gradient.toString(ctx, this.rect.x, this.rect.y, this.rect.w, this.rect.h); } if (this.style) { ctx.fillStyle = this.style; ctx.strokeStyle = this.style; } if (this.font) (0, lib_1.changeFont)(ctx, this.font); if (this.fontSize) (0, lib_1.changeFontSize)(ctx, this.fontSize + 'px'); if (this.multilineOn) { canvas_txt_1.default.align = this.textAlign; canvas_txt_1.default.fontSize = this.fontSize; canvas_txt_1.default.drawText(ctx, this.text, this.rect.x, this.rect.y, this.rect.w, this.rect.h); } else { if (this.strokeOn) { ctx.strokeText(this.text, this.rect.x, this.rect.y, this.rect.w); } else { ctx.fillText(this.text, this.rect.x, this.rect.y, this.rect.w); } } // Restore old style ctx.restore(); } } exports.Text = Text; //# sourceMappingURL=text.js.map