10 examples of 'getcomputedtextlength' in JavaScript

Every line of 'getcomputedtextlength' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
5function getStringWidth(str, style) {
6 try {
7 // Calculate length of each word to be used to determine number of words per line
8 let textEl = document.getElementById(MEASUREMENT_ELEMENT_ID);
9 if (!textEl) {
10 const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
11 svg.style.width = 0;
12 svg.style.height = 0;
13 svg.style.position = 'absolute';
14 svg.style.top = '-100%';
15 svg.style.left = '-100%';
16 textEl = document.createElementNS('http://www.w3.org/2000/svg', 'text');
17 textEl.setAttribute('id', MEASUREMENT_ELEMENT_ID);
18 svg.appendChild(textEl);
19 document.body.appendChild(svg);
20 }
21
22 Object.assign(textEl.style, style);
23 textEl.textContent = str;
24 return textEl.getComputedTextLength();
25 } catch (e) {
26 return null;
27 }
28}
174performMeasurement(node) {
175 return node.element.getComputedTextLength();
176}
776function getTextWidth(text, textFont) {
777 var key = text + ':' + textFont;
778 if (_textWidthCache[key]) {
779 return _textWidthCache[key];
780 }
781 _ctx = _ctx || util.getContext();
782 _ctx.save();
783
784 if (textFont) {
785 _ctx.font = textFont;
786 }
787
788 text = (text + '').split('\n');
789 var width = 0;
790 for (var i = 0, l = text.length; i < l; i++) {
791 width = Math.max(
792 _ctx.measureText(text[i]).width,
793 width
794 );
795 }
796 _ctx.restore();
797
798 _textWidthCache[key] = width;
799 if (++_textWidthCacheCounter > TEXT_CACHE_MAX) {
800 // 内存释放
801 _textWidthCacheCounter = 0;
802 _textWidthCache = {};
803 }
804
805 return width;
806}
51measureText (text) {
52 if (!this._cache[text]) {
53 this._svgText.textContent = text;
54 this._cache[text] = this._svgText.getComputedTextLength();
55 }
56 return this._cache[text];
57}
121function getEllipsesLength(): number {
122 textElement.textContent = "...";
123 return textElement.getComputedTextLength();
124}
302get_longest_line_width() {
303 const font = this.get_font('font');
304 let max_line_width = 0;
305 let line_width = 0;
306
307 for (let i = 0; i < this._text.length; i++) {
308 /** @type {string} */
309 let current = this._text[i];
310 if (this._uppercase) {
311 current = current.toUpperCase();
312 }
313
314 if (current.charCodeAt(0) < 32) {
315 if (current === '\n') {
316 if (line_width > max_line_width) {
317 max_line_width = line_width;
318 }
319 line_width = 0;
320 }
321 } else {
322 let char_width = Math.ceil(font.get_char_size(tmp_vec5, current, this._text[i + 1]).x);
323 line_width += char_width;
324 }
325 }
326
327 if (line_width > max_line_width) {
328 max_line_width = line_width;
329 }
330
331 return max_line_width;
332}
48get maxTextWidth(): number { return this._maxTextWidth; }
252function textHeight(text) {
253
254 // 初始高度
255 var contentHeight = 0;
256
257 // 遍历内容
258 string.forEach(text, function (index, c) {
259 // 因为内容可能没有主动换行符,所以只要第一次字符出现则高度即为1
260 // 随后每次出现一个换行符,则都判定为高度+1
261 if (contentHeight == 0
262 || c == '\n') {
263 contentHeight++;
264 }
265 });
266 return contentHeight;
267}
129get rawTextLength(): number /*int*/ {
130 notImplemented("public flash.text.engine.TextLine::get rawTextLength"); return;
131 // return this._rawTextLength;
132}
180getPlainText(text, start, end) {
181 var plainText;
182 if (text == null) {
183 plainText = this.penManager.plainText;
184 } else {
185 var m, match = this.parser.splitText(text, 1); // PLAINTEXTONLY_MODE
186 plainText = "";
187 for (var i = 0, len = match.length; i < len; i++) {
188 plainText += match[i];
189 }
190 }
191
192 if ((start != null) || (end != null)) {
193 if (start == null) {
194 start = 0;
195 }
196 if (end == null) {
197 end = plainText.length;
198 }
199 plainText = plainText.substring(start, end);
200 }
201
202 return plainText;
203}

Related snippets