showing results for - "calculate width of text javascript"
Rich
10 Aug 2020
1function measureText(str, fontSize = 10) {
2  const widths = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.2796875,0.2765625,0.3546875,0.5546875,0.5546875,0.8890625,0.665625,0.190625,0.3328125,0.3328125,0.3890625,0.5828125,0.2765625,0.3328125,0.2765625,0.3015625,0.5546875,0.5546875,0.5546875,0.5546875,0.5546875,0.5546875,0.5546875,0.5546875,0.5546875,0.5546875,0.2765625,0.2765625,0.584375,0.5828125,0.584375,0.5546875,1.0140625,0.665625,0.665625,0.721875,0.721875,0.665625,0.609375,0.7765625,0.721875,0.2765625,0.5,0.665625,0.5546875,0.8328125,0.721875,0.7765625,0.665625,0.7765625,0.721875,0.665625,0.609375,0.721875,0.665625,0.94375,0.665625,0.665625,0.609375,0.2765625,0.3546875,0.2765625,0.4765625,0.5546875,0.3328125,0.5546875,0.5546875,0.5,0.5546875,0.5546875,0.2765625,0.5546875,0.5546875,0.221875,0.240625,0.5,0.221875,0.8328125,0.5546875,0.5546875,0.5546875,0.5546875,0.3328125,0.5,0.2765625,0.5546875,0.5,0.721875,0.5,0.5,0.5,0.3546875,0.259375,0.353125,0.5890625]
3  const avg = 0.5279276315789471
4  return str
5    .split('')
6    .map(c => c.charCodeAt(0) < widths.length ? widths[c.charCodeAt(0)] : avg)
7    .reduce((cur, acc) => acc + cur) * fontSize
8}
9console.log(measureText("Hello world")); // Output: 49.40625000000001
10console.log(measureText("This is a long text")); // Output: 77.74999999999999
11console.log(measureText("i")); // Output: 2.21875
queries leading to this page
get character width javascriptjs calculate length of text in htmljs show width as textnodejs number string widthget width of text javascriptcalculate exact space div with textset text width javascripthtml js get text widthcalculate width based on length of string javascripthow to get text width from font sizetext size by with and hight testercalculate text length in pixels javascripthow find html text height with javascriptjs get text widthget text width in pixels javascriptmeasure string javascripthow measure px width of string javascriptfont text width in pixelshow to get plain text width jshow to get width of text in jsjs check width of textget the width of text element javascriptjs get width of textset width of a text created via jswidth of text javascriptjs how to get text widthtext display widthjavascript text length in pixels measuretextmeasure letter length using canvas in jshow to get text height jstext width jsget width of text content javascriptget width of text in javascriptget text width jsset width based on text size jsjavascript calculate text widthhow to get text width javascriptcalculate width by fontsizecalculate width of one lettercalculate width of text javascriptjavascript determine text widthhtml get text widthjs get text sizecalculator text jsget width of text in pixelsget width of text in pixels javascripttext width javascriptget a string width in javascriptwidth and length character javascriptjavascript width of letterhow to get text element width jsfont size width javascriptcalculate pixel width javascriptjs estimate text widthjavascript css text width calculateget text width javascripthow to get character width in javascriptstring solve 28width 2cget font widthwidth of text jsinnertext width jshow to calculate the width of the text in jsfind width of text javascripthow to get width of 23text jshow to get width of charlength 2a width jsjavascript calculate string width in pixelshow to get width of text in javascripthow to gte width of text type in javascriptget width of text javascriotget width of a text measure the height of text cavascalculate the font based on device width in javascriptmeasure text width javascriptcalculate text width javascripthow to get width of letter htmljavascript canvas text length pixelcalculate text height based on pixel in angularhow to know the text size before appending to dom elementhow to get width of string in javascriptjavascript width textmetricscalculate actual width of text javascripthow to measure the width of text javascriptbassed on text length find offset width in javascriptjqury get width based number of charachterjavascript calculate text character width based on font sizecalculate the width of the text in javascriptcalculate widtth of text based on font sizeget charact width javascriptget text widthcalculate text size javascriptdimensions in pixels of canvas text javascriptjs calculate text widthjavascript set text widthwidth text jswidth of texthow to get width of char htmlhow to get the width of text in jsget width of a fontjs width of inner textjavascript calculate width of textjavascript get text display widthonly show a certain width of a text in jsjavascript calculate length and widthjavascript predict width of textjavascript measure stringcalculate limit based on pixel in angularjs text widthhow to get text width in javascriptjavascript character widthtext coontent width jscalculate text size in pixelscount string widthcalculate width of text javascript