UNPKG

edeap

Version:

Euler Diagrams Drawn with Ellipses Area-Proportionally (Edeap)

39 lines (38 loc) 1.31 kB
export class TextDimensionsBrowser { constructor() { Object.defineProperty(this, "text", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "textLengthMeasure", { enumerable: true, configurable: true, writable: true, value: void 0 }); } init(fontSize, fontName) { const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); this.text = document.createElementNS("http://www.w3.org/2000/svg", "text"); this.text.setAttribute("style", `font-family: ${fontName}; font-size: ${fontSize}px;`); svg.appendChild(this.text); this.textLengthMeasure = document.createElement("div"); this.textLengthMeasure.appendChild(svg); document.body.appendChild(this.textLengthMeasure); } measure(str) { if (!this.text) throw new Error("init first"); this.text.textContent = str; return { width: this.text.getComputedTextLength(), height: this.text.getBBox().height, }; } destroy() { if (this.textLengthMeasure) document.body.removeChild(this.textLengthMeasure); } }