Skip to content

Instantly share code, notes, and snippets.

@gka
Created November 14, 2013 15:58
Show Gist options
  • Save gka/7469245 to your computer and use it in GitHub Desktop.
Save gka/7469245 to your computer and use it in GitHub Desktop.
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
Copy link
Author

gka commented Nov 14, 2013

feel free to multiply the result according to your font size

@rasmusblockzero
Copy link

Thank you!

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