Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Approximate text width based on estimated character widths...
var textWidth = (function() {
function charW(w, c) {
if (c == 'W' || c == 'M') w += 15;
else if (c == 'w' || c == 'm') w += 12;
else if (c == 'I' || c == 'i' || c == 'l' || c == 't' || c == 'f') w += 4;
else if (c == 'r') w += 8;
else if (c == c.toUpperCase()) w += 12;
else w += 10;
return w;
}
return function(s) {
return _.reduce(s.split(''), charW, 0);
};
})();
@gka

This comment has been minimized.

Copy link
Owner Author

@gka gka commented Nov 14, 2013

feel free to multiply the result according to your font size

@rasmusblockzero

This comment has been minimized.

Copy link

@rasmusblockzero rasmusblockzero commented Oct 17, 2017

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment