Measuring length of string in pixel in javascript -


how find length of string in pixels in javascript , if know font-size , font-family?

the simplest solution create in memory canvas (i.e. 1 isn't added dom) , use measuretext function :

var canvas = document.createelement('canvas'); var ctx = canvas.getcontext("2d"); ctx.font = "11px arial";         var width = ctx.measuretext(str).width; 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -