text-size
Small high performance library to get text width before render
Examples
- With React es6 style
;; Component { return <span> Text width: textSize </span> ; }
- With React es5 style
var React = ;var textSize = ; var TextComponent = React;
- With simple html file
<html> <head> </head> <body> <script src="textSize.js"></script> <script> console; </script> </body></html>
API
getTextWidth(config, type)
config
- {text: String, fontSize: String, fontName: String}, required parametertype
: - optional parameter, can be 'canvas' or 'dom', 'dom' by default
Pefomance
For testing we use Macbook Pro 2015, Chrome 51
DOM
<html> <head> </head> <body> <script src="textSize.js"></script> <script> var startTime = Date; for var i = 0; i < 10000; i++ textSize console; </script> </body></html>
take 35-50ms
Canvas
<html> <head> </head> <body> <script src="textSize.js"></script> <script> var startTime = Date; for var i = 0; i < 10000; i++ textSize console; </script> </body></html>
take 25-45ms