Searching for a solution to this issue? Look no further than the grabbag.js library!
Although lacking in documentation, the library includes a handy method called grabbag.measure.crop which allows you to crop a string to a specific size. To see a demonstration, check out my JS fiddle. Here's a quick example on how to use it:
var result = grabbag.measure.crop(inputString, 300 /* number of pixels */, ele);
Simply provide the input string, desired pixel size, and a reference DOM element for styling. The result will be a string that fits perfectly within the specified pixel limit.
Hopefully, this information proves useful to you.
- Disclaimer: I am a contributor to the development of grabbag.js.